tasync_misc.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import json, asyncdispatch
  2. block: #6100
  3. let done = newFuture[int]()
  4. done.complete(1)
  5. proc asyncSum: Future[int] {.async.} =
  6. for _ in 1..1_000_000:
  7. result += await done
  8. let res = waitFor asyncSum()
  9. doAssert(res == 1_000_000)
  10. block: #7985
  11. proc getData(): Future[JsonNode] {.async.} =
  12. result = %*{"value": 1}
  13. type
  14. MyData = object
  15. value: BiggestInt
  16. proc main() {.async.} =
  17. let data = to(await(getData()), MyData)
  18. doAssert($data == "(value: 1)")
  19. waitFor(main())
  20. block: #8399
  21. proc bar(): Future[string] {.async.} = discard
  22. proc foo(line: string) {.async.} =
  23. var res =
  24. case line[0]
  25. of '+', '-': @[]
  26. of '$': (let x = await bar(); @[""])
  27. else: @[]
  28. doAssert(res == @[""])
  29. waitFor foo("$asd")
  30. block: # nkCheckedFieldExpr
  31. proc bar(): Future[JsonNode] {.async.} =
  32. return newJInt(5)
  33. proc foo() {.async.} =
  34. let n = 10 + (await bar()).num
  35. doAssert(n == 15)
  36. waitFor foo()
  37. block: # 12743
  38. template templ = await sleepAsync 0
  39. proc prc {.async.} = templ
  40. waitFor prc()
  41. block: # issue #13899
  42. proc someConnect() {.async.} =
  43. await sleepAsync(1)
  44. proc someClose() {.async.} =
  45. await sleepAsync(2)
  46. proc testFooFails(): Future[bool] {.async.} =
  47. await someConnect()
  48. defer:
  49. await someClose()
  50. result = true
  51. proc testFooSucceed(): Future[bool] {.async.} =
  52. try:
  53. await someConnect()
  54. finally:
  55. await someClose()
  56. result = true
  57. doAssert waitFor testFooSucceed()
  58. doAssert waitFor testFooFails()
  59. block: # issue #9313
  60. doAssert compiles(block:
  61. proc a() {.async.} =
  62. echo "Hi"
  63. quit(0)
  64. )