py-libp2p/tests/network/test_notify.py

117 lines
3.6 KiB
Python
Raw Normal View History

"""
Test Notify and Notifee by ensuring that the proper events get 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
2019-09-17 21:54:20 +08:00
TODO: Add tests for closed_stream, listen_close when those
2019-03-01 08:17:11 +08:00
features are implemented in swarm
2019-03-01 08:08:13 +08:00
"""
2019-09-17 21:54:20 +08:00
import enum
2019-12-01 16:51:06 +08:00
from async_service import background_trio_service
import pytest
import trio
2019-12-01 16:51:06 +08:00
2019-08-03 13:36:19 +08:00
from libp2p.network.notifee_interface import INotifee
from libp2p.tools.constants import LISTEN_MADDR
from libp2p.tools.factories import SwarmFactory
from libp2p.tools.utils import connect_swarm
2019-09-17 21:54:20 +08:00
2019-09-06 17:26:40 +08:00
2019-09-17 21:54:20 +08:00
class Event(enum.Enum):
OpenedStream = 0
ClosedStream = 1 # Not implemented
Connected = 2
Disconnected = 3
Listen = 4
ListenClose = 5 # Not implemented
2019-03-03 20:54:04 +08:00
2019-03-01 08:08:13 +08:00
class MyNotifee(INotifee):
2019-09-17 21:54:20 +08:00
def __init__(self, events):
2019-03-01 08:08:13 +08:00
self.events = events
2019-03-01 08:11:04 +08:00
async def opened_stream(self, network, stream):
2019-09-17 21:54:20 +08:00
self.events.append(Event.OpenedStream)
2019-03-01 08:08:13 +08:00
2019-03-01 08:11:04 +08:00
async def closed_stream(self, network, stream):
2019-09-17 21:54:20 +08:00
# TODO: It is not implemented yet.
2019-03-01 08:08:13 +08:00
pass
2019-03-01 08:11:04 +08:00
async def connected(self, network, conn):
2019-09-17 21:54:20 +08:00
self.events.append(Event.Connected)
2019-03-01 08:08:13 +08:00
2019-03-01 08:11:04 +08:00
async def disconnected(self, network, conn):
2019-09-17 21:54:20 +08:00
self.events.append(Event.Disconnected)
2019-03-01 08:08:13 +08:00
2019-03-18 09:30:56 +08:00
async def listen(self, network, _multiaddr):
2019-09-17 21:54:20 +08:00
self.events.append(Event.Listen)
2019-03-01 08:08:13 +08:00
2019-03-18 09:30:56 +08:00
async def listen_close(self, network, _multiaddr):
2019-09-17 21:54:20 +08:00
# TODO: It is not implemented yet.
2019-03-01 08:08:13 +08:00
pass
[WIP] PubSub and FloodSub development (#133) * Add notifee interface * Add notify function to network interface * Implement notify feature * Add tests for notify * Make notifee functions all async * Fix linting issue * Fix linting issue * Scaffold pubsub router interface * Scaffold pubsub directory * Store peer_id in muxed connection * Implement pubsub notifee * Remove outdated files * Implement pubsub first attempt * Prepare pubsub for floodsub * Add mplex conn to net stream and add conn in notify tests * Implement floodsub * Use NetStream in generic protocol handler * Debugging async issues * Modify test to perform proper assert. Test passes * Remove callbacks. Reduce sleep time * Add simple three node test * Clean up code. Add message classes * Add test for two topics * Add conn to net stream and conn tests * Refactor test setup to remove duplicate code * Fix linting issues * Fix linting issue * Fix linting issue * Fix outstanding unrelated lint issue in multiselect_client * Add connect function * Remove debug prints * Remove debug prints from floodsub * Use MessageTalk in place of direct message breakdown * Remove extra prints * Remove outdated function * Add message to queues for all topics in message * Debugging * Add message self delivery * Increase read timeout to 5 to get pubsub tests passing * Refactor testing helper func. Add tests * Add tests and increase timeout to get tests passing * Add dummy account demo scaffolding * Attempt to use threads. Test fails * Implement basic dummy node tests using threads * Add generic testing function * Add simple seven node tree test * Add more complex seven node tree tests * Add five node ring tests * Remove unnecessary get_message_type func * Add documentation to classes * Add message id to messages * Add documentation to test helper func * Add docs to dummy account node helper func * Add more docs to dummy account node test helper func * fixed linting errors in floodsub * small notify bugfix * move pubsub into libp2p * fixed pubsub linting * fixing pubsub test failures * linting
2019-03-24 01:52:02 +08:00
2019-12-01 16:51:06 +08:00
@pytest.mark.trio
async def test_notify(security_protocol):
swarms = [SwarmFactory(security_protocol=security_protocol) for _ in range(2)]
2019-09-17 21:54:20 +08:00
events_0_0 = []
events_1_0 = []
events_0_without_listen = []
2019-12-01 16:51:06 +08:00
# Run swarms.
async with background_trio_service(swarms[0]), background_trio_service(swarms[1]):
# Register events before listening, to allow `MyNotifee` is notified with the event
# `listen`.
swarms[0].register_notifee(MyNotifee(events_0_0))
swarms[1].register_notifee(MyNotifee(events_1_0))
# Listen
async with trio.open_nursery() as nursery:
nursery.start_soon(swarms[0].listen, LISTEN_MADDR)
nursery.start_soon(swarms[1].listen, LISTEN_MADDR)
swarms[0].register_notifee(MyNotifee(events_0_without_listen))
# Connected
await connect_swarm(swarms[0], swarms[1])
# OpenedStream: first
await swarms[0].new_stream(swarms[1].get_peer_id())
# OpenedStream: second
await swarms[0].new_stream(swarms[1].get_peer_id())
# OpenedStream: third, but different direction.
await swarms[1].new_stream(swarms[0].get_peer_id())
await trio.sleep(0.01)
# TODO: Check `ClosedStream` and `ListenClose` events after they are ready.
# Disconnected
await swarms[0].close_peer(swarms[1].get_peer_id())
await trio.sleep(0.01)
# Connected again, but different direction.
await connect_swarm(swarms[1], swarms[0])
await trio.sleep(0.01)
# Disconnected again, but different direction.
await swarms[1].close_peer(swarms[0].get_peer_id())
await trio.sleep(0.01)
expected_events_without_listen = [
Event.Connected,
Event.OpenedStream,
Event.OpenedStream,
Event.OpenedStream,
Event.Disconnected,
Event.Connected,
Event.Disconnected,
]
expected_events = [Event.Listen] + expected_events_without_listen
assert events_0_0 == expected_events
assert events_1_0 == expected_events
assert events_0_without_listen == expected_events_without_listen