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:
mhchia
2019-08-29 21:38:06 +08:00
parent 64c0dab3af
commit c61a06706a
15 changed files with 184 additions and 116 deletions

View File

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

View File

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