コード例 #1
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_len(self):
     """
     Ensures the number of nodes in the k-bucket is returned by __len__.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact = PeerNode("12345", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact)
     self.assertEqual(1, len(bucket))
コード例 #2
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_len(self):
     """
     Ensures the number of nodes in the k-bucket is returned by __len__.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact = PeerNode("12345", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact)
     self.assertEqual(1, len(bucket))
コード例 #3
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_remove_contact_with_bad_id(self):
     """
     Ensures a ValueError exception is raised if one attempts to remove a
     non-existent contact from a k-bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact = PeerNode("12345", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact)
     with self.assertRaises(ValueError):
         bucket.remove_contact("54321")
コード例 #4
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_remove_contact_with_bad_id(self):
     """
     Ensures a ValueError exception is raised if one attempts to remove a
     non-existent contact from a k-bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact = PeerNode("12345", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact)
     with self.assertRaises(ValueError):
         bucket.remove_contact("54321")
コード例 #5
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 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))
コード例 #6
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 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))
コード例 #7
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 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))
コード例 #8
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_get_contact_with_bad_id(self):
     """
     Ensures a ValueError exception is raised if one attempts to get a
     contact from the k-bucket with an id that doesn't exist in the
     k-bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact = PeerNode("12345", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact)
     with self.assertRaises(ValueError):
         bucket.get_contact("54321")
コード例 #9
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 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))
コード例 #10
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_get_contact_with_bad_id(self):
     """
     Ensures a ValueError exception is raised if one attempts to get a
     contact from the k-bucket with an id that doesn't exist in the
     k-bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact = PeerNode("12345", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact)
     with self.assertRaises(ValueError):
         bucket.get_contact("54321")
コード例 #11
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 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)
コード例 #12
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_add_contact_to_full_bucket(self):
     """
     Ensures that if one attempts to add a contact to a bucket whose size is
     greater than the constant K, then the BucketFull exception is raised.
     """
     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)
     with self.assertRaises(BucketFull):
         contact_too_many = PeerNode("12345", "192.168.0.2", 8888, 123)
         bucket.add_contact(contact_too_many)
コード例 #13
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 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)
コード例 #14
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_add_contact_to_full_bucket(self):
     """
     Ensures that if one attempts to add a contact to a bucket whose size is
     greater than the constant K, then the BucketFull exception is raised.
     """
     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)
     with self.assertRaises(BucketFull):
         contact_too_many = PeerNode("12345", "192.168.0.2", 8888, 123)
         bucket.add_contact(contact_too_many)
コード例 #15
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_get_contact(self):
     """
     Ensures it is possible to get a contact from the k-bucket with a valid
     id.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     for i in range(K):
         contact = PeerNode(PUBLIC_KEY, "192.168.0.%d" % i, 9999, 123)
         contact.network_id = hex(i)
         bucket.add_contact(contact)
     for i in range(K):
         self.assertTrue(bucket.get_contact(hex(i)),
                         "Could not get contact with id %d" % i)
コード例 #16
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_get_contact(self):
     """
     Ensures it is possible to get a contact from the k-bucket with a valid
     id.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     for i in range(K):
         contact = PeerNode(PUBLIC_KEY, "192.168.0.%d" % i, 9999, 123)
         contact.network_id = hex(i)
         bucket.add_contact(contact)
     for i in range(K):
         self.assertTrue(bucket.get_contact(hex(i)),
                         "Could not get contact with id %d" % i)
コード例 #17
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_remove_contact(self):
     """
     Ensures it is possible to remove a contact with a certain ID from the
     k-bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     for i in range(K):
         contact = PeerNode(PUBLIC_KEY, "192.168.0.%d" % i, 9999, 123)
         contact.network_id = hex(i)
         bucket.add_contact(contact)
     for i in range(K):
         bucket.remove_contact(hex(i))
         self.assertFalse(hex(i) in bucket._contacts,
                          "Could not remove contact with id %s" % hex(i))
コード例 #18
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_remove_contact(self):
     """
     Ensures it is possible to remove a contact with a certain ID from the
     k-bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     for i in range(K):
         contact = PeerNode(PUBLIC_KEY, "192.168.0.%d" % i, 9999, 123)
         contact.network_id = hex(i)
         bucket.add_contact(contact)
     for i in range(K):
         bucket.remove_contact(hex(i))
         self.assertFalse(
             hex(i) in bucket._contacts,
             "Could not remove contact with id %s" % hex(i))
