Avoid hard-coding ports where it is not relevant for Kademlia tests

This commit is contained in:
Alex Stokes 2019-11-05 17:08:18 -08:00
parent 5cb4479534
commit 94984be4df
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
4 changed files with 13 additions and 10 deletions

View File

@ -14,7 +14,7 @@ from tests.utils import (
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_host_routing_success(): async def test_host_routing_success():
routers = await set_up_routers([5678, 5679]) routers = await set_up_routers()
transports = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] transports = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
transport_disc_opt_list = zip(transports, routers) transport_disc_opt_list = zip(transports, routers)
(host_a, host_b) = await set_up_nodes_by_transport_and_disc_opt( (host_a, host_b) = await set_up_nodes_by_transport_and_disc_opt(
@ -43,7 +43,7 @@ async def test_host_routing_success():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_host_routing_fail(): async def test_host_routing_fail():
routers = await set_up_routers([5678, 5679]) routers = await set_up_routers()
transports = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] transports = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
transport_disc_opt_list = zip(transports, routers) transport_disc_opt_list = zip(transports, routers)
(host_a, host_b) = await set_up_nodes_by_transport_and_disc_opt( (host_a, host_b) = await set_up_nodes_by_transport_and_disc_opt(

View File

@ -6,15 +6,15 @@ from libp2p.kademlia.network import KademliaServer
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_example(): async def test_example():
node_a = KademliaServer() node_a = KademliaServer()
await node_a.listen(5678) await node_a.listen()
node_b = KademliaServer() node_b = KademliaServer()
await node_b.listen(5679) await node_b.listen()
# Bootstrap the node by connecting to other known nodes, in this case # Bootstrap the node by connecting to other known nodes, in this case
# replace 123.123.123.123 with the IP of another node and optionally # replace 123.123.123.123 with the IP of another node and optionally
# give as many ip/port combos as you can for other nodes. # give as many ip/port combos as you can for other nodes.
await node_b.bootstrap([("127.0.0.1", 5678)]) await node_b.bootstrap([node_a.address])
# set a value for the key "my-key" on the network # set a value for the key "my-key" on the network
value = "my-value" value = "my-value"

View File

@ -6,11 +6,11 @@ from libp2p.kademlia.network import KademliaServer
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_example(): async def test_example():
node_a = KademliaServer() node_a = KademliaServer()
await node_a.listen(5801) await node_a.listen()
node_b = KademliaServer() node_b = KademliaServer()
await node_b.listen(5802) await node_b.listen()
await node_b.bootstrap([("127.0.0.1", 5801)]) await node_b.bootstrap([node_a.address])
key = "hello" key = "hello"
value = "world" value = "world"

View File

@ -45,7 +45,10 @@ async def set_up_nodes_by_transport_and_disc_opt(transport_disc_opt_list):
return tuple(nodes_list) return tuple(nodes_list)
async def set_up_routers(router_confs): async def set_up_routers(router_confs=[0, 0]):
"""
The default ``router_confs`` selects two free ports local to this machine.
"""
bootstrap_node = KademliaServer() bootstrap_node = KademliaServer()
await bootstrap_node.listen(router_confs[0]) await bootstrap_node.listen(router_confs[0])
@ -54,7 +57,7 @@ async def set_up_routers(router_confs):
node = KademliaServer() node = KademliaServer()
await node.listen(port) await node.listen(port)
await node.bootstrap_node(("127.0.0.1", router_confs[0])) await node.bootstrap_node(bootstrap_node.address)
routers.append(KadmeliaPeerRouter(node)) routers.append(KadmeliaPeerRouter(node))
return routers return routers