summary refs log tree commit diff stats
path: root/tests/async
diff options
context:
space:
mode:
Diffstat (limited to 'tests/async')
-rw-r--r--tests/async/config.nims2
-rw-r--r--tests/async/hello.txt1
-rw-r--r--tests/async/t6100.nim15
-rw-r--r--tests/async/tasync_in_seq_constr.nim21
-rw-r--r--tests/async/tasync_traceback.nim130
-rw-r--r--tests/async/tasyncawait.nim12
-rw-r--r--tests/async/tasyncfile.nim15
-rw-r--r--tests/async/tasynctry2.nim4
-rw-r--r--tests/async/tfuturestream.nim2
-rw-r--r--tests/async/tgeneric_async.nim43
-rw-r--r--tests/async/tjsandnativeasync.nim30
-rw-r--r--tests/async/tlambda.nim4
12 files changed, 192 insertions, 87 deletions
diff --git a/tests/async/config.nims b/tests/async/config.nims
deleted file mode 100644
index 97c2e0aa4..000000000
--- a/tests/async/config.nims
+++ /dev/null
@@ -1,2 +0,0 @@
-when defined(upcoming):
-  patchFile("stdlib", "asyncdispatch", "$lib/upcoming/asyncdispatch")
diff --git a/tests/async/hello.txt b/tests/async/hello.txt
new file mode 100644
index 000000000..854d6c20a
--- /dev/null
+++ b/tests/async/hello.txt
@@ -0,0 +1 @@
+hello humans!
\ No newline at end of file
diff --git a/tests/async/t6100.nim b/tests/async/t6100.nim
new file mode 100644
index 000000000..b4dc0f146
--- /dev/null
+++ b/tests/async/t6100.nim
@@ -0,0 +1,15 @@
+discard """
+  file: "t6100.nim"
+  exitcode: 0
+  output: "10000000"
+"""
+import asyncdispatch
+
+let done = newFuture[int]()
+done.complete(1)
+
+proc asyncSum: Future[int] {.async.} =
+  for _ in 1..10_000_000:
+    result += await done
+
+echo waitFor asyncSum()
\ No newline at end of file
diff --git a/tests/async/tasync_in_seq_constr.nim b/tests/async/tasync_in_seq_constr.nim
index 46ad74451..3d6dae245 100644
--- a/tests/async/tasync_in_seq_constr.nim
+++ b/tests/async/tasync_in_seq_constr.nim
@@ -1,18 +1,25 @@
 discard """
-  errormsg: "invalid control flow: 'yield' within a constructor"
-  line: 16
+  output: '''
+@[1, 2, 3, 4]
+123
+'''
 """
 
 # bug #5314, bug #6626
 
 import asyncdispatch
 
-proc bar(): Future[int] {.async.} =
-    await sleepAsync(500)
-    result = 3
+proc bar(i: int): Future[int] {.async.} =
+    await sleepAsync(2)
+    result = i
 
 proc foo(): Future[seq[int]] {.async.} =
-    await sleepAsync(500)
-    result = @[1, 2, await bar(), 4] # <--- The bug is here
+    await sleepAsync(2)
+    result = @[1, 2, await bar(3), 4] # <--- The bug is here
+
+proc foo2() {.async.} =
+    await sleepAsync(2)
+    echo(await bar(1), await bar(2), await bar(3))
 
 echo waitFor foo()
+waitFor foo2()
diff --git a/tests/async/tasync_traceback.nim b/tests/async/tasync_traceback.nim
index 08f7e7317..618a1dc76 100644
--- a/tests/async/tasync_traceback.nim
+++ b/tests/async/tasync_traceback.nim
@@ -1,61 +1,7 @@
 discard """
   exitcode: 0
   disabled: "windows"
-  output: '''
-b failure
-Async traceback:
-  tasync_traceback.nim(97) tasync_traceback
-  asyncmacro.nim(395)      a
-  asyncmacro.nim(34)       a_continue
-    ## Resumes an async procedure
-  tasync_traceback.nim(95) aIter
-  asyncmacro.nim(395)      b
-  asyncmacro.nim(34)       b_continue
-    ## Resumes an async procedure
-  tasync_traceback.nim(92) bIter
-  #[
-    tasync_traceback.nim(97) tasync_traceback
-    asyncmacro.nim(395)      a
-    asyncmacro.nim(43)       a_continue
-      ## Resumes an async procedure
-    asyncfutures.nim(211)    callback=
-    asyncfutures.nim(190)    addCallback
-    asyncfutures.nim(53)     callSoon
-    asyncmacro.nim(34)       a_continue
-      ## Resumes an async procedure
-    asyncmacro.nim(0)        aIter
-    asyncfutures.nim(304)    read
-  ]#
-Exception message: b failure
-Exception type:
-
-bar failure
-Async traceback:
-  tasync_traceback.nim(113) tasync_traceback
-  asyncdispatch.nim(1492)   waitFor
-  asyncdispatch.nim(1496)   poll
-    ## Processes asynchronous completion events
-  asyncdispatch.nim(1262)   runOnce
-  asyncdispatch.nim(183)    processPendingCallbacks
-    ## Executes pending callbacks
-  asyncmacro.nim(34)        bar_continue
-    ## Resumes an async procedure
-  tasync_traceback.nim(108) barIter
-  #[
-    tasync_traceback.nim(113) tasync_traceback
-    asyncdispatch.nim(1492)   waitFor
-    asyncdispatch.nim(1496)   poll
-      ## Processes asynchronous completion events
-    asyncdispatch.nim(1262)   runOnce
-    asyncdispatch.nim(183)    processPendingCallbacks
-      ## Executes pending callbacks
-    asyncmacro.nim(34)        foo_continue
-      ## Resumes an async procedure
-    asyncmacro.nim(0)         fooIter
-    asyncfutures.nim(304)     read
-  ]#
-Exception message: bar failure
-Exception type:'''
+  output: "Matched"
 """
 import asyncdispatch
 
@@ -87,6 +33,8 @@ import asyncdispatch
 # tasync_traceback.nim(21) a
 # tasync_traceback.nim(18) b
 
+var result = ""
+
 proc b(): Future[int] {.async.} =
   if true:
     raise newException(OSError, "b failure")
@@ -98,8 +46,8 @@ let aFut = a()
 try:
   discard waitFor aFut
 except Exception as exc:
-  echo exc.msg
-echo()
+  result.add(exc.msg & "\n")
+result.add("\n")
 
 # From #6803
 proc bar(): Future[string] {.async.} =
@@ -110,7 +58,69 @@ proc bar(): Future[string] {.async.} =
 proc foo(): Future[string] {.async.} = return await bar()
 
 try:
-  echo waitFor(foo())
+  result.add(waitFor(foo()) & "\n")
 except Exception as exc:
-  echo exc.msg
-echo()
\ No newline at end of file
+  result.add(exc.msg & "\n")
+result.add("\n")
+
+# Use re to parse the result
+import re
+const expected = """
+b failure
+Async traceback:
+  tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
+  asyncmacro\.nim\(\d+?\)\s+?a
+  asyncmacro\.nim\(\d+?\)\s+?a_continue
+    ## Resumes an async procedure
+  tasync_traceback\.nim\(\d+?\)\s+?aIter
+  asyncmacro\.nim\(\d+?\)\s+?b
+  asyncmacro\.nim\(\d+?\)\s+?b_continue
+    ## Resumes an async procedure
+  tasync_traceback\.nim\(\d+?\)\s+?bIter
+  #\[
+    tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
+    asyncmacro\.nim\(\d+?\)\s+?a
+    asyncmacro\.nim\(\d+?\)\s+?a_continue
+      ## Resumes an async procedure
+    tasync_traceback\.nim\(\d+?\)\s+?aIter
+    asyncfutures\.nim\(\d+?\)\s+?read
+  \]#
+Exception message: b failure
+Exception type:
+
+bar failure
+Async traceback:
+  tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
+  asyncdispatch\.nim\(\d+?\)\s+?waitFor
+  asyncdispatch\.nim\(\d+?\)\s+?poll
+    ## Processes asynchronous completion events
+  asyncdispatch\.nim\(\d+?\)\s+?runOnce
+  asyncdispatch\.nim\(\d+?\)\s+?processPendingCallbacks
+    ## Executes pending callbacks
+  asyncmacro\.nim\(\d+?\)\s+?bar_continue
+    ## Resumes an async procedure
+  tasync_traceback\.nim\(\d+?\)\s+?barIter
+  #\[
+    tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
+    asyncdispatch\.nim\(\d+?\)\s+?waitFor
+    asyncdispatch\.nim\(\d+?\)\s+?poll
+      ## Processes asynchronous completion events
+    asyncdispatch\.nim\(\d+?\)\s+?runOnce
+    asyncdispatch\.nim\(\d+?\)\s+?processPendingCallbacks
+      ## Executes pending callbacks
+    asyncmacro\.nim\(\d+?\)\s+?foo_continue
+      ## Resumes an async procedure
+    tasync_traceback\.nim\(\d+?\)\s+?fooIter
+    asyncfutures\.nim\(\d+?\)\s+?read
+  \]#
+Exception message: bar failure
+Exception type:
+"""
+
+if result.match(re(expected)):
+  echo("Matched")
+else:
+  echo("Not matched!")
+  echo()
+  echo(result)
+  quit(QuitFailure)
diff --git a/tests/async/tasyncawait.nim b/tests/async/tasyncawait.nim
index 9fe9507ad..74933f063 100644
--- a/tests/async/tasyncawait.nim
+++ b/tests/async/tasyncawait.nim
@@ -12,11 +12,11 @@ const
 
 var clientCount = 0
 
