tosprocterminate.nim 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. when defined(Windows):
  8. const ProgramWhichDoesNotEnd = "notepad"
  9. elif defined(openbsd):
  10. const ProgramWhichDoesNotEnd = "/bin/cat"
  11. else:
  12. const ProgramWhichDoesNotEnd = "/bin/sh"
  13. echo("starting " & ProgramWhichDoesNotEnd)
  14. var process = startProcess(ProgramWhichDoesNotEnd)
  15. sleep(500)
  16. echo("stopping process")
  17. process.terminate()
  18. var TimeToWait = 5000
  19. while process.running() and TimeToWait > 0:
  20. sleep(100)
  21. TimeToWait = TimeToWait - 100
  22. doAssert not process.running()
  23. echo("stopped process")
  24. process.close()
  25. echo("starting " & ProgramWhichDoesNotEnd)
  26. process = startProcess(ProgramWhichDoesNotEnd)
  27. echo("process should be stopped after 2s")
  28. let start = getMonoTime()
  29. discard process.waitForExit(2000)
  30. let took = getMonoTime() - start
  31. doAssert not process.running()
  32. # some additional time to account for overhead
  33. doAssert took < initDuration(seconds = 3)
  34. echo("stopped process after ", took)