From 62ca6dd9a0a8b7b7d633c6070c9aff17ba8c8a0e Mon Sep 17 00:00:00 2001 From: Tanguy Cizain Date: Wed, 1 Sep 2021 08:38:24 +0200 Subject: [PATCH] Add missing test to testnative (#618) * add missing tests * fix testping --- tests/testnative.nim | 3 + tests/testping.nim | 200 ++++++++++++++++++------------------------- 2 files changed, 87 insertions(+), 116 deletions(-) diff --git a/tests/testnative.nim b/tests/testnative.nim index 0f4a67460..8bfbe88c2 100644 --- a/tests/testnative.nim +++ b/tests/testnative.nim @@ -1,4 +1,5 @@ import testvarint, + testconnection, testminprotobuf, teststreamseq, testsemaphore @@ -26,4 +27,6 @@ import testtcptransport, testswitch, testnoise, testpeerinfo, + testpeerstore, + testping, testmplex diff --git a/tests/testping.nim b/tests/testping.nim index f894b6298..b17959810 100644 --- a/tests/testping.nim +++ b/tests/testping.nim @@ -17,131 +17,99 @@ import ./helpers when defined(nimHasUsed): {.used.} suite "Ping": - teardown: + var + ma {.threadvar.}: MultiAddress + serverFut {.threadvar.}: Future[void] + acceptFut {.threadvar.}: Future[void] + pingProto1 {.threadvar.}: Ping + pingProto2 {.threadvar.}: Ping + transport1 {.threadvar.}: Transport + transport2 {.threadvar.}: Transport + msListen {.threadvar.}: MultistreamSelect + msDial {.threadvar.}: MultistreamSelect + conn {.threadvar.}: Connection + pingReceivedCount {.threadvar.}: int + + asyncSetup: + ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet() + + transport1 = TcpTransport.new(upgrade = Upgrade()) + transport2 = TcpTransport.new(upgrade = Upgrade()) + + proc handlePing(peer: PeerInfo) {.async, gcsafe, closure.} = + inc pingReceivedCount + pingProto1 = Ping.new() + pingProto2 = Ping.new(handlePing) + + msListen = newMultistream() + msDial = newMultistream() + + pingReceivedCount = 0 + + asyncTeardown: + await conn.close() + await acceptFut + await transport1.stop() + await serverFut + await transport2.stop() checkTrackers() - suite "handle ping message": - var - ma {.threadvar.}: MultiAddress - serverFut {.threadvar.}: Future[void] - acceptFut {.threadvar.}: Future[void] - pingProto1 {.threadvar.}: Ping - pingProto2 {.threadvar.}: Ping - transport1 {.threadvar.}: Transport - transport2 {.threadvar.}: Transport - msListen {.threadvar.}: MultistreamSelect - msDial {.threadvar.}: MultistreamSelect - conn {.threadvar.}: Connection - pingReceivedCount {.threadvar.}: int + asyncTest "simple ping": + msListen.addHandler(PingCodec, pingProto1) + serverFut = transport1.start(ma) + proc acceptHandler(): Future[void] {.async, gcsafe.} = + let c = await transport1.accept() + await msListen.handle(c) - asyncSetup: - ma = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet() + acceptFut = acceptHandler() + conn = await transport2.dial(transport1.ma) - transport1 = TcpTransport.new(upgrade = Upgrade()) - transport2 = TcpTransport.new(upgrade = Upgrade()) + discard await msDial.select(conn, PingCodec) + let time = await pingProto2.ping(conn) - proc handlePing(peer: PeerInfo) {.async, gcsafe, closure.} = - inc pingReceivedCount - pingProto1 = Ping.new() - pingProto2 = Ping.new(handlePing) + check not time.isZero() - msListen = newMultistream() - msDial = newMultistream() + asyncTest "ping callback": + msDial.addHandler(PingCodec, pingProto2) + serverFut = transport1.start(ma) + proc acceptHandler(): Future[void] {.async, gcsafe.} = + let c = await transport1.accept() + discard await msListen.select(c, PingCodec) + discard await pingProto1.ping(c) - pingReceivedCount = 0 + acceptFut = acceptHandler() + conn = await transport2.dial(transport1.ma) - asyncTeardown: - await conn.close() - await acceptFut - await transport1.stop() - await serverFut - await transport2.stop() + await msDial.handle(conn) + check pingReceivedCount == 1 - asyncTest "simple ping": - msListen.addHandler(PingCodec, pingProto1) - serverFut = transport1.start(ma) - proc acceptHandler(): Future[void] {.async, gcsafe.} = - let c = await transport1.accept() - await msListen.handle(c) + asyncTest "bad ping data ack": + type FakePing = ref object of LPProtocol + let fakePingProto = FakePing() + proc fakeHandle(conn: Connection, proto: string) {.async, gcsafe, closure.} = + var + buf: array[32, byte] + fakebuf: array[32, byte] + await conn.readExactly(addr buf[0], 32) + await conn.write(addr fakebuf[0], 32) + fakePingProto.codec = PingCodec + fakePingProto.handler = fakeHandle - acceptFut = acceptHandler() - conn = await transport2.dial(transport1.ma) + msListen.addHandler(PingCodec, fakePingProto) + serverFut = transport1.start(ma) + proc acceptHandler(): Future[void] {.async, gcsafe.} = + let c = await transport1.accept() + await msListen.handle(c) - discard await msDial.select(conn, PingCodec) - let time = await pingProto2.ping(conn) + acceptFut = acceptHandler() + conn = await transport2.dial(transport1.ma) - check not time.isZero() - - asyncTest "networked cancel ping": - proc testPing(): Future[void] {.async.} = - let baseMa = Multiaddress.init("/ip4/127.0.0.1/tcp/0").tryGet() - - let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade()) - let transportdialer: TcpTransport = TcpTransport.new(upgrade = Upgrade()) - asyncSpawn transport.start(baseMa) - - proc acceptHandler() {.async, gcsafe.} = - let handler = Ping.new().handler - let conn = await transport.accept() - await handler(conn, "na") - - let handlerWait = acceptHandler() - - let streamTransport = await transportdialer.dial(transport.ma) - - discard await pingProto2.ping(streamTransport) - - for pollCount in 0..20: - #echo "Polling ", pollCount, " times" - let p = testPing() - for _ in 0..