From d1f1e1b31ed8d45fe83250298c2c0934bce6a2d1 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 11 Aug 2020 23:23:49 -0600 Subject: [PATCH] add missing mplex half closed test (#326) --- libp2p/stream/connection.nim | 2 +- tests/testmplex.nim | 39 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/libp2p/stream/connection.nim b/libp2p/stream/connection.nim index a5925d717..95e0e8105 100644 --- a/libp2p/stream/connection.nim +++ b/libp2p/stream/connection.nim @@ -77,7 +77,7 @@ method initStream*(s: Connection) = s.timeoutHandler = proc() {.async.} = await s.close() - trace "timeout", timeout = $s.timeout.millis + trace "timeout set at", timeout = $s.timeout.millis doAssert(isNil(s.timerTaskFut)) # doAssert(s.timeout > 0.millis) if s.timeout > 0.millis: diff --git a/tests/testmplex.nim b/tests/testmplex.nim index 6119dc393..c17ca1c6b 100644 --- a/tests/testmplex.nim +++ b/tests/testmplex.nim @@ -112,7 +112,7 @@ suite "Mplex": waitFor(testDecodeHeader()) - test "half closed - channel should close for write": + test "half closed (local close) - should close for write": proc testClosedForWrite(): Future[bool] {.async.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} = discard let @@ -130,7 +130,40 @@ suite "Mplex": check: waitFor(testClosedForWrite()) == true - test "half closed - channel should close for read by remote": + test "half closed (local close) - should allow reads until remote closes": + proc testOpenForRead(): Future[bool] {.async.} = + let + conn = newBufferStream( + proc (data: seq[byte]) {.gcsafe, async.} = + discard, + timeout = 5.minutes + ) + chann = LPChannel.init(1, conn, true) + + await chann.pushTo(("Hello!").toBytes) + + var data = newSeq[byte](6) + await chann.close() # closing channel + # should be able to read on local clsoe + await chann.readExactly(addr data[0], 3) + # closing remote end + let closeFut = chann.closeRemote() + # should still allow reading until buffer EOF + await chann.readExactly(addr data[3], 3) + await closeFut + try: + # this should fail now + await chann.readExactly(addr data[0], 3) + except LPStreamEOFError: + result = true + finally: + await chann.close() + await conn.close() + + check: + waitFor(testOpenForRead()) == true + + test "half closed (remote close) - channel should close for reading by remote": proc testClosedForRead(): Future[bool] {.async.} = let conn = newBufferStream( @@ -158,7 +191,7 @@ suite "Mplex": check: waitFor(testClosedForRead()) == true - test "half closed - channel should allow writting on remote close": + test "half closed (remote close) - channel should allow writting on remote close": proc testClosedForRead(): Future[bool] {.async.} = let testData = "Hello!".toBytes