Skip to content

Commit 9adc151

Browse files
author
Jaremy J. Creechley
committed
reformatting tests and add proper blocking test
1 parent 02ca248 commit 9adc151

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
t*
2+
!t*.nim

tests/common.nim

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,46 @@ template runMultithreadInOrderTest*[T](chan: Chan[T]) =
1414

1515
# Launch the first worker.
1616
createThread(worker1) do:
17+
var lg = newConsoleLogger(logLevel, verboseFmtStr)
18+
lg.log(lvlDebug, "[worker1] sending message... ")
1719
chan.send("Hello World!")
1820

1921
# Wait for the thread to exit before moving on to the next example.
2022
worker1.joinThread()
2123

2224
var dest = ""
2325
chan.recv(dest)
24-
logger.log(lvlDebug, "Received msg: " & $dest)
26+
logger.log(lvlDebug, "[main] Received msg: " & $dest)
2527
doAssert dest == "Hello World!"
2628

2729

28-
template runMultithreadBockTest*[T](chan: Chan[T]) =
30+
template runMultithreadNonRecvBlockTest*[T](chan: Chan[T]) =
2931
var worker2: Thread[void]
3032
# Launch the other worker.
3133
createThread(worker2) do:
3234
# This is another proc to run in a background thread. This proc takes a while
3335
# to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
36+
var lg = newConsoleLogger(logLevel, verboseFmtStr)
3437
sleep(2000)
38+
lg.log(lvlDebug, "[worker2] sending message... ")
3539
chan.send("Another message")
3640

3741
# This time, use a non-blocking approach with tryRecv.
3842
# Since the main thread is not blocked, it could be used to perform other
3943
# useful work while it waits for data to arrive on the channel.
4044

45+
logger.log(lvlDebug, "[main] running non-blocking tryRecv")
4146
var messages: seq[string]
4247
var msg = ""
4348
while true:
4449
let tried = chan.tryRecv(msg)
4550
if tried:
46-
logger.log(lvlDebug, "Receive msg: " & $msg)
51+
logger.log(lvlDebug, "[main] Receive msg: " & $msg)
4752
messages.add move(msg)
4853
break
4954

5055
messages.add "Pretend I'm doing useful work..."
51-
logger.log(lvlDebug, "add fakeMsg to messages (other work): " & $messages.len())
56+
logger.log(lvlDebug, "[main] add fakeMsg to messages (other work): " & $messages.len())
5257
sleep(400)
5358

5459
# Wait for the second thread to exit before cleaning up the channel.
@@ -57,3 +62,37 @@ template runMultithreadBockTest*[T](chan: Chan[T]) =
5762
# Clean up the channel.
5863
check messages[^1] == "Another message"
5964
check messages.len >= 2
65+
66+
template runMultithreadRecvBlockTest*[T](chan: Chan[T]) =
67+
var worker2: Thread[void]
68+
# Launch the other worker.
69+
createThread(worker2) do:
70+
# This is another proc to run in a background thread. This proc takes a while
71+
# to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
72+
var lg = newConsoleLogger(logLevel, verboseFmtStr)
73+
sleep(2000)
74+
lg.log(lvlDebug, "[worker2] sending message... ")
75+
chan.send("Another message")
76+
77+
# This time, use a non-blocking approach with tryRecv.
78+
# Since the main thread is not blocked, it could be used to perform other
79+
# useful work while it waits for data to arrive on the channel.
80+
81+
logger.log(lvlDebug, "[main] running blocking recv")
82+
var messages: seq[string]
83+
var msg = ""
84+
85+
chan.recv(msg)
86+
logger.log(lvlDebug, "[main] Receive msg: " & $msg)
87+
messages.add move(msg)
88+
89+
messages.add "Pretend I'm doing useful work..."
90+
logger.log(lvlDebug, "[main] add fakeMsg to messages (other work): " & $messages.len())
91+
sleep(400)
92+
93+
# Wait for the second thread to exit before cleaning up the channel.
94+
worker2.joinThread()
95+
96+
# Clean up the channel.
97+
check messages[0] == "Another message"
98+
check messages.len >= 2

tests/tchannels_overwrite.nim

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ suite "testing Chan with overwrite mode":
3838
check messages == @["msg7", "msg8", "msg9", "msg10"]
3939

4040
test "basic overwrite tests":
41-
4241
var chan = newChan[string](elements = 4, overwrite = true)
43-
4442
runMultithreadInOrderTest(chan)
4543

46-
test "basic blocking multithread":
47-
var chan = newChan[string]()
48-
runMultithreadBockTest(chan)
44+
test "basic non-blocking recv multithread":
45+
var chan = newChan[string](overwrite=true)
46+
runMultithreadNonRecvBlockTest(chan)
47+
48+
test "basic blocking recv multithread":
49+
var chan = newChan[string](overwrite=true)
50+
runMultithreadRecvBlockTest(chan)
4951

5052

5153

tests/tchannels_simple.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ suite "testing Chan with overwrite mode":
2525
var chan = newChan[string]()
2626
runMultithreadInOrderTest(chan)
2727

28-
test "basic blocking multithread":
28+
test "basic non-blocking multithread":
2929
var chan = newChan[string]()
30-
runMultithreadBockTest(chan)
30+
runMultithreadNonRecvBlockTest(chan)
31+
32+
test "basic blocking multithread":
33+
var chan = newChan[string](overwrite=true)
34+
runMultithreadRecvBlockTest(chan)

0 commit comments

Comments
 (0)