summary refs log tree commit diff stats
path: root/tests/async
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-05-04 16:02:50 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-05-04 16:02:50 +0200
commit764cc0217735454c166cb7dd490db58f5fa6fe26 (patch)
tree63633b39ae24815e8b677ccbc9bac2cb01c39d3b /tests/async
parentafa80092d378a6dbc116c0aa3ed3964fd8c599d6 (diff)
parentc1aa973758a60d7ef0e698c94861b74132612de5 (diff)
downloadNim-764cc0217735454c166cb7dd490db58f5fa6fe26.tar.gz
Merge branch 'devel' into araq
Diffstat (limited to 'tests/async')
-rw-r--r--tests/async/tasyncdial.nim53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/async/tasyncdial.nim b/tests/async/tasyncdial.nim
new file mode 100644
index 000000000..d70e14020
--- /dev/null
+++ b/tests/async/tasyncdial.nim
@@ -0,0 +1,53 @@
+discard """
+  file: "tasyncdial.nim"
+  output: '''
+OK AF_INET
+OK AF_INET6
+'''
+"""
+
+import
+  nativesockets, os, asyncdispatch
+
+proc setupServerSocket(hostname: string, port: Port, domain: Domain): AsyncFD =
+  ## Creates a socket, binds it to the specified address, and starts listening for connecitons.
+  ## Registers the descriptor with the dispatcher of the current thread
+  ## Raises OSError in case of an error.
+  let fd = newNativeSocket(domain)
+  setSockOptInt(fd, SOL_SOCKET, SO_REUSEADDR, 1)
+  var aiList = getAddrInfo(hostname, port, domain)
+  if bindAddr(fd, aiList.ai_addr, aiList.ai_addrlen.Socklen) < 0'i32:
+    freeAddrInfo(aiList)
+    raiseOSError(osLastError())
+  freeAddrInfo(aiList)
+  if listen(fd) != 0:
+    raiseOSError(osLastError())
+  setBlocking(fd, false)
+  result = fd.AsyncFD
+  register(result)
+
+proc doTest(domain: static[Domain]) {.async.} =
+  const
+    testHost = when domain == Domain.AF_INET6: "::1" else: "127.0.0.1"
+    testPort = Port(17384)
+  let serverFd = setupServerSocket(testHost, testPort, domain)
+  let acceptFut = serverFd.accept()
+  let clientFdFut = dial(testHost, testPort)
+
+  let serverClientFd = await acceptFut
+  serverFd.closeSocket()
+
+  let clientFd = await clientFdFut
+
+  let recvFut = serverClientFd.recv(2)
+  await clientFd.send("Hi")
+  let msg = await recvFut
+
+  serverClientFd.closeSocket()
+  clientFd.closeSocket()
+
+  if msg == "Hi":
+    echo "OK ", domain
+
+waitFor(doTest(Domain.AF_INET))
+waitFor(doTest(Domain.AF_INET6))