Add connect utility function

This commit is contained in:
mhchia 2019-09-02 18:40:12 +08:00
parent 56ef0b962c
commit a883816881
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A
3 changed files with 52 additions and 10 deletions

View File

@ -1,8 +1,7 @@
import asyncio
from multiaddr import Multiaddr
import pytest import pytest
from .utils import connect
@pytest.mark.parametrize("num_hosts", (1,)) @pytest.mark.parametrize("num_hosts", (1,))
@pytest.mark.asyncio @pytest.mark.asyncio
@ -11,19 +10,15 @@ async def test_connect(hosts, p2pds):
host = hosts[0] host = hosts[0]
assert len(await p2pd.control.list_peers()) == 0 assert len(await p2pd.control.list_peers()) == 0
# Test: connect from Py # Test: connect from Py
await host.connect(p2pd.peer_info) await connect(host, p2pd)
assert len(await p2pd.control.list_peers()) == 1 assert len(await p2pd.control.list_peers()) == 1
# Test: `disconnect` from Py # Test: `disconnect` from Py
await host.disconnect(p2pd.peer_id) await host.disconnect(p2pd.peer_id)
assert len(await p2pd.control.list_peers()) == 0 assert len(await p2pd.control.list_peers()) == 0
# Test: connect from Go # Test: connect from Go
py_peer_id = host.get_id() await connect(p2pd, host)
await p2pd.control.connect(
host.get_id(),
[host.get_addrs()[0].decapsulate(Multiaddr(f"/p2p/{py_peer_id.to_string()}"))],
)
assert len(host.get_network().connections) == 1 assert len(host.get_network().connections) == 1
# Test: `disconnect` from Go # Test: `disconnect` from Go
await p2pd.control.disconnect(py_peer_id) await p2pd.control.disconnect(host.get_id())
# FIXME: Failed to handle disconnect # FIXME: Failed to handle disconnect
# assert len(host.get_network().connections) == 0 # assert len(host.get_network().connections) == 0

View File

@ -0,0 +1,7 @@
import pytest
@pytest.mark.parametrize("num_hosts", (1,))
@pytest.mark.asyncio
async def test_gossipsub(pubsubs_gsub, p2pds):
pass

40
tests/interop/utils.py Normal file
View File

@ -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)