tasynctry.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. discard """
  2. output: '''
  3. Generic except: Test
  4. Specific except
  5. Multiple idents in except
  6. Multiple except branches
  7. Multiple except branches 2
  8. success
  9. '''
  10. targets: "c"
  11. """
  12. import asyncdispatch, strutils
  13. # Here we are testing the ability to catch exceptions.
  14. proc foobar() {.async.} =
  15. if 5 == 5:
  16. raise newException(IndexDefect, "Test")
  17. proc catch() {.async.} =
  18. # TODO: Create a test for when exceptions are not caught.
  19. try:
  20. await foobar()
  21. except:
  22. echo("Generic except: ", getCurrentExceptionMsg().splitLines[0])
  23. try:
  24. await foobar()
  25. except IndexDefect:
  26. echo("Specific except")
  27. try:
  28. await foobar()
  29. except OSError, FieldDefect, IndexDefect:
  30. echo("Multiple idents in except")
  31. try:
  32. await foobar()
  33. except OSError, FieldDefect:
  34. assert false
  35. except IndexDefect:
  36. echo("Multiple except branches")
  37. try:
  38. await foobar()
  39. except IndexDefect:
  40. echo("Multiple except branches 2")
  41. except OSError, FieldDefect:
  42. assert false
  43. waitFor catch()
  44. proc test(): Future[bool] {.async.} =
  45. result = false
  46. try:
  47. raise newException(OSError, "Foobar")
  48. except:
  49. result = true
  50. return
  51. proc foo(): Future[bool] {.async.} = discard
  52. proc test2(): Future[bool] {.async.} =
  53. result = false
  54. try:
  55. discard await foo()
  56. raise newException(OSError, "Foobar")
  57. except:
  58. result = true
  59. return
  60. proc test3(): Future[int] {.async.} =
  61. result = 0
  62. try:
  63. try:
  64. discard await foo()
  65. raise newException(OSError, "Hello")
  66. except:
  67. result = 1
  68. raise
  69. except:
  70. result = 2
  71. return
  72. proc test4(): Future[int] {.async.} =
  73. try:
  74. discard await foo()
  75. raise newException(ValueError, "Test4")
  76. except OSError:
  77. result = 1
  78. except:
  79. result = 2
  80. var x = test()
  81. assert x.waitFor()
  82. x = test2()
  83. assert x.waitFor()
  84. var y = test3()
  85. assert y.waitFor() == 2
  86. y = test4()
  87. assert y.waitFor() == 2
  88. # bug #14279
  89. proc expandValue: Future[int] {.async.} =
  90. return 0
  91. proc a(b: int): Future[void] {.async.} =
  92. return
  93. proc b: Future[void] {.async.} =
  94. await a(await expandValue())
  95. echo "success"
  96. waitFor(b())