Path: blob/master/src/packages/conat/core/sticky.test.ts
1542 views
import { consistentHashingChoice } from "./sticky";12describe("tests of consistentHashingChoice", () => {3it("throws when set has size 0", () => {4expect(() => consistentHashingChoice(new Set(), "x")).toThrow("size");5});67it("for size one it just returns the unique item", () => {8expect(consistentHashingChoice(new Set(["foo"]), "bar")).toEqual("foo");9});1011it("for size 3 it gives the same result every time for the same input (and also that it's not stupidly slow)", () => {12const v = new Set(["a", "b", "x"]);13const resource = "thing";14const choice = consistentHashingChoice(v, resource);15expect(v.has(choice)).toBe(true);16for (let i = 0; i < 1000; i++) {17expect(consistentHashingChoice(v, resource)).toBe(choice);18}19});2021it("the results are uniformly distributed when the resources are different", () => {22const v = new Set(["a", "b", "x"]);23const c = { a: 0, b: 0, x: 0 };24for (let i = 0; i < 1000; i++) {25c[consistentHashingChoice(v, `${i}`)] += 1;26}27// just roughly in the direction of uniform...28expect(c.a).toBeGreaterThan(250);29expect(c.b).toBeGreaterThan(250);30expect(c.x).toBeGreaterThan(250);31});32});333435