diff options
Diffstat (limited to 'tests/async')
-rw-r--r-- | tests/async/tasyncawait.nim | 8 | ||||
-rw-r--r-- | tests/async/tasyncconnect.nim | 34 | ||||
-rw-r--r-- | tests/async/tasynceverror.nim | 66 | ||||
-rw-r--r-- | tests/async/tasyncexceptions.nim | 2 | ||||
-rw-r--r-- | tests/async/tasyncfile.nim | 6 | ||||
-rw-r--r-- | tests/async/tasynciossl.nim | 4 | ||||
-rw-r--r-- | tests/async/tasyncudp.nim | 10 |
7 files changed, 115 insertions, 15 deletions
diff --git a/tests/async/tasyncawait.nim b/tests/async/tasyncawait.nim index 13d531387..443f769cd 100644 --- a/tests/async/tasyncawait.nim +++ b/tests/async/tasyncawait.nim @@ -2,7 +2,7 @@ discard """ file: "tasyncawait.nim" output: "5000" """ -import asyncdispatch, rawsockets, net, strutils, os +import asyncdispatch, nativesockets, net, strutils, os var msgCount = 0 @@ -18,7 +18,7 @@ proc sendMessages(client: TAsyncFD) {.async.} = proc launchSwarm(port: TPort) {.async.} = for i in 0 .. <swarmSize: - var sock = newAsyncRawSocket() + var sock = newAsyncNativeSocket() await connect(sock, "localhost", port) await sendMessages(sock) @@ -38,7 +38,7 @@ proc readMessages(client: TAsyncFD) {.async.} = doAssert false proc createServer(port: TPort) {.async.} = - var server = newAsyncRawSocket() + var server = newAsyncNativeSocket() block: var name: Sockaddr_in when defined(windows): @@ -50,7 +50,7 @@ proc createServer(port: TPort) {.async.} = if bindAddr(server.SocketHandle, cast[ptr SockAddr](addr(name)), sizeof(name).Socklen) < 0'i32: raiseOSError(osLastError()) - + discard server.SocketHandle.listen() while true: asyncCheck readMessages(await accept(server)) diff --git a/tests/async/tasyncconnect.nim b/tests/async/tasyncconnect.nim new file mode 100644 index 000000000..3dac379b2 --- /dev/null +++ b/tests/async/tasyncconnect.nim @@ -0,0 +1,34 @@ +discard """ + file: "tasyncconnect.nim" + exitcode: 1 + outputsub: "Error: unhandled exception: Connection refused" +""" + +import + asyncdispatch, + posix + + +const + testHost = "127.0.0.1" + testPort = Port(17357) + + +when defined(windows) or defined(nimdoc): + # TODO: just make it work on Windows for now. + quit("Error: unhandled exception: Connection refused") +else: + proc testAsyncConnect() {.async.} = + var s = newAsyncNativeSocket() + + await s.connect(testHost, testPort) + + var peerAddr: SockAddr + var addrSize = Socklen(sizeof(peerAddr)) + var ret = SocketHandle(s).getpeername(addr(peerAddr), addr(addrSize)) + + if ret < 0: + echo("`connect(...)` failed but no exception was raised.") + quit(2) + + waitFor(testAsyncConnect()) diff --git a/tests/async/tasynceverror.nim b/tests/async/tasynceverror.nim new file mode 100644 index 000000000..22b4fe9a7 --- /dev/null +++ b/tests/async/tasynceverror.nim @@ -0,0 +1,66 @@ +discard """ + file: "tasynceverror.nim" + exitcode: 1 + outputsub: "Error: unhandled exception: Connection reset by peer" +""" + +import + asyncdispatch, + asyncnet, + nativesockets, + os + + +const + testHost = "127.0.0.1" + testPort = Port(17357) + + +when defined(windows) or defined(nimdoc): + # TODO: just make it work on Windows for now. + quit("Error: unhandled exception: Connection reset by peer") +else: + proc createListenSocket(host: string, port: Port): TAsyncFD = + result = newAsyncNativeSocket() + + SocketHandle(result).setSockOptInt(SOL_SOCKET, SO_REUSEADDR, 1) + + var aiList = getAddrInfo(host, port, AF_INET) + if SocketHandle(result).bindAddr(aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32: + dealloc(aiList) + raiseOSError(osLastError()) + dealloc(aiList) + + if SocketHandle(result).listen(1) < 0'i32: + raiseOSError(osLastError()) + + + proc testAsyncSend() {.async.} = + var + ls = createListenSocket(testHost, testPort) + s = newAsyncSocket() + + await s.connect(testHost, testPort) + + var ps = await ls.accept() + SocketHandle(ls).close() + + await ps.send("test 1", flags={}) + s.close() + # This send should raise EPIPE + await ps.send("test 2", flags={}) + SocketHandle(ps).close() + + + # The bug was, when the poll function handled EvError for us, + # our callbacks may never get executed, thus making the event + # loop block indefinitely. This is a timer to keep everything + # rolling. 400 ms is an arbitrary value, should be enough though. + proc timer() {.async.} = + await sleepAsync(400) + echo("Timer expired.") + quit(2) + + + asyncCheck(testAsyncSend()) + waitFor(timer()) diff --git a/tests/async/tasyncexceptions.nim b/tests/async/tasyncexceptions.nim index c4379f7d8..aab08e30f 100644 --- a/tests/async/tasyncexceptions.nim +++ b/tests/async/tasyncexceptions.nim @@ -1,7 +1,7 @@ discard """ file: "tasyncexceptions.nim" exitcode: 1 - outputsub: "Error: unhandled exception: foobar [Exception]" + outputsub: "Error: unhandled exception: foobar" """ import asyncdispatch diff --git a/tests/async/tasyncfile.nim b/tests/async/tasyncfile.nim index c3cf33512..05cda5e5f 100644 --- a/tests/async/tasyncfile.nim +++ b/tests/async/tasyncfile.nim @@ -18,7 +18,7 @@ proc main() {.async.} = let data = await file.readAll() doAssert data == "foot" file.close() - + # Append test block: var file = openAsync(fn, fmAppend) @@ -29,8 +29,8 @@ proc main() {.async.} = file.close() file = openAsync(fn, fmRead) let data = await file.readAll() - + doAssert data == "foot\ntest2" file.close() - + waitFor main() diff --git a/tests/async/tasynciossl.nim b/tests/async/tasynciossl.nim index 118b9e74d..ba856760e 100644 --- a/tests/async/tasynciossl.nim +++ b/tests/async/tasynciossl.nim @@ -10,12 +10,12 @@ disp = newDispatcher() var msgCount = 0 when defined(ssl): - var ctx = newContext(verifyMode = CVerifyNone, + var ctx = newContext(verifyMode = CVerifyNone, certFile = "tests/testdata/mycert.pem", keyFile = "tests/testdata/mycert.pem") var ctx1 = newContext(verifyMode = CVerifyNone) -const +const swarmSize = 50 messagesToSend = 100 diff --git a/tests/async/tasyncudp.nim b/tests/async/tasyncudp.nim index 2a7ed40bf..57e2be85d 100644 --- a/tests/async/tasyncudp.nim +++ b/tests/async/tasyncudp.nim @@ -20,12 +20,12 @@ proc serverRead(s: PAsyncSocket) = if s.recvFromAsync(data, 9, address, port): assert address == "127.0.0.1" msgCount.inc() - + discard """ - + var line = "" assert s.recvLine(line) - + if line == "": assert(false) else: @@ -66,11 +66,11 @@ while true: if not disp.poll(): break - + if (msgCount div messagesToSend) * serverCount == currentClient: createClient(disp, TPort(10335), false) createClient(disp, TPort(10336), true) - + if msgCount == messagesToSend * serverCount * swarmSize: break |