tosprocterminate.nim 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. discard """
  2. cmd: "nim $target $options -r $file"
  3. targets: "c cpp"
  4. matrix: "--threads:on; "
  5. """
  6. import os, osproc, times, std / monotimes
  7. import std/assertions
  8. when defined(windows):
  9. const ProgramWhichDoesNotEnd = "notepad"
  10. elif defined(openbsd):
  11. const ProgramWhichDoesNotEnd = "/bin/cat"
  12. else:
  13. const ProgramWhichDoesNotEnd = "/bin/sh"
  14. echo("starting " & ProgramWhichDoesNotEnd)
  15. var process = startProcess(ProgramWhichDoesNotEnd)
  16. sleep(500)
  17. echo("stopping process")
  18. process.terminate()
  19. var TimeToWait = 5000
  20. while process.running() and TimeToWait > 0:
  21. sleep(100)
  22. TimeToWait = TimeToWait - 100
  23. doAssert not process.running()
  24. echo("stopped process")
  25. process.close()
  26. echo("starting " & ProgramWhichDoesNotEnd)
  27. process = startProcess(ProgramWhichDoesNotEnd)
  28. echo("process should be stopped after 2s")
  29. let start = getMonoTime()
  30. discard process.waitForExit(2000)
  31. let took = getMonoTime() - start
  32. doAssert not process.running()
  33. # some additional time to account for overhead
  34. doAssert took < initDuration(seconds = 3)
  35. echo("stopped process after ", took)