titerators.nim 749 B

12345678910111213141516171819202122232425
  1. import coro
  2. include system/timers
  3. var
  4. stackCheckValue = 1100220033
  5. numbers = newSeqOfCap[int](10)
  6. iterator theIterator(id: int, sleep: float): int =
  7. for i in 0..<5:
  8. yield 10 * id + i
  9. suspend(sleep)
  10. proc theCoroutine(id: int, sleep: float32) =
  11. for n in theIterator(id, sleep):
  12. numbers.add(n)
  13. var start = getTicks()
  14. start(proc() = theCoroutine(1, 0.01))
  15. start(proc() = theCoroutine(2, 0.011))
  16. run()
  17. var executionTime = getTicks() - start
  18. doAssert(executionTime >= 55_000_000.Nanos and executionTime < 56_000_000.Nanos, "Coroutines executed too short")
  19. doAssert(stackCheckValue == 1100220033, "Thread stack got corrupted")
  20. doAssert(numbers == @[10, 20, 11, 21, 12, 22, 13, 23, 14, 24], "Coroutines executed in incorrect order")