@@ -4,64 +4,74 @@ discard """
44"""
55
66import 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