diff --git a/tests/interop/test_bindings.py b/tests/interop/test_bindings.py index 7844d4b..1189e0b 100644 --- a/tests/interop/test_bindings.py +++ b/tests/interop/test_bindings.py @@ -1,8 +1,7 @@ -import asyncio - -from multiaddr import Multiaddr import pytest +from .utils import connect + @pytest.mark.parametrize("num_hosts", (1,)) @pytest.mark.asyncio @@ -11,19 +10,15 @@ async def test_connect(hosts, p2pds): host = hosts[0] assert len(await p2pd.control.list_peers()) == 0 # Test: connect from Py - await host.connect(p2pd.peer_info) + await connect(host, p2pd) assert len(await p2pd.control.list_peers()) == 1 # Test: `disconnect` from Py await host.disconnect(p2pd.peer_id) assert len(await p2pd.control.list_peers()) == 0 # Test: connect from Go - py_peer_id = host.get_id() - await p2pd.control.connect( - host.get_id(), - [host.get_addrs()[0].decapsulate(Multiaddr(f"/p2p/{py_peer_id.to_string()}"))], - ) + await connect(p2pd, host) assert len(host.get_network().connections) == 1 # Test: `disconnect` from Go - await p2pd.control.disconnect(py_peer_id) + await p2pd.control.disconnect(host.get_id()) # FIXME: Failed to handle disconnect # assert len(host.get_network().connections) == 0 diff --git a/tests/interop/test_pubsub.py b/tests/interop/test_pubsub.py new file mode 100644 index 0000000..a9ad1f8 --- /dev/null +++ b/tests/interop/test_pubsub.py @@ -0,0 +1,7 @@ +import pytest + + +@pytest.mark.parametrize("num_hosts", (1,)) +@pytest.mark.asyncio +async def test_gossipsub(pubsubs_gsub, p2pds): + pass diff --git a/tests/interop/utils.py b/tests/interop/utils.py new file mode 100644 index 0000000..d613727 --- /dev/null +++ b/tests/interop/utils.py @@ -0,0 +1,40 @@ +import asyncio +from typing import Union + +from multiaddr import Multiaddr + +from libp2p.host.host_interface import IHost +from libp2p.peer.peerinfo import PeerInfo + +from .daemon import Daemon + +TDaemonOrHost = Union[IHost, Daemon] + + +async def connect(a: TDaemonOrHost, b: TDaemonOrHost) -> None: + # Type check + err_msg = ( + f"Type of type(a)={type(a)} or type(b)={type(b)} is wrong." + "Should be either `IHost` or `Daemon`" + ) + assert all( + [isinstance(node, IHost) or isinstance(node, Daemon) for node in (a, b)] + ), err_msg + + # TODO: Get peer info + peer_info: PeerInfo + if isinstance(b, Daemon): + peer_info = b.peer_info + else: # isinstance(b, IHost) + peer_id = b.get_id() + maddrs = [ + b.get_addrs()[0].decapsulate(Multiaddr(f"/p2p/{peer_id.to_string()}")) + ] + peer_info = PeerInfo(peer_id, maddrs) + # TODO: connect to peer info + if isinstance(a, Daemon): + await a.control.connect(peer_info.peer_id, peer_info.addrs) + else: # isinstance(b, IHost) + await a.connect(peer_info) + # Allow additional sleep for both side to establish the connection. + await asyncio.sleep(0.01)