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_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_removeNode(self): bucket = KBucket(0, 10, 2) self.assertTrue(bucket.addNode(mknode(intid=1))) self.assertTrue(bucket.addNode(mknode(intid=2))) bucket.removeNode(mknode(intid=1)) self.assertEqual(len(bucket), 1) bucket.replacementNodes.push(mknode(intid=3)) bucket.removeNode(mknode(intid=2)) self.assertEqual(len(bucket), 1)