tblocking_channel.nim 751 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. discard """
  2. output: ""
  3. """
  4. import threadpool, os
  5. var chan: Channel[int]
  6. chan.open(2)
  7. chan.send(1)
  8. chan.send(2)
  9. doAssert(not chan.trySend(3)) # At this point chan is at max capacity
  10. proc receiver() =
  11. doAssert(chan.recv() == 1)
  12. doAssert(chan.recv() == 2)
  13. doAssert(chan.recv() == 3)
  14. doAssert(chan.recv() == 4)
  15. doAssert(chan.recv() == 5)
  16. var msgSent = false
  17. proc emitter() =
  18. chan.send(3)
  19. msgSent = true
  20. spawn emitter()
  21. # At this point emitter should be stuck in `send`
  22. sleep(50) # Sleep a bit to ensure that it is still stuck
  23. doAssert(not msgSent)
  24. spawn receiver()
  25. sleep(50) # Sleep a bit to let receicer consume the messages
  26. doAssert(msgSent) # Sender should be unblocked
  27. doAssert(chan.trySend(4))
  28. chan.send(5)
  29. sync()