Use better async pattern in test (Notice this causes a issue to show up)

This commit is contained in:
Giovanni Petrantoni 2020-02-09 12:46:52 +09:00 committed by Dmitriy Ryajov
parent 0d0a6e9cfb
commit 20f65e0dc8
1 changed files with 5 additions and 15 deletions

View File

@ -119,16 +119,12 @@ suite "Mplex":
test "e2e - read/write receiver": test "e2e - read/write receiver":
proc testNewStream(): Future[bool] {.async.} = proc testNewStream(): Future[bool] {.async.} =
let let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
lock = newFuture[void]()
timeout = sleepAsync(5_000)
proc connHandler(conn: Connection) {.async, gcsafe.} = proc connHandler(conn: Connection) {.async, gcsafe.} =
proc handleMplexListen(stream: Connection) {.async, gcsafe.} = proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
let msg = await stream.readLp() let msg = await stream.readLp()
check cast[string](msg) == "Hello from stream!" check cast[string](msg) == "Hello from stream!"
lock.complete()
await stream.close() await stream.close()
let mplexListen = newMplex(conn) let mplexListen = newMplex(conn)
@ -176,9 +172,6 @@ suite "Mplex":
let stream = await mplexDial.newStream("", true) let stream = await mplexDial.newStream("", true)
let openState = cast[LPChannel](stream.stream).isOpen let openState = cast[LPChannel](stream.stream).isOpen
await stream.writeLp("Hello from stream!") await stream.writeLp("Hello from stream!")
await lock or timeout
check lock.finished
timeout.cancel()
await conn.close() await conn.close()
check not openState # assert lazy check not openState # assert lazy
result = true result = true
@ -190,15 +183,14 @@ suite "Mplex":
proc testNewStream(): Future[bool] {.async.} = proc testNewStream(): Future[bool] {.async.} =
let let
ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0")
lock = newFuture[void]() listenJob = newFuture[void]()
timeout = sleepAsync(5_000)
proc connHandler(conn: Connection) {.async, gcsafe.} = proc connHandler(conn: Connection) {.async, gcsafe.} =
proc handleMplexListen(stream: Connection) {.async, gcsafe.} = proc handleMplexListen(stream: Connection) {.async, gcsafe.} =
let msg = await stream.readLp() let msg = await stream.readLp()
lock.complete()
check cast[string](msg) == "Hello from stream!" check cast[string](msg) == "Hello from stream!"
await stream.close() await stream.close()
listenJob.complete()
let mplexListen = newMplex(conn) let mplexListen = newMplex(conn)
mplexListen.streamHandler = handleMplexListen mplexListen.streamHandler = handleMplexListen
@ -214,12 +206,10 @@ suite "Mplex":
let stream = await mplexDial.newStream() let stream = await mplexDial.newStream()
var bigseq = newSeqOfCap[uint8](MaxMsgSize + 1) var bigseq = newSeqOfCap[uint8](MaxMsgSize + 1)
for _ in 0..<MaxMsgSize: for _ in 0..<MaxMsgSize:
bigseq.add(uint8(rand(int('A')..int('z')))) bigseq.add(uint8(rand(uint('A')..uint('z'))))
await stream.writeLp(bigseq) await stream.writeLp(bigseq)
await lock or timeout
check timeout.finished # this test has to timeout!
lock.cancel();
await conn.close() await conn.close()
await listenJob.wait(seconds(5))
result = true result = true
check: check: