def testFallbackPrecedenceWhenServerRemoved(self): mockHasher = MockHasher() hashSpace = Flexihash(mockHasher, 1) mockHasher.setHashValue(10) hashSpace.addTarget("t1") mockHasher.setHashValue(20) hashSpace.addTarget("t2") mockHasher.setHashValue(30) hashSpace.addTarget("t3") mockHasher.setHashValue(15) self.assertEqual(hashSpace.lookup('resource'), 't2') self.assertEqual(hashSpace.lookupList('resource', 3), ['t2', 't3', 't1']) hashSpace.removeTarget('t2') self.assertEqual(hashSpace.lookup('resource'), 't3') self.assertEqual(hashSpace.lookupList('resource', 3), ['t3', 't1']) hashSpace.removeTarget('t3') self.assertEqual(hashSpace.lookup('resource'), 't1') self.assertEqual(hashSpace.lookupList('resource', 3), ['t1'])
def testLookupListReturnsWithShortListIfAllTargetsUsed(self): hashSpace = Flexihash() # both have CRC32 of 1253617450 hashSpace.addTarget("x").addTarget("y") # make the list non-empty, non-one-value, to avoid shortcuts hashSpace.addTarget("80726") # add a value hashSpace.addTarget("14746907") # add a different value with the same hash, to clobber the first hashSpace.removeTarget("14746907") # remove the fourth value; with the third clobbered, only X and Y are left result = hashSpace.lookupList('test', 3) # try to get 3 results, our target list is X, Y, 80726 self.assertEqual(len(result), 2) # but 80726 isn't reachable since it was clobbered self.assertIn("x", result) # all that's left is x self.assertIn("y", result) # and y
def testLookupListReturnsWithShortListIfAllTargetsUsed(self): hashSpace = Flexihash() # both have CRC32 of 1253617450 hashSpace.addTarget("x").addTarget( "y") # make the list non-empty, non-one-value, to avoid shortcuts hashSpace.addTarget("80726") # add a value hashSpace.addTarget( "14746907" ) # add a different value with the same hash, to clobber the first hashSpace.removeTarget( "14746907" ) # remove the fourth value; with the third clobbered, only X and Y are left result = hashSpace.lookupList( 'test', 3) # try to get 3 results, our target list is X, Y, 80726 self.assertEqual(len(result), 2) # but 80726 isn't reachable since it was clobbered self.assertIn("x", result) # all that's left is x self.assertIn("y", result) # and y
def testFallbackPrecedenceWhenServerRemoved(self): mockHasher = MockHasher() hashSpace = Flexihash(mockHasher, 1) mockHasher.setHashValue(10) hashSpace.addTarget("t1") mockHasher.setHashValue(20) hashSpace.addTarget("t2") mockHasher.setHashValue(30) hashSpace.addTarget("t3") mockHasher.setHashValue(15) self.assertEqual(hashSpace.lookup('resource'), 't2') self.assertEqual( hashSpace.lookupList('resource', 3), ['t2', 't3', 't1'] ) hashSpace.removeTarget('t2') self.assertEqual(hashSpace.lookup('resource'), 't3') self.assertEqual( hashSpace.lookupList('resource', 3), ['t3', 't1'] ) hashSpace.removeTarget('t3') self.assertEqual(hashSpace.lookup('resource'), 't1') self.assertEqual( hashSpace.lookupList('resource', 3), ['t1'] )