@@ -12,12 +12,45 @@ def test_rendezvous_rank():
1212
1313
1414def test_rendezvous_independence ():
15- # check that the relative ranking of two buckets doesn't depend on the
16- # presence of a third bucket
15+ # check that the relative ranking of 80 buckets doesn't depend on the
16+ # presence of 20 extra buckets
1717 key = "k1"
18- two_buckets = utils .rendezvous_rank (["b1" , "b3" ], key )
19- three_buckets = utils .rendezvous_rank (["b1" , "b2" , "b3" ], key )
20- # remove "b2" and check ranking
21- three_buckets .remove ("b2" )
18+ eighty_buckets = utils .rendezvous_rank (["b%i" % i for i in range (80 )], key )
19+ hundred_buckets = utils .rendezvous_rank (["b%i" % i for i in range (100 )], key )
2220
23- assert two_buckets == three_buckets
21+ for i in range (80 , 100 ):
22+ hundred_buckets .remove ("b%i" % i )
23+
24+ assert eighty_buckets == hundred_buckets
25+
26+
27+ def test_rendezvous_redistribution ():
28+ # check that approximately a third of keys move to the new bucket
29+ # when one is added
30+ n_keys = 3000
31+
32+ # counnt how many keys were moved and which bucket were they moved from
33+ n_moved = 0
34+ from_b1 = 0
35+ from_b2 = 0
36+
37+ for i in range (n_keys ):
38+ key = f"key-{ i } "
39+ two_buckets = utils .rendezvous_rank (["b1" , "b2" ], key )
40+ three_buckets = utils .rendezvous_rank (["b1" , "b2" , "b3" ], key )
41+
42+ if two_buckets [0 ] != three_buckets [0 ]:
43+ n_moved += 1
44+ if two_buckets [0 ] == "b1" :
45+ from_b1 += 1
46+ if two_buckets [0 ] == "b2" :
47+ from_b2 += 1
48+ # should always move to the newly added bucket
49+ assert three_buckets [0 ] == "b3"
50+
51+ # because of statistical fluctuations we have to leave some room when
52+ # making this comparison
53+ assert 0.31 < n_moved / n_keys < 0.35
54+ # keys should move from the two original buckets with approximately
55+ # equal probability
56+ assert abs (from_b1 - from_b2 ) < 10
0 commit comments