Skip to content

Commit 2fabbb9

Browse files
committed
Updated some async event tests.
1 parent 29b6e93 commit 2fabbb9

3 files changed

+185
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
discard """
2+
output:'''
3+
Thread 1: iteration 0
4+
Thread 2: iteration 0
5+
Thread 1: iteration 1
6+
Thread 2: iteration 1
7+
Thread 1: iteration 2
8+
Thread 2: iteration 2
9+
Thread 1: iteration 3
10+
Thread 2: iteration 3
11+
Thread 1: iteration 4
12+
Thread 2: iteration 4
13+
Thread 1: iteration 5
14+
Thread 2: iteration 5
15+
Thread 1: iteration 6
16+
Thread 2: iteration 6
17+
Thread 1: iteration 7
18+
Thread 2: iteration 7
19+
Thread 1: iteration 8
20+
Thread 2: iteration 8
21+
Thread 1: iteration 9
22+
Thread 2: iteration 9
23+
Thread 1: iteration 10
24+
Thread 2: iteration 10
25+
Thread 1: iteration 11
26+
Thread 2: iteration 11
27+
Thread 1: iteration 12
28+
Thread 2: iteration 12
29+
Thread 1: iteration 13
30+
Thread 2: iteration 13
31+
Thread 1: iteration 14
32+
Thread 2: iteration 14
33+
Thread 1: iteration 15
34+
Thread 2: iteration 15
35+
Thread 1: iteration 16
36+
Thread 2: iteration 16
37+
Thread 1: iteration 17
38+
Thread 2: iteration 17
39+
Thread 1: iteration 18
40+
Thread 2: iteration 18
41+
Thread 1: iteration 19
42+
Thread 2: iteration 19
43+
Thread 1: iteration 20
44+
Thread 2: iteration 20
45+
Thread 1: iteration 21
46+
Thread 2: iteration 21
47+
Thread 1: iteration 22
48+
Thread 2: iteration 22
49+
Thread 1: iteration 23
50+
Thread 2: iteration 23
51+
Thread 1: iteration 24
52+
Thread 2: iteration 24
53+
Thread 1: iteration 25
54+
Thread 2: iteration 25
55+
Thread 1: iteration 26
56+
Thread 2: iteration 26
57+
Thread 1: iteration 27
58+
Thread 2: iteration 27
59+
Thread 1: iteration 28
60+
Thread 2: iteration 28
61+
Thread 1: iteration 29
62+
Thread 2: iteration 29
63+
Thread 1: iteration 30
64+
Thread 2: iteration 30
65+
Thread 1: iteration 31
66+
Thread 2: iteration 31
67+
Thread 1: iteration 32
68+
Thread 2: iteration 32
69+
Thread 1: iteration 33
70+
Thread 2: iteration 33
71+
Thread 1: iteration 34
72+
Thread 2: iteration 34
73+
Thread 1: iteration 35
74+
Thread 2: iteration 35
75+
Thread 1: iteration 36
76+
Thread 2: iteration 36
77+
Thread 1: iteration 37
78+
Thread 2: iteration 37
79+
Thread 1: iteration 38
80+
Thread 2: iteration 38
81+
Thread 1: iteration 39
82+
Thread 2: iteration 39
83+
Thread 1: iteration 40
84+
Thread 2: iteration 40
85+
Thread 1: iteration 41
86+
Thread 2: iteration 41
87+
Thread 1: iteration 42
88+
Thread 2: iteration 42
89+
Thread 1: iteration 43
90+
Thread 2: iteration 43
91+
Thread 1: iteration 44
92+
Thread 2: iteration 44
93+
Thread 1: iteration 45
94+
Thread 2: iteration 45
95+
Thread 1: iteration 46
96+
Thread 2: iteration 46
97+
Thread 1: iteration 47
98+
Thread 2: iteration 47
99+
Thread 1: iteration 48
100+
Thread 2: iteration 48
101+
Thread 1: iteration 49
102+
Thread 2: iteration 49
103+
Thread 1: iteration 50
104+
Thread 2: iteration 50
105+
'''
106+
"""
107+
108+
import os, asyncdispatch
109+
110+
type
111+
ThreadArg = object
112+
event1: AsyncEvent
113+
event2: AsyncEvent
114+
115+
when not(compileOption("threads")):
116+
{.fatal: "Please, compile this program with the --threads:on option!".}
117+
118+
proc wait(event: AsyncEvent): Future[void] =
119+
var retFuture = newFuture[void]("AsyncEvent.wait")
120+
proc continuation(fd: AsyncFD): bool {.gcsafe.} =
121+
if not retFuture.finished:
122+
retFuture.complete()
123+
result = true
124+
addEvent(event, continuation)
125+
return retFuture
126+
127+
proc asyncProc1(args: ThreadArg) {.async.} =
128+
for i in 0 .. 50:
129+
# why 50 iterations? It's arbitrary. We can't run forever, but we want to run long enough
130+
# to sanity check against a race condition or deadlock.
131+
await args.event1.wait()
132+
args.event2.trigger()
133+
# Why echoes and not just a count? Because this test is about coordination of threads.
134+
# We need to make sure the threads get properly synchronized on each iteration.
135+
echo "Thread 1: iteration ", i
136+
137+
proc asyncProc2(args: ThreadArg) {.async.} =
138+
for i in 0 .. 50:
139+
args.event1.trigger()
140+
await args.event2.wait()
141+
echo "Thread 2: iteration ", i
142+
143+
proc threadProc1(args: ThreadArg) {.thread.} =
144+
## We create new dispatcher explicitly to avoid bugs.
145+
let loop = getGlobalDispatcher()
146+
waitFor asyncProc1(args)
147+
148+
proc threadProc2(args: ThreadArg) {.thread.} =
149+
## We create new dispatcher explicitly avoid bugs.
150+
let loop = getGlobalDispatcher()
151+
waitFor asyncProc2(args)
152+
153+
proc main() =
154+
var
155+
args: ThreadArg
156+
thread1: Thread[ThreadArg]
157+
thread2: Thread[ThreadArg]
158+
159+
args.event1 = newAsyncEvent()
160+
args.event2 = newAsyncEvent()
161+
thread1.createThread(threadProc1, args)
162+
# make sure the threads startup in order, or we will either deadlock or error.
163+
sleep(100)
164+
thread2.createThread(threadProc2, args)
165+
joinThreads(thread1, thread2)
166+
167+
when isMainModule:
168+
main()

