mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Fix Mplex and Swarm
This commit is contained in:
@ -1,8 +1,5 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from libp2p.tools.constants import LISTEN_MADDR
|
||||
from libp2p.tools.factories import HostFactory
|
||||
|
||||
|
||||
@ -17,17 +14,6 @@ def num_hosts():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def hosts(num_hosts, is_host_secure):
|
||||
_hosts = HostFactory.create_batch(num_hosts, is_secure=is_host_secure)
|
||||
await asyncio.gather(
|
||||
*[_host.get_network().listen(LISTEN_MADDR) for _host in _hosts]
|
||||
)
|
||||
try:
|
||||
async def hosts(num_hosts, is_host_secure, nursery):
|
||||
async with HostFactory.create_batch_and_listen(is_host_secure, num_hosts) as _hosts:
|
||||
yield _hosts
|
||||
finally:
|
||||
# TODO: It's possible that `close` raises exceptions currently,
|
||||
# due to the connection reset things. Though we don't care much about that when
|
||||
# cleaning up the tasks, it is probably better to handle the exceptions properly.
|
||||
await asyncio.gather(
|
||||
*[_host.close() for _host in _hosts], return_exceptions=True
|
||||
)
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from libp2p.tools.factories import (
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from libp2p.tools.factories import mplex_conn_pair_factory, mplex_stream_pair_factory
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import asyncio
|
||||
import trio
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_conn(mplex_conn_pair):
|
||||
conn_0, conn_1 = mplex_conn_pair
|
||||
|
||||
@ -16,19 +16,19 @@ async def test_mplex_conn(mplex_conn_pair):
|
||||
|
||||
# Test: Open a stream, and both side get 1 more stream.
|
||||
stream_0 = await conn_0.open_stream()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
assert len(conn_0.streams) == 1
|
||||
assert len(conn_1.streams) == 1
|
||||
# Test: From another side.
|
||||
stream_1 = await conn_1.open_stream()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
assert len(conn_0.streams) == 2
|
||||
assert len(conn_1.streams) == 2
|
||||
|
||||
# Close from one side.
|
||||
await conn_0.close()
|
||||
# Sleep for a while for both side to handle `close`.
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
# Test: Both side is closed.
|
||||
assert conn_0.event_shutting_down.is_set()
|
||||
assert conn_0.event_closed.is_set()
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import pytest
|
||||
import trio
|
||||
from trio.testing import wait_all_tasks_blocked
|
||||
|
||||
from libp2p.stream_muxer.mplex.exceptions import (
|
||||
MplexStreamClosed,
|
||||
@ -37,37 +38,65 @@ async def test_mplex_stream_pair_read_until_eof(mplex_stream_pair):
|
||||
async def read_until_eof():
|
||||
read_bytes.extend(await stream_1.read())
|
||||
|
||||
task = trio.ensure_future(read_until_eof())
|
||||
|
||||
expected_data = bytearray()
|
||||
|
||||
# Test: `read` doesn't return before `close` is called.
|
||||
await stream_0.write(DATA)
|
||||
expected_data.extend(DATA)
|
||||
await trio.sleep(0.01)
|
||||
assert len(read_bytes) == 0
|
||||
# Test: `read` doesn't return before `close` is called.
|
||||
await stream_0.write(DATA)
|
||||
expected_data.extend(DATA)
|
||||
await trio.sleep(0.01)
|
||||
assert len(read_bytes) == 0
|
||||
async with trio.open_nursery() as nursery:
|
||||
nursery.start_soon(read_until_eof)
|
||||
# Test: `read` doesn't return before `close` is called.
|
||||
await stream_0.write(DATA)
|
||||
expected_data.extend(DATA)
|
||||
await trio.sleep(0.01)
|
||||
assert len(read_bytes) == 0
|
||||
# Test: `read` doesn't return before `close` is called.
|
||||
await stream_0.write(DATA)
|
||||
expected_data.extend(DATA)
|
||||
await trio.sleep(0.01)
|
||||
assert len(read_bytes) == 0
|
||||
|
||||
# Test: Close the stream, `read` returns, and receive previous sent data.
|
||||
await stream_0.close()
|
||||
|
||||
# Test: Close the stream, `read` returns, and receive previous sent data.
|
||||
await stream_0.close()
|
||||
await trio.sleep(0.01)
|
||||
assert read_bytes == expected_data
|
||||
|
||||
task.cancel()
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_read_after_remote_closed(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
assert not stream_1.event_remote_closed.is_set()
|
||||
await stream_0.write(DATA)
|
||||
await stream_0.close()
|
||||
assert not stream_0.event_local_closed.is_set()
|
||||
await trio.sleep(0.01)
|
||||
await wait_all_tasks_blocked()
|
||||
await stream_0.close()
|
||||
assert stream_0.event_local_closed.is_set()
|
||||
await trio.sleep(0.01)
|
||||
print(
|
||||
"!@# ",
|
||||
stream_0.muxed_conn.event_shutting_down.is_set(),
|
||||
stream_0.muxed_conn.event_closed.is_set(),
|
||||
stream_1.muxed_conn.event_shutting_down.is_set(),
|
||||
stream_1.muxed_conn.event_closed.is_set(),
|
||||
)
|
||||
# await trio.sleep(100000)
|
||||
await wait_all_tasks_blocked()
|
||||
print(
|
||||
"!@# ",
|
||||
stream_0.muxed_conn.event_shutting_down.is_set(),
|
||||
stream_0.muxed_conn.event_closed.is_set(),
|
||||
stream_1.muxed_conn.event_shutting_down.is_set(),
|
||||
stream_1.muxed_conn.event_closed.is_set(),
|
||||
)
|
||||
print("!@# sleeping")
|
||||
print("!@# result=", stream_1.event_remote_closed.is_set())
|
||||
# await trio.sleep_forever()
|
||||
assert stream_1.event_remote_closed.is_set()
|
||||
print(
|
||||
"!@# ",
|
||||
stream_0.muxed_conn.event_shutting_down.is_set(),
|
||||
stream_0.muxed_conn.event_closed.is_set(),
|
||||
stream_1.muxed_conn.event_shutting_down.is_set(),
|
||||
stream_1.muxed_conn.event_closed.is_set(),
|
||||
)
|
||||
assert (await stream_1.read(MAX_READ_LEN)) == DATA
|
||||
with pytest.raises(MplexStreamEOF):
|
||||
await stream_1.read(MAX_READ_LEN)
|
||||
@ -87,7 +116,8 @@ async def test_mplex_stream_read_after_remote_reset(mplex_stream_pair):
|
||||
await stream_0.write(DATA)
|
||||
await stream_0.reset()
|
||||
# Sleep to let `stream_1` receive the message.
|
||||
await trio.sleep(0.01)
|
||||
await trio.sleep(0.1)
|
||||
await wait_all_tasks_blocked()
|
||||
with pytest.raises(MplexStreamReset):
|
||||
await stream_1.read(MAX_READ_LEN)
|
||||
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
from libp2p.utils import TrioQueue
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_trio_queue():
|
||||
queue = TrioQueue()
|
||||
|
||||
async def queue_get(task_status=None):
|
||||
result = await queue.get()
|
||||
task_status.started(result)
|
||||
|
||||
async with trio.open_nursery() as nursery:
|
||||
nursery.start_soon(queue.put, 123)
|
||||
result = await nursery.start(queue_get)
|
||||
|
||||
assert result == 123
|
||||
Reference in New Issue
Block a user