示例#1
0
 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)
示例#2
0
 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))
示例#3
0
    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)))
示例#4
0
    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)
示例#5
0
    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])
示例#6
0
 def test_addContact(self):
     self.router.addContact(mknode())
     self.assertTrue(len(self.router.buckets), 1)
     self.assertTrue(len(self.router.buckets[0].nodes), 1)
示例#7
0
 def setUp(self):
     self.id = mknode().id
     self.protocol = FakeProtocol(self.id)
     self.router = self.protocol.router
示例#8
0
 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)))