def test_random_guid_in_range_is_random(self): range_min, range_max = 0, 2**constants.BIT_NODE_ID_LEN guid1 = util.random_guid_in_range(range_min, range_max) guid2 = util.random_guid_in_range(range_min, range_max) # There is a tiny chance that the same guid is returned # in consecutive calls, but there is a much higher chance # of a bad (i.e non-random) implementation. self.assertNotEqual(guid1, guid2)
def get_refresh_list(self, force=False): """ Find all buckets that need refreshing and return a list of guids that will help do so. Args: force: If True, guids from all buckets will be returned, regardless of the time they were last accessed. Returns: A list of guids that should be searched for in order to refresh the RoutingTable. """ if force: buckets_to_refresh = self._buckets else: buckets_to_refresh = ( bucket for bucket in self._buckets if bucket.is_stale() ) return [ util.random_guid_in_range(b.range_min, b.range_max) for b in buckets_to_refresh ]
def test_find_close_nodes_from_many_buckets(self): rt = self._make_rt_with_n_buckets(3) self.assertLess(len(rt[0]), constants.K) self.assertEqual(len(rt[1]), constants.K) r_guid = util.random_guid_in_range(rt[0].range_min, rt[0].range_max) node_list = rt.find_close_nodes(r_guid) self.assertEqual(len(node_list), constants.K) for node in node_list: self.assertTrue(node in rt[0] or node in rt[1])
def test_find_close_nodes_with_count(self): rt = self._make_rt_with_n_buckets(3) mid_bucket = rt[1] self.assertEqual(len(mid_bucket), constants.K) r_guid = util.random_guid_in_range( mid_bucket.range_min, mid_bucket.range_max ) node_list = rt.find_close_nodes(r_guid, count=2) self.assertEqual(len(node_list), 2) for node in node_list: self.assertIn(node, mid_bucket)
def test_random_guid_in_range_is_half_open(self): range_min, range_max = 0, 4 guids = (util.random_guid_in_range(range_min, range_max) for _ in range(100)) numbers = tuple(int(guid, base=16) for guid in guids) for number in numbers: self.assertLessEqual(range_min, number) self.assertLess(number, range_max) # There is a tiny chance that the guid corresponding # to the lower limit is not returned, but there is a # much higher chance of a bad implementation. self.assertIn(range_min, numbers)
def test_random_guid_in_range_is_half_open(self): range_min, range_max = 0, 4 guids = ( util.random_guid_in_range(range_min, range_max) for _ in range(100) ) numbers = tuple(int(guid, base=16) for guid in guids) for number in numbers: self.assertLessEqual(range_min, number) self.assertLess(number, range_max) # There is a tiny chance that the guid corresponding # to the lower limit is not returned, but there is a # much higher chance of a bad implementation. self.assertIn(range_min, numbers)
def get_refresh_list(self, force=False): """ Find all buckets that need refreshing and return a list of guids that will help do so. Args: force: If True, guids from all buckets will be returned, regardless of the time they were last accessed. Returns: A list of guids that should be searched for in order to refresh the RoutingTable. """ if force: buckets_to_refresh = self._buckets else: buckets_to_refresh = (bucket for bucket in self._buckets if bucket.is_stale()) return [ util.random_guid_in_range(b.range_min, b.range_max) for b in buckets_to_refresh ]