def test_get_contacts_empty(self): """ If the k-bucket is empty, the result of getContacts is an empty list. """ range_min = 12345 range_max = 98765 bucket = Bucket(range_min, range_max) result = bucket.get_contacts() self.assertEqual(0, len(result))
def test_get_contacts_all(self): """ Ensures get_contacts works as expected. """ range_min = 12345 range_max = 98765 bucket = Bucket(range_min, range_max) for i in range(K): contact = PeerNode("%d" % i, "192.168.0.%d" % i, 9999, 123) bucket.add_contact(contact) result = bucket.get_contacts() self.assertEqual(20, len(result))
def test_get_contacts_count_too_big(self): """ If the "count" argument is bigger than the number of contacts in the bucket then all the contacts are returned. """ range_min = 12345 range_max = 98765 bucket = Bucket(range_min, range_max) for i in range(10): contact = PeerNode("%d" % i, "192.168.0.%d" % i, 9999, 123) bucket.add_contact(contact) result = bucket.get_contacts(count=20) self.assertEqual(10, len(result))
def test_get_contacts_with_exclusion(self): """ If a contact is passed as the excludeContact argument then it won't be in the result list. """ range_min = 12345 range_max = 98765 bucket = Bucket(range_min, range_max) for i in range(K): contact = PeerNode("%d" % i, "192.168.0.%d" % i, 9999, 123) bucket.add_contact(contact) result = bucket.get_contacts(count=20, exclude_contact=contact) self.assertEqual(19, len(result)) self.assertFalse(contact in result)