py-libp2p/tests/kademlia/test_basic.py

79 lines
2.4 KiB
Python
Raw Normal View History

2019-03-27 03:44:01 +08:00
import pytest
from libp2p.kademlia.network import KademliaServer
2019-03-27 03:44:01 +08:00
@pytest.mark.asyncio
async def test_example():
node_a = KademliaServer()
2019-03-27 03:44:01 +08:00
await node_a.listen(5678)
node_b = KademliaServer()
2019-03-27 03:44:01 +08:00
await node_b.listen(5679)
# 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
# give as many ip/port combos as you can for other nodes.
await node_b.bootstrap([("127.0.0.1", 5678)])
# set a value for the key "my-key" on the network
value = "my-value"
key = "my-key"
await node_b.set(key, value)
# get the value associated with "my-key" from the network
assert await node_b.get(key) == value
assert await node_a.get(key) == value
@pytest.mark.parametrize("nodes_nr", [(2**i) for i in range(2, 5)])
@pytest.mark.asyncio
async def test_multiple_nodes_bootstrap_set_get(nodes_nr):
node_bootstrap = KademliaServer()
2019-04-18 09:21:17 +08:00
await node_bootstrap.listen(3000 + nodes_nr * 2)
2019-03-27 03:44:01 +08:00
nodes = []
for i in range(nodes_nr):
node = KademliaServer()
2019-04-18 09:21:17 +08:00
addrs = [("127.0.0.1", 3000 + nodes_nr * 2)]
await node.listen(3001 + i + nodes_nr * 2)
2019-03-27 03:44:01 +08:00
await node.bootstrap(addrs)
nodes.append(node)
for i, node in enumerate(nodes):
# set a value for the key "my-key" on the network
value = "my awesome value %d" % i
key = "set from %d" % i
await node.set(key, value)
for i in range(nodes_nr):
for node in nodes:
value = "my awesome value %d" % i
key = "set from %d" % i
assert await node.get(key) == value
@pytest.mark.parametrize("nodes_nr", [(2**i) for i in range(2, 5)])
@pytest.mark.asyncio
async def test_multiple_nodes_set_bootstrap_get(nodes_nr):
node_bootstrap = KademliaServer()
await node_bootstrap.listen(2000 + nodes_nr * 2)
2019-04-18 09:44:45 +08:00
2019-03-27 03:44:01 +08:00
nodes = []
for i in range(nodes_nr):
node = KademliaServer()
addrs = [("127.0.0.1", 2000 + nodes_nr * 2)]
await node.listen(2001 + i + nodes_nr * 2)
2019-03-27 03:44:01 +08:00
await node.bootstrap(addrs)
value = "my awesome value %d" % i
key = "set from %d" % i
await node.set(key, value)
nodes.append(node)
for i in range(nodes_nr):
for node in nodes:
value = "my awesome value %d" % i
key = "set from %d" % i
assert await node.get(key) == value