mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-09 14:40:53 +00:00
Rewrite factories, made some of the test running
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
import trio
|
||||
|
||||
@ -12,25 +10,26 @@ from libp2p.tools.constants import MAX_READ_LEN, LISTEN_MADDR
|
||||
from libp2p.tools.factories import SwarmFactory
|
||||
from libp2p.tools.utils import connect_swarm
|
||||
|
||||
|
||||
DATA = b"data_123"
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_read_write(nursery):
|
||||
swarm0, swarm1 = SwarmFactory(), SwarmFactory()
|
||||
await swarm0.listen(LISTEN_MADDR, nursery=nursery)
|
||||
await swarm1.listen(LISTEN_MADDR, nursery=nursery)
|
||||
await connect_swarm(swarm0, swarm1, nursery)
|
||||
conn_0 = swarm0.connections[swarm1.get_peer_id()]
|
||||
conn_1 = swarm1.connections[swarm0.get_peer_id()]
|
||||
stream_0 = await conn_0.muxed_conn.open_stream()
|
||||
await trio.sleep(1)
|
||||
stream_1 = tuple(conn_1.muxed_conn.streams.values())[0]
|
||||
await stream_0.write(DATA)
|
||||
assert (await stream_1.read(MAX_READ_LEN)) == DATA
|
||||
async def test_mplex_stream_read_write():
|
||||
async with SwarmFactory.create_batch_and_listen(False, 2) as swarms:
|
||||
await swarms[0].listen(LISTEN_MADDR)
|
||||
await swarms[1].listen(LISTEN_MADDR)
|
||||
await connect_swarm(swarms[0], swarms[1])
|
||||
conn_0 = swarms[0].connections[swarms[1].get_peer_id()]
|
||||
conn_1 = swarms[1].connections[swarms[0].get_peer_id()]
|
||||
stream_0 = await conn_0.muxed_conn.open_stream()
|
||||
await trio.sleep(1)
|
||||
stream_1 = tuple(conn_1.muxed_conn.streams.values())[0]
|
||||
await stream_0.write(DATA)
|
||||
assert (await stream_1.read(MAX_READ_LEN)) == DATA
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_pair_read_until_eof(mplex_stream_pair):
|
||||
read_bytes = bytearray()
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
@ -38,43 +37,43 @@ 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 = asyncio.ensure_future(read_until_eof())
|
||||
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 asyncio.sleep(0.01)
|
||||
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 asyncio.sleep(0.01)
|
||||
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()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
assert read_bytes == expected_data
|
||||
|
||||
task.cancel()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@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()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
assert stream_1.event_remote_closed.is_set()
|
||||
assert (await stream_1.read(MAX_READ_LEN)) == DATA
|
||||
with pytest.raises(MplexStreamEOF):
|
||||
await stream_1.read(MAX_READ_LEN)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_read_after_local_reset(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_0.reset()
|
||||
@ -82,29 +81,29 @@ async def test_mplex_stream_read_after_local_reset(mplex_stream_pair):
|
||||
await stream_0.read(MAX_READ_LEN)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_read_after_remote_reset(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_0.write(DATA)
|
||||
await stream_0.reset()
|
||||
# Sleep to let `stream_1` receive the message.
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
with pytest.raises(MplexStreamReset):
|
||||
await stream_1.read(MAX_READ_LEN)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_read_after_remote_closed_and_reset(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_0.write(DATA)
|
||||
await stream_0.close()
|
||||
await stream_0.reset()
|
||||
# Sleep to let `stream_1` receive the message.
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
assert (await stream_1.read(MAX_READ_LEN)) == DATA
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_write_after_local_closed(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_0.write(DATA)
|
||||
@ -113,7 +112,7 @@ async def test_mplex_stream_write_after_local_closed(mplex_stream_pair):
|
||||
await stream_0.write(DATA)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_write_after_local_reset(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_0.reset()
|
||||
@ -121,16 +120,16 @@ async def test_mplex_stream_write_after_local_reset(mplex_stream_pair):
|
||||
await stream_0.write(DATA)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_write_after_remote_reset(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_1.reset()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
with pytest.raises(MplexStreamClosed):
|
||||
await stream_0.write(DATA)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_both_close(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
# Flags are not set initially.
|
||||
@ -144,7 +143,7 @@ async def test_mplex_stream_both_close(mplex_stream_pair):
|
||||
|
||||
# Test: Close one side.
|
||||
await stream_0.close()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
|
||||
assert stream_0.event_local_closed.is_set()
|
||||
assert not stream_1.event_local_closed.is_set()
|
||||
@ -156,7 +155,7 @@ async def test_mplex_stream_both_close(mplex_stream_pair):
|
||||
|
||||
# Test: Close the other side.
|
||||
await stream_1.close()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
# Both sides are closed.
|
||||
assert stream_0.event_local_closed.is_set()
|
||||
assert stream_1.event_local_closed.is_set()
|
||||
@ -170,11 +169,11 @@ async def test_mplex_stream_both_close(mplex_stream_pair):
|
||||
await stream_0.reset()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.trio
|
||||
async def test_mplex_stream_reset(mplex_stream_pair):
|
||||
stream_0, stream_1 = mplex_stream_pair
|
||||
await stream_0.reset()
|
||||
await asyncio.sleep(0.01)
|
||||
await trio.sleep(0.01)
|
||||
|
||||
# Both sides are closed.
|
||||
assert stream_0.event_local_closed.is_set()
|
||||
|
||||
Reference in New Issue
Block a user