fix: testIntegrationUsecase

This commit is contained in:
Richard Ramos
2025-10-30 21:37:44 -04:00
parent 718df6d474
commit 1ec78bf225

View File

@@ -1,3 +1,4 @@
import sequtils
import results
import pkg/unittest2
import pkg/quic
@@ -23,26 +24,24 @@ template deferStop(listener: Listener) =
listener.destroy()
suite "Quic integration usecases":
test "client to server send and receive message":
asyncTest "client to server send and receive message":
let message = newData(1024 * 1024)
let address = initTAddress("127.0.0.1:12345")
proc outgoing() {.async.} =
let client = makeClient()
let connection = await client.dial(address)
check connection.certificates().len == 1
let
client = makeClient()
server = makeServer()
listener = server.listen(address)
dialing = client.dial(address)
accepting = listener.accept()
proc outgoing(connection: Connection) {.async.} =
check connection.certificates().len == 1
let stream = await connection.openStream()
await stream.write(message)
await stream.close()
await connection.close()
proc incoming() {.async.} =
let server = makeServer()
let listener = server.listen(address)
listener.deferStop()
let connection = await listener.accept()
proc incoming(connection: Connection) {.async.} =
check connection.certificates().len == 1
let stream = await connection.incomingStream()
@@ -50,9 +49,13 @@ suite "Quic integration usecases":
checkEqual(message, receivedData)
await stream.close()
await connection.close()
waitFor allFutures(incoming(), outgoing())
let clientConn = await dialing
let serverConn = await accepting
await allFutures(serverConn.incoming(), clientConn.outgoing())
await allFutures(clientConn.close(), serverConn.close())
await listener.stop()
asyncTest "connect many clients to single server":
const connectionsCount = 20
@@ -72,28 +75,25 @@ suite "Quic integration usecases":
checkEqual(message, receivedData)
await stream.close()
await connection.close()
serverWg.done()
proc runClient() {.async.} =
let client = makeClient()
let connection = await client.dial(address)
asyncSpawn accept(listener, handleServerConn)
proc runClient(connection: Connection) {.async.} =
let stream = await connection.openStream()
await stream.write(message)
await stream.close()
# client needs to wait some time before closing connections.
# because if connection is closed too early data will not be transmitted to server.
await sleepAsync(300.milliseconds)
await connection.close()
clientWg.done()
asyncSpawn accept(listener, handleServerConn)
var clientConnections: seq[Connection] = @[]
for i in 0 ..< connectionsCount:
asyncSpawn runClient()
waitFor allFutures(serverWg.wait(), clientWg.wait())
let client = makeClient()
let connection = await client.dial(address)
clientConnections.add(connection)
asyncSpawn runClient(connection)
await allFutures(serverWg.wait(), clientWg.wait())
await allFutures(clientConnections.mapIt(it.close()))
asyncTest "connections with many streams":
const connectionsCount = 3