fix existing tests

This commit is contained in:
zixuanzh 2019-03-17 19:33:40 -04:00
parent 72a8d55faa
commit 1886258fbf
4 changed files with 34 additions and 56 deletions

View File

@ -1,7 +1,8 @@
import pytest import pytest
import asyncio import asyncio
import multiaddr
from tests.utils import cleanup from tests.utils import cleanup, set_up_nodes_by_transport_opt
from libp2p import new_node from libp2p import new_node
from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.peer.peerinfo import info_from_p2p_addr
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
@ -9,7 +10,6 @@ from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
PROTOCOL_ID = '/chat/1.0.0' PROTOCOL_ID = '/chat/1.0.0'
async def hello_world(host_a, host_b): async def hello_world(host_a, host_b):
async def stream_handler(stream): async def stream_handler(stream):
read = await stream.read() read = await stream.read()
@ -100,8 +100,8 @@ async def no_common_protocol(host_a, host_b):
(no_common_protocol), (no_common_protocol),
]) ])
async def test_chat(test): async def test_chat(test):
host_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
host_b = await new_node(transport_opt=["/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] addr = host_a.get_addrs()[0]
info = info_from_p2p_addr(addr) info = info_from_p2p_addr(addr)

View File

@ -1,17 +1,16 @@
import multiaddr import multiaddr
import pytest import pytest
from tests.utils import cleanup from tests.utils import cleanup, set_up_nodes_by_transport_opt
from libp2p import new_node from libp2p import new_node
from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.peer.peerinfo import info_from_p2p_addr
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_simple_messages(): async def test_simple_messages():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def stream_handler(stream): async def stream_handler(stream):
while True: while True:
@ -41,8 +40,8 @@ async def test_simple_messages():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_double_response(): async def test_double_response():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def stream_handler(stream): async def stream_handler(stream):
while True: while True:
@ -78,8 +77,8 @@ async def test_double_response():
async def test_multiple_streams(): async def test_multiple_streams():
# Node A should be able to open a stream with node B and then vice versa. # Node A should be able to open a stream with node B and then vice versa.
# Stream IDs should be generated uniquely so that the stream state is not overwritten # Stream IDs should be generated uniquely so that the stream state is not overwritten
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def stream_handler_a(stream): async def stream_handler_a(stream):
while True: while True:
@ -124,8 +123,8 @@ async def test_multiple_streams():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_multiple_streams_same_initiator_different_protocols(): async def test_multiple_streams_same_initiator_different_protocols():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def stream_handler_a1(stream): async def stream_handler_a1(stream):
while True: while True:
@ -184,8 +183,8 @@ async def test_multiple_streams_same_initiator_different_protocols():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_multiple_streams_two_initiators(): async def test_multiple_streams_two_initiators():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def stream_handler_a1(stream): async def stream_handler_a1(stream):
while True: while True:
@ -262,9 +261,9 @@ async def test_multiple_streams_two_initiators():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_triangle_nodes_connection(): async def test_triangle_nodes_connection():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"],\
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) ["/ip4/127.0.0.1/tcp/0"]]
node_c = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b, node_c) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def stream_handler(stream): async def stream_handler(stream):
while True: while True:
@ -315,8 +314,8 @@ async def test_triangle_nodes_connection():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_host_connect(): async def test_host_connect():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
assert not node_a.get_peerstore().peers() assert not node_a.get_peerstore().peers()

View File

@ -11,7 +11,7 @@ features are implemented in swarm
import pytest import pytest
from tests.utils import cleanup from tests.utils import *
from libp2p import new_node from libp2p import new_node
from libp2p.network.notifee_interface import INotifee from libp2p.network.notifee_interface import INotifee
@ -65,26 +65,9 @@ class InvalidNotifee():
async def listen(self): async def listen(self):
assert False assert False
async def perform_two_host_simple_set_up(): async def perform_two_host_set_up_custom_handler(handler):
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
async def my_stream_handler(stream):
while True:
read_string = (await stream.read()).decode()
resp = "ack:" + read_string
await stream.write(resp.encode())
node_b.set_stream_handler("/echo/1.0.0", my_stream_handler)
# Associate the peer with local ip address (see default parameters of Libp2p())
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
return node_a, node_b
async def perform_two_host_simple_set_up_custom_handler(handler):
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
node_b.set_stream_handler("/echo/1.0.0", handler) node_b.set_stream_handler("/echo/1.0.0", handler)
@ -94,7 +77,7 @@ async def perform_two_host_simple_set_up_custom_handler(handler):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_one_notifier(): async def test_one_notifier():
node_a, node_b = await perform_two_host_simple_set_up() node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler)
# Add notifee for node_a # Add notifee for node_a
events = [] events = []
@ -135,7 +118,7 @@ async def test_one_notifier_on_two_nodes():
resp = "ack:" + read_string resp = "ack:" + read_string
await stream.write(resp.encode()) await stream.write(resp.encode())
node_a, node_b = await perform_two_host_simple_set_up_custom_handler(my_stream_handler) node_a, node_b = await perform_two_host_set_up_custom_handler(my_stream_handler)
# Add notifee for node_a # Add notifee for node_a
events_a = [] events_a = []
@ -165,7 +148,7 @@ async def test_one_notifier_on_two_nodes():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_two_notifiers(): async def test_two_notifiers():
node_a, node_b = await perform_two_host_simple_set_up() node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler)
# Add notifee for node_a # Add notifee for node_a
events0 = [] events0 = []
@ -198,7 +181,7 @@ async def test_two_notifiers():
async def test_ten_notifiers(): async def test_ten_notifiers():
num_notifiers = 10 num_notifiers = 10
node_a, node_b = await perform_two_host_simple_set_up() node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler)
# Add notifee for node_a # Add notifee for node_a
events_lst = [] events_lst = []
@ -244,7 +227,7 @@ async def test_ten_notifiers_on_two_nodes():
resp = "ack:" + read_string resp = "ack:" + read_string
await stream.write(resp.encode()) await stream.write(resp.encode())
node_a, node_b = await perform_two_host_simple_set_up_custom_handler(my_stream_handler) node_a, node_b = await perform_two_host_set_up_custom_handler(my_stream_handler)
# Add notifee for node_a and node_b # Add notifee for node_a and node_b
events_lst_a = [] events_lst_a = []
@ -278,7 +261,7 @@ async def test_ten_notifiers_on_two_nodes():
async def test_invalid_notifee(): async def test_invalid_notifee():
num_notifiers = 10 num_notifiers = 10
node_a, node_b = await perform_two_host_simple_set_up() node_a, node_b = await perform_two_host_set_up_custom_handler(echo_stream_handler)
# Add notifee for node_a # Add notifee for node_a
events_lst = [] events_lst = []

View File

@ -1,6 +1,6 @@
import pytest import pytest
from tests.utils import cleanup from tests.utils import cleanup, set_up_nodes_by_transport_opt
from libp2p import new_node from libp2p import new_node
from libp2p.protocol_muxer.multiselect_client import MultiselectClientError from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
@ -15,12 +15,8 @@ from libp2p.protocol_muxer.multiselect_client import MultiselectClientError
async def perform_simple_test(expected_selected_protocol, async def perform_simple_test(expected_selected_protocol,
protocols_for_client, protocols_with_handlers): protocols_for_client, protocols_with_handlers):
transport_opt_a = ["/ip4/127.0.0.1/tcp/0"] transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
transport_opt_b = ["/ip4/127.0.0.1/tcp/0"] (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
node_a = await new_node(
transport_opt=transport_opt_a)
node_b = await new_node(
transport_opt=transport_opt_b)
async def stream_handler(stream): async def stream_handler(stream):
while True: while True: