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)
|
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
import strutils, net, unittest
|
2019-02-06 14:49:11 +00:00
|
|
|
import ../chronos
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2018-11-07 20:57:54 +00:00
|
|
|
const
|
|
|
|
TestsCount = 2000
|
|
|
|
ClientsCount = 20
|
|
|
|
MessagesCount = 20
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client1(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-05-16 08:22:34 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-05-16 08:22:34 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("REQUEST"):
|
|
|
|
var numstr = data[7..^1]
|
|
|
|
var num = parseInt(numstr)
|
|
|
|
var ans = "ANSWER" & $num
|
2018-06-15 10:54:26 +00:00
|
|
|
await transp.sendTo(raddr, addr ans[0], len(ans))
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
|
|
|
var err = "ERROR"
|
2018-06-15 10:54:26 +00:00
|
|
|
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()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client2(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-05-16 08:22:34 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-05-16 08:22:34 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-16 08:22:34 +00:00
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
2018-06-02 14:25:26 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33336")
|
2018-05-16 08:22:34 +00:00
|
|
|
var req = "REQUEST" & $counterPtr[]
|
2018-06-15 10:54:26 +00:00
|
|
|
await transp.sendTo(ta, addr req[0], len(req))
|
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()
|
|
|
|
else:
|
|
|
|
## 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-06-14 06:49:59 +00:00
|
|
|
proc client3(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-05-16 08:22:34 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-05-16 08:22:34 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-16 08:22:34 +00:00
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.send(addr req[0], len(req))
|
|
|
|
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()
|
|
|
|
else:
|
|
|
|
## 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-06-14 06:49:59 +00:00
|
|
|
proc client4(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-05-16 08:22:34 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-05-16 08:22:34 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-16 08:22:34 +00:00
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == MessagesCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.send(addr req[0], len(req))
|
|
|
|
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()
|
|
|
|
else:
|
|
|
|
## 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
|
2018-05-17 23:40:42 +00:00
|
|
|
transp.close()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client5(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-05-17 23:40:42 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-05-17 23:40:42 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-05-17 23:40:42 +00:00
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == MessagesCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
2018-10-27 16:17:37 +00:00
|
|
|
await transp.sendTo(raddr, addr req[0], len(req))
|
2018-05-17 23:40:42 +00:00
|
|
|
else:
|
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()
|
|
|
|
else:
|
|
|
|
## 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
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client6(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-06-05 20:21:07 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-06-05 20:21:07 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("REQUEST"):
|
|
|
|
var numstr = data[7..^1]
|
|
|
|
var num = parseInt(numstr)
|
|
|
|
var ans = "ANSWER" & $num
|
2018-06-15 10:54:26 +00:00
|
|
|
await transp.sendTo(raddr, ans)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
|
|
|
var err = "ERROR"
|
2018-06-15 10:54:26 +00:00
|
|
|
await transp.sendTo(raddr, err)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
|
|
|
## 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()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client7(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-06-05 20:21:07 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-06-05 20:21:07 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
2018-10-27 16:17:37 +00:00
|
|
|
await transp.sendTo(raddr, req)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
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()
|
|
|
|
else:
|
|
|
|
## 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()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client8(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-06-05 20:21:07 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-06-05 20:21:07 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
counterPtr[] = counterPtr[] + 1
|
|
|
|
if counterPtr[] == TestsCount:
|
|
|
|
transp.close()
|
|
|
|
else:
|
|
|
|
var req = "REQUEST" & $counterPtr[]
|
|
|
|
await transp.send(req)
|
|
|
|
else:
|
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()
|
|
|
|
else:
|
|
|
|
## 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()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client9(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-06-05 20:21:07 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-06-05 20:21:07 +00:00
|
|
|
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))
|
2018-06-15 10:54:26 +00:00
|
|
|
await transp.sendTo(raddr, ansseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
|
|
|
var err = "ERROR"
|
|
|
|
var errseq = newSeq[byte](len(err))
|
|
|
|
copyMem(addr errseq[0], addr err[0], len(err))
|
2018-06-15 10:54:26 +00:00
|
|
|
await transp.sendTo(raddr, errseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
|
|
|
## 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()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client10(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-06-05 20:21:07 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-06-05 20:21:07 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
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))
|
2018-10-27 16:17:37 +00:00
|
|
|
await transp.sendTo(raddr, reqseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
else:
|
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()
|
|
|
|
else:
|
|
|
|
## 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()
|
|
|
|
|
2018-06-14 06:49:59 +00:00
|
|
|
proc client11(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
var pbytes: seq[byte]
|
|
|
|
var nbytes: int
|
|
|
|
transp.peekMessage(pbytes, nbytes)
|
|
|
|
if nbytes > 0:
|
2018-06-05 20:21:07 +00:00
|
|
|
var data = newString(nbytes + 1)
|
2018-06-14 06:49:59 +00:00
|
|
|
copyMem(addr data[0], addr pbytes[0], nbytes)
|
2018-06-05 20:21:07 +00:00
|
|
|
data.setLen(nbytes)
|
|
|
|
if data.startsWith("ANSWER"):
|
2018-06-14 06:49:59 +00:00
|
|
|
var counterPtr = cast[ptr int](transp.udata)
|
2018-06-05 20:21:07 +00:00
|
|
|
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)
|
|
|
|
else:
|
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()
|
|
|
|
else:
|
|
|
|
## 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()
|
|
|
|
|
|
|
|
proc testPointerSendTo(): Future[int] {.async.} =
|
|
|
|
## sendTo(pointer) test
|
2018-06-02 14:25:26 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33336")
|
2018-05-16 08:22:34 +00:00
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client1, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client2, udata = addr counter)
|
|
|
|
var data = "REQUEST0"
|
2018-06-15 10:54:26 +00:00
|
|
|
await dgram2.sendTo(ta, addr data[0], len(data))
|
2018-05-16 08:22:34 +00:00
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
2018-10-27 17:49:36 +00:00
|
|
|
await dgram1.join()
|
2018-05-16 08:22:34 +00:00
|
|
|
result = counter
|
|
|
|
|
2018-06-05 20:21:07 +00:00
|
|
|
proc testPointerSend(): Future[int] {.async.} =
|
|
|
|
## send(pointer) test
|
2018-06-02 14:25:26 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33337")
|
2018-05-16 08:22:34 +00:00
|
|
|
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()
|
2018-10-27 17:49:36 +00:00
|
|
|
await dgram1.join()
|
2018-05-16 08:22:34 +00:00
|
|
|
result = counter
|
|
|
|
|
2018-06-05 20:21:07 +00:00
|
|
|
proc testStringSendTo(): Future[int] {.async.} =
|
|
|
|
## sendTo(string) test
|
2018-10-27 16:17:37 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33338")
|
2018-06-05 20:21:07 +00:00
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client6, udata = addr counter, local = ta)
|
|
|
|
var dgram2 = newDatagramTransport(client7, udata = addr counter)
|
|
|
|
var data = "REQUEST0"
|
2018-06-15 10:54:26 +00:00
|
|
|
await dgram2.sendTo(ta, data)
|
2018-06-05 20:21:07 +00:00
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
2018-10-27 17:49:36 +00:00
|
|
|
await dgram1.join()
|
2018-06-05 20:21:07 +00:00
|
|
|
result = counter
|
|
|
|
|
|
|
|
proc testStringSend(): Future[int] {.async.} =
|
|
|
|
## send(string) test
|
2018-10-27 16:17:37 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33339")
|
2018-06-05 20:21:07 +00:00
|
|
|
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()
|
2018-10-27 17:49:36 +00:00
|
|
|
await dgram1.join()
|
2018-06-05 20:21:07 +00:00
|
|
|
result = counter
|
|
|
|
|
|
|
|
proc testSeqSendTo(): Future[int] {.async.} =
|
|
|
|
## sendTo(string) test
|
2018-10-27 16:17:37 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33340")
|
2018-06-05 20:21:07 +00:00
|
|
|
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))
|
2018-06-15 10:54:26 +00:00
|
|
|
await dgram2.sendTo(ta, dataseq)
|
2018-06-05 20:21:07 +00:00
|
|
|
await dgram2.join()
|
|
|
|
dgram1.close()
|
2018-10-27 17:49:36 +00:00
|
|
|
await dgram1.join()
|
2018-06-05 20:21:07 +00:00
|
|
|
result = counter
|
|
|
|
|
|
|
|
proc testSeqSend(): Future[int] {.async.} =
|
|
|
|
## send(string) test
|
2018-10-27 16:17:37 +00:00
|
|
|
var ta = initTAddress("127.0.0.1:33341")
|
2018-06-05 20:21:07 +00:00
|
|
|
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()
|
2018-10-27 17:49:36 +00:00
|
|
|
await dgram1.join()
|
2018-06-05 20:21:07 +00:00
|
|
|
result = counter
|
|
|
|
|
|
|
|
#
|
|
|
|
|
2018-05-16 08:22:34 +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-17 23:40:42 +00:00
|
|
|
proc test3(bounded: bool): Future[int] {.async.} =
|
2018-08-24 12:20:08 +00:00
|
|
|
var ta: TransportAddress
|
|
|
|
if bounded:
|
2018-10-27 16:17:37 +00:00
|
|
|
ta = initTAddress("127.0.0.1:33240")
|
2018-08-24 12:20:08 +00:00
|
|
|
else:
|
2018-10-27 16:17:37 +00:00
|
|
|
ta = initTAddress("127.0.0.1:33241")
|
2018-05-16 08:22:34 +00:00
|
|
|
var counter = 0
|
|
|
|
var dgram1 = newDatagramTransport(client1, udata = addr counter, local = ta)
|
|
|
|
var clients = newSeq[Future[void]](ClientsCount)
|
2018-06-05 08:31:18 +00:00
|
|
|
var grams = newSeq[DatagramTransport](ClientsCount)
|
2018-05-16 08:22:34 +00:00
|
|
|
var counters = newSeq[int](ClientsCount)
|
|
|
|
for i in 0..<ClientsCount:
|
|
|
|
var data = "REQUEST0"
|
2018-05-17 23:40:42 +00:00
|
|
|
if bounded:
|
2018-06-05 08:31:18 +00:00
|
|
|
grams[i] = newDatagramTransport(client4, udata = addr counters[i],
|
|
|
|
remote = ta)
|
|
|
|
await grams[i].send(addr data[0], len(data))
|
2018-05-17 23:40:42 +00:00
|
|
|
else:
|
2018-06-05 08:31:18 +00:00
|
|
|
grams[i] = newDatagramTransport(client5, udata = addr counters[i])
|
2018-06-15 10:54:26 +00:00
|
|
|
await grams[i].sendTo(ta, addr data[0], len(data))
|
2018-06-05 08:31:18 +00:00
|
|
|
clients[i] = grams[i].join()
|
2018-05-17 23:40:42 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
await waitAll(clients)
|
|
|
|
dgram1.close()
|
2018-10-25 15:18:36 +00:00
|
|
|
await dgram1.join()
|
2018-05-16 08:22:34 +00:00
|
|
|
result = 0
|
|
|
|
for i in 0..<ClientsCount:
|
|
|
|
result += counters[i]
|
|
|
|
|
2018-06-15 00:28:02 +00:00
|
|
|
proc testConnReset(): Future[bool] {.async.} =
|
|
|
|
var ta = initTAddress("127.0.0.1:65000")
|
|
|
|
var counter = 0
|
2018-10-02 10:50:14 +00:00
|
|
|
proc clientMark(transp: DatagramTransport,
|
|
|
|
raddr: TransportAddress): Future[void] {.async.} =
|
|
|
|
counter = 1
|
|
|
|
transp.close()
|
2018-06-15 00:28:02 +00:00
|
|
|
var dgram1 = newDatagramTransport(client1, local = ta)
|
|
|
|
dgram1.close()
|
2018-10-25 15:18:36 +00:00
|
|
|
await dgram1.join()
|
2018-10-02 10:50:14 +00:00
|
|
|
var dgram2 = newDatagramTransport(clientMark)
|
2018-06-15 00:28:02 +00:00
|
|
|
var data = "MESSAGE"
|
2018-10-02 10:50:14 +00:00
|
|
|
asyncCheck dgram2.sendTo(ta, data)
|
2018-06-15 00:28:02 +00:00
|
|
|
await sleepAsync(1000)
|
|
|
|
result = (counter == 0)
|
2018-10-27 17:49:36 +00:00
|
|
|
dgram2.close()
|
|
|
|
await dgram2.join()
|
2018-06-15 00:28:02 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
when isMainModule:
|
2018-05-17 23:40:42 +00:00
|
|
|
const
|
2018-06-05 20:21:07 +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 &
|
2018-05-17 23:40:42 +00:00
|
|
|
" clients x " & $MessagesCount & " messages)"
|
2018-06-05 20:21:07 +00:00
|
|
|
m8 = "Bounded multiple clients with messages (" & $ClientsCount &
|
2018-05-17 23:40:42 +00:00
|
|
|
" clients x " & $MessagesCount & " messages)"
|
2018-05-16 08:22:34 +00:00
|
|
|
suite "Datagram Transport test suite":
|
2018-05-17 23:40:42 +00:00
|
|
|
test m1:
|
2018-06-05 20:21:07 +00:00
|
|
|
check waitFor(testPointerSendTo()) == TestsCount
|
2018-05-17 23:40:42 +00:00
|
|
|
test m2:
|
2018-06-05 20:21:07 +00:00
|
|
|
check waitFor(testPointerSend()) == TestsCount
|
2018-05-17 23:40:42 +00:00
|
|
|
test m3:
|
2018-06-05 20:21:07 +00:00
|
|
|
check waitFor(testStringSendTo()) == TestsCount
|
2018-05-17 23:40:42 +00:00
|
|
|
test m4:
|
2018-06-05 20:21:07 +00:00
|
|
|
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:
|
2018-05-17 23:40:42 +00:00
|
|
|
check waitFor(test3(true)) == ClientsCount * MessagesCount
|
2018-06-15 00:28:02 +00:00
|
|
|
test "Datagram connection reset test":
|
|
|
|
check waitFor(testConnReset()) == true
|