nim-libp2p/tests/commontransport.nim
Tanguy Cizain af3be7966b
Websocket Transport (#593)
* start of websocket transport

* more ws tests

* switch to common test

* add close to wsstream

* update ws & chronicles version

* cleanup

* removed multicodec

* clean ws outgoing connections

* renamed to websock

* removed stream from logs

* renamed ws to websock

* add connection closing test to common transport

* close incoming connection on ws stop

* renamed testwebsocket.nim -> testwstransport.nim

* removed raise todo

* split out/in connections

* add wss to tests

* Fix tls (#608)

* change log level

* fixed issue related to stopping

some cosmetic cleanup

* use `allFutures` to stop/close things

Prevent potential race conditions when stopping two or more transports

* misc

* point websock to server-case-object branch

* interop test with go

* removed websock version specification

* add daemon -> native ws test

* fix & test closed read/write

* update readOnce, thanks jangko

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
2021-08-03 15:48:03 +02:00

162 lines
4.6 KiB
Nim

{.used.}
import sequtils
import chronos, stew/byteutils
import ../libp2p/[stream/connection,
transports/transport,
upgrademngrs/upgrade,
multiaddress,
errors,
wire]
import ./helpers
type TransportProvider* = proc(): Transport {.gcsafe.}
proc commonTransportTest*(name: string, prov: TransportProvider, ma: string) =
suite name & " common tests":
teardown:
checkTrackers()
asyncTest "can handle local address":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
check transport1.handles(transport1.ma)
await transport1.stop()
asyncTest "e2e: handle write":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
proc acceptHandler() {.async, gcsafe.} =
let conn = await transport1.accept()
await conn.write("Hello!")
await conn.close()
let handlerWait = acceptHandler()
let transport2 = prov()
let conn = await transport2.dial(transport1.ma)
var msg = newSeq[byte](6)
await conn.readExactly(addr msg[0], 6)
await conn.close() #for some protocols, closing requires actively reading, so we must close here
await allFuturesThrowing(
allFinished(
transport1.stop(),
transport2.stop()))
check string.fromBytes(msg) == "Hello!"
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
asyncTest "e2e: handle read":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
proc acceptHandler() {.async, gcsafe.} =
let conn = await transport1.accept()
var msg = newSeq[byte](6)
await conn.readExactly(addr msg[0], 6)
check string.fromBytes(msg) == "Hello!"
await conn.close()
let handlerWait = acceptHandler()
let transport2 = prov()
let conn = await transport2.dial(transport1.ma)
await conn.write("Hello!")
await conn.close() #for some protocols, closing requires actively reading, so we must close here
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
await allFuturesThrowing(
allFinished(
transport1.stop(),
transport2.stop()))
asyncTest "e2e: handle dial cancellation":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
let transport2 = prov()
let cancellation = transport2.dial(transport1.ma)
await cancellation.cancelAndWait()
check cancellation.cancelled
await allFuturesThrowing(
allFinished(
transport1.stop(),
transport2.stop()))
asyncTest "e2e: handle accept cancellation":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
let acceptHandler = transport1.accept()
await acceptHandler.cancelAndWait()
check acceptHandler.cancelled
await transport1.stop()
asyncTest "e2e: stopping transport kills connections":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
let transport2 = prov()
let acceptHandler = transport1.accept()
let conn = await transport2.dial(transport1.ma)
let serverConn = await acceptHandler
await allFuturesThrowing(
allFinished(
transport1.stop(),
transport2.stop()))
check serverConn.closed()
check conn.closed()
asyncTest "read or write on closed connection":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1 = prov()
await transport1.start(ma)
proc acceptHandler() {.async, gcsafe.} =
let conn = await transport1.accept()
await conn.close()
let handlerWait = acceptHandler()
let conn = await transport1.dial(transport1.ma)
var msg = newSeq[byte](6)
try:
await conn.readExactly(addr msg[0], 6)
check false
except CatchableError as exc:
check true
# we don't HAVE to throw on write on EOF
# (at least TCP doesn't)
try:
await conn.write(msg)
except CatchableError as exc:
check true
await conn.close() #for some protocols, closing requires actively reading, so we must close here
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
await transport1.stop()