mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Add mplex tests and fix error in SwarmConn.close
This commit is contained in:
@ -15,6 +15,8 @@ from libp2p.pubsub.pubsub import Pubsub
|
||||
from libp2p.security.base_transport import BaseSecureTransport
|
||||
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport
|
||||
import libp2p.security.secio.transport as secio
|
||||
from libp2p.stream_muxer.mplex.mplex import MPLEX_PROTOCOL_ID, Mplex
|
||||
from libp2p.transport.typing import TMuxerOptions
|
||||
from libp2p.typing import TProtocol
|
||||
from tests.configs import LISTEN_MADDR
|
||||
from tests.pubsub.configs import (
|
||||
@ -34,10 +36,10 @@ def security_transport_factory(
|
||||
return {secio.ID: secio.Transport(key_pair)}
|
||||
|
||||
|
||||
def SwarmFactory(is_secure: bool) -> Swarm:
|
||||
def SwarmFactory(is_secure: bool, muxer_opt: TMuxerOptions = None) -> Swarm:
|
||||
key_pair = generate_new_rsa_identity()
|
||||
sec_opt = security_transport_factory(False, key_pair)
|
||||
return initialize_default_swarm(key_pair, sec_opt=sec_opt)
|
||||
sec_opt = security_transport_factory(is_secure, key_pair)
|
||||
return initialize_default_swarm(key_pair, sec_opt=sec_opt, muxer_opt=muxer_opt)
|
||||
|
||||
|
||||
class ListeningSwarmFactory(factory.Factory):
|
||||
@ -45,17 +47,22 @@ class ListeningSwarmFactory(factory.Factory):
|
||||
model = Swarm
|
||||
|
||||
@classmethod
|
||||
async def create_and_listen(cls, is_secure: bool) -> Swarm:
|
||||
swarm = SwarmFactory(is_secure)
|
||||
async def create_and_listen(
|
||||
cls, is_secure: bool, muxer_opt: TMuxerOptions = None
|
||||
) -> Swarm:
|
||||
swarm = SwarmFactory(is_secure, muxer_opt=muxer_opt)
|
||||
await swarm.listen(LISTEN_MADDR)
|
||||
return swarm
|
||||
|
||||
@classmethod
|
||||
async def create_batch_and_listen(
|
||||
cls, is_secure: bool, number: int
|
||||
cls, is_secure: bool, number: int, muxer_opt: TMuxerOptions = None
|
||||
) -> Tuple[Swarm, ...]:
|
||||
return await asyncio.gather(
|
||||
*[cls.create_and_listen(is_secure) for _ in range(number)]
|
||||
*[
|
||||
cls.create_and_listen(is_secure, muxer_opt=muxer_opt)
|
||||
for _ in range(number)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@ -112,8 +119,12 @@ class PubsubFactory(factory.Factory):
|
||||
cache_size = None
|
||||
|
||||
|
||||
async def swarm_pair_factory(is_secure: bool) -> Tuple[Swarm, Swarm]:
|
||||
swarms = await ListeningSwarmFactory.create_batch_and_listen(is_secure, 2)
|
||||
async def swarm_pair_factory(
|
||||
is_secure: bool, muxer_opt: TMuxerOptions = None
|
||||
) -> Tuple[Swarm, Swarm]:
|
||||
swarms = await ListeningSwarmFactory.create_batch_and_listen(
|
||||
is_secure, 2, muxer_opt=muxer_opt
|
||||
)
|
||||
await connect_swarm(swarms[0], swarms[1])
|
||||
return swarms[0], swarms[1]
|
||||
|
||||
@ -130,7 +141,7 @@ async def host_pair_factory(is_secure) -> Tuple[BasicHost, BasicHost]:
|
||||
|
||||
|
||||
async def swarm_conn_pair_factory(
|
||||
is_secure
|
||||
is_secure: bool, muxer_opt: TMuxerOptions = None
|
||||
) -> Tuple[SwarmConn, Swarm, SwarmConn, Swarm]:
|
||||
swarms = await swarm_pair_factory(is_secure)
|
||||
conn_0 = swarms[0].connections[swarms[1].get_peer_id()]
|
||||
@ -138,6 +149,14 @@ async def swarm_conn_pair_factory(
|
||||
return conn_0, swarms[0], conn_1, swarms[1]
|
||||
|
||||
|
||||
async def mplex_conn_pair_factory(is_secure):
|
||||
muxer_opt = {MPLEX_PROTOCOL_ID: Mplex}
|
||||
conn_0, swarm_0, conn_1, swarm_1 = await swarm_conn_pair_factory(
|
||||
is_secure, muxer_opt=muxer_opt
|
||||
)
|
||||
return conn_0.conn, swarm_0, conn_1.conn, swarm_1
|
||||
|
||||
|
||||
async def net_stream_pair_factory(
|
||||
is_secure: bool
|
||||
) -> Tuple[INetStream, BasicHost, INetStream, BasicHost]:
|
||||
|
||||
@ -41,3 +41,5 @@ async def test_swarm_conn_streams(swarm_conn_pair):
|
||||
assert len(await conn_0.get_streams()) == 1
|
||||
conn_0.remove_stream(stream_0_1)
|
||||
assert len(await conn_0.get_streams()) == 0
|
||||
# Nothing happen if `stream_0_1` is not present or already removed.
|
||||
conn_0.remove_stream(stream_0_1)
|
||||
|
||||
0
tests/stream_muxer/__init__.py
Normal file
0
tests/stream_muxer/__init__.py
Normal file
16
tests/stream_muxer/conftest.py
Normal file
16
tests/stream_muxer/conftest.py
Normal file
@ -0,0 +1,16 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.factories import mplex_conn_pair_factory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mplex_conn_pair(is_host_secure):
|
||||
mplex_conn_0, swarm_0, mplex_conn_1, swarm_1 = await mplex_conn_pair_factory(
|
||||
is_host_secure
|
||||
)
|
||||
try:
|
||||
yield mplex_conn_0, mplex_conn_1
|
||||
finally:
|
||||
await asyncio.gather(*[swarm_0.close(), swarm_1.close()])
|
||||
6
tests/stream_muxer/test_mplex_conn.py
Normal file
6
tests/stream_muxer/test_mplex_conn.py
Normal file
@ -0,0 +1,6 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_mplex_conn(mplex_conn_pair):
|
||||
conn_0, conn_1 = mplex_conn_pair
|
||||
Reference in New Issue
Block a user