m14634.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #[
  2. Tool to investigate underlying reasons for https://github.com/nim-lang/Nim/pull/14634
  3. nim r --threads:on -d:threadsafe tests/m14634.nim
  4. ]#
  5. when not defined(windows):
  6. import std/selectors
  7. type TestData = object
  8. s1, s2, s3: int
  9. proc timerNotificationTestImpl(data: var TestData) =
  10. var selector = newSelector[int]()
  11. let t0 = 5
  12. var timer = selector.registerTimer(t0, false, 0)
  13. let t = 2000
  14. # values too close to `t0` cause the test to be flaky in CI on OSX+freebsd
  15. # When running locally, t0=100, t=98 will succeed some of the time which indicates
  16. # there is some lag involved. Note that the higher `t-t0` is, the less times
  17. # the test fails.
  18. var rc1 = selector.select(t)
  19. var rc2 = selector.select(t)
  20. assert len(rc1) <= 1 and len(rc2) <= 1
  21. data.s1 += ord(len(rc1) == 1)
  22. data.s2 += ord(len(rc2) == 1)
  23. selector.unregister(timer)
  24. discard selector.select(0)
  25. selector.registerTimer(t0, true, 0)
  26. # same comment as above
  27. var rc4 = selector.select(t)
  28. let t2 = 100
  29. # this can't be too large as it'll actually wait that long:
  30. # timer_notification_test.n * t2
  31. var rc5 = selector.select(t2)
  32. assert len(rc4) + len(rc5) <= 1
  33. data.s3 += ord(len(rc4) + len(rc5) == 1)
  34. assert(selector.isEmpty())
  35. selector.close()
  36. proc timerNotificationTest() =
  37. var data: TestData
  38. let n = 10
  39. for i in 0..<n:
  40. timerNotificationTestImpl(data)
  41. doAssert data.s1 == n and data.s2 == n and data.s3 == n, $data
  42. when isMainModule:
  43. timerNotificationTest()