def test_routingtable_split_bucket(): table = kademlia.RoutingTable(random_node()) assert len(table.buckets) == 1 old_bucket = table.buckets[0] table.split_bucket(0) assert len(table.buckets) == 2 assert old_bucket not in table.buckets
def test_routingtable_remove_node(): table = kademlia.RoutingTable(random_node()) node1 = random_node() assert table.add_node(node1) is None assert node1 in table table.remove_node(node1) assert node1 not in table
def test_routingtable_add_node(): table = kademlia.RoutingTable(random_node()) for i in range(table.buckets[0].k): # As long as the bucket is not full, the new node is added to the bucket and None is # returned. assert table.add_node(random_node()) is None assert len(table.buckets) == 1 assert len(table) == i + 1 assert table.buckets[0].is_full # Now that the bucket is full, an add_node() should cause it to be split. assert table.add_node(random_node()) is None
def test_routingtable_edgecase_neighbours(): # tests the edge cases where sorting by midpoint distance is not enough table = kademlia.RoutingTable(random_node(0)) table.buckets = [kademlia.KBucket(0, 127), kademlia.KBucket(128, 1024)] table.add_node(random_node(126)) table.add_node(random_node(129)) assert [(b.start, b.end) for b in table.buckets_by_distance_to(128)] == [ (0, 127), (128, 1024), ] assert [n.id for n in table.neighbours(128)] == [129, 126]
def test_routingtable_get_random_nodes(): table = kademlia.RoutingTable(random_node()) for i in range(100): assert table.add_node(random_node()) is None nodes = list(table.get_random_nodes(50)) assert len(nodes) == 50 assert len(set(nodes)) == 50 # If we ask for more nodes than what the routing table contains, we'll get only what the # routing table contains, without duplicates. nodes = list(table.get_random_nodes(200)) assert len(nodes) == 100 assert len(set(nodes)) == 100
def test_routingtable_neighbours(): table = kademlia.RoutingTable(random_node()) for i in range(1000): assert table.add_node(random_node()) is None assert i == len(table) - 1 for i in range(100): node = random_node() nearest_bucket = table.buckets_by_distance_to(node.id)[0] if not nearest_bucket.nodes: continue # Change nodeid to something that is in this bucket's range. node_a = nearest_bucket.nodes[0] node_b = random_node(node_a.id + 1) assert node_a == table.neighbours(node_b.id)[0]
def test_routingtable_add_node_error(): table = kademlia.RoutingTable(random_node()) with pytest.raises(ValueError): table.add_node(random_node(kademlia.k_max_node_id + 1))
def test_routingtable_wrong_split(): table = kademlia.RoutingTable(random_node()) with pytest.raises(ValueError): for i in range(1000): table.split_bucket(0)