diff --git a/tests/host/test_ping.py b/tests/host/test_ping.py index a5296f13..37975a86 100644 --- a/tests/host/test_ping.py +++ b/tests/host/test_ping.py @@ -4,25 +4,18 @@ import secrets import pytest from libp2p.host.ping import ID, PING_LENGTH -from libp2p.peer.peerinfo import info_from_p2p_addr -from tests.utils import set_up_nodes_by_transport_opt +from tests.factories import pair_of_connected_hosts @pytest.mark.asyncio async def test_ping_once(): - transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] - (host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list) - - addr = host_a.get_addrs()[0] - info = info_from_p2p_addr(addr) - await host_b.connect(info) - - stream = await host_b.new_stream(host_a.get_id(), (ID,)) - some_ping = secrets.token_bytes(PING_LENGTH) - await stream.write(some_ping) - some_pong = await stream.read(PING_LENGTH) - assert some_ping == some_pong - await stream.close() + async with pair_of_connected_hosts() 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) + some_pong = await stream.read(PING_LENGTH) + assert some_ping == some_pong + await stream.close() SOME_PING_COUNT = 3 @@ -30,21 +23,15 @@ SOME_PING_COUNT = 3 @pytest.mark.asyncio async def test_ping_several(): - transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] - (host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list) - - addr = host_a.get_addrs()[0] - info = info_from_p2p_addr(addr) - await host_b.connect(info) - - stream = await host_b.new_stream(host_a.get_id(), (ID,)) - for _ in range(SOME_PING_COUNT): - some_ping = secrets.token_bytes(PING_LENGTH) - await stream.write(some_ping) - some_pong = await stream.read(PING_LENGTH) - assert some_ping == some_pong - # 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 stream.close() + async with pair_of_connected_hosts() 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) + await stream.write(some_ping) + some_pong = await stream.read(PING_LENGTH) + assert some_ping == some_pong + # 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 stream.close() diff --git a/tests/identity/identify/test_protocol.py b/tests/identity/identify/test_protocol.py index 71faf673..e36e7ca5 100644 --- a/tests/identity/identify/test_protocol.py +++ b/tests/identity/identify/test_protocol.py @@ -2,23 +2,16 @@ import pytest from libp2p.identity.identify.pb.identify_pb2 import Identify from libp2p.identity.identify.protocol import ID, _mk_identify_protobuf -from libp2p.peer.peerinfo import info_from_p2p_addr -from tests.utils import set_up_nodes_by_transport_opt +from tests.factories import pair_of_connected_hosts @pytest.mark.asyncio async def test_identify_protocol(): - transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] - (host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list) + async with pair_of_connected_hosts() as (host_a, host_b): + stream = await host_b.new_stream(host_a.get_id(), (ID,)) + response = await stream.read() + await stream.close() - addr = host_a.get_addrs()[0] - info = info_from_p2p_addr(addr) - await host_b.connect(info) - - stream = await host_b.new_stream(host_a.get_id(), (ID,)) - response = await stream.read() - await stream.close() - - identify_response = Identify() - identify_response.ParseFromString(response) - assert identify_response == _mk_identify_protobuf(host_a) + identify_response = Identify() + identify_response.ParseFromString(response) + assert identify_response == _mk_identify_protobuf(host_a)