2022-02-10 07:05:44 +00:00
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2022-07-09 08:55:15 +00:00
|
|
|
std/[tables, options],
|
2022-06-17 20:45:37 +00:00
|
|
|
chronos,
|
2022-02-10 07:05:44 +00:00
|
|
|
testutils/unittests,
|
|
|
|
../../eth/utp/utp_router,
|
|
|
|
../../eth/utp/utp_protocol,
|
|
|
|
../../eth/keys,
|
|
|
|
../../eth/p2p/discoveryv5/random2
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
proc connectTillSuccess(p: UtpProtocol, to: TransportAddress, maxTries: int = 20): Future[UtpSocket[TransportAddress]] {.async.} =
|
2022-02-10 07:05:44 +00:00
|
|
|
var i = 0
|
|
|
|
while true:
|
|
|
|
let res = await p.connectTo(to)
|
|
|
|
|
|
|
|
if res.isOk():
|
|
|
|
return res.unsafeGet()
|
|
|
|
else:
|
|
|
|
inc i
|
|
|
|
if i >= maxTries:
|
|
|
|
raise newException(CatchableError, "Connection failed")
|
|
|
|
|
|
|
|
proc buildAcceptConnection(
|
|
|
|
t: ref Table[UtpSocketKey[TransportAddress], UtpSocket[TransportAddress]]
|
2022-06-17 20:45:37 +00:00
|
|
|
): AcceptConnectionCallback[TransportAddress] =
|
2022-02-10 07:05:44 +00:00
|
|
|
return (
|
|
|
|
proc (server: UtpRouter[TransportAddress], client: UtpSocket[TransportAddress]): Future[void] =
|
|
|
|
let fut = newFuture[void]()
|
|
|
|
let key = client.socketKey
|
|
|
|
t[key] = client
|
|
|
|
fut.complete()
|
|
|
|
return fut
|
|
|
|
)
|
|
|
|
|
|
|
|
proc getServerSocket(
|
2022-06-17 20:45:37 +00:00
|
|
|
t: ref Table[UtpSocketKey[TransportAddress], UtpSocket[TransportAddress]],
|
2022-02-10 07:05:44 +00:00
|
|
|
clientAddress: TransportAddress,
|
|
|
|
clientConnectionId: uint16): Option[UtpSocket[TransportAddress]] =
|
|
|
|
let serverSocketKey = UtpSocketKey[TransportAddress](remoteAddress: clientAddress, rcvId: clientConnectionId + 1)
|
|
|
|
let srvSocket = t.getOrDefault(serverSocketKey)
|
|
|
|
if srvSocket == nil:
|
|
|
|
return none[UtpSocket[TransportAddress]]()
|
|
|
|
else:
|
|
|
|
return some(srvSocket)
|
|
|
|
|
|
|
|
procSuite "Utp protocol over udp tests with loss and delays":
|
|
|
|
let rng = newRng()
|
|
|
|
|
|
|
|
proc sendBuilder(maxDelay: int, packetDropRate: int): SendCallbackBuilder =
|
|
|
|
return (
|
|
|
|
proc (d: DatagramTransport): SendCallback[TransportAddress] =
|
|
|
|
return (
|
|
|
|
proc (to: TransportAddress, data: seq[byte]): Future[void] {.async.} =
|
|
|
|
let i = rand(rng[], 99)
|
|
|
|
if i >= packetDropRate:
|
|
|
|
let delay = milliseconds(rand(rng[], maxDelay))
|
|
|
|
await sleepAsync(delay)
|
|
|
|
await d.sendTo(to, data)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
proc testScenario(maxDelay: int, dropRate: int, cfg: SocketConfig = SocketConfig.init()):
|
2022-02-10 07:05:44 +00:00
|
|
|
Future[(
|
2022-06-17 20:45:37 +00:00
|
|
|
UtpProtocol,
|
|
|
|
UtpSocket[TransportAddress],
|
|
|
|
UtpProtocol,
|
2022-02-10 07:05:44 +00:00
|
|
|
UtpSocket[TransportAddress])
|
|
|
|
] {.async.} =
|
|
|
|
|
|
|
|
var connections1 = newTable[UtpSocketKey[TransportAddress], UtpSocket[TransportAddress]]()
|
|
|
|
let address1 = initTAddress("127.0.0.1", 9080)
|
2022-06-17 20:45:37 +00:00
|
|
|
let utpProt1 =
|
2022-02-10 07:05:44 +00:00
|
|
|
UtpProtocol.new(
|
2022-06-17 20:45:37 +00:00
|
|
|
buildAcceptConnection(connections1),
|
2022-02-10 07:05:44 +00:00
|
|
|
address1,
|
|
|
|
socketConfig = cfg,
|
2022-06-17 20:45:37 +00:00
|
|
|
sendCallbackBuilder = sendBuilder(maxDelay, dropRate),
|
2022-02-10 07:05:44 +00:00
|
|
|
rng = rng)
|
|
|
|
|
|
|
|
var connections2 = newTable[UtpSocketKey[TransportAddress], UtpSocket[TransportAddress]]()
|
|
|
|
let address2 = initTAddress("127.0.0.1", 9081)
|
2022-06-17 20:45:37 +00:00
|
|
|
let utpProt2 =
|
2022-02-10 07:05:44 +00:00
|
|
|
UtpProtocol.new(
|
|
|
|
buildAcceptConnection(connections2),
|
|
|
|
address2,
|
|
|
|
socketConfig = cfg,
|
|
|
|
sendCallbackBuilder = sendBuilder(maxDelay, dropRate),
|
|
|
|
rng = rng)
|
|
|
|
|
|
|
|
let clientSocket = await utpProt1.connectTillSuccess(address2)
|
|
|
|
let maybeServerSocket = connections2.getServerSocket(address1, clientSocket.socketKey.rcvId)
|
|
|
|
|
|
|
|
let serverSocket = maybeServerSocket.unsafeGet()
|
|
|
|
|
|
|
|
return (utpProt1, clientSocket, utpProt2, serverSocket)
|
|
|
|
|
|
|
|
type TestCase = object
|
|
|
|
# in miliseconds
|
|
|
|
maxDelay: int
|
|
|
|
dropRate: int
|
|
|
|
bytesToTransfer: int
|
|
|
|
bytesPerRead: int
|
|
|
|
cfg: SocketConfig
|
|
|
|
|
|
|
|
proc init(
|
2022-06-17 20:45:37 +00:00
|
|
|
T: type TestCase,
|
|
|
|
maxDelay: int,
|
|
|
|
dropRate: int,
|
2022-02-10 07:05:44 +00:00
|
|
|
bytesToTransfer: int,
|
|
|
|
cfg: SocketConfig = SocketConfig.init(),
|
|
|
|
bytesPerRead: int = 0): TestCase =
|
|
|
|
TestCase(maxDelay: maxDelay, dropRate: dropRate, bytesToTransfer: bytesToTransfer, cfg: cfg, bytesPerRead: bytesPerRead)
|
|
|
|
|
|
|
|
let testCases = @[
|
2022-07-11 06:24:03 +00:00
|
|
|
TestCase.init(15, 3, 40000),
|
2022-02-10 07:05:44 +00:00
|
|
|
# super small recv buffer which will be constantly on the brink of being full
|
2022-07-11 06:24:03 +00:00
|
|
|
TestCase.init(10, 3, 40000, SocketConfig.init(optRcvBuffer = uint32(10000), remoteWindowResetTimeout = seconds(5))),
|
|
|
|
TestCase.init(10, 6, 40000, SocketConfig.init(optRcvBuffer = uint32(10000), remoteWindowResetTimeout = seconds(5)))
|
2022-02-10 07:05:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
asyncTest "Write and Read large data in different network conditions":
|
|
|
|
for testCase in testCases:
|
|
|
|
|
|
|
|
let (
|
|
|
|
clientProtocol,
|
|
|
|
clientSocket,
|
|
|
|
serverProtocol,
|
|
|
|
serverSocket) = await testScenario(testCase.maxDelay, testCase.dropRate, testcase.cfg)
|
|
|
|
|
|
|
|
let smallBytes = 10
|
2022-06-17 20:45:37 +00:00
|
|
|
let smallBytesToTransfer = rng[].generateBytes(smallBytes)
|
2022-02-10 07:05:44 +00:00
|
|
|
# first transfer and read to make server socket connecteced
|
|
|
|
let write1 = await clientSocket.write(smallBytesToTransfer)
|
|
|
|
let read1 = await serverSocket.read(smallBytes)
|
|
|
|
|
|
|
|
check:
|
|
|
|
write1.isOk()
|
|
|
|
read1 == smallBytesToTransfer
|
|
|
|
|
|
|
|
let numBytes = testCase.bytesToTransfer
|
2022-06-17 20:45:37 +00:00
|
|
|
let bytesToTransfer = rng[].generateBytes(numBytes)
|
2022-02-10 07:05:44 +00:00
|
|
|
|
|
|
|
discard clientSocket.write(bytesToTransfer)
|
|
|
|
discard serverSocket.write(bytesToTransfer)
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-02-10 07:05:44 +00:00
|
|
|
let serverReadFut = serverSocket.read(numBytes)
|
|
|
|
let clientReadFut = clientSocket.read(numBytes)
|
|
|
|
|
2022-07-11 06:24:03 +00:00
|
|
|
await allFutures(serverReadFut, clientReadFut)
|
2022-02-10 07:05:44 +00:00
|
|
|
|
|
|
|
let clientRead = clientReadFut.read()
|
|
|
|
let serverRead = serverReadFut.read()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientRead == bytesToTransfer
|
|
|
|
serverRead == bytesToTransfer
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-02-10 07:05:44 +00:00
|
|
|
await clientProtocol.shutdownWait()
|
|
|
|
await serverProtocol.shutdownWait()
|
|
|
|
|
|
|
|
let testCases1 = @[
|
|
|
|
# small buffers so it will fill up between reads
|
2022-07-11 06:24:03 +00:00
|
|
|
TestCase.init(5, 3, 40000, SocketConfig.init(optRcvBuffer = uint32(10000), remoteWindowResetTimeout = seconds(5)), 10000),
|
|
|
|
TestCase.init(10, 6, 40000, SocketConfig.init(optRcvBuffer = uint32(10000), remoteWindowResetTimeout = seconds(5)), 10000),
|
|
|
|
TestCase.init(15, 6, 40000, SocketConfig.init(optRcvBuffer = uint32(10000), remoteWindowResetTimeout = seconds(5)), 10000)
|
2022-02-10 07:05:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
proc readWithMultipleReads(s: UtpSocket[TransportAddress], numOfReads: int, bytesPerRead: int): Future[seq[byte]] {.async.}=
|
|
|
|
var i = 0
|
|
|
|
var res: seq[byte] = @[]
|
|
|
|
while i < numOfReads:
|
|
|
|
let bytes = await s.read(bytesPerRead)
|
|
|
|
res.add(bytes)
|
|
|
|
inc i
|
|
|
|
return res
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-02-10 07:05:44 +00:00
|
|
|
asyncTest "Write and Read large data in different network conditions split over several reads":
|
|
|
|
for testCase in testCases1:
|
|
|
|
|
|
|
|
let (
|
|
|
|
clientProtocol,
|
|
|
|
clientSocket,
|
|
|
|
serverProtocol,
|
|
|
|
serverSocket) = await testScenario(testCase.maxDelay, testCase.dropRate, testcase.cfg)
|
|
|
|
|
|
|
|
let smallBytes = 10
|
2022-06-17 20:45:37 +00:00
|
|
|
let smallBytesToTransfer = rng[].generateBytes(smallBytes)
|
2022-02-10 07:05:44 +00:00
|
|
|
# first transfer and read to make server socket connecteced
|
|
|
|
let write1 = await clientSocket.write(smallBytesToTransfer)
|
|
|
|
let read1 = await serverSocket.read(smallBytes)
|
|
|
|
|
|
|
|
check:
|
|
|
|
read1 == smallBytesToTransfer
|
|
|
|
|
|
|
|
let numBytes = testCase.bytesToTransfer
|
2022-06-17 20:45:37 +00:00
|
|
|
let bytesToTransfer = rng[].generateBytes(numBytes)
|
2022-02-10 07:05:44 +00:00
|
|
|
|
|
|
|
discard clientSocket.write(bytesToTransfer)
|
|
|
|
discard serverSocket.write(bytesToTransfer)
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2022-02-10 07:05:44 +00:00
|
|
|
let numOfReads = int(testCase.bytesToTransfer / testCase.bytesPerRead)
|
|
|
|
let serverReadFut = serverSocket.readWithMultipleReads(numOfReads, testCase.bytesPerRead)
|
|
|
|
let clientReadFut = clientSocket.readWithMultipleReads(numOfReads, testCase.bytesPerRead)
|
|
|
|
|
2022-07-11 06:24:03 +00:00
|
|
|
await allFutures(serverReadFut, clientReadFut)
|
2022-02-10 07:05:44 +00:00
|
|
|
|
|
|
|
let clientRead = clientReadFut.read()
|
|
|
|
let serverRead = serverReadFut.read()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientRead == bytesToTransfer
|
|
|
|
serverRead == bytesToTransfer
|
|
|
|
|
|
|
|
await clientProtocol.shutdownWait()
|
|
|
|
await serverProtocol.shutdownWait()
|
|
|
|
|
2022-03-03 21:38:13 +00:00
|
|
|
let testCase2 = @[
|
2022-07-11 06:24:03 +00:00
|
|
|
TestCase.init(10, 0, 80000),
|
|
|
|
TestCase.init(10, 3, 40000),
|
|
|
|
TestCase.init(15, 6, 40000, SocketConfig.init(optRcvBuffer = uint32(10000), remoteWindowResetTimeout = seconds(5)))
|
2022-03-03 21:38:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
asyncTest "Write large data and read till EOF":
|
|
|
|
for testCase in testCase2:
|
|
|
|
let (
|
|
|
|
clientProtocol,
|
|
|
|
clientSocket,
|
|
|
|
serverProtocol,
|
|
|
|
serverSocket) = await testScenario(testCase.maxDelay, testCase.dropRate, testcase.cfg)
|
|
|
|
|
|
|
|
|
|
|
|
let numBytes = testCase.bytesToTransfer
|
2022-06-17 20:45:37 +00:00
|
|
|
let bytesToTransfer = rng[].generateBytes(numBytes)
|
2022-03-03 21:38:13 +00:00
|
|
|
|
|
|
|
discard await clientSocket.write(bytesToTransfer)
|
|
|
|
clientSocket.close()
|
|
|
|
|
|
|
|
let read = await serverSocket.read()
|
|
|
|
|
|
|
|
check:
|
|
|
|
read == bytesToTransfer
|
|
|
|
|
|
|
|
await clientProtocol.shutdownWait()
|
|
|
|
await serverProtocol.shutdownWait()
|