mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
reorg test structure to match tox and CI jobs, drop bumpversion for bump-my-version and move config to pyproject.toml, fix docs building
This commit is contained in:
24
tests/core/host/test_basic_host.py
Normal file
24
tests/core/host/test_basic_host.py
Normal file
@ -0,0 +1,24 @@
|
||||
from libp2p import (
|
||||
new_swarm,
|
||||
)
|
||||
from libp2p.crypto.rsa import (
|
||||
create_new_key_pair,
|
||||
)
|
||||
from libp2p.host.basic_host import (
|
||||
BasicHost,
|
||||
)
|
||||
from libp2p.host.defaults import (
|
||||
get_default_protocols,
|
||||
)
|
||||
|
||||
|
||||
def test_default_protocols():
|
||||
key_pair = create_new_key_pair()
|
||||
swarm = new_swarm(key_pair)
|
||||
host = BasicHost(swarm)
|
||||
|
||||
mux = host.get_mux()
|
||||
handlers = mux.handlers
|
||||
# NOTE: comparing keys for equality as handlers may be closures that do not compare
|
||||
# in the way this test is concerned with
|
||||
assert handlers.keys() == get_default_protocols(host).keys()
|
||||
49
tests/core/host/test_ping.py
Normal file
49
tests/core/host/test_ping.py
Normal file
@ -0,0 +1,49 @@
|
||||
import secrets
|
||||
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
from libp2p.host.ping import (
|
||||
ID,
|
||||
PING_LENGTH,
|
||||
)
|
||||
from libp2p.tools.factories import (
|
||||
host_pair_factory,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_ping_once(security_protocol):
|
||||
async with host_pair_factory(security_protocol=security_protocol) as (
|
||||
host_a,
|
||||
host_b,
|
||||
):
|
||||
stream = await host_b.new_stream(host_a.get_id(), (ID,))
|
||||
some_ping = secrets.token_bytes(PING_LENGTH)
|
||||
await stream.write(some_ping)
|
||||
await trio.sleep(0.01)
|
||||
some_pong = await stream.read(PING_LENGTH)
|
||||
assert some_ping == some_pong
|
||||
await stream.close()
|
||||
|
||||
|
||||
SOME_PING_COUNT = 3
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_ping_several(security_protocol):
|
||||
async with host_pair_factory(security_protocol=security_protocol) as (
|
||||
host_a,
|
||||
host_b,
|
||||
):
|
||||
stream = await host_b.new_stream(host_a.get_id(), (ID,))
|
||||
for _ in range(SOME_PING_COUNT):
|
||||
some_ping = secrets.token_bytes(PING_LENGTH)
|
||||
await stream.write(some_ping)
|
||||
some_pong = await stream.read(PING_LENGTH)
|
||||
assert some_ping == some_pong
|
||||
# NOTE: simulate some time to sleep to mirror a real
|
||||
# world usage where a peer sends pings on some periodic interval
|
||||
# NOTE: this interval can be `0` for this test.
|
||||
await trio.sleep(0)
|
||||
await stream.close()
|
||||
32
tests/core/host/test_routed_host.py
Normal file
32
tests/core/host/test_routed_host.py
Normal file
@ -0,0 +1,32 @@
|
||||
import pytest
|
||||
|
||||
from libp2p.host.exceptions import (
|
||||
ConnectionFailure,
|
||||
)
|
||||
from libp2p.peer.peerinfo import (
|
||||
PeerInfo,
|
||||
)
|
||||
from libp2p.tools.factories import (
|
||||
HostFactory,
|
||||
RoutedHostFactory,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_host_routing_success():
|
||||
async with RoutedHostFactory.create_batch_and_listen(2) as hosts:
|
||||
# forces to use routing as no addrs are provided
|
||||
await hosts[0].connect(PeerInfo(hosts[1].get_id(), []))
|
||||
await hosts[1].connect(PeerInfo(hosts[0].get_id(), []))
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_host_routing_fail():
|
||||
async with RoutedHostFactory.create_batch_and_listen(
|
||||
2
|
||||
) as routed_hosts, HostFactory.create_batch_and_listen(1) as basic_hosts:
|
||||
# routing fails because host_c does not use routing
|
||||
with pytest.raises(ConnectionFailure):
|
||||
await routed_hosts[0].connect(PeerInfo(basic_hosts[0].get_id(), []))
|
||||
with pytest.raises(ConnectionFailure):
|
||||
await routed_hosts[1].connect(PeerInfo(basic_hosts[0].get_id(), []))
|
||||
Reference in New Issue
Block a user