diff options
Diffstat (limited to 'tests/async')
-rw-r--r-- | tests/async/tasyncRecvLine.nim | 54 | ||||
-rw-r--r-- | tests/async/tasync_forward.nim | 9 | ||||
-rw-r--r-- | tests/async/tasyncall.nim | 77 | ||||
-rw-r--r-- | tests/async/tioselectors.nim | 12 | ||||
-rw-r--r-- | tests/async/tlambda.nim | 3 |
5 files changed, 113 insertions, 42 deletions
diff --git a/tests/async/tasyncRecvLine.nim b/tests/async/tasyncRecvLine.nim new file mode 100644 index 000000000..679831b27 --- /dev/null +++ b/tests/async/tasyncRecvLine.nim @@ -0,0 +1,54 @@ +discard """ + file: "tasyncRecvLine.nim" + output: ''' +Hello World +Hello World +''' +""" + +import asyncdispatch, asyncnet + +const recvLinePort = Port(6047) + +proc setupTestServer(): AsyncSocket = + result = newAsyncSocket() + result.setSockOpt(OptReuseAddr, true) + result.bindAddr(recvLinePort) + result.listen() + +proc testUnbuffered(): Future[void] {.async.} = + let serverSock = setupTestServer() + let serverAcceptClientFut = serverSock.accept() + + let clientSock = newAsyncSocket(buffered = false) + let clientConnectFut = clientSock.connect("localhost", recvLinePort) + + let serverAcceptedClient = await serverAcceptClientFut + await clientConnectFut + + await serverAcceptedClient.send("Hello World\c\L") + + echo await clientSock.recvLine() + + clientSock.close() + serverSock.close() + +proc testBuffered(): Future[void] {.async.} = + let serverSock = setupTestServer() + let serverAcceptClientFut = serverSock.accept() + + let clientSock = newAsyncSocket(buffered = true) + let clientConnectFut = clientSock.connect("localhost", recvLinePort) + + let serverAcceptedClient = await serverAcceptClientFut + await clientConnectFut + + await serverAcceptedClient.send("Hello World\c\L") + + echo await clientSock.recvLine() + + clientSock.close() + serverSock.close() + +waitFor testUnbuffered() +waitFor testBuffered() diff --git a/tests/async/tasync_forward.nim b/tests/async/tasync_forward.nim index ffb7acafd..99527032f 100644 --- a/tests/async/tasync_forward.nim +++ b/tests/async/tasync_forward.nim @@ -7,3 +7,12 @@ proc foo {.async.} proc foo {.async.} = discard + +# With additional pragmas: +proc bar {.async, cdecl.} + +proc bar {.async.} = + discard + +proc verifyCdeclPresent(p: proc : Future[void] {.cdecl.}) = discard +verifyCdeclPresent(bar) diff --git a/tests/async/tasyncall.nim b/tests/async/tasyncall.nim index 63b2945a6..7daecd9ef 100644 --- a/tests/async/tasyncall.nim +++ b/tests/async/tasyncall.nim @@ -2,19 +2,19 @@ discard """ file: "tasyncall.nim" exitcode: 0 """ -import times, sequtils +import times, sequtils, unittest import asyncdispatch const taskCount = 10 - sleepDuration = 500 + sleepDuration = 50 proc futureWithValue(x: int): Future[int] {.async.} = await sleepAsync(sleepDuration) return x proc futureWithoutValue() {.async.} = - await sleepAsync(1000) + await sleepAsync(sleepDuration) proc testFuturesWithValue(x: int): seq[int] = var tasks = newSeq[Future[int]](taskCount) @@ -40,38 +40,39 @@ proc testVarargs(x, y, z: int): seq[int] = result = waitFor all(a, b, c) -block: - let - startTime = cpuTime() - results = testFuturesWithValue(42) - expected = repeat(42, taskCount) - execTime = cpuTime() - startTime - - doAssert execTime * 1000 < taskCount * sleepDuration - doAssert results == expected - -block: - let startTime = cpuTime() - testFuturesWithoutValues() - let execTime = cpuTime() - startTime - - doAssert execTime * 1000 < taskCount * sleepDuration - -block: - let - startTime = cpuTime() - results = testVarargs(1, 2, 3) - expected = @[1, 2, 3] - execTime = cpuTime() - startTime - - doAssert execTime * 100 < taskCount * sleepDuration - doAssert results == expected - -block: - let - noIntFuturesFut = all(newSeq[Future[int]]()) - noVoidFuturesFut = all(newSeq[Future[void]]()) - - doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed - doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed - doAssert noIntFuturesFut.read() == @[] +suite "tasyncall": + test "testFuturesWithValue": + let + startTime = cpuTime() + results = testFuturesWithValue(42) + expected = repeat(42, taskCount) + execTime = cpuTime() - startTime + + doAssert execTime * 1000 < taskCount * sleepDuration + doAssert results == expected + + test "testFuturesWithoutValues": + let startTime = cpuTime() + testFuturesWithoutValues() + let execTime = cpuTime() - startTime + + doAssert execTime * 1000 < taskCount * sleepDuration + + test "testVarargs": + let + startTime = cpuTime() + results = testVarargs(1, 2, 3) + expected = @[1, 2, 3] + execTime = cpuTime() - startTime + + doAssert execTime * 100 < taskCount * sleepDuration + doAssert results == expected + + test "all on seq[Future]": + let + noIntFuturesFut = all(newSeq[Future[int]]()) + noVoidFuturesFut = all(newSeq[Future[void]]()) + + doAssert noIntFuturesFut.finished and not noIntFuturesFut.failed + doAssert noVoidFuturesFut.finished and not noVoidFuturesFut.failed + doAssert noIntFuturesFut.read() == @[] diff --git a/tests/async/tioselectors.nim b/tests/async/tioselectors.nim index e2b9b94d5..034c2185c 100644 --- a/tests/async/tioselectors.nim +++ b/tests/async/tioselectors.nim @@ -508,10 +508,14 @@ else: freeAddrInfo(aiList) # for some reason Windows select doesn't return both # descriptors from first call, so we need to make 2 calls - var rcm1 = selector.select(1000) - var rcm2 = selector.select(1000) - let rcm = len(rcm1) + len(rcm2) - assert(rcm >= 2 and rcm <= 4) + var n = 0 + var rcm = selector.select(1000) + while n < 10 and len(rcm) < 2: + sleep(1000) + rcm = selector.select(1000) + inc(n) + + assert(len(rcm) == 2) var sockAddress = SockAddr() var addrLen = sizeof(sockAddress).Socklen diff --git a/tests/async/tlambda.nim b/tests/async/tlambda.nim index e0ff1f483..d187c0d50 100644 --- a/tests/async/tlambda.nim +++ b/tests/async/tlambda.nim @@ -51,5 +51,8 @@ proc main() = var builder = newBuilder() + # Test {.async.} pragma with do notation: #5995 + builder.client = newClient("builder") do(client: Client, msg: JsonNode) {.async.}: + await onMessage(builder, msg) main() |