|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import JumpConsistentHash from "./jump-hash.js"; |
| 3 | + |
| 4 | +describe("JumpConsistentHash", () => { |
| 5 | + describe("constructor & size()", () => { |
| 6 | + test("initializes with provided number of indexes", () => { |
| 7 | + const jch = new JumpConsistentHash(16); |
| 8 | + expect(jch.size()).toBe(16); |
| 9 | + }); |
| 10 | + |
| 11 | + test("throws for invalid indexes in constructor", () => { |
| 12 | + expect(() => new JumpConsistentHash()).toThrow("Indexes must be a positive integer"); |
| 13 | + expect(() => new JumpConsistentHash(0)).toThrow("Indexes must be a positive integer"); |
| 14 | + expect(() => new JumpConsistentHash(-1)).toThrow("Indexes must be a positive integer"); |
| 15 | + expect(() => new JumpConsistentHash(1.2)).toThrow("Indexes must be a positive integer"); |
| 16 | + }); |
| 17 | + |
| 18 | + test("setIndexes() updates and validates", () => { |
| 19 | + const jch = new JumpConsistentHash(4); |
| 20 | + expect(jch.size()).toBe(4); |
| 21 | + jch.setIndexes(32); |
| 22 | + expect(jch.size()).toBe(32); |
| 23 | + expect(() => jch.setIndexes(0)).toThrow("Indexes must be a positive integer"); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe("getIndex()", () => { |
| 28 | + test("throws for empty key", () => { |
| 29 | + const jch = new JumpConsistentHash(8); |
| 30 | + expect(() => jch.getIndex("")).toThrow("Key must be a non-empty string"); |
| 31 | + }); |
| 32 | + |
| 33 | + test("returns index within range", () => { |
| 34 | + const jch = new JumpConsistentHash(8); |
| 35 | + const idx = jch.getIndex("alpha"); |
| 36 | + expect(idx).toBeGreaterThanOrEqual(0); |
| 37 | + expect(idx).toBeLessThan(8); |
| 38 | + }); |
| 39 | + |
| 40 | + test("deterministic for same key", () => { |
| 41 | + const jch = new JumpConsistentHash(8); |
| 42 | + const k = "user-12345"; |
| 43 | + const a = jch.getIndex(k); |
| 44 | + const b = jch.getIndex(k); |
| 45 | + expect(a).toBe(b); |
| 46 | + }); |
| 47 | + |
| 48 | + test("one bucket always maps to 0", () => { |
| 49 | + const jch = new JumpConsistentHash(1); |
| 50 | + const keys = ["a", "b", "c", "d", "e"]; |
| 51 | + for (const k of keys) { |
| 52 | + expect(jch.getIndex(k)).toBe(0); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + test("changes reflect with updated bucket count", () => { |
| 57 | + const jch = new JumpConsistentHash(4); |
| 58 | + const k = "remap-key"; |
| 59 | + const before = jch.getIndex(k); |
| 60 | + expect(before).toBeGreaterThanOrEqual(0); |
| 61 | + expect(before).toBeLessThan(4); |
| 62 | + jch.setIndexes(9); |
| 63 | + const after = jch.getIndex(k); |
| 64 | + expect(after).toBeGreaterThanOrEqual(0); |
| 65 | + expect(after).toBeLessThan(9); |
| 66 | + }); |
| 67 | + |
| 68 | + test("distributes keys relatively evenly", () => { |
| 69 | + const buckets = 8; |
| 70 | + const jch = new JumpConsistentHash(buckets); |
| 71 | + const totalKeys = 10000; |
| 72 | + const counts = new Array(buckets).fill(0); |
| 73 | + for (let i = 0; i < totalKeys; i++) { |
| 74 | + const idx = jch.getIndex(`key-${i}`); |
| 75 | + counts[idx]++; |
| 76 | + } |
| 77 | + const avg = totalKeys / buckets; |
| 78 | + const tolerance = avg * 0.25; // 25% tolerance |
| 79 | + for (const c of counts) { |
| 80 | + expect(Math.abs(c - avg)).toBeLessThanOrEqual(tolerance); |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments