summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-02-26 17:35:08 +0100
committerGitHub <noreply@github.com>2017-02-26 17:35:08 +0100
commite5341a391d028c24800082ac6895688669852bcd (patch)
treefa725d0510990c8171774888eeba1c2ad2e0a17e /tests
parent5ce8e0b5cd2d9f0aa181dd03334bb99e398af089 (diff)
parent843099d8aea0b39fa4c92c5f8d2725e230c03efb (diff)
downloadNim-e5341a391d028c24800082ac6895688669852bcd.tar.gz
Merge pull request #5373 from nim-lang/feature/async-streams
Async streams and HTTP client streaming downloads support
Diffstat (limited to 'tests')
-rw-r--r--tests/async/tfuturestream.nim53
-rw-r--r--tests/stdlib/thttpclient.nim10
2 files changed, 60 insertions, 3 deletions
diff --git a/tests/async/tfuturestream.nim b/tests/async/tfuturestream.nim
new file mode 100644
index 000000000..9a8e986a0
--- /dev/null
+++ b/tests/async/tfuturestream.nim
@@ -0,0 +1,53 @@
+discard """
+  file: "tfuturestream.nim"
+  exitcode: 0
+  output: '''
+0
+1
+2
+3
+4
+5
+Done
+Finished
+'''
+"""
+import asyncdispatch
+
+var fs = newFutureStream[int]()
+
+proc alpha() {.async.} =
+  for i in 0 .. 5:
+    await sleepAsync(1000)
+    await fs.write(i)
+
+  echo("Done")
+  fs.complete()
+
+proc beta() {.async.} =
+  while not fs.finished:
+    let (hasValue, value) = await fs.read()
+    if hasValue:
+      echo(value)
+
+  echo("Finished")
+
+asyncCheck alpha()
+waitFor beta()
+
+# TODO: Something like this should work eventually.
+# proc delta(): FutureStream[string] {.async.} =
+#   for i in 0 .. 5:
+#     await sleepAsync(1000)
+#     result.put($i)
+
+#   return ""
+
+# proc omega() {.async.} =
+#   let fut = delta()
+#   while not fut.finished():
+#     echo(await fs.takeAsync())
+
+#   echo("Finished")
+
+# waitFor omega()
\ No newline at end of file
diff --git a/tests/stdlib/thttpclient.nim b/tests/stdlib/thttpclient.nim
index 7b1111f9b..62c1ebee7 100644
--- a/tests/stdlib/thttpclient.nim
+++ b/tests/stdlib/thttpclient.nim
@@ -13,7 +13,9 @@ proc asyncTest() {.async.} =
   var client = newAsyncHttpClient()
   var resp = await client.request("http://example.com/")
   doAssert(resp.code.is2xx)
-  doAssert("<title>Example Domain</title>" in resp.body)
+  var body = await resp.body
+  body = await resp.body # Test caching
+  doAssert("<title>Example Domain</title>" in body)
 
   resp = await client.request("http://example.com/404")
   doAssert(resp.code.is4xx)
@@ -47,7 +49,8 @@ proc asyncTest() {.async.} =
       echo("Downloaded ", progress, " of ", total)
       echo("Current rate: ", speed div 1000, "kb/s")
     client.onProgressChanged = onProgressChanged
-    discard await client.getContent("http://speedtest-ams2.digitalocean.com/100mb.test")
+    await client.downloadFile("http://speedtest-ams2.digitalocean.com/100mb.test",
+                              "100mb.test")
 
   client.close()
 
@@ -94,7 +97,8 @@ proc syncTest() =
       echo("Downloaded ", progress, " of ", total)
       echo("Current rate: ", speed div 1000, "kb/s")
     client.onProgressChanged = onProgressChanged
-    discard client.getContent("http://speedtest-ams2.digitalocean.com/100mb.test")
+    client.downloadFile("http://speedtest-ams2.digitalocean.com/100mb.test",
+                        "100mb.test")
 
   client.close()