summary refs log tree commit diff stats
path: root/tests/async/tawaitsemantics.nim
blob: 3e0c3903eac4e9fe119431ed0e2d9292a9565cc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
discard """
  file: "tawaitsemantics.nim"
  exitcode: 0
  output: '''
Error caught
Test infix
Test call
'''
"""

import asyncdispatch

# This tests the behaviour of 'await' under different circumstances.
# For example, when awaiting Future variable and this future has failed the
# exception shouldn't be raised as described here
# https://github.com/nim-lang/Nim/issues/4170

proc thrower(): Future[void] =
  result = newFuture[void]()
  result.fail(newException(Exception, "Test"))

proc dummy: Future[void] =
  result = newFuture[void]()
  result.complete()

proc testInfix() {.async.} =
  # Test the infix operator semantics.
  var fut = thrower()
  var fut2 = dummy()
  await fut or fut2 # Shouldn't raise.
  # TODO: what about: await thrower() or fut2?

proc testCall() {.async.} =
  await thrower()

proc tester() {.async.} =
  # Test that we can handle exceptions without 'try'
  var fut = thrower()
  doAssert fut.finished
  doAssert fut.failed
  doAssert fut.error.msg == "Test"
  await fut # We are awaiting a 'Future', so no `read` occurs.
  doAssert fut.finished
  doAssert fut.failed
  doAssert fut.error.msg == "Test"
  echo("Error caught")

  fut = testInfix()
  await fut
  doAssert fut.finished
  doAssert(not fut.failed)
  echo("Test infix")

  fut = testCall()
  await fut
  doAssert fut.failed
  echo("Test call")

waitFor(tester())