tests/threads/testVirtualAsyncThreadCoordination.nim

+16-5
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,30 @@ proc asyncProc1(args: ThreadArg) {.async.} =
128128
for i in 0 .. 50:
129129
# why 50 iterations? It's arbitrary. We can't run forever, but we want to run long enough
130130
# to sanity check against a race condition or deadlock.
131-
let waiting = args.event1.wait()
132-
await waiting
131+
await args.event1.wait()
133132
args.event2.trigger()
133+
#while true:
134+
# the other thread may not have registered the event with it's event loop yet.
135+
# keep trying to trigger, eventually the other thread will be ready.
136+
#try:
137+
#args.event2.trigger()
138+
#break;
139+
#except ValueError:
140+
#continue
134141
# Why echoes and not just a count? Because this test is about coordination of threads.
135142
# We need to make sure the threads get properly synchronized on each iteration.
136143
echo "Thread 1: iteration ", i
137-
args.event2.trigger()
138144

139145
proc asyncProc2(args: ThreadArg) {.async.} =
140146
for i in 0 .. 50:
141-
let waiting = args.event2.wait()
147+
#while true:
148+
#try:
149+
#args.event1.trigger()
150+
#break;
151+
#except ValueError:
152+
#continue
142153
args.event1.trigger()
143-
await waiting
154+
await args.event2.wait()
144155
echo "Thread 2: iteration ", i
145156

146157
proc threadProc1(args: ThreadArg) {.thread.} =

tests/threads/testasynceventwiththreads.nim

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ proc threadTask(ev: AsyncEvent) =
1414
ev.trigger()
1515

1616
for i in 0 ..< 1000:
17-
echo "i: ", i
1817
var ev = newAsyncEvent()
1918
evs.add ev
20-
addEvent(ev, proc(fd: AsyncFD): bool {.gcsafe,closure.} = triggerCount += 1; echo "triggerCount: ", triggerCount; true)
19+
addEvent(ev, proc(fd: AsyncFD): bool {.gcsafe,closure.} = triggerCount += 1; true)
2120
spawn(threadTask(ev))
2221

2322
drain()

0 commit comments

Comments
 (0)