def test_iteration(self): heap = NodeHeap(mknode(intid=0), 5) nodes = [mknode(intid=x) for x in range(10)] for index, node in enumerate(nodes): heap.push(node) for index, node in enumerate(heap): self.assertEqual(index, node.long_id) self.assertTrue(index < 5)
def test_split(self): bucket = KBucket(0, 10, 5) bucket.addNode(mknode(intid=5)) bucket.addNode(mknode(intid=6)) one, two = bucket.split() self.assertEqual(len(one), 1) self.assertEqual(one.range, (0, 5)) self.assertEqual(len(two), 1) self.assertEqual(two.range, (6, 10))
def test_maxSize(self): n = NodeHeap(mknode(intid=0), 3) self.assertEqual(0, len(n)) for d in range(10): n.push(mknode(intid=d)) self.assertEqual(3, len(n)) self.assertEqual(3, len(list(n)))
def test_remove(self): heap = NodeHeap(mknode(intid=0), 5) nodes = [mknode(intid=x) for x in range(10)] for node in nodes: heap.push(node) heap.remove([nodes[0].id, nodes[1].id]) self.assertEqual(len(list(heap)), 5) for index, node in enumerate(heap): self.assertEqual(index + 2, node.long_id) self.assertTrue(index < 5)
def test_addNode(self): # when full, return false bucket = KBucket(0, 10, 2) self.assertTrue(bucket.addNode(mknode())) self.assertTrue(bucket.addNode(mknode())) self.assertFalse(bucket.addNode(mknode())) self.assertEqual(len(bucket), 2) # make sure when a node is double added it's put at the end bucket = KBucket(0, 10, 3) nodes = [mknode(), mknode(), mknode()] for node in nodes: bucket.addNode(node) for index, node in enumerate(bucket.getNodes()): self.assertEqual(node, nodes[index])
def test_addContact(self): self.router.addContact(mknode()) self.assertTrue(len(self.router.buckets), 1) self.assertTrue(len(self.router.buckets[0].nodes), 1)
def setUp(self): self.id = mknode().id self.protocol = FakeProtocol(self.id) self.router = self.protocol.router
def test_inRange(self): bucket = KBucket(0, 10, 10) self.assertTrue(bucket.hasInRange(mknode(intid=5))) self.assertFalse(bucket.hasInRange(mknode(intid=11))) self.assertTrue(bucket.hasInRange(mknode(intid=10))) self.assertTrue(bucket.hasInRange(mknode(intid=0)))