summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/async/tfuturevar.nim47
-rw-r--r--tests/async/tupcoming_async.nim13
-rw-r--r--tests/stdlib/thttpclient.nim79
3 files changed, 106 insertions, 33 deletions
diff --git a/tests/async/tfuturevar.nim b/tests/async/tfuturevar.nim
new file mode 100644
index 000000000..73c0fddf7
--- /dev/null
+++ b/tests/async/tfuturevar.nim
@@ -0,0 +1,47 @@
+import asyncdispatch
+
+proc completeOnReturn(fut: FutureVar[string], x: bool) {.async.} =
+  if x:
+    fut.mget() = ""
+    fut.mget.add("foobar")
+    return
+
+proc completeOnImplicitReturn(fut: FutureVar[string], x: bool) {.async.} =
+  if x:
+    fut.mget() = ""
+    fut.mget.add("foobar")
+
+proc failureTest(fut: FutureVar[string], x: bool) {.async.} =
+  if x:
+    raise newException(Exception, "Test")
+
+proc manualComplete(fut: FutureVar[string], x: bool) {.async.} =
+  if x:
+    fut.mget() = "Hello World"
+    fut.complete()
+    return
+
+proc main() {.async.} =
+  var fut: FutureVar[string]
+
+  fut = newFutureVar[string]()
+  await completeOnReturn(fut, true)
+  doAssert(fut.read() == "foobar")
+
+  fut = newFutureVar[string]()
+  await completeOnImplicitReturn(fut, true)
+  doAssert(fut.read() == "foobar")
+
+  fut = newFutureVar[string]()
+  let retFut = failureTest(fut, true)
+  yield retFut
+  doAssert(fut.read().isNil)
+  doAssert(fut.finished)
+
+  fut = newFutureVar[string]()
+  await manualComplete(fut, true)
+  doAssert(fut.read() == "Hello World")
+
+
+waitFor main()
+
diff --git a/tests/async/tupcoming_async.nim b/tests/async/tupcoming_async.nim
index e53482dae..137794afd 100644
--- a/tests/async/tupcoming_async.nim
+++ b/tests/async/tupcoming_async.nim
@@ -44,19 +44,8 @@ when defined(upcoming):
     ev.setEvent()
 
   proc timerTest() =
-    var timeout = 200
-    var errorRate = 40.0
-    var start = epochTime()
     waitFor(waitTimer(200))
-    var finish = epochTime()
-    var lowlimit = float(timeout) - float(timeout) * errorRate / 100.0
-    var highlimit = float(timeout) + float(timeout) * errorRate / 100.0
-    var elapsed = (finish - start) * 1_000 # convert to milliseconds
-    if elapsed >= lowlimit and elapsed < highlimit:
-      echo "OK"
-    else:
-      echo "timerTest: Timeout = " & $(elapsed) & ", but must be inside of [" &
-                                   $lowlimit & ", " & $highlimit & ")"
+    echo "OK"
 
   proc eventTest() =
     var event = newAsyncEvent()
diff --git a/tests/stdlib/thttpclient.nim b/tests/stdlib/thttpclient.nim
index 9412a5afa..dd9a6139a 100644
--- a/tests/stdlib/thttpclient.nim
+++ b/tests/stdlib/thttpclient.nim
@@ -7,6 +7,8 @@ from net import TimeoutError
 
 import httpclient, asyncdispatch
 
+const manualTests = false
+
 proc asyncTest() {.async.} =
   var client = newAsyncHttpClient()
   var resp = await client.request("http://example.com/")
@@ -20,12 +22,40 @@ proc asyncTest() {.async.} =
 
   resp = await client.request("https://google.com/")
   doAssert(resp.code.is2xx or resp.code.is3xx)
+
+  # getContent
+  try:
+    discard await client.getContent("https://google.com/404")
+    doAssert(false, "HttpRequestError should have been raised")
+  except HttpRequestError:
+    discard
+  except:
+    doAssert(false, "HttpRequestError should have been raised")
+
+
+  # Multipart test.
+  var data = newMultipartData()
+  data["output"] = "soap12"
+  data["uploaded_file"] = ("test.html", "text/html",
+    "<html><head></head><body><p>test</p></body></html>")
+  resp = await client.post("http://validator.w3.org/check", multipart=data)
+  doAssert(resp.code.is2xx)
+
+  # onProgressChanged
+  when manualTests:
+    proc onProgressChanged(total, progress, speed: BiggestInt) {.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")
+
   client.close()
 
   # Proxy test
-  #client = newAsyncHttpClient(proxy = newProxy("http://51.254.106.76:80/"))
-  #var resp = await client.request("https://github.com")
-  #echo resp
+  #when manualTests:
+  #  client = newAsyncHttpClient(proxy = newProxy("http://51.254.106.76:80/"))
+  #  var resp = await client.request("https://github.com")
+  #  echo resp
 
 proc syncTest() =
   var client = newHttpClient()
@@ -41,6 +71,31 @@ proc syncTest() =
   resp = client.request("https://google.com/")
   doAssert(resp.code.is2xx or resp.code.is3xx)
 
+  # getContent
+  try:
+    discard client.getContent("https://google.com/404")
+    doAssert(false, "HttpRequestError should have been raised")
+  except HttpRequestError:
+    discard
+  except:
+    doAssert(false, "HttpRequestError should have been raised")
+
+  # Multipart test.
+  var data = newMultipartData()
+  data["output"] = "soap12"
+  data["uploaded_file"] = ("test.html", "text/html",
+    "<html><head></head><body><p>test</p></body></html>")
+  resp = client.post("http://validator.w3.org/check", multipart=data)
+  doAssert(resp.code.is2xx)
+
+  # onProgressChanged
+  when manualTests:
+    proc onProgressChanged(total, progress, speed: BiggestInt) =
+      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.close()
 
   # Timeout test.
@@ -56,21 +111,3 @@ proc syncTest() =
 syncTest()
 
 waitFor(asyncTest())
-
-#[
-
-  else:
-    #downloadFile("http://force7.de/nim/index.html", "nimindex.html")
-    #downloadFile("http://www.httpwatch.com/", "ChunkTest.html")
-    #downloadFile("http://validator.w3.org/check?uri=http%3A%2F%2Fgoogle.com",
-    # "validator.html")
-
-    #var r = get("http://validator.w3.org/check?uri=http%3A%2F%2Fgoogle.com&
-    #  charset=%28detect+automatically%29&doctype=Inline&group=0")
-
-    var data = newMultipartData()
-    data["output"] = "soap12"
-    data["uploaded_file"] = ("test.html", "text/html",
-      "<html><head></head><body><p>test</p></body></html>")
-
-    echo postContent("http://validator.w3.org/check", multipart=data)]#