|
| 1 | +import { describe, test, expect } from 'bun:test' |
| 2 | +import ConsistentHash from './consistent-hash.js' |
| 3 | + |
| 4 | +// ── Item 1: constructor validates virtualNodes ────────────────────────────── |
| 5 | +describe('ConsistentHash constructor validation', () => { |
| 6 | + test('throws on virtualNodes = 0', () => { |
| 7 | + expect(() => new ConsistentHash({ virtualNodes: 0 })).toThrow() |
| 8 | + }) |
| 9 | + |
| 10 | + test('throws on negative virtualNodes', () => { |
| 11 | + expect(() => new ConsistentHash({ virtualNodes: -5 })).toThrow() |
| 12 | + }) |
| 13 | + |
| 14 | + test('throws on fractional virtualNodes', () => { |
| 15 | + expect(() => new ConsistentHash({ virtualNodes: 10.5 })).toThrow() |
| 16 | + }) |
| 17 | + |
| 18 | + test('throws on non-numeric virtualNodes', () => { |
| 19 | + expect(() => new ConsistentHash({ virtualNodes: '100' })).toThrow() |
| 20 | + }) |
| 21 | + |
| 22 | + test('accepts a positive integer', () => { |
| 23 | + const ch = new ConsistentHash({ virtualNodes: 4 }) |
| 24 | + expect(ch.virtualNodes).toBe(4) |
| 25 | + }) |
| 26 | + |
| 27 | + test('default is 100', () => { |
| 28 | + const ch = new ConsistentHash() |
| 29 | + expect(ch.virtualNodes).toBe(100) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +// ── Item 2: ring uses 32-bit numeric hashes (numeric compare, no strings) ─── |
| 34 | +describe('numeric ring representation', () => { |
| 35 | + test('ring entries are 32-bit numbers, not hex strings', () => { |
| 36 | + const ch = new ConsistentHash({ virtualNodes: 8 }) |
| 37 | + ch.addNode('a') |
| 38 | + expect(ch.ring.length).toBe(8) |
| 39 | + for (const h of ch.ring) { |
| 40 | + expect(typeof h).toBe('number') |
| 41 | + expect(Number.isInteger(h)).toBe(true) |
| 42 | + expect(h).toBeGreaterThanOrEqual(0) |
| 43 | + expect(h).toBeLessThanOrEqual(0xffffffff) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + test('ring is sorted numerically ascending', () => { |
| 48 | + const ch = new ConsistentHash({ virtualNodes: 32 }) |
| 49 | + for (const n of ['a', 'b', 'c']) ch.addNode(n) |
| 50 | + for (let i = 1; i < ch.ring.length; i++) { |
| 51 | + expect(ch.ring[i - 1] <= ch.ring[i]).toBe(true) |
| 52 | + } |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +// ── Item 4: batch addNodes sorts once ──────────────────────────────────────── |
| 57 | +describe('addNodes batch API', () => { |
| 58 | + test('addNodes accepts a list and registers all nodes', () => { |
| 59 | + const ch = new ConsistentHash({ virtualNodes: 16 }) |
| 60 | + ch.addNodes(['a', 'b', 'c']) |
| 61 | + expect(ch.size()).toBe(3) |
| 62 | + expect(ch.getNode('key-1')).toBeDefined() |
| 63 | + }) |
| 64 | + |
| 65 | + test('addNodes rejects an empty or invalid list', () => { |
| 66 | + const ch = new ConsistentHash({ virtualNodes: 4 }) |
| 67 | + expect(() => ch.addNodes([])).toThrow() |
| 68 | + expect(() => ch.addNodes('a')).toThrow() |
| 69 | + }) |
| 70 | + |
| 71 | + test('addNodes rejects duplicate nodes within the batch', () => { |
| 72 | + const ch = new ConsistentHash({ virtualNodes: 4 }) |
| 73 | + expect(() => ch.addNodes(['a', 'a'])).toThrow() |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +// ── Item 5: ring invariants ────────────────────────────────────────────────── |
| 78 | +describe('ring invariants', () => { |
| 79 | + const keys = Array.from({ length: 2000 }, (_, i) => `key-${i}`) |
| 80 | + |
| 81 | + test('getNode returns null on an empty ring', () => { |
| 82 | + const ch = new ConsistentHash({ virtualNodes: 8 }) |
| 83 | + expect(ch.getNode('any-key')).toBeNull() |
| 84 | + }) |
| 85 | + |
| 86 | + test('getNode output is deterministic', () => { |
| 87 | + const ch = new ConsistentHash({ virtualNodes: 16 }) |
| 88 | + ch.addNode('a') |
| 89 | + ch.addNode('b') |
| 90 | + for (const k of keys) { |
| 91 | + expect(ch.getNode(k)).toBe(ch.getNode(k)) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + test('monotonicity: removing a node only remaps its keys', () => { |
| 96 | + const ch = new ConsistentHash({ virtualNodes: 64 }) |
| 97 | + ch.addNodes(['a', 'b', 'c']) |
| 98 | + const before = new Map(keys.map((k) => [k, ch.getNode(k)])) |
| 99 | + |
| 100 | + ch.removeNode('b') |
| 101 | + const moved = keys.filter((k) => ch.getNode(k) !== before.get(k)) |
| 102 | + // Every moved key must now land on a surviving node. |
| 103 | + for (const k of moved) { |
| 104 | + expect(['a', 'c']).toContain(ch.getNode(k)) |
| 105 | + } |
| 106 | + // Minimal disruption: removed node owned ~1/3 of keys; moved set must be |
| 107 | + // exactly the set previously owned by 'b' (ring + virtual nodes guarantee). |
| 108 | + const movedFraction = moved.length / keys.length |
| 109 | + expect(movedFraction).toBeGreaterThan(0.2) |
| 110 | + expect(movedFraction).toBeLessThan(0.45) |
| 111 | + }) |
| 112 | + |
| 113 | + test('monotonicity: adding a node only remaps a minority of keys', () => { |
| 114 | + const ch = new ConsistentHash({ virtualNodes: 64 }) |
| 115 | + ch.addNodes(['a', 'b']) |
| 116 | + const before = new Map(keys.map((k) => [k, ch.getNode(k)])) |
| 117 | + ch.addNode('c') |
| 118 | + const moved = keys.filter((k) => ch.getNode(k) !== before.get(k)).length / keys.length |
| 119 | + expect(moved).toBeGreaterThan(0.1) // should win ~1/3 of keys |
| 120 | + expect(moved).toBeLessThan(0.5) |
| 121 | + }) |
| 122 | + |
| 123 | + test('only node removal leaves an empty ring and getNode returns null', () => { |
| 124 | + const ch = new ConsistentHash({ virtualNodes: 8 }) |
| 125 | + ch.addNode('a') |
| 126 | + ch.removeNode('a') |
| 127 | + expect(ch.size()).toBe(0) |
| 128 | + expect(ch.getNode('x')).toBeNull() |
| 129 | + }) |
| 130 | + |
| 131 | + test('distribution across nodes is balanced within 25%', () => { |
| 132 | + const ch = new ConsistentHash({ virtualNodes: 100 }) |
| 133 | + ch.addNodes(['a', 'b', 'c']) |
| 134 | + const counts = { a: 0, b: 0, c: 0 } |
| 135 | + for (const k of keys) counts[ch.getNode(k)]++ |
| 136 | + const avg = keys.length / 3 |
| 137 | + for (const c of Object.values(counts)) { |
| 138 | + expect(Math.abs(c - avg)).toBeLessThanOrEqual(avg * 0.25) |
| 139 | + } |
| 140 | + }) |
| 141 | +}) |
0 commit comments