Add initial test for Swarm.close_peer
This commit is contained in:
parent
e7304538da
commit
276ac4d8ab
|
@ -6,7 +6,6 @@ import factory
|
||||||
from libp2p import generate_new_rsa_identity, initialize_default_swarm
|
from libp2p import generate_new_rsa_identity, initialize_default_swarm
|
||||||
from libp2p.crypto.keys import KeyPair
|
from libp2p.crypto.keys import KeyPair
|
||||||
from libp2p.host.basic_host import BasicHost
|
from libp2p.host.basic_host import BasicHost
|
||||||
from libp2p.host.host_interface import IHost
|
|
||||||
from libp2p.network.stream.net_stream_interface import INetStream
|
from libp2p.network.stream.net_stream_interface import INetStream
|
||||||
from libp2p.network.swarm import Swarm
|
from libp2p.network.swarm import Swarm
|
||||||
from libp2p.pubsub.floodsub import FloodSub
|
from libp2p.pubsub.floodsub import FloodSub
|
||||||
|
@ -34,19 +33,19 @@ def security_transport_factory(
|
||||||
return {secio.ID: secio.Transport(key_pair)}
|
return {secio.ID: secio.Transport(key_pair)}
|
||||||
|
|
||||||
|
|
||||||
class SwarmFactory(factory.Factory):
|
def SwarmFactory(is_secure: bool) -> 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)
|
||||||
|
|
||||||
|
|
||||||
|
class ListeningSwarmFactory(factory.Factory):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Swarm
|
model = Swarm
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _create(cls, is_secure=False):
|
|
||||||
key_pair = generate_new_rsa_identity()
|
|
||||||
sec_opt = security_transport_factory(is_secure, key_pair)
|
|
||||||
return initialize_default_swarm(key_pair, sec_opt=sec_opt)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create_and_listen(cls, is_secure: bool) -> Swarm:
|
async def create_and_listen(cls, is_secure: bool) -> Swarm:
|
||||||
swarm = cls._create(is_secure)
|
swarm = SwarmFactory(is_secure)
|
||||||
await swarm.listen(LISTEN_MADDR)
|
await swarm.listen(LISTEN_MADDR)
|
||||||
return swarm
|
return swarm
|
||||||
|
|
||||||
|
@ -69,9 +68,16 @@ class HostFactory(factory.Factory):
|
||||||
network = factory.LazyAttribute(lambda o: SwarmFactory(o.is_secure))
|
network = factory.LazyAttribute(lambda o: SwarmFactory(o.is_secure))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create_and_listen(cls, is_secure: bool) -> IHost:
|
async def create_and_listen(cls, is_secure: bool) -> BasicHost:
|
||||||
swarm = await SwarmFactory.create_and_listen(is_secure)
|
swarms = await ListeningSwarmFactory.create_batch_and_listen(is_secure, 1)
|
||||||
return BasicHost(swarm)
|
return BasicHost(swarms[0])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def create_batch_and_listen(
|
||||||
|
cls, is_secure: bool, number: int
|
||||||
|
) -> Tuple[BasicHost, ...]:
|
||||||
|
swarms = await ListeningSwarmFactory.create_batch_and_listen(is_secure, number)
|
||||||
|
return tuple(BasicHost(swarm) for swarm in range(swarms))
|
||||||
|
|
||||||
|
|
||||||
class FloodsubFactory(factory.Factory):
|
class FloodsubFactory(factory.Factory):
|
||||||
|
@ -106,7 +112,7 @@ class PubsubFactory(factory.Factory):
|
||||||
|
|
||||||
|
|
||||||
async def swarm_pair_factory(is_secure: bool) -> Tuple[Swarm, Swarm]:
|
async def swarm_pair_factory(is_secure: bool) -> Tuple[Swarm, Swarm]:
|
||||||
swarms = await SwarmFactory.create_batch_and_listen(2)
|
swarms = await ListeningSwarmFactory.create_batch_and_listen(2)
|
||||||
await connect_swarm(swarms[0], swarms[1])
|
await connect_swarm(swarms[0], swarms[1])
|
||||||
return swarms[0], swarms[1]
|
return swarms[0], swarms[1]
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,13 @@ import asyncio
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tests.factories import SwarmFactory
|
from tests.factories import ListeningSwarmFactory
|
||||||
from tests.utils import connect_swarm
|
from tests.utils import connect_swarm
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_swarm_close_peer(is_host_secure):
|
async def test_swarm_close_peer(is_host_secure):
|
||||||
swarms = await SwarmFactory.create_batch_and_listen(is_host_secure, 3)
|
swarms = await ListeningSwarmFactory.create_batch_and_listen(is_host_secure, 3)
|
||||||
# 0 <> 1 <> 2
|
# 0 <> 1 <> 2
|
||||||
await connect_swarm(swarms[0], swarms[1])
|
await connect_swarm(swarms[0], swarms[1])
|
||||||
await connect_swarm(swarms[1], swarms[2])
|
await connect_swarm(swarms[1], swarms[2])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user