tasync_traceback2.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import std/asyncdispatch
  2. import std/re
  3. from std/strutils import dedent
  4. proc err() =
  5. raise newException(ValueError, "the_error_msg")
  6. block:
  7. proc recursion(i: int) {.async.} =
  8. if i == 5:
  9. err()
  10. await sleepAsync(1)
  11. await recursion(i+1)
  12. proc main {.async.} =
  13. await recursion(0)
  14. try:
  15. waitFor main()
  16. doAssert false
  17. except ValueError as err:
  18. let expected = """
  19. the_error_msg
  20. Async traceback:
  21. tasync_traceback2\.nim\(\d+\) tasync_traceback2
  22. tasync_traceback2\.nim\(\d+\) main \(Async\)
  23. tasync_traceback2\.nim\(\d+\) recursion \(Async\)
  24. tasync_traceback2\.nim\(\d+\) recursion \(Async\)
  25. tasync_traceback2\.nim\(\d+\) err
  26. Exception message: the_error_msg
  27. """.dedent
  28. doAssert match(err.msg, re(expected)), err.getStackTrace & err.msg
  29. block:
  30. proc baz() =
  31. err()
  32. proc bar() =
  33. baz()
  34. proc foo() {.async.} =
  35. await sleepAsync(1)
  36. bar()
  37. proc main {.async.} =
  38. await foo()
  39. try:
  40. waitFor main()
  41. doAssert false
  42. except ValueError as err:
  43. let expected = """
  44. the_error_msg
  45. Async traceback:
  46. tasync_traceback2\.nim\(\d+\) tasync_traceback2
  47. tasync_traceback2\.nim\(\d+\) main \(Async\)
  48. tasync_traceback2\.nim\(\d+\) foo \(Async\)
  49. tasync_traceback2\.nim\(\d+\) bar
  50. tasync_traceback2\.nim\(\d+\) baz
  51. tasync_traceback2\.nim\(\d+\) err
  52. Exception message: the_error_msg
  53. """.dedent
  54. doAssert match(err.msg, re(expected)), err.getStackTrace & err.msg
  55. block: # async work
  56. proc baz() {.async.} =
  57. await sleepAsync(1)
  58. raise newException(ValueError, "the_error_msg")
  59. proc bar() {.async.} =
  60. await sleepAsync(1)
  61. await baz()
  62. proc foo() {.async.} =
  63. await sleepAsync(1)
  64. await bar()
  65. proc main {.async.} =
  66. await foo()
  67. try:
  68. waitFor main()
  69. doAssert false
  70. except ValueError as err:
  71. let expected = """
  72. the_error_msg
  73. Async traceback:
  74. tasync_traceback2\.nim\(\d+\) tasync_traceback2
  75. tasync_traceback2\.nim\(\d+\) main \(Async\)
  76. tasync_traceback2\.nim\(\d+\) foo \(Async\)
  77. tasync_traceback2\.nim\(\d+\) bar \(Async\)
  78. tasync_traceback2\.nim\(\d+\) baz \(Async\)
  79. Exception message: the_error_msg
  80. """.dedent
  81. doAssert match(err.msg, re(expected)), err.getStackTrace & err.msg
  82. block: # interleaved async work
  83. proc baz() {.async.} =
  84. await sleepAsync(1)
  85. raise newException(ValueError, "the_error_msg")
  86. proc bar() {.async.} =
  87. #await sleepAsync(1)
  88. await baz()
  89. proc foo() {.async.} =
  90. await sleepAsync(1)
  91. await bar()
  92. proc main {.async.} =
  93. await foo()
  94. try:
  95. waitFor main()
  96. doAssert false
  97. except ValueError as err:
  98. let expected = """
  99. the_error_msg
  100. Async traceback:
  101. tasync_traceback2\.nim\(\d+\) tasync_traceback2
  102. tasync_traceback2\.nim\(\d+\) main \(Async\)
  103. tasync_traceback2\.nim\(\d+\) foo \(Async\)
  104. tasync_traceback2\.nim\(\d+\) bar \(Async\)
  105. tasync_traceback2\.nim\(\d+\) baz \(Async\)
  106. Exception message: the_error_msg
  107. """.dedent
  108. doAssert match(err.msg, re(expected)), err.getStackTrace & err.msg
  109. block: # no async work
  110. proc baz() {.async.} =
  111. raise newException(ValueError, "the_error_msg")
  112. proc bar() {.async.} =
  113. await baz()
  114. proc foo() {.async.} =
  115. await bar()
  116. proc main {.async.} =
  117. await foo()
  118. try:
  119. waitFor main()
  120. doAssert false
  121. except ValueError as err:
  122. let expected = """
  123. the_error_msg
  124. Async traceback:
  125. tasync_traceback2\.nim\(\d+\) tasync_traceback2
  126. tasync_traceback2\.nim\(\d+\) main \(Async\)
  127. tasync_traceback2\.nim\(\d+\) foo \(Async\)
  128. tasync_traceback2\.nim\(\d+\) bar \(Async\)
  129. tasync_traceback2\.nim\(\d+\) baz \(Async\)
  130. Exception message: the_error_msg
  131. """.dedent
  132. doAssert match(err.msg, re(expected)), err.getStackTrace & err.msg
  133. echo "ok"