-proc sendMessages(client: TAsyncFD) {.async.} =
+proc sendMessages(client: AsyncFD) {.async.} =
   for i in 0 .. <messagesToSend:
     await send(client, "Message " & $i & "\c\L")
 
-proc launchSwarm(port: TPort) {.async.} =
+proc launchSwarm(port: Port) {.async.} =
   for i in 0 .. <swarmSize:
     var sock = newAsyncNativeSocket()
 
@@ -24,7 +24,7 @@ proc launchSwarm(port: TPort) {.async.} =
     await sendMessages(sock)
     closeSocket(sock)
 
-proc readMessages(client: TAsyncFD) {.async.} =
+proc readMessages(client: AsyncFD) {.async.} =
   while true:
     var line = await recvLine(client)
     if line == "":
@@ -37,7 +37,7 @@ proc readMessages(client: TAsyncFD) {.async.} =
       else:
         doAssert false
 
-proc createServer(port: TPort) {.async.} =
+proc createServer(port: Port) {.async.} =
   var server = newAsyncNativeSocket()
   block:
     var name: Sockaddr_in
@@ -55,8 +55,8 @@ proc createServer(port: TPort) {.async.} =
   while true:
     asyncCheck readMessages(await accept(server))
 
-asyncCheck createServer(TPort(10335))
-asyncCheck launchSwarm(TPort(10335))
+asyncCheck createServer(Port(10335))
+asyncCheck launchSwarm(Port(10335))
 while true:
   poll()
   if clientCount == swarmSize: break
diff --git a/tests/async/tasyncfile.nim b/tests/async/tasyncfile.nim
index 6c0725c88..c7b71a2f7 100644
--- a/tests/async/tasyncfile.nim
+++ b/tests/async/tasyncfile.nim
@@ -1,4 +1,8 @@
 discard """
+  output: '''13
+hello humans!
+13
+'''
   file: "tasyncfile.nim"
   exitcode: 0
 """
@@ -41,12 +45,19 @@ proc main() {.async.} =
     await file.write("test2")
     file.close()
     file = openAsync(fn, fmWrite)
-    await file.write("test3")
+    await file.write("t3")
     file.close()
     file = openAsync(fn, fmRead)
     let data = await file.readAll()
-    doAssert data == "test3"
+    doAssert data == "t3"
     file.close()
 
+  # Issue #7347
+  block:
+    let appDir = getAppDir()
+    var file = openAsync(appDir & DirSep & "hello.txt")
+    echo file.getFileSize()
+    echo await file.readAll()
+    echo file.getFilePos()
 
 waitFor main()
diff --git a/tests/async/tasynctry2.nim b/tests/async/tasynctry2.nim
index 444a058be..f82b6cfe0 100644
--- a/tests/async/tasynctry2.nim
+++ b/tests/async/tasynctry2.nim
@@ -1,10 +1,12 @@
 discard """
   file: "tasynctry2.nim"
   errormsg: "\'yield\' cannot be used within \'try\' in a non-inlined iterator"
-  line: 15
+  line: 17
 """
 import asyncdispatch
 
