2020-05-08 20:10:06 +00:00
|
|
|
{.used.}
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
import unittest, sequtils
|
2020-06-03 02:21:11 +00:00
|
|
|
import chronos, stew/byteutils
|
2020-06-19 17:29:43 +00:00
|
|
|
import ../libp2p/[stream/connection,
|
2019-10-29 18:51:48 +00:00
|
|
|
transports/transport,
|
2019-10-03 19:30:22 +00:00
|
|
|
transports/tcptransport,
|
2019-10-29 18:51:48 +00:00
|
|
|
multiaddress,
|
2020-11-19 02:06:42 +00:00
|
|
|
errors,
|
2019-10-03 19:30:22 +00:00
|
|
|
wire]
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2020-05-08 20:10:06 +00:00
|
|
|
import ./helpers
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-08-31 17:59:21 +00:00
|
|
|
suite "TCP transport":
|
2020-04-21 01:24:42 +00:00
|
|
|
teardown:
|
2020-09-21 17:48:19 +00:00
|
|
|
checkTrackers()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
asyncTest "test listener: handle write":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
2020-11-19 02:06:42 +00:00
|
|
|
asyncCheck transport.start(ma)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport.accept()
|
|
|
|
await conn.write("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
let streamTransport = await connect(transport.ma)
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
let msg = await streamTransport.read(6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
await streamTransport.closeWait()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
asyncTest "test listener: handle read":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-11-19 02:06:42 +00:00
|
|
|
|
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
|
|
|
asyncCheck transport.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
2020-05-15 04:02:05 +00:00
|
|
|
var msg = newSeq[byte](6)
|
2020-11-19 02:06:42 +00:00
|
|
|
let conn = await transport.accept()
|
2020-05-15 04:02:05 +00:00
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-11-13 03:44:02 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
await conn.close()
|
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
let handlerWait = acceptHandler()
|
2020-11-13 03:44:02 +00:00
|
|
|
let streamTransport: StreamTransport = await connect(transport.ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
let sent = await streamTransport.write("Hello!")
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
await streamTransport.closeWait()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
check sent == 6
|
|
|
|
|
|
|
|
asyncTest "test dialer: handle write":
|
|
|
|
let address = initTAddress("0.0.0.0:0")
|
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc serveClient(server: StreamServer,
|
|
|
|
transp: StreamTransport) {.async, gcsafe.} =
|
|
|
|
var wstream = newAsyncStreamWriter(transp)
|
|
|
|
await wstream.write("Hello!")
|
|
|
|
await wstream.finish()
|
|
|
|
await wstream.closeWait()
|
|
|
|
await transp.closeWait()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
handlerWait.complete()
|
|
|
|
|
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
|
|
|
let conn = await transport.dial(ma)
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
|
|
|
|
await conn.close()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
|
|
|
|
|
|
|
asyncTest "test dialer: handle write":
|
|
|
|
let address = initTAddress("0.0.0.0:0")
|
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc serveClient(server: StreamServer,
|
|
|
|
transp: StreamTransport) {.async, gcsafe.} =
|
|
|
|
var rstream = newAsyncStreamReader(transp)
|
|
|
|
let msg = await rstream.read(6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
await rstream.closeWait()
|
|
|
|
await transp.closeWait()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
handlerWait.complete()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
|
|
|
let conn = await transport.dial(ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
await conn.write("Hello!")
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
await conn.close()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
|
|
|
|
|
|
|
asyncTest "e2e: handle write":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
|
|
|
await conn.write("Hello!")
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
let handlerWait = acceptHandler()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
await conn.close()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport2.stop()
|
|
|
|
await transport1.stop()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
|
|
|
asyncTest "e2e: handle read":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-11-19 02:06:42 +00:00
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
|
|
|
asyncCheck transport1.start(ma)
|
|
|
|
|
|
|
|
proc acceptHandler() {.async, gcsafe.} =
|
|
|
|
let conn = await transport1.accept()
|
2020-11-13 03:44:02 +00:00
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
2020-04-21 01:24:42 +00:00
|
|
|
await conn.close()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
2020-11-19 02:06:42 +00:00
|
|
|
let handlerWait = acceptHandler()
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
|
|
|
let conn = await transport2.dial(transport1.ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
await conn.write("Hello!")
|
2020-11-13 03:44:02 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-11-13 03:44:02 +00:00
|
|
|
await conn.close()
|
2020-11-19 02:06:42 +00:00
|
|
|
await transport2.stop()
|
|
|
|
await transport1.stop()
|
|
|
|
|
|
|
|
asyncTest "e2e: handle dial cancellation":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
|
|
|
let cancellation = transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
try:
|
|
|
|
cancellation.cancel()
|
|
|
|
except CancelledError as exc:
|
|
|
|
await sleepAsync(100.millis)
|
|
|
|
check cancellation.cancelled
|
|
|
|
|
|
|
|
await transport2.stop()
|
|
|
|
await transport1.stop()
|
|
|
|
|
|
|
|
asyncTest "e2e: handle accept cancellation":
|
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
|
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
|
|
|
await transport1.start(ma)
|
|
|
|
|
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
|
|
|
let connFut = transport2.dial(transport1.ma)
|
|
|
|
|
|
|
|
let acceptHandler = transport1.accept()
|
|
|
|
try:
|
|
|
|
acceptHandler.cancel()
|
|
|
|
except CancelledError as exc:
|
|
|
|
await sleepAsync(100.millis)
|
|
|
|
check acceptHandler.cancelled
|
|
|
|
check isNil((await connFut))
|
|
|
|
|
|
|
|
await transport1.stop()
|
|
|
|
await transport2.stop()
|