t12221.nim 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import asyncdispatch, os, times
  2. proc doubleSleep(hardSleep: int) {.async.} =
  3. await sleepAsync(50)
  4. sleep(hardSleep)
  5. template assertTime(target, timeTook: float): untyped {.dirty.} =
  6. doAssert(timeTook*1000 > target - 1000, "Took too short, should've taken " &
  7. $target & "ms, but took " & $(timeTook*1000) & "ms")
  8. doAssert(timeTook*1000 < target + 1000, "Took too long, should've taken " &
  9. $target & "ms, but took " & $(timeTook*1000) & "ms")
  10. var
  11. start: float
  12. fut: Future[void]
  13. # NOTE: this uses poll(3000) to limit timing error potential.
  14. start = epochTime()
  15. fut = sleepAsync(40) and sleepAsync(100) and doubleSleep(20)
  16. while not fut.finished:
  17. poll(1000)
  18. assertTime(150, epochTime() - start)
  19. start = epochTime()
  20. fut = sleepAsync(40) and sleepAsync(100) and doubleSleep(50)
  21. while not fut.finished:
  22. poll(1000)
  23. assertTime(200, epochTime() - start)
  24. start = epochTime()
  25. fut = sleepAsync(40) and sleepAsync(100) and doubleSleep(20) and sleepAsync(200)
  26. while not fut.finished:
  27. poll(1000)
  28. assertTime(300, epochTime() - start)
  29. start = epochTime()
  30. fut = (sleepAsync(40) and sleepAsync(100) and doubleSleep(20)) or sleepAsync(300)
  31. while not fut.finished:
  32. poll(1000)
  33. assertTime(150, epochTime() - start)