2020-05-08 20:10:06 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-08-21 04:58:12 +00:00
|
|
|
import unittest
|
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,
|
2019-10-03 19:30:22 +00:00
|
|
|
wire]
|
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-05-08 20:10:06 +00:00
|
|
|
for tracker in testTrackers():
|
|
|
|
check tracker.isLeaked() == false
|
2020-04-21 01:24:42 +00:00
|
|
|
|
2019-08-21 22:53:16 +00:00
|
|
|
test "test listener: handle write":
|
2019-08-30 15:28:07 +00:00
|
|
|
proc testListener(): Future[bool] {.async, gcsafe.} =
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-04-21 01:24:42 +00:00
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
await conn.write(cstring("Hello!"), 6)
|
|
|
|
await conn.close()
|
|
|
|
handlerWait.complete()
|
2019-08-21 04:58:12 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
asyncCheck transport.listen(ma, connHandler)
|
|
|
|
|
|
|
|
let streamTransport = await connect(transport.ma)
|
|
|
|
|
2019-08-21 19:15:51 +00:00
|
|
|
let msg = await streamTransport.read(6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
2019-08-21 22:53:16 +00:00
|
|
|
await streamTransport.closeWait()
|
2020-04-21 01:24:42 +00:00
|
|
|
await transport.close()
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-06-03 02:21:11 +00:00
|
|
|
result = string.fromBytes(msg) == "Hello!"
|
2019-08-21 04:58:12 +00:00
|
|
|
|
|
|
|
check:
|
2019-08-26 15:37:15 +00:00
|
|
|
waitFor(testListener()) == true
|
2019-08-21 22:53:16 +00:00
|
|
|
|
|
|
|
test "test listener: handle read":
|
|
|
|
proc testListener(): Future[bool] {.async.} =
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-04-21 01:24:42 +00:00
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
2020-05-15 04:02:05 +00:00
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-06-03 02:21:11 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
2020-04-21 01:24:42 +00:00
|
|
|
await conn.close()
|
|
|
|
handlerWait.complete()
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
2019-09-12 02:10:38 +00:00
|
|
|
asyncCheck await transport.listen(ma, connHandler)
|
2019-09-25 17:36:39 +00:00
|
|
|
let streamTransport: StreamTransport = await connect(transport.ma)
|
2019-08-21 22:53:16 +00:00
|
|
|
let sent = await streamTransport.write("Hello!", 6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
await streamTransport.closeWait()
|
|
|
|
await transport.close()
|
|
|
|
|
2019-08-21 22:53:16 +00:00
|
|
|
result = sent == 6
|
|
|
|
|
|
|
|
check:
|
2019-08-26 15:37:15 +00:00
|
|
|
waitFor(testListener()) == true
|
2019-08-21 22:53:16 +00:00
|
|
|
|
|
|
|
test "test dialer: handle write":
|
|
|
|
proc testDialer(address: TransportAddress): Future[bool] {.async.} =
|
2020-04-21 01:24:42 +00:00
|
|
|
let handlerWait = newFuture[void]()
|
2019-08-21 22:53:16 +00:00
|
|
|
proc serveClient(server: StreamServer,
|
2019-08-30 15:28:07 +00:00
|
|
|
transp: StreamTransport) {.async, gcsafe.} =
|
2019-08-21 22:53:16 +00:00
|
|
|
var wstream = newAsyncStreamWriter(transp)
|
|
|
|
await wstream.write("Hello!")
|
|
|
|
await wstream.finish()
|
|
|
|
await wstream.closeWait()
|
|
|
|
await transp.closeWait()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
handlerWait.complete()
|
2019-08-21 22:53:16 +00:00
|
|
|
|
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
2019-08-21 22:53:16 +00:00
|
|
|
let conn = await transport.dial(ma)
|
2020-05-15 04:02:05 +00:00
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-06-03 02:21:11 +00:00
|
|
|
result = string.fromBytes(msg) == "Hello!"
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await transport.close()
|
|
|
|
|
2019-08-21 22:53:16 +00:00
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
waitFor(testDialer(initTAddress("0.0.0.0:0"))) == true
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2019-08-21 23:13:20 +00:00
|
|
|
test "test dialer: handle write":
|
2019-08-30 15:28:07 +00:00
|
|
|
proc testDialer(address: TransportAddress): Future[bool] {.async, gcsafe.} =
|
2020-04-21 01:24:42 +00:00
|
|
|
let handlerWait = newFuture[void]()
|
2019-08-26 15:37:15 +00:00
|
|
|
proc serveClient(server: StreamServer,
|
2019-08-30 15:28:07 +00:00
|
|
|
transp: StreamTransport) {.async, gcsafe.} =
|
2019-08-26 15:37:15 +00:00
|
|
|
var rstream = newAsyncStreamReader(transp)
|
|
|
|
let msg = await rstream.read(6)
|
2020-06-03 02:21:11 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
await rstream.closeWait()
|
|
|
|
await transp.closeWait()
|
|
|
|
server.stop()
|
|
|
|
server.close()
|
2020-04-21 01:24:42 +00:00
|
|
|
handlerWait.complete()
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
var server = createStreamServer(address, serveClient, {ReuseAddr})
|
|
|
|
server.start()
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = MultiAddress.init(server.sock.getLocalAddress()).tryGet()
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.init()
|
2019-08-26 15:37:15 +00:00
|
|
|
let conn = await transport.dial(ma)
|
|
|
|
await conn.write(cstring("Hello!"), 6)
|
|
|
|
result = true
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-04-21 01:24:42 +00:00
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await transport.close()
|
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
server.stop()
|
|
|
|
server.close()
|
|
|
|
await server.join()
|
2020-04-21 01:24:42 +00:00
|
|
|
check:
|
|
|
|
waitFor(testDialer(initTAddress("0.0.0.0:0"))) == true
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
test "e2e: handle write":
|
2019-08-21 23:13:20 +00:00
|
|
|
proc testListenerDialer(): Future[bool] {.async.} =
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-04-21 01:24:42 +00:00
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
|
|
|
await conn.write(cstring("Hello!"), 6)
|
|
|
|
await conn.close()
|
|
|
|
handlerWait.complete()
|
2019-08-21 22:53:16 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
2020-04-21 01:24:42 +00:00
|
|
|
asyncCheck transport1.listen(ma, connHandler)
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
2019-09-25 17:36:39 +00:00
|
|
|
let conn = await transport2.dial(transport1.ma)
|
2020-05-15 04:02:05 +00:00
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await transport2.close()
|
2019-08-21 23:13:20 +00:00
|
|
|
await transport1.close()
|
|
|
|
|
2020-06-03 02:21:11 +00:00
|
|
|
result = string.fromBytes(msg) == "Hello!"
|
2019-08-21 23:13:20 +00:00
|
|
|
|
|
|
|
check:
|
2019-08-26 15:37:15 +00:00
|
|
|
waitFor(testListenerDialer()) == true
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2019-08-26 15:37:15 +00:00
|
|
|
test "e2e: handle read":
|
2019-08-21 23:13:20 +00:00
|
|
|
proc testListenerDialer(): Future[bool] {.async.} =
|
2020-05-31 14:22:49 +00:00
|
|
|
let ma: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
2020-04-21 01:24:42 +00:00
|
|
|
let handlerWait = newFuture[void]()
|
|
|
|
proc connHandler(conn: Connection) {.async, gcsafe.} =
|
2020-05-15 04:02:05 +00:00
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
2020-06-03 02:21:11 +00:00
|
|
|
check string.fromBytes(msg) == "Hello!"
|
2020-04-21 01:24:42 +00:00
|
|
|
await conn.close()
|
|
|
|
handlerWait.complete()
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport1: TcpTransport = TcpTransport.init()
|
2020-04-21 01:24:42 +00:00
|
|
|
asyncCheck transport1.listen(ma, connHandler)
|
2019-08-21 23:13:20 +00:00
|
|
|
|
2020-05-18 19:04:05 +00:00
|
|
|
let transport2: TcpTransport = TcpTransport.init()
|
2019-09-25 17:36:39 +00:00
|
|
|
let conn = await transport2.dial(transport1.ma)
|
2019-08-21 22:53:16 +00:00
|
|
|
await conn.write(cstring("Hello!"), 6)
|
2020-04-21 01:24:42 +00:00
|
|
|
|
|
|
|
await handlerWait.wait(5000.millis) # when no issues will not wait that long!
|
|
|
|
|
|
|
|
await conn.close()
|
|
|
|
await transport2.close()
|
2019-08-21 23:13:20 +00:00
|
|
|
await transport1.close()
|
2019-08-21 22:53:16 +00:00
|
|
|
result = true
|
|
|
|
|
2019-08-21 23:13:20 +00:00
|
|
|
check:
|
2019-08-26 15:37:15 +00:00
|
|
|
waitFor(testListenerDialer()) == true
|