Fix ping protocol

This commit is contained in:
mhchia
2019-12-01 19:17:32 +08:00
parent 6149aacc01
commit eb494e8682
4 changed files with 29 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import asyncio
import trio
import secrets
import pytest
@ -7,12 +7,13 @@ from libp2p.host.ping import ID, PING_LENGTH
from libp2p.tools.factories import host_pair_factory
@pytest.mark.asyncio
async def test_ping_once():
async with host_pair_factory() as (host_a, host_b):
@pytest.mark.trio
async def test_ping_once(is_host_secure):
async with host_pair_factory(is_host_secure) 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()
@ -21,9 +22,9 @@ async def test_ping_once():
SOME_PING_COUNT = 3
@pytest.mark.asyncio
async def test_ping_several():
async with host_pair_factory() as (host_a, host_b):
@pytest.mark.trio
async def test_ping_several(is_host_secure):
async with host_pair_factory(is_host_secure) 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)
@ -33,5 +34,5 @@ async def test_ping_several():
# 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 asyncio.sleep(0)
await trio.sleep(0)
await stream.close()