nim-libp2p-experimental/tests/commontransport.nim
Tanguy Cizain 26e47d7da5
Various transports improvement (#594)
* little transport cleanup

* rename TcpTransport.init -> TcpTransport.new

* moved transport e2e to common file

* remove localAddress

* rename testtransport -> testtcptransport

* add checktrackers to commontransports

* removed multicodec from transports
2021-06-30 10:59:30 +02:00

94 lines
3.0 KiB
Nim

{.used.}
import sequtils
import chronos, stew/byteutils
import ../libp2p/[stream/connection,
transports/transport,
upgrademngrs/upgrade,
multiaddress,
errors,
wire]
import ./helpers
proc commonTransportTest*(transportType: typedesc[Transport], ma: string) =
suite $transportType & " common":
teardown:
checkTrackers()
asyncTest "e2e: handle write":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1: transportType = transportType.new(upgrade = Upgrade())
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: transportType = transportType.new(upgrade = Upgrade())
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, so we must close here
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
await transport2.stop()
await transport1.stop()
check string.fromBytes(msg) == "Hello!"
asyncTest "e2e: handle read":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1: transportType = transportType.new(upgrade = Upgrade())
asyncSpawn 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: transportType = transportType.new(upgrade = Upgrade())
let conn = await transport2.dial(transport1.ma)
await conn.write("Hello!")
await conn.close() #for some protocols, closing requires actively, so we must close here
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
await transport2.stop()
await transport1.stop()
asyncTest "e2e: handle dial cancellation":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1: transportType = transportType.new(upgrade = Upgrade())
await transport1.start(ma)
let transport2: transportType = transportType.new(upgrade = Upgrade())
let cancellation = transport2.dial(transport1.ma)
await cancellation.cancelAndWait()
check cancellation.cancelled
await transport2.stop()
await transport1.stop()
asyncTest "e2e: handle accept cancellation":
let ma: MultiAddress = Multiaddress.init(ma).tryGet()
let transport1: transportType = transportType.new(upgrade = Upgrade())
await transport1.start(ma)
let acceptHandler = transport1.accept()
await acceptHandler.cancelAndWait()
check acceptHandler.cancelled
await transport1.stop()