2020-05-08 20:10:06 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
import 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,
|
2021-03-18 15:20:36 +00:00
|
|
|
upgrademngrs/upgrade,
|
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
|
|
|
|
2021-06-30 08:59:30 +00:00
|
|
|
import ./helpers, ./commontransport
|
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":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2021-06-14 23:21:44 +00:00
|
|
|
asyncSpawn 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
|
|
|
|
2021-11-24 20:01:12 +00:00
|
|
|
let streamTransport = await connect(transport.addrs[0])
|
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
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-13 03:44:02 +00:00
|
|
|
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":
|
2021-12-16 10:05:20 +00:00
|
|
|
let ma = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]
|
2020-11-19 02:06:42 +00:00
|
|
|
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2021-06-14 23:21:44 +00:00
|
|
|
asyncSpawn transport.start(ma)
|
2020-11-19 02:06:42 +00:00
|
|
|
|
|
|
|
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()
|
2021-11-24 20:01:12 +00:00
|
|
|
let streamTransport: StreamTransport = await connect(transport.addrs[0])
|
2020-11-19 02:06:42 +00:00
|
|
|
let sent = await streamTransport.write("Hello!")
|
2020-11-13 03:44:02 +00:00
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
2020-11-13 03:44:02 +00:00
|
|
|
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()
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2020-11-13 03:44:02 +00:00
|
|
|
let conn = await transport.dial(ma)
|
|
|
|
var msg = newSeq[byte](6)
|
|
|
|
await conn.readExactly(addr msg[0], 6)
|
|
|
|
check string.fromBytes(msg) == "Hello!"
|
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # when no issues will not wait that long!
|
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 "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()
|
2021-06-30 08:59:30 +00:00
|
|
|
let transport: TcpTransport = TcpTransport.new(upgrade = Upgrade())
|
2020-11-13 03:44:02 +00:00
|
|
|
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
|
|
|
|
2021-06-14 23:21:44 +00:00
|
|
|
await handlerWait.wait(1.seconds) # 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()
|
|
|
|
|
2022-11-04 18:20:23 +00:00
|
|
|
proc transProvider(): Transport = TcpTransport.new(upgrade = Upgrade())
|
|
|
|
|
2021-08-03 13:48:03 +00:00
|
|
|
commonTransportTest(
|
2022-11-04 18:20:23 +00:00
|
|
|
transProvider,
|
2021-08-03 13:48:03 +00:00
|
|
|
"/ip4/0.0.0.0/tcp/0")
|