summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@gmail.com>2016-09-25 16:05:22 +0200
committerDominik Picheta <dominikpicheta@gmail.com>2016-09-25 16:05:22 +0200
commit927fce4c7fdd2e2d505de0149d2b74a2ba333da6 (patch)
tree0b406e5b08eb9843f89ec03d7a6c67ee54d7b110 /tests
parent242af696ddea7e2185ab664299f375ba19d84afc (diff)
downloadNim-927fce4c7fdd2e2d505de0149d2b74a2ba333da6.tar.gz
FutureVar[T] parameters are now completed automatically.
Diffstat (limited to 'tests')
-rw-r--r--tests/async/tfuturevar.nim47
1 files changed, 47 insertions, 0 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()
+