tblocking_channel.nim 801 B

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