Move test utilities to tools (#356)

* move test factories to libp2p/tools

* remove unused inits

* move pubsub test utils to tools

* cleanup test_interop

* fix typing libp2p/tools/utils

* add typing to pubsub utils

* fix factories typing

* fix typing for floodsub_integration_test_settings

* fix rest of the typing

* fix isort
This commit is contained in:
Chih Cheng Liang 2019-11-21 11:47:54 +08:00 committed by GitHub
parent 74198c70b1
commit bcd7890124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 171 additions and 198 deletions

View File

@ -1,4 +1,4 @@
FILES_TO_LINT = libp2p tests examples setup.py
FILES_TO_LINT = libp2p tests tests_interop examples setup.py
PB = libp2p/crypto/pb/crypto.proto \
libp2p/pubsub/pb/rpc.proto \
libp2p/security/insecure/pb/plaintext.proto \

View File

@ -117,7 +117,7 @@ async def new_node(
sec_opt: TSecurityOptions = None,
peerstore_opt: IPeerStore = None,
disc_opt: IPeerRouting = None,
) -> IHost:
) -> BasicHost:
"""
create new libp2p node.

View File

@ -25,6 +25,7 @@ from libp2p.peer.id import ID as PeerID
from libp2p.security.base_session import BaseSession
from libp2p.security.base_transport import BaseSecureTransport
from libp2p.security.secure_conn_interface import ISecureConn
from libp2p.typing import TProtocol
from .exceptions import (
IncompatibleChoices,
@ -37,7 +38,7 @@ from .exceptions import (
)
from .pb.spipe_pb2 import Exchange, Propose
ID = "/secio/1.0.0"
ID = TProtocol("/secio/1.0.0")
NONCE_SIZE = 16 # bytes

View File

@ -1,7 +1,18 @@
from typing import NamedTuple
import multiaddr
from libp2p.pubsub import floodsub, gossipsub
# Just a arbitrary large number.
# It is used when calling `MplexStream.read(MAX_READ_LEN)`,
# to avoid `MplexStream.read()`, which blocking reads until EOF.
MAX_READ_LEN = 2 ** 32 - 1
LISTEN_MADDR = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/0")
FLOODSUB_PROTOCOL_ID = floodsub.PROTOCOL_ID
GOSSIPSUB_PROTOCOL_ID = gossipsub.PROTOCOL_ID

View File

@ -1,6 +1,6 @@
import asyncio
from contextlib import asynccontextmanager
from typing import Dict, Tuple
from typing import Any, AsyncIterator, Dict, Tuple, cast
import factory
@ -23,13 +23,14 @@ from libp2p.transport.tcp.tcp import TCP
from libp2p.transport.typing import TMuxerOptions
from libp2p.transport.upgrader import TransportUpgrader
from libp2p.typing import TProtocol
from tests.configs import LISTEN_MADDR
from tests.pubsub.configs import (
from .constants import (
FLOODSUB_PROTOCOL_ID,
GOSSIPSUB_PARAMS,
GOSSIPSUB_PROTOCOL_ID,
LISTEN_MADDR,
)
from tests.utils import connect, connect_swarm
from .utils import connect, connect_swarm
def security_transport_factory(
@ -66,7 +67,7 @@ class SwarmFactory(factory.Factory):
# `factory.Factory.__init__` does *not* prepare a *default value* if we pass
# an argument explicitly with `None`. If an argument is `None`, we don't pass it to
# `factory.Factory.__init__`, in order to let the function initialize it.
optional_kwargs = {}
optional_kwargs: Dict[str, Any] = {}
if key_pair is not None:
optional_kwargs["key_pair"] = key_pair
if muxer_opt is not None:
@ -79,7 +80,8 @@ class SwarmFactory(factory.Factory):
async def create_batch_and_listen(
cls, is_secure: bool, number: int, muxer_opt: TMuxerOptions = None
) -> Tuple[Swarm, ...]:
return await asyncio.gather(
# Ignore typing since we are removing asyncio soon
return await asyncio.gather( # type: ignore
*[
cls.create_and_listen(is_secure=is_secure, muxer_opt=muxer_opt)
for _ in range(number)
@ -158,14 +160,16 @@ async def swarm_pair_factory(
return swarms[0], swarms[1]
async def host_pair_factory(is_secure) -> Tuple[BasicHost, BasicHost]:
async def host_pair_factory(is_secure: bool) -> Tuple[BasicHost, BasicHost]:
hosts = await HostFactory.create_batch_and_listen(is_secure, 2)
await connect(hosts[0], hosts[1])
return hosts[0], hosts[1]
@asynccontextmanager
async def pair_of_connected_hosts(is_secure=True):
async def pair_of_connected_hosts(
is_secure: bool = True
) -> AsyncIterator[Tuple[BasicHost, BasicHost]]:
a, b = await host_pair_factory(is_secure)
yield a, b
close_tasks = (a.close(), b.close())
@ -178,7 +182,7 @@ async def swarm_conn_pair_factory(
swarms = await swarm_pair_factory(is_secure)
conn_0 = swarms[0].connections[swarms[1].get_peer_id()]
conn_1 = swarms[1].connections[swarms[0].get_peer_id()]
return conn_0, swarms[0], conn_1, swarms[1]
return cast(SwarmConn, conn_0), swarms[0], cast(SwarmConn, conn_1), swarms[1]
async def mplex_conn_pair_factory(is_secure: bool) -> Tuple[Mplex, Swarm, Mplex, Swarm]:
@ -186,7 +190,12 @@ async def mplex_conn_pair_factory(is_secure: bool) -> Tuple[Mplex, Swarm, Mplex,
conn_0, swarm_0, conn_1, swarm_1 = await swarm_conn_pair_factory(
is_secure, muxer_opt=muxer_opt
)
return conn_0.muxed_conn, swarm_0, conn_1.muxed_conn, swarm_1
return (
cast(Mplex, conn_0.muxed_conn),
swarm_0,
cast(Mplex, conn_1.muxed_conn),
swarm_1,
)
async def mplex_stream_pair_factory(
@ -202,13 +211,13 @@ async def mplex_stream_pair_factory(
if len(mplex_conn_1.streams) != 1:
raise Exception("Mplex should not have any stream upon connection")
stream_1 = tuple(mplex_conn_1.streams.values())[0]
return stream_0, swarm_0, stream_1, swarm_1
return cast(MplexStream, stream_0), swarm_0, stream_1, swarm_1
async def net_stream_pair_factory(
is_secure: bool
) -> Tuple[INetStream, BasicHost, INetStream, BasicHost]:
protocol_id = "/example/id/1"
protocol_id = TProtocol("/example/id/1")
stream_1: INetStream

View File

@ -1,6 +1,6 @@
import asyncio
import time
from typing import Any, List
from typing import Any, Awaitable, Callable, List
import multiaddr
from multiaddr import Multiaddr
@ -19,7 +19,9 @@ P2PD_PATH = GO_BIN_PATH / "p2pd"
TIMEOUT_DURATION = 30
async def try_until_success(coro_func, timeout=TIMEOUT_DURATION):
async def try_until_success(
coro_func: Callable[[], Awaitable[Any]], timeout: int = TIMEOUT_DURATION
) -> None:
"""
Keep running ``coro_func`` until either it succeed or time is up.
@ -78,11 +80,11 @@ class P2PDProcess:
self._tasks = []
async def wait_until_ready(self):
async def wait_until_ready(self) -> None:
lines_head_pattern = (b"Control socket:", b"Peer ID:", b"Peer Addrs:")
lines_head_occurred = {line: False for line in lines_head_pattern}
async def read_from_daemon_and_check():
async def read_from_daemon_and_check() -> bool:
line = await self.proc.stdout.readline()
for head_pattern in lines_head_occurred:
if line.startswith(head_pattern):
@ -166,10 +168,10 @@ async def make_p2pd(
daemon_control_port: int,
client_callback_port: int,
is_secure: bool,
is_pubsub_enabled=True,
is_gossipsub=True,
is_pubsub_signing=False,
is_pubsub_signing_strict=False,
is_pubsub_enabled: bool = True,
is_gossipsub: bool = True,
is_pubsub_signing: bool = False,
is_pubsub_signing_strict: bool = False,
) -> Daemon:
control_maddr = Multiaddr(f"/ip4/{LOCALHOST_IP}/tcp/{daemon_control_port}")
p2pd_proc = P2PDProcess(

View File

@ -1,13 +1,12 @@
import asyncio
from typing import Dict
import uuid
from libp2p.host.host_interface import IHost
from libp2p.pubsub.floodsub import FloodSub
from libp2p.pubsub.pubsub import Pubsub
from tests.configs import LISTEN_MADDR
from tests.factories import FloodsubFactory, PubsubFactory
from .utils import message_id_generator
from libp2p.tools.constants import LISTEN_MADDR
from libp2p.tools.factories import FloodsubFactory, PubsubFactory
CRYPTO_TOPIC = "ethereum"
@ -36,12 +35,11 @@ class DummyAccountNode:
self.libp2p_node = libp2p_node
self.pubsub = pubsub
self.floodsub = floodsub
self.balances = {}
self.next_msg_id_func = message_id_generator(0)
self.balances: Dict[str, int] = {}
self.node_id = str(uuid.uuid1())
@classmethod
async def create(cls):
async def create(cls) -> "DummyAccountNode":
"""
Create a new DummyAccountNode and attach a libp2p node, a floodsub, and
a pubsub instance to this new node.
@ -54,7 +52,7 @@ class DummyAccountNode:
await pubsub.host.get_network().listen(LISTEN_MADDR)
return cls(libp2p_node=pubsub.host, pubsub=pubsub, floodsub=pubsub.router)
async def handle_incoming_msgs(self):
async def handle_incoming_msgs(self) -> None:
"""Handle all incoming messages on the CRYPTO_TOPIC from peers."""
while True:
incoming = await self.q.get()
@ -65,14 +63,16 @@ class DummyAccountNode:
elif msg_comps[0] == "set":
self.handle_set_crypto(msg_comps[1], int(msg_comps[2]))
async def setup_crypto_networking(self):
async def setup_crypto_networking(self) -> None:
"""Subscribe to CRYPTO_TOPIC and perform call to function that handles
all incoming messages on said topic."""
self.q = await self.pubsub.subscribe(CRYPTO_TOPIC)
asyncio.ensure_future(self.handle_incoming_msgs())
async def publish_send_crypto(self, source_user, dest_user, amount):
async def publish_send_crypto(
self, source_user: str, dest_user: str, amount: int
) -> None:
"""
Create a send crypto message and publish that message to all other
nodes.
@ -84,7 +84,7 @@ class DummyAccountNode:
msg_contents = "send," + source_user + "," + dest_user + "," + str(amount)
await self.pubsub.publish(CRYPTO_TOPIC, msg_contents.encode())
async def publish_set_crypto(self, user, amount):
async def publish_set_crypto(self, user: str, amount: int) -> None:
"""
Create a set crypto message and publish that message to all other
nodes.
@ -95,7 +95,7 @@ class DummyAccountNode:
msg_contents = "set," + user + "," + str(amount)
await self.pubsub.publish(CRYPTO_TOPIC, msg_contents.encode())
def handle_send_crypto(self, source_user, dest_user, amount):
def handle_send_crypto(self, source_user: str, dest_user: str, amount: int) -> None:
"""
Handle incoming send_crypto message.
@ -113,7 +113,7 @@ class DummyAccountNode:
else:
self.balances[dest_user] = amount
def handle_set_crypto(self, dest_user, amount):
def handle_set_crypto(self, dest_user: str, amount: int) -> None:
"""
Handle incoming set_crypto message.
@ -122,7 +122,7 @@ class DummyAccountNode:
"""
self.balances[dest_user] = amount
def get_balance(self, user):
def get_balance(self, user: str) -> int:
"""
Get balance in crypto for a particular user.

View File

@ -1,12 +1,13 @@
# type: ignore
# To add typing to this module, it's better to do it after refactoring test cases into classes
import asyncio
import pytest
from tests.configs import LISTEN_MADDR
from tests.factories import PubsubFactory
from tests.utils import connect
from .configs import FLOODSUB_PROTOCOL_ID
from libp2p.tools.constants import FLOODSUB_PROTOCOL_ID, LISTEN_MADDR
from libp2p.tools.factories import PubsubFactory
from libp2p.tools.utils import connect
SUPPORTED_PROTOCOLS = [FLOODSUB_PROTOCOL_ID]
@ -142,7 +143,7 @@ floodsub_protocol_pytest_params = [
]
async def perform_test_from_obj(obj, router_factory):
async def perform_test_from_obj(obj, router_factory) -> None:
"""
Perform pubsub tests from a test obj.
test obj are composed as follows:

View File

@ -0,0 +1,32 @@
from typing import Sequence
from libp2p.host.host_interface import IHost
from libp2p.peer.id import ID
from libp2p.pubsub.pb import rpc_pb2
from libp2p.tools.utils import connect
def make_pubsub_msg(
origin_id: ID, topic_ids: Sequence[str], data: bytes, seqno: bytes
) -> rpc_pb2.Message:
return rpc_pb2.Message(
from_id=origin_id.to_bytes(), seqno=seqno, data=data, topicIDs=list(topic_ids)
)
# TODO: Implement sparse connect
async def dense_connect(hosts: Sequence[IHost]) -> None:
await connect_some(hosts, 10)
# FIXME: `degree` is not used at all
async def connect_some(hosts: Sequence[IHost], degree: int) -> None:
for i, host in enumerate(hosts):
for host2 in hosts[i + 1 :]:
await connect(host, host2)
async def one_to_all_connect(hosts: Sequence[IHost], central_host_index: int) -> None:
for i, host in enumerate(hosts):
if i != central_host_index:
await connect(hosts[central_host_index], host)

View File

@ -1,13 +1,22 @@
from typing import List, Sequence, Tuple
import multiaddr
from libp2p import new_node
from libp2p.host.basic_host import BasicHost
from libp2p.host.host_interface import IHost
from libp2p.kademlia.network import KademliaServer
from libp2p.network.stream.net_stream_interface import INetStream
from libp2p.network.swarm import Swarm
from libp2p.peer.peerinfo import info_from_p2p_addr
from libp2p.routing.interfaces import IPeerRouting
from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
from tests.constants import MAX_READ_LEN
from libp2p.typing import StreamHandlerFn, TProtocol
from .constants import MAX_READ_LEN
async def connect_swarm(swarm_0, swarm_1):
async def connect_swarm(swarm_0: Swarm, swarm_1: Swarm) -> None:
peer_id = swarm_1.get_peer_id()
addrs = tuple(
addr
@ -20,14 +29,16 @@ async def connect_swarm(swarm_0, swarm_1):
assert swarm_1.get_peer_id() in swarm_0.connections
async def connect(node1, node2):
async def connect(node1: IHost, node2: IHost) -> None:
"""Connect node1 to node2."""
addr = node2.get_addrs()[0]
info = info_from_p2p_addr(addr)
await node1.connect(info)
async def set_up_nodes_by_transport_opt(transport_opt_list):
async def set_up_nodes_by_transport_opt(
transport_opt_list: Sequence[Sequence[str]]
) -> Tuple[BasicHost, ...]:
nodes_list = []
for transport_opt in transport_opt_list:
node = await new_node(transport_opt=transport_opt)
@ -36,7 +47,9 @@ async def set_up_nodes_by_transport_opt(transport_opt_list):
return tuple(nodes_list)
async def set_up_nodes_by_transport_and_disc_opt(transport_disc_opt_list):
async def set_up_nodes_by_transport_and_disc_opt(
transport_disc_opt_list: Sequence[Tuple[Sequence[str], IPeerRouting]]
) -> Tuple[BasicHost, ...]:
nodes_list = []
for transport_opt, disc_opt in transport_disc_opt_list:
node = await new_node(transport_opt=transport_opt, disc_opt=disc_opt)
@ -45,15 +58,17 @@ async def set_up_nodes_by_transport_and_disc_opt(transport_disc_opt_list):
return tuple(nodes_list)
async def set_up_routers(router_confs=(0, 0)):
async def set_up_routers(
router_confs: Tuple[int, int] = (0, 0)
) -> List[KadmeliaPeerRouter]:
"""The default ``router_confs`` selects two free ports local to this
machine."""
bootstrap_node = KademliaServer()
bootstrap_node = KademliaServer() # type: ignore
await bootstrap_node.listen(router_confs[0])
routers = [KadmeliaPeerRouter(bootstrap_node)]
for port in router_confs[1:]:
node = KademliaServer()
node = KademliaServer() # type: ignore
await node.listen(port)
await node.bootstrap_node(bootstrap_node.address)
@ -61,7 +76,7 @@ async def set_up_routers(router_confs=(0, 0)):
return routers
async def echo_stream_handler(stream):
async def echo_stream_handler(stream: INetStream) -> None:
while True:
read_string = (await stream.read(MAX_READ_LEN)).decode()
@ -69,11 +84,13 @@ async def echo_stream_handler(stream):
await stream.write(resp.encode())
async def perform_two_host_set_up(handler=echo_stream_handler):
async def perform_two_host_set_up(
handler: StreamHandlerFn = echo_stream_handler
) -> Tuple[BasicHost, BasicHost]:
transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
(node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
node_b.set_stream_handler("/echo/1.0.0", handler)
node_b.set_stream_handler(TProtocol("/echo/1.0.0"), 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)

View File

@ -1,3 +0,0 @@
import multiaddr
LISTEN_MADDR = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/0")

View File

@ -2,8 +2,8 @@ import asyncio
import pytest
from .configs import LISTEN_MADDR
from .factories import HostFactory
from libp2p.tools.constants import LISTEN_MADDR
from libp2p.tools.factories import HostFactory
@pytest.fixture

View File

@ -1,4 +0,0 @@
# Just a arbitrary large number.
# It is used when calling `MplexStream.read(MAX_READ_LEN)`,
# to avoid `MplexStream.read()`, which blocking reads until EOF.
MAX_READ_LEN = 2 ** 32 - 1

View File

@ -4,7 +4,7 @@ import pytest
from libp2p.host.exceptions import StreamFailure
from libp2p.peer.peerinfo import info_from_p2p_addr
from tests.utils import set_up_nodes_by_transport_opt
from libp2p.tools.utils import set_up_nodes_by_transport_opt
PROTOCOL_ID = "/chat/1.0.0"

View File

@ -4,7 +4,7 @@ import secrets
import pytest
from libp2p.host.ping import ID, PING_LENGTH
from tests.factories import pair_of_connected_hosts
from libp2p.tools.factories import pair_of_connected_hosts
@pytest.mark.asyncio

View File

@ -5,7 +5,7 @@ import pytest
from libp2p.host.exceptions import ConnectionFailure
from libp2p.peer.peerinfo import PeerInfo
from libp2p.routing.kademlia.kademlia_peer_router import peer_info_to_str
from tests.utils import (
from libp2p.tools.utils import (
set_up_nodes_by_transport_and_disc_opt,
set_up_nodes_by_transport_opt,
set_up_routers,

View File

@ -2,7 +2,7 @@ import pytest
from libp2p.identity.identify.pb.identify_pb2 import Identify
from libp2p.identity.identify.protocol import ID, _mk_identify_protobuf
from tests.factories import pair_of_connected_hosts
from libp2p.tools.factories import pair_of_connected_hosts
@pytest.mark.asyncio

View File

@ -2,8 +2,8 @@ import multiaddr
import pytest
from libp2p.peer.peerinfo import info_from_p2p_addr
from tests.constants import MAX_READ_LEN
from tests.utils import set_up_nodes_by_transport_opt
from libp2p.tools.constants import MAX_READ_LEN
from libp2p.tools.utils import set_up_nodes_by_transport_opt
@pytest.mark.asyncio

View File

@ -2,7 +2,7 @@ import asyncio
import pytest
from tests.factories import (
from libp2p.tools.factories import (
net_stream_pair_factory,
swarm_conn_pair_factory,
swarm_pair_factory,

View File

@ -3,7 +3,7 @@ import asyncio
import pytest
from libp2p.network.stream.exceptions import StreamClosed, StreamEOF, StreamReset
from tests.constants import MAX_READ_LEN
from libp2p.tools.constants import MAX_READ_LEN
DATA = b"data_123"

View File

@ -15,9 +15,9 @@ import enum
import pytest
from libp2p.network.notifee_interface import INotifee
from tests.configs import LISTEN_MADDR
from tests.factories import SwarmFactory
from tests.utils import connect_swarm
from libp2p.tools.constants import LISTEN_MADDR
from libp2p.tools.factories import SwarmFactory
from libp2p.tools.utils import connect_swarm
class Event(enum.Enum):

View File

@ -3,8 +3,8 @@ import asyncio
import pytest
from libp2p.network.exceptions import SwarmException
from tests.factories import SwarmFactory
from tests.utils import connect_swarm
from libp2p.tools.factories import SwarmFactory
from libp2p.tools.utils import connect_swarm
@pytest.mark.asyncio

View File

View File

@ -1,7 +1,7 @@
import pytest
from libp2p.host.exceptions import StreamFailure
from tests.utils import echo_stream_handler, set_up_nodes_by_transport_opt
from libp2p.tools.utils import echo_stream_handler, set_up_nodes_by_transport_opt
# TODO: Add tests for multiple streams being opened on different
# protocols through the same connection

View File

@ -1,8 +1,7 @@
import pytest
from tests.factories import FloodsubFactory, GossipsubFactory, PubsubFactory
from .configs import GOSSIPSUB_PARAMS
from libp2p.tools.constants import GOSSIPSUB_PARAMS
from libp2p.tools.factories import FloodsubFactory, GossipsubFactory, PubsubFactory
def _make_pubsubs(hosts, pubsub_routers, cache_size):

View File

@ -3,9 +3,8 @@ from threading import Thread
import pytest
from tests.utils import connect
from .dummy_account_node import DummyAccountNode
from libp2p.tools.pubsub.dummy_account_node import DummyAccountNode
from libp2p.tools.utils import connect
def create_setup_in_new_thread_func(dummy_node):

View File

@ -3,13 +3,12 @@ import asyncio
import pytest
from libp2p.peer.id import ID
from tests.factories import FloodsubFactory
from tests.utils import connect
from .floodsub_integration_test_settings import (
from libp2p.tools.factories import FloodsubFactory
from libp2p.tools.pubsub.floodsub_integration_test_settings import (
floodsub_protocol_pytest_params,
perform_test_from_obj,
)
from libp2p.tools.utils import connect
@pytest.mark.parametrize("num_hosts", (2,))

View File

@ -3,10 +3,9 @@ import random
import pytest
from tests.utils import connect
from .configs import GossipsubParams
from .utils import dense_connect, one_to_all_connect
from libp2p.tools.constants import GossipsubParams
from libp2p.tools.pubsub.utils import dense_connect, one_to_all_connect
from libp2p.tools.utils import connect
@pytest.mark.parametrize(

View File

@ -2,10 +2,9 @@ import functools
import pytest
from tests.factories import GossipsubFactory
from .configs import FLOODSUB_PROTOCOL_ID
from .floodsub_integration_test_settings import (
from libp2p.tools.constants import FLOODSUB_PROTOCOL_ID
from libp2p.tools.factories import GossipsubFactory
from libp2p.tools.pubsub.floodsub_integration_test_settings import (
floodsub_protocol_pytest_params,
perform_test_from_obj,
)

View File

@ -6,10 +6,9 @@ import pytest
from libp2p.exceptions import ValidationError
from libp2p.peer.id import ID
from libp2p.pubsub.pb import rpc_pb2
from libp2p.tools.pubsub.utils import make_pubsub_msg
from libp2p.tools.utils import connect
from libp2p.utils import encode_varint_prefixed
from tests.utils import connect
from .utils import make_pubsub_msg
TESTING_TOPIC = "TEST_SUBSCRIBE"
TESTING_DATA = b"data"

View File

@ -1,83 +0,0 @@
import struct
from typing import Sequence
from libp2p.peer.id import ID
from libp2p.pubsub.pb import rpc_pb2
from tests.utils import connect
def message_id_generator(start_val):
"""
Generate a unique message id.
:param start_val: value to start generating messages at
:return: message id
"""
val = start_val
def generator():
# Allow manipulation of val within closure
nonlocal val
# Increment id
val += 1
# Convert val to big endian
return struct.pack(">Q", val)
return generator
def make_pubsub_msg(
origin_id: ID, topic_ids: Sequence[str], data: bytes, seqno: bytes
) -> rpc_pb2.Message:
return rpc_pb2.Message(
from_id=origin_id.to_bytes(), seqno=seqno, data=data, topicIDs=list(topic_ids)
)
# FIXME: There is no difference between `sparse_connect` and `dense_connect`,
# before `connect_some` is fixed.
async def sparse_connect(hosts):
await connect_some(hosts, 3)
async def dense_connect(hosts):
await connect_some(hosts, 10)
async def connect_all(hosts):
for i, host in enumerate(hosts):
for host2 in hosts[i + 1 :]:
await connect(host, host2)
# FIXME: `degree` is not used at all
async def connect_some(hosts, degree):
for i, host in enumerate(hosts):
for host2 in hosts[i + 1 :]:
await connect(host, host2)
# TODO: USE THE CODE BELOW
# for i, host in enumerate(hosts):
# j = 0
# while j < degree:
# n = random.randint(0, len(hosts) - 1)
# if n == i:
# j -= 1
# continue
# neighbor = hosts[n]
# await connect(host, neighbor)
# j += 1
async def one_to_all_connect(hosts, central_host_index):
for i, host in enumerate(hosts):
if i != central_host_index:
await connect(hosts[central_host_index], host)

View File

@ -5,8 +5,8 @@ import pytest
from libp2p import new_node
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.security.insecure.transport import InsecureSession, InsecureTransport
from tests.configs import LISTEN_MADDR
from tests.utils import connect
from libp2p.tools.constants import LISTEN_MADDR
from libp2p.tools.utils import connect
# TODO: Add tests for multiple streams being opened on different
# protocols through the same connection

View File

@ -2,7 +2,7 @@ import asyncio
import pytest
from tests.factories import mplex_conn_pair_factory, mplex_stream_pair_factory
from libp2p.tools.factories import mplex_conn_pair_factory, mplex_stream_pair_factory
@pytest.fixture

View File

@ -7,7 +7,7 @@ from libp2p.stream_muxer.mplex.exceptions import (
MplexStreamEOF,
MplexStreamReset,
)
from tests.constants import MAX_READ_LEN
from libp2p.tools.constants import MAX_READ_LEN
DATA = b"data_123"

View File

@ -7,17 +7,15 @@ import pexpect
import pytest
from libp2p.io.abc import ReadWriteCloser
from tests.configs import LISTEN_MADDR
from tests.factories import (
from libp2p.tools.constants import GOSSIPSUB_PARAMS, LISTEN_MADDR
from libp2p.tools.factories import (
FloodsubFactory,
GossipsubFactory,
HostFactory,
PubsubFactory,
)
from tests.pubsub.configs import GOSSIPSUB_PARAMS
from .daemon import Daemon, make_p2pd
from .utils import connect
from libp2p.tools.interop.daemon import Daemon, make_p2pd
from libp2p.tools.interop.utils import connect
@pytest.fixture

View File

@ -2,7 +2,7 @@ import asyncio
import pytest
from .utils import connect
from libp2p.tools.interop.utils import connect
@pytest.mark.parametrize("num_hosts", (1,))

View File

@ -4,11 +4,10 @@ from multiaddr import Multiaddr
import pytest
from libp2p.peer.peerinfo import info_from_p2p_addr
from libp2p.tools.interop.constants import PEXPECT_NEW_LINE
from libp2p.tools.interop.envs import GO_BIN_PATH
from libp2p.typing import TProtocol
from .constants import PEXPECT_NEW_LINE
from .envs import GO_BIN_PATH
ECHO_PATH = GO_BIN_PATH / "echo"
ECHO_PROTOCOL_ID = TProtocol("/echo/1.0.0")

View File

@ -3,7 +3,7 @@ import asyncio
import pytest
from libp2p.network.stream.exceptions import StreamClosed, StreamEOF, StreamReset
from tests.constants import MAX_READ_LEN
from libp2p.tools.constants import MAX_READ_LEN
DATA = b"data"

View File

@ -6,10 +6,9 @@ import pytest
from libp2p.peer.id import ID
from libp2p.pubsub.pb import rpc_pb2
from libp2p.tools.interop.utils import connect
from libp2p.utils import read_varint_prefixed_bytes
from .utils import connect
TOPIC_0 = "ABALA"
TOPIC_1 = "YOOOO"