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