summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKay Zheng <l04m33@gmail.com>2015-04-17 22:24:06 +0800
committerKay Zheng <l04m33@gmail.com>2015-04-17 22:24:06 +0800
commit371d2739249ad33267e0b330e582a04d7d28fd98 (patch)
treef6221eb36f32388ccc45a2a738deccd115d4e494
parenta7df3ffbe0ed6d10cc952e47f4e92a69403363dc (diff)
downloadNim-371d2739249ad33267e0b330e582a04d7d28fd98.tar.gz
Add a test case for the EvError handling issue
-rw-r--r--tests/async/tasynceverror.nim62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/async/tasynceverror.nim b/tests/async/tasynceverror.nim
new file mode 100644
index 000000000..564df569e
--- /dev/null
+++ b/tests/async/tasynceverror.nim
@@ -0,0 +1,62 @@
+discard """
+  file: "tasynceverror.nim"
+  exitcode: 1
+  outputsub: "Error: unhandled exception: Connection reset by peer [Exception]"
+"""
+
+import
+    asyncdispatch,
+    asyncnet,
+    rawsockets,
+    os
+
+
+const
+    testHost = "127.0.0.1"
+    testPort = Port(17357)
+
+
+proc createListenSocket(host: string, port: Port): TAsyncFD =
+    result = newAsyncRawSocket()
+
+    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())