mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Refactor interop tests and factories
- Add `close` and `disconnect` in `Host` - Add `close` and `close_peer` in `Network` - Change `IListener.close` to async, to await for server's closing - Add factories for security transports, and modify `HostFactory`
This commit is contained in:
@ -70,3 +70,11 @@ class INetwork(ABC):
|
||||
:param notifee: object implementing Notifee interface
|
||||
:return: true if notifee registered successfully, false otherwise
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def close(self) -> None:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def close_peer(self, peer_id: ID) -> None:
|
||||
pass
|
||||
|
||||
@ -264,12 +264,24 @@ class Swarm(INetwork):
|
||||
def add_router(self, router: IPeerRouting) -> None:
|
||||
self.router = router
|
||||
|
||||
# TODO: `tear_down`
|
||||
async def tear_down(self) -> None:
|
||||
# Reference: https://github.com/libp2p/go-libp2p-swarm/blob/8be680aef8dea0a4497283f2f98470c2aeae6b65/swarm.go#L118 # noqa: E501
|
||||
pass
|
||||
async def close(self) -> None:
|
||||
# TODO: Prevent from new listeners and conns being added.
|
||||
# Reference: https://github.com/libp2p/go-libp2p-swarm/blob/8be680aef8dea0a4497283f2f98470c2aeae6b65/swarm.go#L124-L134 # noqa: E501
|
||||
|
||||
# TODO: `disconnect`?
|
||||
# Close listeners
|
||||
await asyncio.gather(
|
||||
*[listener.close() for listener in self.listeners.values()]
|
||||
)
|
||||
|
||||
# Close connections
|
||||
await asyncio.gather(
|
||||
*[connection.close() for connection in self.connections.values()]
|
||||
)
|
||||
|
||||
async def close_peer(self, peer_id: ID) -> None:
|
||||
connection = self.connections[peer_id]
|
||||
del self.connections[peer_id]
|
||||
await connection.close()
|
||||
|
||||
|
||||
def create_generic_protocol_handler(swarm: Swarm) -> GenericProtocolHandlerFn:
|
||||
|
||||
Reference in New Issue
Block a user