mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Add tests for SwarmConn
This commit is contained in:
@ -2,7 +2,11 @@ import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.factories import net_stream_pair_factory, swarm_pair_factory
|
||||
from tests.factories import (
|
||||
net_stream_pair_factory,
|
||||
swarm_conn_pair_factory,
|
||||
swarm_pair_factory,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -21,3 +25,12 @@ async def swarm_pair(is_host_secure):
|
||||
yield swarm_0, swarm_1
|
||||
finally:
|
||||
await asyncio.gather(*[swarm_0.close(), swarm_1.close()])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def swarm_conn_pair(is_host_secure):
|
||||
conn_0, swarm_0, conn_1, swarm_1 = await swarm_conn_pair_factory(is_host_secure)
|
||||
try:
|
||||
yield conn_0, conn_1
|
||||
finally:
|
||||
await asyncio.gather(*[swarm_0.close(), swarm_1.close()])
|
||||
|
||||
43
tests/network/test_swarm_conn.py
Normal file
43
tests/network/test_swarm_conn.py
Normal file
@ -0,0 +1,43 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_swarm_conn_close(swarm_conn_pair):
|
||||
conn_0, conn_1 = swarm_conn_pair
|
||||
|
||||
assert not conn_0.event_closed.is_set()
|
||||
assert not conn_1.event_closed.is_set()
|
||||
|
||||
await conn_0.close()
|
||||
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
assert conn_0.event_closed.is_set()
|
||||
assert conn_1.event_closed.is_set()
|
||||
assert conn_0 not in conn_0.swarm.connections.values()
|
||||
assert conn_1 not in conn_1.swarm.connections.values()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_swarm_conn_streams(swarm_conn_pair):
|
||||
conn_0, conn_1 = swarm_conn_pair
|
||||
|
||||
assert len(await conn_0.get_streams()) == 0
|
||||
assert len(await conn_1.get_streams()) == 0
|
||||
|
||||
stream_0_0 = await conn_0.new_stream()
|
||||
await asyncio.sleep(0.01)
|
||||
assert len(await conn_0.get_streams()) == 1
|
||||
assert len(await conn_1.get_streams()) == 1
|
||||
|
||||
stream_0_1 = await conn_0.new_stream()
|
||||
await asyncio.sleep(0.01)
|
||||
assert len(await conn_0.get_streams()) == 2
|
||||
assert len(await conn_1.get_streams()) == 2
|
||||
|
||||
conn_0.remove_stream(stream_0_0)
|
||||
assert len(await conn_0.get_streams()) == 1
|
||||
conn_0.remove_stream(stream_0_1)
|
||||
assert len(await conn_0.get_streams()) == 0
|
||||
Reference in New Issue
Block a user