2019-09-10 17:51:39 +08:00
|
|
|
import pytest
|
2020-01-07 14:14:34 +08:00
|
|
|
import trio
|
2019-09-10 17:51:39 +08:00
|
|
|
|
|
|
|
from libp2p.network.stream.exceptions import StreamClosed, StreamEOF, StreamReset
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.tools.constants import MAX_READ_LEN
|
2019-09-10 17:51:39 +08:00
|
|
|
|
|
|
|
DATA = b"data"
|
|
|
|
|
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_read_write(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, stream_daemon = py_to_daemon_stream_pair
|
|
|
|
assert (
|
|
|
|
stream_py.protocol_id is not None
|
|
|
|
and stream_py.protocol_id == stream_daemon.stream_info.proto
|
|
|
|
)
|
|
|
|
await stream_py.write(DATA)
|
|
|
|
assert (await stream_daemon.read(MAX_READ_LEN)) == DATA
|
|
|
|
|
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_read_after_remote_closed(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, stream_daemon = py_to_daemon_stream_pair
|
|
|
|
await stream_daemon.write(DATA)
|
|
|
|
await stream_daemon.close()
|
2020-01-07 14:14:34 +08:00
|
|
|
await trio.sleep(0.01)
|
2019-09-10 17:51:39 +08:00
|
|
|
assert (await stream_py.read(MAX_READ_LEN)) == DATA
|
|
|
|
# EOF
|
|
|
|
with pytest.raises(StreamEOF):
|
|
|
|
await stream_py.read(MAX_READ_LEN)
|
|
|
|
|
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_read_after_local_reset(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, _ = py_to_daemon_stream_pair
|
|
|
|
await stream_py.reset()
|
|
|
|
with pytest.raises(StreamReset):
|
|
|
|
await stream_py.read(MAX_READ_LEN)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("is_to_fail_daemon_stream", (True,))
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_read_after_remote_reset(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, _ = py_to_daemon_stream_pair
|
2020-01-07 14:14:34 +08:00
|
|
|
await trio.sleep(0.01)
|
2019-09-10 17:51:39 +08:00
|
|
|
with pytest.raises(StreamReset):
|
|
|
|
await stream_py.read(MAX_READ_LEN)
|
|
|
|
|
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_write_after_local_closed(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, _ = py_to_daemon_stream_pair
|
|
|
|
await stream_py.write(DATA)
|
|
|
|
await stream_py.close()
|
|
|
|
with pytest.raises(StreamClosed):
|
|
|
|
await stream_py.write(DATA)
|
|
|
|
|
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_write_after_local_reset(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, stream_daemon = py_to_daemon_stream_pair
|
|
|
|
await stream_py.reset()
|
|
|
|
with pytest.raises(StreamClosed):
|
|
|
|
await stream_py.write(DATA)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("is_to_fail_daemon_stream", (True,))
|
2020-01-07 14:14:34 +08:00
|
|
|
@pytest.mark.trio
|
2019-09-10 17:51:39 +08:00
|
|
|
async def test_net_stream_write_after_remote_reset(py_to_daemon_stream_pair, p2pds):
|
|
|
|
stream_py, _ = py_to_daemon_stream_pair
|
2020-01-07 14:14:34 +08:00
|
|
|
await trio.sleep(0.01)
|
2019-09-10 17:51:39 +08:00
|
|
|
with pytest.raises(StreamClosed):
|
|
|
|
await stream_py.write(DATA)
|