py-libp2p/tests/kademlia/test_basic.py

80 lines
2.4 KiB
Python
Raw Normal View History

2019-03-27 03:44:01 +08:00
import pytest
2019-08-03 13:36:19 +08:00
from libp2p.kademlia.network import KademliaServer
2019-03-27 03:44:01 +08:00
@pytest.mark.asyncio
async def test_example():
node_a = KademliaServer()
await node_a.listen()
2019-03-27 03:44:01 +08:00
node_b = KademliaServer()
await node_b.listen()
2019-03-27 03:44:01 +08:00
# 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([node_a.address])
2019-03-27 03:44:01 +08:00
# 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
2019-08-01 06:00:12 +08:00
@pytest.mark.parametrize("nodes_nr", [(2 ** i) for i in range(2, 5)])
2019-03-27 03:44:01 +08:00
@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
2019-08-01 06:00:12 +08:00
@pytest.mark.parametrize("nodes_nr", [(2 ** i) for i in range(2, 5)])
2019-03-27 03:44:01 +08:00
@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