py-libp2p/tests/libp2p/test_notify.py

154 lines
4.7 KiB
Python
Raw Normal View History

2019-03-01 08:17:11 +08:00
import pytest
2019-03-01 08:08:13 +08:00
from tests.utils import cleanup
from libp2p import new_node
from libp2p.network.notifee_interface import INotifee
# pylint: disable=too-many-locals
"""
Test Notify and Notifee by ensuring that the proper events get
2019-03-01 08:17:11 +08:00
called, and that the stream passed into opened_stream is correct
2019-03-01 08:08:13 +08:00
Note: Listen event does not get hit because MyNotifee is passed
into network after network has already started listening
TODO: Add tests to ensure conn is the correct connection
2019-03-01 08:17:11 +08:00
TODO: Add tests for closed_stream disconnected, listen_close when those
features are implemented in swarm
2019-03-01 08:08:13 +08:00
"""
2019-03-01 08:17:11 +08:00
2019-03-01 08:08:13 +08:00
class MyNotifee(INotifee):
# pylint: disable=too-many-instance-attributes, cell-var-from-loop
def __init__(self, events, val_to_append_to_event):
self.events = events
self.val_to_append_to_event = val_to_append_to_event
2019-03-01 08:11:04 +08:00
async def opened_stream(self, network, stream):
2019-03-01 08:08:13 +08:00
self.events.append(["opened_stream" + \
self.val_to_append_to_event, stream])
2019-03-01 08:11:04 +08:00
async def closed_stream(self, network, stream):
2019-03-01 08:08:13 +08:00
pass
2019-03-01 08:11:04 +08:00
async def connected(self, network, conn):
2019-03-03 20:34:49 +08:00
self.events.append(["connected" + self.val_to_append_to_event,\
conn])
2019-03-01 08:08:13 +08:00
2019-03-01 08:11:04 +08:00
async def disconnected(self, network, conn):
2019-03-01 08:08:13 +08:00
pass
2019-03-01 08:11:04 +08:00
async def listen(self, network, multiaddr):
2019-03-01 08:08:13 +08:00
pass
2019-03-01 08:11:04 +08:00
async def listen_close(self, network, multiaddr):
2019-03-01 08:08:13 +08:00
pass
async def perform_two_host_simple_set_up():
2019-03-01 08:08:13 +08:00
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"])
2019-03-03 20:49:28 +08:00
async def my_stream_handler(stream):
2019-03-01 08:08:13 +08:00
while True:
read_string = (await stream.read()).decode()
2019-03-03 20:49:28 +08:00
resp = "ack:" + read_string
await stream.write(resp.encode())
2019-03-01 08:08:13 +08:00
2019-03-03 20:49:28 +08:00
node_b.set_stream_handler("/echo/1.0.0", my_stream_handler)
2019-03-01 08:08:13 +08:00
# 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
@pytest.mark.asyncio
async def test_one_notifier():
node_a, node_b = await perform_two_host_simple_set_up()
2019-03-01 08:08:13 +08:00
# Add notifee for node_a
events = []
node_a.get_network().notify(MyNotifee(events, "0"))
stream = await node_a.new_stream(node_b.get_id(), ["/echo/1.0.0"])
# Ensure the connected and opened_stream events were hit in MyNotifee obj
# and that stream passed into opened_stream matches the stream created on
# node_a
2019-03-03 20:34:49 +08:00
assert events == [["connected0", stream.mplex_conn], \
["opened_stream0", stream]]
2019-03-01 08:08:13 +08:00
messages = ["hello", "hello"]
for message in messages:
await stream.write(message.encode())
response = (await stream.read()).decode()
assert response == ("ack:" + message)
# Success, terminate pending tasks.
await cleanup()
@pytest.mark.asyncio
async def test_two_notifiers():
node_a, node_b = await perform_two_host_simple_set_up()
2019-03-01 08:08:13 +08:00
# Add notifee for node_a
events0 = []
node_a.get_network().notify(MyNotifee(events0, "0"))
events1 = []
node_a.get_network().notify(MyNotifee(events1, "1"))
stream = await node_a.new_stream(node_b.get_id(), ["/echo/1.0.0"])
# Ensure the connected and opened_stream events were hit in both Notifee objs
# and that the stream passed into opened_stream matches the stream created on
# node_a
2019-03-03 20:34:49 +08:00
assert events0 == [["connected0", stream.mplex_conn], ["opened_stream0", stream]]
assert events1 == [["connected1", stream.mplex_conn], ["opened_stream1", stream]]
2019-03-01 08:08:13 +08:00
messages = ["hello", "hello"]
for message in messages:
await stream.write(message.encode())
response = (await stream.read()).decode()
assert response == ("ack:" + message)
# Success, terminate pending tasks.
await cleanup()
@pytest.mark.asyncio
async def test_ten_notifiers():
num_notifiers = 10
node_a, node_b = await perform_two_host_simple_set_up()
2019-03-01 08:08:13 +08:00
# Add notifee for node_a
events_lst = []
for i in range(num_notifiers):
events_lst.append([])
node_a.get_network().notify(MyNotifee(events_lst[i], str(i)))
stream = await node_a.new_stream(node_b.get_id(), ["/echo/1.0.0"])
# Ensure the connected and opened_stream events were hit in both Notifee objs
# and that the stream passed into opened_stream matches the stream created on
# node_a
for i in range(num_notifiers):
2019-03-03 20:34:49 +08:00
assert events_lst[i] == [["connected" + str(i), stream.mplex_conn], \
2019-03-01 08:08:13 +08:00
["opened_stream" + str(i), stream]]
messages = ["hello", "hello"]
for message in messages:
await stream.write(message.encode())
response = (await stream.read()).decode()
assert response == ("ack:" + message)
# Success, terminate pending tasks.
2019-03-01 08:17:11 +08:00
await cleanup()