コード例 #19
0
 def test_split_bucket_cache_update(self):
     """
     Ensures that if there are cached contacts for the split bucket then
     the two new buckets are topped up, the old cache is removed and two
     new caches created (one for each of the new buckets).
     """
     parent_node_id = 'deadbeef'
     r = RoutingTable(parent_node_id)
     bucket = Bucket(0, 100)
     contact1 = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
     contact1.network_id = hex(20)
     bucket.add_contact(contact1)
     contact2 = PeerNode(PUBLIC_KEY, '192.168.0.2', 8888, 0)
     contact2.network_id = hex(40)
     bucket.add_contact(contact2)
     contact3 = PeerNode(PUBLIC_KEY, '192.168.0.3', 8888, 0)
     contact3.network_id = hex(60)
     bucket.add_contact(contact3)
     contact4 = PeerNode(PUBLIC_KEY, '192.168.0.4', 8888, 0)
     contact4.network_id = hex(80)
     bucket.add_contact(contact4)
     r._buckets[0] = bucket
     # Add two items to the cache.
     cache = []
     cache_contact1 = PeerNode(PUBLIC_KEY, '192.168.0.5', 8888, 0)
     cache_contact1.network_id = hex(10)
     cache.append(cache_contact1)
     cache_contact2 = PeerNode(PUBLIC_KEY, '192.168.0.6', 8888, 0)
     cache_contact2.network_id = hex(70)
     cache.append(cache_contact2)
     r._replacement_cache = {
         (0, 100): cache
     }
     # Two buckets!
     r._split_bucket(0)
     self.assertEqual(2, len(r._buckets))
     bucket1 = r._buckets[0]
     bucket2 = r._buckets[1]
     # Ensure the right number of contacts are in each bucket in the correct
     # order (most recently added at the head of the list).
     self.assertEqual(3, len(bucket1._contacts))
     self.assertEqual(3, len(bucket2._contacts))
     self.assertEqual(contact1, bucket1._contacts[0])
     self.assertEqual(contact2, bucket1._contacts[1])
     self.assertEqual(cache_contact1, bucket1._contacts[2])
     self.assertEqual(contact3, bucket2._contacts[0])
     self.assertEqual(contact4, bucket2._contacts[1])
     self.assertEqual(cache_contact2, bucket2._contacts[2])
     # Ensure the _replacement_cache is in the expected state.
     self.assertEqual(2, len(r._replacement_cache))
     self.assertNotIn((0, 100), r._replacement_cache)
     self.assertIn((0, 50), r._replacement_cache)
     self.assertIn((50, 100), r._replacement_cache)
コード例 #20
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_add_existing_contact(self):
     """
     Ensures that if an existing contact is re-added to the kbucket it is
     simply moved to the end of the _contacts list (as specified in the
     original Kademlia paper) signifying that it is the most recently seen
     contact within this bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact1 = PeerNode("1", "192.168.0.1", 9999, 123)
     bucket.add_contact(contact1)
     contact2 = PeerNode("2", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact2)
     bucket.add_contact(contact1)
     # There should still only be two contacts in the bucket.
     self.assertEqual(2, len(bucket._contacts),
                      "Too many contacts in the k-bucket.")
     # The end contact should be the most recently added contact.
     self.assertEqual(contact1, bucket._contacts[-1:][0],
                      "The expected most recent contact is wrong.")
コード例 #21
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_add_existing_contact(self):
     """
     Ensures that if an existing contact is re-added to the kbucket it is
     simply moved to the end of the _contacts list (as specified in the
     original Kademlia paper) signifying that it is the most recently seen
     contact within this bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact1 = PeerNode("1", "192.168.0.1", 9999, 123)
     bucket.add_contact(contact1)
     contact2 = PeerNode("2", "192.168.0.2", 8888, 123)
     bucket.add_contact(contact2)
     bucket.add_contact(contact1)
     # There should still only be two contacts in the bucket.
     self.assertEqual(2, len(bucket._contacts),
                      "Too many contacts in the k-bucket.")
     # The end contact should be the most recently added contact.
     self.assertEqual(contact1, bucket._contacts[-1:][0],
                      "The expected most recent contact is wrong.")
コード例 #22
0
ファイル: test_bucket.py プロジェクト: waseem18/drogulus
 def test_add_new_contact(self):
     """
     Ensures that a new contact, when added to the kbucket is appended to
     the end of the _contacts list (as specified in the original Kademlia
     paper) signifying that it is the most recently seen contact within
     this bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact1 = PeerNode(PUBLIC_KEY, "192.168.0.1", 9999, 123)
     contact1.network_id = hex(1)
     bucket.add_contact(contact1)
     self.assertEqual(1, len(bucket._contacts),
                      "Single contact not added to k-bucket.")
     contact2 = PeerNode(PUBLIC_KEY, "192.168.0.2", 8888, 123)
     contact2.network_id = hex(2)
     bucket.add_contact(contact2)
     self.assertEqual(2, len(bucket._contacts),
                      "K-bucket's contact list not the expected length.")
     self.assertEqual(contact2, bucket._contacts[-1:][0],
                      "K-bucket's most recent (last) contact wrong.")
コード例 #23
0
ファイル: test_bucket.py プロジェクト: No9/drogulus
 def test_add_new_contact(self):
     """
     Ensures that a new contact, when added to the kbucket is appended to
     the end of the _contacts list (as specified in the original Kademlia
     paper) signifying that it is the most recently seen contact within
     this bucket.
     """
     range_min = 12345
     range_max = 98765
     bucket = Bucket(range_min, range_max)
     contact1 = PeerNode(PUBLIC_KEY, "192.168.0.1", 9999, 123)
     contact1.network_id = hex(1)
     bucket.add_contact(contact1)
     self.assertEqual(1, len(bucket._contacts),
                      "Single contact not added to k-bucket.")
     contact2 = PeerNode(PUBLIC_KEY, "192.168.0.2", 8888, 123)
     contact2.network_id = hex(2)
     bucket.add_contact(contact2)
     self.assertEqual(2, len(bucket._contacts),
                      "K-bucket's contact list not the expected length.")
     self.assertEqual(contact2, bucket._contacts[-1:][0],
                      "K-bucket's most recent (last) contact wrong.")
コード例 #24
0
 def test_split_bucket_cache_too_full(self):
     """
     If the split occurs and there are too many contacts and cached
     contacts for a new bucket, the remainder or cached contacts are added
     to the cache for the new bucket.
     """
     parent_node_id = 'deadbeef'
     r = RoutingTable(parent_node_id)
     bucket = Bucket(0, 100)
     low_contacts = []
     high_contacts = []
     for i in range(10):
         contact = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
         contact.network_id = hex(i)
         bucket.add_contact(contact)
         low_contacts.append(contact)
     for i in range(50, 60):
         contact = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
         contact.network_id = hex(i)
         bucket.add_contact(contact)
         high_contacts.append(contact)
     r._buckets[0] = bucket
     # Add items to the cache.
     cache = []
     low_cache = []
     high_cache = []
     for i in range(10, 30):
         contact = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
         contact.network_id = hex(i)
         cache.append(contact)
         low_cache.append(contact)
     for i in range(60, 80):
         contact = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
         contact.network_id = hex(i)
         cache.append(contact)
         high_cache.append(contact)
     r._replacement_cache = {
         (0, 100): cache
     }
     # Two buckets!
     r._split_bucket(0)
     self.assertEqual(2, len(r._buckets))
     bucket1 = r._buckets[0]
     bucket2 = r._buckets[1]
     # Ensure the right number of contacts are in each bucket in the correct
     # order (most recently added at the head of the list).
     self.assertEqual(20, len(bucket1._contacts))
     self.assertEqual(20, len(bucket2._contacts))
     for i in range(10):
         self.assertEqual(low_contacts[i], bucket1._contacts[i])
         self.assertEqual(low_cache[i], bucket1._contacts[i + 10])
     for i in range(10):
         self.assertEqual(high_contacts[i], bucket2._contacts[i])
         self.assertEqual(high_cache[i], bucket2._contacts[i + 10])
     # Ensure the _replacement_cache is in the expected state.
     self.assertEqual(2, len(r._replacement_cache))
     self.assertNotIn((0, 100), r._replacement_cache)
     self.assertIn((0, 50), r._replacement_cache)
     self.assertIn((50, 100), r._replacement_cache)
     self.assertEqual(10, len(r._replacement_cache[(0, 50)]))
     self.assertEqual(10, len(r._replacement_cache[(50, 100)]))
     for i in range(10):
         self.assertEqual(low_cache[i + 10],
                          r._replacement_cache[(0, 50)][i])
         self.assertEqual(high_cache[i + 10],
                          r._replacement_cache[(50, 100)][i])
コード例 #25
0
 def test_split_bucket(self):
     """
     Ensures that the correct bucket is split in two and that the contacts
     are found in the right place.
     """
     parent_node_id = 'deadbeef'
     r = RoutingTable(parent_node_id)
     bucket = Bucket(0, 100)
     contact1 = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
     contact1.network_id = hex(20)
     bucket.add_contact(contact1)
     contact2 = PeerNode(PUBLIC_KEY, '192.168.0.2', 8888, 0)
     contact2.network_id = hex(40)
     bucket.add_contact(contact2)
     contact3 = PeerNode(PUBLIC_KEY, '192.168.0.3', 8888, 0)
     contact3.network_id = hex(60)
     bucket.add_contact(contact3)
     contact4 = PeerNode(PUBLIC_KEY, '192.168.0.4', 8888, 0)
     contact4.network_id = hex(80)
     bucket.add_contact(contact4)
     r._buckets[0] = bucket
     # Sanity check
     self.assertEqual(1, len(r._buckets))
     r._split_bucket(0)
     # Two buckets!
     self.assertEqual(2, len(r._buckets))
     bucket1 = r._buckets[0]
     bucket2 = r._buckets[1]
     # Ensure the right number of contacts are in each bucket in the correct
     # order (most recently added at the head of the list).
     self.assertEqual(2, len(bucket1._contacts))
     self.assertEqual(2, len(bucket2._contacts))
     self.assertEqual(contact1, bucket1._contacts[0])
     self.assertEqual(contact2, bucket1._contacts[1])
     self.assertEqual(contact3, bucket2._contacts[0])
     self.assertEqual(contact4, bucket2._contacts[1])
     # Split the new bucket again, ensuring that only the target bucket is
     # modified.
     r._split_bucket(1)
     self.assertEqual(3, len(r._buckets))
     bucket3 = r._buckets[2]
     # bucket1 remains un-changed
     self.assertEqual(2, len(bucket1._contacts))
     # bucket2 only contains the lower half of its original contacts.
     self.assertEqual(1, len(bucket2._contacts))
     self.assertEqual(contact3, bucket2._contacts[0])
     # bucket3 now contains the upper half of the original contacts.
     self.assertEqual(1, len(bucket3._contacts))
     self.assertEqual(contact4, bucket3._contacts[0])
     # Split the bucket at position 0 and ensure the resulting buckets are
     # in the correct position with the correct content.
     r._split_bucket(0)
     self.assertEqual(4, len(r._buckets))
     bucket1, bucket2, bucket3, bucket4 = r._buckets
     self.assertEqual(1, len(bucket1._contacts))
     self.assertEqual(contact1, bucket1._contacts[0])
     self.assertEqual(1, len(bucket2._contacts))
     self.assertEqual(contact2, bucket2._contacts[0])
     self.assertEqual(1, len(bucket3._contacts))
     self.assertEqual(contact3, bucket3._contacts[0])
     self.assertEqual(1, len(bucket4._contacts))
     self.assertEqual(contact4, bucket4._contacts[0])