1 local hashring
= require
"util.hashring";
3 describe("util.hashring", function ()
5 local sha256
= require
"util.hashes".sha256
;
7 local ring
= hashring
.new(128, sha256
);
9 it("should fail to get a node that does not exist", function ()
10 assert.is_nil(ring
:get_node("foo"))
13 it("should support adding nodes", function ()
14 ring
:add_node("node1");
17 it("should return a single node for all keys if only one node exists", function ()
19 assert.is_equal("node1", ring
:get_node(tostring(i
)))
23 it("should support adding a second node", function ()
24 ring
:add_node("node2");
27 it("should fail to remove a non-existent node", function ()
28 assert.is_falsy(ring
:remove_node("node3"));
31 it("should succeed to remove a node", function ()
32 assert.is_truthy(ring
:remove_node("node1"));
35 it("should return the only node for all keys", function ()
37 assert.is_equal("node2", ring
:get_node(tostring(i
)))
41 it("should support adding multiple nodes", function ()
42 ring
:add_nodes({ "node1", "node3", "node4", "node5" });
45 it("should disrupt a minimal number of keys on node removal", function ()
46 local orig_ring
= ring
:clone();
47 local node_tallies
= {};
52 local key
= tostring(i
);
53 local node
= ring
:get_node(key
);
54 node_tallies
[node
] = (node_tallies
[node
] or 0) + 1;
58 for node, key_count in pairs(node_tallies) do
59 print(node, key_count, ("%.2f%%"):format((key_count/n)*100));
63 ring
:remove_node("node5");
65 local disrupted_keys
= 0;
67 local key
= tostring(i
);
68 if orig_ring
:get_node(key
) ~= ring
:get_node(key
) then
69 disrupted_keys
= disrupted_keys
+ 1;
72 assert.is_equal(node_tallies
["node5"], disrupted_keys
);
75 it("should support removing multiple nodes", function ()
76 ring
:remove_nodes({"node2", "node3", "node4", "node5"});
79 it("should return a single node for all keys if only one node remains", function ()
81 assert.is_equal("node1", ring
:get_node(tostring(i
)))