summary refs log tree commit diff stats
path: root/tests/parallel
diff options
context:
space:
mode:
authorYuriy Glukhov <yglukhov@users.noreply.github.com>2017-07-31 21:06:55 +0300
committerAndreas Rumpf <rumpf_a@web.de>2017-07-31 20:06:55 +0200
commit3d543b1539f8d5dde5746c90c5de5fa3df1cadfd (patch)
treea3efeafc2493aec7f2610c4868c6a5844b41034d /tests/parallel
parent6b38b37b4fa3168f1bf96822b3ff4471d8fd68c0 (diff)
downloadNim-3d543b1539f8d5dde5746c90c5de5fa3df1cadfd.tar.gz
Channels can now block depending on maxItems (#6153)
Diffstat (limited to 'tests/parallel')
-rw-r--r--tests/parallel/tblocking_channel.nim37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/parallel/tblocking_channel.nim b/tests/parallel/tblocking_channel.nim
new file mode 100644
index 000000000..8b8b49454
--- /dev/null
+++ b/tests/parallel/tblocking_channel.nim
@@ -0,0 +1,37 @@
+discard """
+output: ""
+"""
+import threadpool, os
+
+var chan: Channel[int]
+
+chan.open(2)
+chan.send(1)
+chan.send(2)
+doAssert(not chan.trySend(3)) # At this point chan is at max capacity
+
+proc receiver() =
+    doAssert(chan.recv() == 1)
+    doAssert(chan.recv() == 2)
+    doAssert(chan.recv() == 3)
+    doAssert(chan.recv() == 4)
+    doAssert(chan.recv() == 5)
+
+var msgSent = false
+
+proc emitter() =
+    chan.send(3)
+    msgSent = true
+
+spawn emitter()
+# At this point emitter should be stuck in `send`
+sleep(100) # Sleep a bit to ensure that it is still stuck
+doAssert(not msgSent)
+
+spawn receiver()
+sleep(100) # Sleep a bit to let receicer consume the messages
+doAssert(msgSent) # Sender should be unblocked
+
+doAssert(chan.trySend(4))
+chan.send(5)
+sync()