diff --git a/tests/testmplex.nim b/tests/testmplex.nim index 2bf51d108..dfb8965b7 100644 --- a/tests/testmplex.nim +++ b/tests/testmplex.nim @@ -10,7 +10,9 @@ import ../libp2p/[connection, muxers/mplex/mplex, muxers/mplex/coder, muxers/mplex/types, - muxers/mplex/lpchannel] + muxers/mplex/lpchannel, + vbuffer, + varint] when defined(nimHasUsed): {.used.} @@ -376,6 +378,77 @@ suite "Mplex": # expect LPStreamEOFError: # waitFor(testClosedForRead()) + test "jitter - channel should be able to handle erratic read/writes": + proc test(): Future[bool] {.async.} = + let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") + + var complete = newFuture[void]() + const MsgSize = 512 * 10 + proc connHandler(conn: Connection) {.async, gcsafe.} = + proc handleMplexListen(stream: Connection) {.async, gcsafe.} = + let msg = await stream.readLp() + check msg.len == MsgSize + await stream.close() + complete.complete() + + let mplexListen = newMplex(conn) + mplexListen.streamHandler = handleMplexListen + discard mplexListen.handle() + + let transport1: TcpTransport = newTransport(TcpTransport) + discard await transport1.listen(ma, connHandler) + + defer: + await transport1.close() + + let transport2: TcpTransport = newTransport(TcpTransport) + let conn = await transport2.dial(transport1.ma) + + let mplexDial = newMplex(conn) + let stream = await mplexDial.newStream() + var bigseq = newSeqOfCap[uint8](MaxMsgSize + 1) + for _ in 0.. max or len <= 0: + len = rand(min..max) + + len = if len > buf.buffer.len: buf.buffer.len else: len + var send = buf.buffer[0..