2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-17 23:40:42 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
import std/[strutils, net]
|
2023-07-14 10:35:08 +00:00
|
|
|
import ".."/chronos/unittest2/asynctests
|
|
|
|
import ".."/chronos
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
{.used.}
|
2019-10-24 13:01:57 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
suite "Datagram Transport test suite":
|
|
|
|
const
|
|
|
|
TestsCount = 2000
|
|
|
|
ClientsCount = 20
|
|
|
|
MessagesCount = 20
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
m1 = "sendTo(pointer) test (" & $TestsCount & " messages)"
|
|
|
|
m2 = "send(pointer) test (" & $TestsCount & " messages)"
|
|
|
|
m3 = "sendTo(string) test (" & $TestsCount & " messages)"
|
|
|
|
m4 = "send(string) test (" & $TestsCount & " messages)"
|
|
|
|
m5 = "sendTo(seq[byte]) test (" & $TestsCount & " messages)"
|
|
|
|
m6 = "send(seq[byte]) test (" & $TestsCount & " messages)"
|
|
|
|
m7 = "Unbounded multiple clients with messages (" & $ClientsCount &
|
|
|
|
" clients x " & $MessagesCount & " messages)"
|
|
|
|
m8 = "Bounded multiple clients with messages (" & $ClientsCount &
|
|
|
|
" clients x " & $MessagesCount & " messages)"
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client1(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("REQUEST"):
|
|
|
|
var numstr = data[7..^1]
|
|
|
|
var num = parseInt(numstr)
|
|
|
|
var ans = "ANSWER" & $num
|
|
|
|
await transp.sendTo(raddr, addr ans[0], len(ans))
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var err = "ERROR"
|
|
|
|
await transp.sendTo(raddr, addr err[0], len(err))
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-16 08:22:34 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client2(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var ta = initTAddress("127.0.0.1:33336")
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.sendTo(ta, addr req[0], len(req))
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-16 08:22:34 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client3(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.send(addr req[0], len(req))
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-16 08:22:34 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-05-17 23:40:42 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client4(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == MessagesCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.send(addr req[0], len(req))
|
2018-05-17 23:40:42 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-05-17 23:40:42 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-17 23:40:42 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client5(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == MessagesCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.sendTo(raddr, addr req[0], len(req))
|
|
|
|
else:
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2019-03-30 22:31:10 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
|
|
|
proc client6(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("REQUEST"):
|
|
|
|
var numstr = data[7..^1]
|
|
|
|
var num = parseInt(numstr)
|
|
|
|
var ans = "ANSWER" & $num
|
|
|
|
await transp.sendTo(raddr, ans)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var err = "ERROR"
|
|
|
|
await transp.sendTo(raddr, err)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client7(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.sendTo(raddr, req)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client8(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.send(req)
|
|
|
|
else:
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2019-03-30 22:31:10 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
|
|
|
proc client9(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("REQUEST"):
|
|
|
|
var numstr = data[7..^1]
|
|
|
|
var num = parseInt(numstr)
|
|
|
|
var ans = "ANSWER" & $num
|
|
|
|
var ansseq = newSeq[byte](len(ans))
|
|
|
|
copyMem(addr ansseq[0], addr ans[0], len(ans))
|
|
|
|
await transp.sendTo(raddr, ansseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var err = "ERROR"
|
|
|
|
var errseq = newSeq[byte](len(err))
|
|
|
|
copyMem(addr errseq[0], addr err[0], len(err))
|
|
|
|
await transp.sendTo(raddr, errseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc client10(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
var reqseq = newSeq[byte](len(req))
|
|
|
|
copyMem(addr reqseq[0], addr req[0], len(req))
|
|
|
|
await transp.sendTo(raddr, reqseq)
|
|
|
|
else:
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
2018-06-05 20:21:07 +00:00
|
|
|
transp.close()
|
2019-03-30 22:31:10 +00:00
|
|
|
else:
|
|
|
|
## Read operation failed with error
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
|
|
|
proc client11(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes = transp.getMessage()
|
|
|
|
var nbytes = len(pbytes)
|
|
|
|
if nbytes > 0:
|
|
|
|
var data = newString(nbytes + 1)
|
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
var reqseq = newSeq[byte](len(req))
|
|
|
|
copyMem(addr reqseq[0], addr req[0], len(req))
|
|
|
|
await transp.send(reqseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
## Read operation failed with error
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
counterPtr[] = -1
|
|
|
|
transp.close()
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testPointerSendTo(): Future[int] {.async.} =
|
|
|
|
## sendTo(pointer) test
|
|
|
|
var ta = initTAddress("127.0.0.1:33336")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client1, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client2, udata = addr counter)
|
|
|
|
var data = "REQUEST0"
|
|
|
|
await dgram2.sendTo(ta, addr data[0], len(data))
|
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = counter
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testPointerSend(): Future[int] {.async.} =
|
|
|
|
## send(pointer) test
|
|
|
|
var ta = initTAddress("127.0.0.1:33337")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client1, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client3, udata = addr counter, remote = ta)
|
|
|
|
var data = "REQUEST0"
|
|
|
|
await dgram2.send(addr data[0], len(data))
|
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = counter
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testStringSendTo(): Future[int] {.async.} =
|
|
|
|
## sendTo(string) test
|
|
|
|
var ta = initTAddress("127.0.0.1:33338")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client6, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client7, udata = addr counter)
|
|
|
|
var data = "REQUEST0"
|
|
|
|
await dgram2.sendTo(ta, data)
|
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = counter
|
2018-06-05 20:21:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testStringSend(): Future[int] {.async.} =
|
|
|
|
## send(string) test
|
|
|
|
var ta = initTAddress("127.0.0.1:33339")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client6, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client8, udata = addr counter, remote = ta)
|
|
|
|
var data = "REQUEST0"
|
|
|
|
await dgram2.send(data)
|
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = counter
|
2018-06-05 20:21:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testSeqSendTo(): Future[int] {.async.} =
|
|
|
|
## sendTo(string) test
|
|
|
|
var ta = initTAddress("127.0.0.1:33340")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client9, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client10, udata = addr counter)
|
|
|
|
var data = "REQUEST0"
|
|
|
|
var dataseq = newSeq[byte](len(data))
|
|
|
|
copyMem(addr dataseq[0], addr data[0], len(data))
|
|
|
|
await dgram2.sendTo(ta, dataseq)
|
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = counter
|
2018-06-05 20:21:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testSeqSend(): Future[int] {.async.} =
|
|
|
|
## send(seq) test
|
|
|
|
var ta = initTAddress("127.0.0.1:33341")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client9, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client11, udata = addr counter, remote = ta)
|
|
|
|
var data = "REQUEST0"
|
|
|
|
var dataseq = newSeq[byte](len(data))
|
|
|
|
copyMem(addr dataseq[0], addr data[0], len(data))
|
|
|
|
await dgram2.send(data)
|
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = counter
|
2018-06-05 20:21:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
#
|
2018-06-05 20:21:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc waitAll(futs: seq[Future[void]]): Future[void] =
|
|
|
|
var counter = len(futs)
|
|
|
|
var retFuture = newFuture[void]("waitAll")
|
|
|
|
proc cb(udata: pointer) =
|
|
|
|
dec(counter)
|
|
|
|
if counter == 0:
|
|
|
|
retFuture.complete()
|
|
|
|
for fut in futs:
|
|
|
|
fut.addCallback(cb)
|
|
|
|
return retFuture
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc test3(bounded: bool): Future[int] {.async.} =
|
|
|
|
var ta: TransportAddress
|
2018-05-17 23:40:42 +00:00
|
|
|
if bounded:
|
2019-03-30 22:31:10 +00:00
|
|
|
ta = initTAddress("127.0.0.1:33240")
|
2018-05-17 23:40:42 +00:00
|
|
|
else:
|
2019-03-30 22:31:10 +00:00
|
|
|
ta = initTAddress("127.0.0.1:33241")
|
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client1, udata = addr counter, local = ta)
|
|
|
|
var clients = newSeq[Future[void]](ClientsCount)
|
|
|
|
var grams = newSeq[DatagramTransport](ClientsCount)
|
|
|
|
var counters = newSeq[int](ClientsCount)
|
|
|
|
for i in 0..<ClientsCount:
|
|
|
|
var data = "REQUEST0"
|
|
|
|
if bounded:
|
|
|
|
grams[i] = newDatagramTransport(client4, udata = addr counters[i],
|
|
|
|
remote = ta)
|
|
|
|
await grams[i].send(addr data[0], len(data))
|
|
|
|
else:
|
|
|
|
grams[i] = newDatagramTransport(client5, udata = addr counters[i])
|
|
|
|
await grams[i].sendTo(ta, addr data[0], len(data))
|
|
|
|
clients[i] = grams[i].join()
|
2018-05-17 23:40:42 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
await waitAll(clients)
|
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
result = 0
|
|
|
|
for i in 0..<ClientsCount:
|
|
|
|
result += counters[i]
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testConnReset(): Future[bool] {.async.} =
|
2020-01-28 10:47:38 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:0")
|
2019-03-30 22:31:10 +00:00
|
|
|
var counter = 0
|
|
|
|
proc clientMark(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
counter = 1
|
|
|
|
transp.close()
|
|
|
|
var dgram1 = newDatagramTransport(client1, local = ta)
|
2020-01-28 10:47:38 +00:00
|
|
|
var localta = dgram1.localAddress()
|
2019-03-30 22:31:10 +00:00
|
|
|
dgram1.close()
|
|
|
|
await dgram1.join()
|
|
|
|
var dgram2 = newDatagramTransport(clientMark)
|
|
|
|
var data = "MESSAGE"
|
2021-05-07 20:52:24 +00:00
|
|
|
asyncSpawn dgram2.sendTo(localta, data)
|
2019-03-30 22:31:10 +00:00
|
|
|
await sleepAsync(2000.milliseconds)
|
|
|
|
result = (counter == 0)
|
|
|
|
dgram2.close()
|
|
|
|
await dgram2.join()
|
2018-06-15 00:28:02 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testTransportClose(): Future[bool] {.async.} =
|
|
|
|
var ta = initTAddress("127.0.0.1:45000")
|
|
|
|
proc clientMark(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
discard
|
|
|
|
var dgram = newDatagramTransport(clientMark, local = ta)
|
|
|
|
dgram.close()
|
|
|
|
try:
|
|
|
|
await wait(dgram.join(), 1.seconds)
|
|
|
|
result = true
|
2023-06-05 11:03:38 +00:00
|
|
|
except CatchableError:
|
2019-03-30 22:31:10 +00:00
|
|
|
discard
|
|
|
|
|
2019-03-31 06:18:21 +00:00
|
|
|
proc testBroadcast(): Future[int] {.async.} =
|
|
|
|
const expectMessage = "BROADCAST MESSAGE"
|
|
|
|
var ta1 = initTAddress("0.0.0.0:45010")
|
|
|
|
var bta = initTAddress("255.255.255.255:45010")
|
|
|
|
var res = 0
|
|
|
|
proc clientMark(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var bmsg = transp.getMessage()
|
|
|
|
var smsg = cast[string](bmsg)
|
|
|
|
if smsg == expectMessage:
|
|
|
|
inc(res)
|
|
|
|
transp.close()
|
|
|
|
var dgram1 = newDatagramTransport(clientMark, local = ta1,
|
2019-04-15 01:27:12 +00:00
|
|
|
flags = {Broadcast}, ttl = 2)
|
2019-03-31 06:18:21 +00:00
|
|
|
await dgram1.sendTo(bta, expectMessage)
|
|
|
|
await wait(dgram1.join(), 5.seconds)
|
|
|
|
result = res
|
|
|
|
|
2020-02-25 21:50:39 +00:00
|
|
|
proc testAnyAddress(): Future[int] {.async.} =
|
|
|
|
var expectStr = "ANYADDRESS MESSAGE"
|
|
|
|
var expectSeq = cast[seq[byte]](expectStr)
|
|
|
|
let ta = initTAddress("0.0.0.0:0")
|
|
|
|
var res = 0
|
|
|
|
var event = newAsyncEvent()
|
|
|
|
|
|
|
|
proc clientMark1(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var bmsg = transp.getMessage()
|
|
|
|
var smsg = cast[string](bmsg)
|
|
|
|
if smsg == expectStr:
|
|
|
|
inc(res)
|
|
|
|
event.fire()
|
|
|
|
|
|
|
|
proc clientMark2(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
var dgram1 = newDatagramTransport(clientMark1, local = ta)
|
|
|
|
let la = dgram1.localAddress()
|
|
|
|
var dgram2 = newDatagramTransport(clientMark2)
|
|
|
|
var dgram3 = newDatagramTransport(clientMark2,
|
|
|
|
remote = la)
|
|
|
|
await dgram2.sendTo(la, addr expectStr[0], len(expectStr))
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
await dgram2.sendTo(la, expectStr)
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
await dgram2.sendTo(la, expectSeq)
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
await dgram3.send(addr expectStr[0], len(expectStr))
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
await dgram3.send(expectStr)
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
await dgram3.send(expectSeq)
|
|
|
|
await event.wait()
|
|
|
|
event.clear()
|
|
|
|
|
|
|
|
await dgram1.closeWait()
|
|
|
|
await dgram2.closeWait()
|
|
|
|
await dgram3.closeWait()
|
|
|
|
|
|
|
|
result = res
|
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
test "close(transport) test":
|
|
|
|
check waitFor(testTransportClose()) == true
|
|
|
|
test m1:
|
|
|
|
check waitFor(testPointerSendTo()) == TestsCount
|
|
|
|
test m2:
|
|
|
|
check waitFor(testPointerSend()) == TestsCount
|
|
|
|
test m3:
|
|
|
|
check waitFor(testStringSendTo()) == TestsCount
|
|
|
|
test m4:
|
|
|
|
check waitFor(testStringSend()) == TestsCount
|
|
|
|
test m5:
|
|
|
|
check waitFor(testSeqSendTo()) == TestsCount
|
|
|
|
test m6:
|
|
|
|
check waitFor(testSeqSend()) == TestsCount
|
|
|
|
test m7:
|
|
|
|
check waitFor(test3(false)) == ClientsCount * MessagesCount
|
|
|
|
test m8:
|
|
|
|
check waitFor(test3(true)) == ClientsCount * MessagesCount
|
|
|
|
test "Datagram connection reset test":
|
|
|
|
check waitFor(testConnReset()) == true
|
2019-03-31 06:18:21 +00:00
|
|
|
test "Broadcast test":
|
|
|
|
check waitFor(testBroadcast()) == 1
|
2020-02-25 21:50:39 +00:00
|
|
|
test "0.0.0.0/::0 (INADDR_ANY) test":
|
|
|
|
check waitFor(testAnyAddress()) == 6
|
2019-04-04 09:34:23 +00:00
|
|
|
test "Transports leak test":
|
2023-07-14 10:35:08 +00:00
|
|
|
checkLeaks()
|