tupcoming_async.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. discard """
  2. output: '''
  3. OK
  4. '''
  5. """
  6. when defined(upcoming):
  7. import asyncdispatch, times, streams, posix
  8. from ioselectors import ioselSupportedPlatform
  9. proc delayedSet(ev: AsyncEvent, timeout: int): Future[void] {.async.} =
  10. await sleepAsync(timeout)
  11. ev.trigger()
  12. proc waitEvent(ev: AsyncEvent, closeEvent = false): Future[void] =
  13. var retFuture = newFuture[void]("waitEvent")
  14. proc cb(fd: AsyncFD): bool =
  15. retFuture.complete()
  16. if closeEvent:
  17. return true
  18. else:
  19. return false
  20. addEvent(ev, cb)
  21. return retFuture
  22. proc eventTest() =
  23. var event = newAsyncEvent()
  24. var fut = waitEvent(event)
  25. asyncCheck(delayedSet(event, 500))
  26. waitFor(fut or sleepAsync(1000))
  27. if not fut.finished:
  28. echo "eventTest: Timeout expired before event received!"
  29. proc eventTest5304() =
  30. # Event should not be signaled if it was uregistered,
  31. # even in case, when poll() was not called yet.
  32. # Issue #5304.
  33. var unregistered = false
  34. let e = newAsyncEvent()
  35. addEvent(e) do (fd: AsyncFD) -> bool:
  36. assert(not unregistered)
  37. e.trigger()
  38. e.unregister()
  39. unregistered = true
  40. poll()
  41. proc eventTest5298() =
  42. # Event must raise `AssertionDefect` if event was unregistered twice.
  43. # Issue #5298.
  44. let e = newAsyncEvent()
  45. var eventReceived = false
  46. addEvent(e) do (fd: AsyncFD) -> bool:
  47. eventReceived = true
  48. return true
  49. e.trigger()
  50. while not eventReceived:
  51. poll()
  52. try:
  53. e.unregister()
  54. except AssertionDefect:
  55. discard
  56. e.close()
  57. proc eventTest5331() =
  58. # Event must not raise any exceptions while was unregistered inside of
  59. # own callback.
  60. # Issue #5331.
  61. let e = newAsyncEvent()
  62. addEvent(e) do (fd: AsyncFD) -> bool:
  63. e.unregister()
  64. e.close()
  65. e.trigger()
  66. poll()
  67. when ioselSupportedPlatform or defined(windows):
  68. import osproc
  69. proc waitTimer(timeout: int): Future[void] =
  70. var retFuture = newFuture[void]("waitTimer")
  71. proc cb(fd: AsyncFD): bool =
  72. retFuture.complete()
  73. addTimer(timeout, true, cb)
  74. return retFuture
  75. proc waitProcess(p: Process): Future[void] =
  76. var retFuture = newFuture[void]("waitProcess")
  77. proc cb(fd: AsyncFD): bool =
  78. retFuture.complete()
  79. addProcess(p.processID(), cb)
  80. return retFuture
  81. proc timerTest() =
  82. waitFor(waitTimer(200))
  83. proc processTest() =
  84. when defined(windows):
  85. var process = startProcess("ping.exe", "",
  86. ["127.0.0.1", "-n", "2", "-w", "100"], nil,
  87. {poStdErrToStdOut, poUsePath, poInteractive,
  88. poDaemon})
  89. else:
  90. var process = startProcess("sleep", "", ["1"], nil,
  91. {poStdErrToStdOut, poUsePath})
  92. var fut = waitProcess(process)
  93. waitFor(fut or waitTimer(2000))
  94. if fut.finished and process.peekExitCode() == 0:
  95. discard
  96. else:
  97. echo "processTest: Timeout expired before process exited!"
  98. when ioselSupportedPlatform:
  99. proc waitSignal(signal: int): Future[void] =
  100. var retFuture = newFuture[void]("waitSignal")
  101. proc cb(fd: AsyncFD): bool =
  102. retFuture.complete()
  103. addSignal(signal, cb)
  104. return retFuture
  105. proc delayedSignal(signal: int, timeout: int): Future[void] {.async.} =
  106. await waitTimer(timeout)
  107. var pid = posix.getpid()
  108. discard posix.kill(pid, signal.cint)
  109. proc signalTest() =
  110. var fut = waitSignal(posix.SIGINT)
  111. asyncCheck(delayedSignal(posix.SIGINT, 500))
  112. waitFor(fut or waitTimer(1000))
  113. if not fut.finished:
  114. echo "signalTest: Timeout expired before signal received!"
  115. when ioselSupportedPlatform:
  116. timerTest()
  117. eventTest()
  118. eventTest5304()
  119. eventTest5298()
  120. eventTest5331()
  121. processTest()
  122. signalTest()
  123. echo "OK"
  124. elif defined(windows):
  125. timerTest()
  126. eventTest()
  127. eventTest5304()
  128. eventTest5298()
  129. eventTest5331()
  130. processTest()
  131. echo "OK"
  132. else:
  133. eventTest()
  134. eventTest5304()
  135. eventTest5298()
  136. eventTest5331()
  137. echo "OK"
  138. else:
  139. echo "OK"