Skip to content

Commit 7f4efb5

Browse files
author
Jaremy J. Creechley
committed
updating tests
1 parent c7de8a1 commit 7f4efb5

File tree

2 files changed

+124
-62
lines changed

2 files changed

+124
-62
lines changed

tests/tchannels_overwrite.nim

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ discard """
44
"""
55

66
import threading/channels
7-
import std/[logging, unittest]
7+
import std/[os, logging, unittest]
88

99
suite "testing Chan with overwrite mode":
1010
var logger = newConsoleLogger(levelThreshold=lvlInfo)
@@ -36,4 +36,56 @@ suite "testing Chan with overwrite mode":
3636
logger.log(lvlDebug, "got messages: " & $messages)
3737
check messages == @["msg7", "msg8", "msg9", "msg10"]
3838

39+
test "basic overwrite tests":
40+
41+
var chan = newChan[string](elements = 4, overwrite = true)
42+
43+
# Launch the worker.
44+
var worker1: Thread[void]
45+
var worker2: Thread[void]
46+
47+
## start worker1
48+
createThread(worker1, proc () =
49+
chan.send("Hello World!"))
50+
51+
# Block until the message arrives, then print it out.
52+
block:
53+
var dest = ""
54+
chan.recv(dest)
55+
doAssert dest == "Hello World!"
56+
57+
# Wait for the thread to exit before moving on to the next example.
58+
worker1.joinThread()
59+
60+
# Launch the other worker.
61+
createThread(worker2, proc () =
62+
# This is another proc to run in a background thread. This proc takes a while
63+
# to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
64+
sleep(2000)
65+
chan.send("Another message"))
66+
67+
# This time, use a non-blocking approach with tryRecv.
68+
# Since the main thread is not blocked, it could be used to perform other
69+
# useful work while it waits for data to arrive on the channel.
70+
var messages: seq[string]
71+
block:
72+
var msg = ""
73+
while true:
74+
let tried = chan.tryRecv(msg)
75+
if tried:
76+
messages.add move(msg)
77+
break
78+
79+
messages.add "Pretend I'm doing useful work..."
80+
# For this example, sleep in order not to flood stdout with the above
81+
# message.
82+
sleep(400)
83+
84+
# Wait for the second thread to exit before cleaning up the channel.
85+
worker2.joinThread()
86+
87+
# Clean up the channel.
88+
doAssert messages[^1] == "Another message"
89+
doAssert messages.len >= 2
90+
3991

tests/tchannels_simple.nim

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,74 @@ discard """
44
"""
55

66
import threading/channels
7-
import std/os
8-
9-
var chan = newChan[string]()
10-
11-
# This proc will be run in another thread using the threads module.
12-
proc firstWorker() =
13-
chan.send("Hello World!")
14-
15-
# This is another proc to run in a background thread. This proc takes a while
16-
# to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
17-
proc secondWorker() =
18-
sleep(2000)
19-
chan.send("Another message")
20-
21-
22-
# Launch the worker.
23-
var worker1: Thread[void]
24-
createThread(worker1, firstWorker)
25-
26-
# Block until the message arrives, then print it out.
27-
var dest = ""
28-
chan.recv(dest)
29-
doAssert dest == "Hello World!"
30-
31-
# Wait for the thread to exit before moving on to the next example.
32-
worker1.joinThread()
33-
34-
# Launch the other worker.
35-
var worker2: Thread[void]
36-
createThread(worker2, secondWorker)
37-
# This time, use a non-blocking approach with tryRecv.
38-
# Since the main thread is not blocked, it could be used to perform other
39-
# useful work while it waits for data to arrive on the channel.
40-
41-
var messages: seq[string]
42-
var msg = ""
43-
while true:
44-
let tried = chan.tryRecv(msg)
45-
if tried:
46-
messages.add move(msg)
47-
break
48-
49-
messages.add "Pretend I'm doing useful work..."
50-
# For this example, sleep in order not to flood stdout with the above
51-
# message.
52-
sleep(400)
53-
54-
# Wait for the second thread to exit before cleaning up the channel.
55-
worker2.joinThread()
56-
57-
# Clean up the channel.
58-
doAssert messages[^1] == "Another message"
59-
doAssert messages.len >= 2
60-
61-
62-
block:
63-
let chan0 = newChan[int]()
64-
let chan1 = chan0
65-
block:
66-
let chan3 = chan0
67-
let chan4 = chan0
7+
import std/[os, logging, unittest]
8+
9+
template runMultithreadTest*[T](chan: Chan[T]) =
10+
# This proc will be run in another thread using the threads module.
11+
proc firstWorker() =
12+
chan.send("Hello World!")
13+
14+
# This is another proc to run in a background thread. This proc takes a while
15+
# to send the message since it sleeps for 2 seconds (or 2000 milliseconds).
16+
proc secondWorker() =
17+
sleep(2000)
18+
chan.send("Another message")
19+
20+
21+
# Launch the worker.
22+
var worker1: Thread[void]
23+
createThread(worker1, firstWorker)
24+
25+
# Block until the message arrives, then print it out.
26+
var dest = ""
27+
chan.recv(dest)
28+
doAssert dest == "Hello World!"
29+
30+
# Wait for the thread to exit before moving on to the next example.
31+
worker1.joinThread()
32+
33+
# Launch the other worker.
34+
var worker2: Thread[void]
35+
createThread(worker2, secondWorker)
36+
# This time, use a non-blocking approach with tryRecv.
37+
# Since the main thread is not blocked, it could be used to perform other
38+
# useful work while it waits for data to arrive on the channel.
39+
40+
var messages: seq[string]
41+
var msg = ""
42+
while true:
43+
let tried = chan.tryRecv(msg)
44+
if tried:
45+
messages.add move(msg)
46+
break
47+
48+
messages.add "Pretend I'm doing useful work..."
49+
# For this example, sleep in order not to flood stdout with the above
50+
# message.
51+
sleep(400)
52+
53+
# Wait for the second thread to exit before cleaning up the channel.
54+
worker2.joinThread()
55+
56+
# Clean up the channel.
57+
check messages[^1] == "Another message"
58+
check messages.len >= 2
59+
60+
61+
suite "testing Chan with overwrite mode":
62+
var logger = newConsoleLogger(levelThreshold=lvlInfo)
63+
64+
setup:
65+
discard "run before each test"
66+
67+
test "basic init tests":
68+
block:
69+
let chan0 = newChan[int]()
70+
let chan1 = chan0
71+
block:
72+
let chan3 = chan0
73+
let chan4 = chan0
74+
75+
test "basic multithread":
76+
var chan = newChan[string]()
77+
runMultithreadTest(chan)

0 commit comments

Comments
 (0)