twaitforexit.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import std/[osproc, os, times]
  2. block: # bug #5091
  3. when defined(linux):
  4. const filename = "false"
  5. var p = startProcess(filename, options = {poStdErrToStdOut, poUsePath})
  6. os.sleep(1000) # make sure process has exited already
  7. let atStart = getTime()
  8. const msWait = 2000
  9. try:
  10. discard waitForExit(p, msWait)
  11. except OSError:
  12. discard
  13. # check that we don't have to wait msWait milliseconds
  14. doAssert(getTime() < atStart + milliseconds(msWait))
  15. block: # bug #23825
  16. # the sleep command might not be available in all Windows installations
  17. when defined(linux):
  18. var thr: array[0..99, Thread[int]]
  19. proc threadFunc(i: int) {.thread.} =
  20. let sleepTime = float(i) / float(thr.len + 1)
  21. doAssert sleepTime < 1.0
  22. let p = startProcess("sleep", workingDir = "", args = @[$sleepTime], options = {poUsePath, poParentStreams})
  23. # timeout = 1_000_000 seconds ~= 278 hours ~= 11.5 days
  24. doAssert p.waitForExit(timeout=1_000_000_000) == 0
  25. for i in low(thr)..high(thr):
  26. createThread(thr[i], threadFunc, i)
  27. joinThreads(thr)