-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtchannels_singlebuf.nim
More file actions
61 lines (45 loc) · 1.44 KB
/
tchannels_singlebuf.nim
File metadata and controls
61 lines (45 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## Test for and edge case of a channel with a single-element buffer:
## https://github.com/nim-lang/threading/pull/27#issue-1652851878
## Also tests `trySend` and `tryRecv` templates.
import threading/channels, std/os
const Message = "Hello"
block trySend_recv:
var attempts = 0
proc test(chan: Chan[string]) {.thread.} =
var notSent = true
let msg = Message
while notSent:
notSent = not chan.trySend(msg)
if notSent:
atomicInc(attempts)
var chan = newChan[string](elements = 1)
# Fill the channel before spawning the thread
chan.send("Dummy message")
var thread: Thread[Chan[string]]
createThread(thread, test, chan)
sleep 10
# Receive the dummy message to make room for the real message
discard chan.recv()
var dest: string
chan.recv(dest)
doAssert dest == Message
thread.joinThread()
doAssert attempts > 0, "trySend should have been attempted multiple times"
block send_tryRecv:
var attempts = 0
proc test(chan: Chan[string]) {.thread.} =
var notReceived = true
var msg: string
while notReceived:
notReceived = not chan.tryRecv(msg)
if notReceived:
atomicInc(attempts)
doAssert msg == Message
var chan = newChan[string](elements = 1)
var thread: Thread[Chan[string]]
createThread(thread, test, chan)
sleep 10
let src = Message
chan.send(src)
thread.joinThread()
doAssert attempts > 0, "tryRecv should have been attempted multiple times"