Add mplex tests and fix error in SwarmConn.close

This commit is contained in:
mhchia
2019-09-18 15:44:45 +08:00
parent d61327f5f9
commit a9ad37bc6f
14 changed files with 96 additions and 44 deletions

View File

@ -66,10 +66,19 @@ class SwarmConn(INetConn):
await self.close()
async def _call_stream_handler(self, net_stream: NetStream) -> None:
try:
await self.swarm.common_stream_handler(net_stream)
# TODO: More exact exceptions
except Exception:
# TODO: Emit logs.
# TODO: Clean up and remove the stream from SwarmConn if there is anything wrong.
self.remove_stream(net_stream)
async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None:
net_stream = await self._add_stream(muxed_stream)
if self.swarm.common_stream_handler is not None:
await self.run_task(self.swarm.common_stream_handler(net_stream))
await self.run_task(self._call_stream_handler(net_stream))
async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream:
net_stream = NetStream(muxed_stream)
@ -97,4 +106,6 @@ class SwarmConn(INetConn):
# TODO: Called by `Stream` whenever it is time to remove the stream.
def remove_stream(self, stream: NetStream) -> None:
if stream not in self.streams:
return
self.streams.remove(stream)