+{.experimental: "oldIterTransf".}
+
 proc foo(): Future[bool] {.async.} = discard
 
 proc test5(): Future[int] {.async.} =
diff --git a/tests/async/tfuturestream.nim b/tests/async/tfuturestream.nim
index 9a8e986a0..d76752b7e 100644
--- a/tests/async/tfuturestream.nim
+++ b/tests/async/tfuturestream.nim
@@ -18,8 +18,8 @@ var fs = newFutureStream[int]()
 
 proc alpha() {.async.} =
   for i in 0 .. 5:
-    await sleepAsync(1000)
     await fs.write(i)
+    await sleepAsync(1000)
 
   echo("Done")
   fs.complete()
diff --git a/tests/async/tgeneric_async.nim b/tests/async/tgeneric_async.nim
index af6370181..bab2d1a31 100644
--- a/tests/async/tgeneric_async.nim
+++ b/tests/async/tgeneric_async.nim
@@ -1,9 +1,40 @@
+discard """
+output: "1\nmessa"
+"""
 
-import asyncdispatch
+import async
 
-when true:
-  # bug #2377
-  proc test[T](v: T) {.async.} =
-    echo $v
+# bug #2377
+proc test[T](v: T) {.async.} =
+  echo $v
+
+asyncCheck test[int](1)
+
+# More complex case involving typedesc and static params
+type
+  SomeMsg = object
+    data: string
+
+template msgId(M: type SomeMsg): int = 1
+
+proc recvMsg(): Future[tuple[msgId: int, msgData: string]] {.async.} =
+  return (1, "message")
+
+proc read(data: string, T: type SomeMsg, maxBytes: int): T =
+  result.data = data[0 ..< min(data.len, maxBytes)]
+
+proc nextMsg*(MsgType: typedesc,
+              maxBytes: static[int]): Future[MsgType] {.async.} =
+  const wantedId = MsgType.msgId
+
+  while true:
+    var (nextMsgId, nextMsgData) = await recvMsg()
+    if nextMsgId == wantedId:
+      return nextMsgData.read(MsgType, maxBytes)
+
+proc main {.async.} =
+  let msg = await nextMsg(SomeMsg, 5)
+  echo msg.data
+
+asyncCheck main()
 
-  asyncCheck test[int](1)
diff --git a/tests/async/tjsandnativeasync.nim b/tests/async/tjsandnativeasync.nim
new file mode 100644
index 000000000..45839899f
--- /dev/null
+++ b/tests/async/tjsandnativeasync.nim
@@ -0,0 +1,30 @@
+discard """
+  output: '''hi
+bye'''
+"""
+
+import async, times
+when defined(js):
+    proc sleepAsync(t: int): Future[void] =
+        var promise = newPromise() do(resolve: proc()):
+            {.emit: """
+            setTimeout(function(){
+                `resolve`();
+            }, `t`);
+            """.}
+        result = promise
+else:
+    from asyncdispatch import sleepAsync, waitFor
+
+proc foo() {.async.} =
+    echo "hi"
+    var s = epochTime()
+    await sleepAsync(500)
+    var e = epochTime()
+    doAssert(e - s > 0.1)
+    echo "bye"
+
+when defined(js):
+    discard foo()
+else:
+    waitFor foo()
diff --git a/tests/async/tlambda.nim b/tests/async/tlambda.nim
index d187c0d50..8f570689b 100644
--- a/tests/async/tlambda.nim
+++ b/tests/async/tlambda.nim
@@ -1,7 +1,7 @@
 
 # bug 2007
 
-import asyncdispatch, asyncnet, logging, json, uri, strutils, future
+import asyncdispatch, asyncnet, logging, json, uri, strutils, sugar
 
 type
   Builder = ref object
@@ -27,7 +27,7 @@ proc newBuild*(onProgress: ProgressCB): Build =
   result.onProgress = onProgress
 
 proc start(build: Build, repo, hash: string) {.async.} =
-  let path = repo.parseUri().path.toLower()
+  let path = repo.parseUri().path.toLowerAscii()
 
 proc onProgress(builder: Builder, message: string) {.async.} =
   debug($message)