tblocking_channel.nim 785 B

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