aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/cancel.lua191
1 files changed, 103 insertions, 88 deletions
diff --git a/tests/cancel.lua b/tests/cancel.lua
index 0d9d143..001483a 100644
--- a/tests/cancel.lua
+++ b/tests/cancel.lua
@@ -1,36 +1,42 @@
1local which_tests = {}
2for k,v in ipairs{...} do
3 which_tests[v] = true
4end
5
1local lanes = require "lanes" .configure{ with_timers = false} 6local lanes = require "lanes" .configure{ with_timers = false}
2 7
3local linda = lanes.linda() 8local linda = lanes.linda()
4
5--####################################################################
6print "\n\n####################################################################\nbegin genlock & genatomic cancel test\n"
7
8-- get a lock and a atomic operator
9local lock = lanes.genlock( linda, "lock", 1)
10local atomic = lanes.genatomic( linda, "atomic")
11
12-- check that cancelled lindas give cancel_error as they should
13linda:cancel()
14assert( linda:get( "empty") == lanes.cancel_error)
15assert( lanes.genlock( linda, "any", 1) == lanes.cancel_error)
16assert( lanes.genatomic( linda, "any") == lanes.cancel_error)
17
18-- check that lock and atomic functions return cancel_error if the linda was cancelled
19assert( lock( 1) == lanes.cancel_error)
20assert( lock( -1) == lanes.cancel_error)
21assert( atomic( 1) == lanes.cancel_error)
22
23-- reset the linda so that the other tests work
24linda:cancel( "none")
25linda:limit( "lock", -1)
26linda:set( "lock")
27linda:limit( "atomic", -1)
28linda:set( "atomic")
29
30-- a numeric value to read 9-- a numeric value to read
31linda:set( "val", 33.0) 10linda:set( "val", 33.0)
32 11
33print "test OK" 12--####################################################################
13if #which_tests and which_tests.genlock then
14 print "\n\n####################################################################\nbegin genlock & genatomic cancel test\n"
15
16 -- get a lock and a atomic operator
17 local lock = lanes.genlock( linda, "lock", 1)
18 local atomic = lanes.genatomic( linda, "atomic")
19
20 -- check that cancelled lindas give cancel_error as they should
21 linda:cancel()
22 assert( linda:get( "empty") == lanes.cancel_error)
23 assert( lanes.genlock( linda, "any", 1) == lanes.cancel_error)
24 assert( lanes.genatomic( linda, "any") == lanes.cancel_error)
25
26 -- check that lock and atomic functions return cancel_error if the linda was cancelled
27 assert( lock( 1) == lanes.cancel_error)
28 assert( lock( -1) == lanes.cancel_error)
29 assert( atomic( 1) == lanes.cancel_error)
30
31 -- reset the linda so that the other tests work
32 linda:cancel( "none")
33 linda:limit( "lock", -1)
34 linda:set( "lock")
35 linda:limit( "atomic", -1)
36 linda:set( "atomic")
37
38 print "test OK"
39end
34--#################################################################### 40--####################################################################
35 41
36local waitCancellation = function( h, expected_status) 42local waitCancellation = function( h, expected_status)
@@ -119,91 +125,100 @@ end
119--#################################################################### 125--####################################################################
120--#################################################################### 126--####################################################################
121 127
122print "\n\n####################################################################\nbegin linda cancel test\n" 128if #which_tests and which_tests.linda then
123h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda 129 print "\n\n####################################################################\nbegin linda cancel test\n"
130 h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda
124 131
125print "wait 1s" 132 print "wait 1s"
126linda:receive( 1, "yeah") 133 linda:receive( 1, "yeah")
127 134
128-- linda cancel: linda:receive() returns cancel_error immediately 135 -- linda cancel: linda:receive() returns cancel_error immediately
129linda:cancel( "both") 136 linda:cancel( "both")
130 137
131-- wait until cancellation is effective. 138 -- wait until cancellation is effective.
132waitCancellation( h, "done") 139 waitCancellation( h, "done")
133 140
134-- reset the linda so that the other tests work 141 -- reset the linda so that the other tests work
135linda:cancel( "none") 142 linda:cancel( "none")
136 143end
137print "\n\n####################################################################\nbegin soft cancel test\n"
138h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda
139
140print "wait 1s"
141linda:receive( 1, "yeah")
142 144
143-- soft cancel, no awakening of waiting linda operations, should timeout 145if #which_tests and which_tests.soft then
144local a, b = h:cancel( "soft", 1, false) 146 print "\n\n####################################################################\nbegin soft cancel test\n"
145-- cancellation should fail as the lane is still waiting on its linda 147 h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda
146assert( a == false and b == "timeout")
147waitCancellation( h, "waiting")
148 148
149-- soft cancel, this time awakens waiting linda operations, which returns cancel_error immediately, no timeout. 149 print "wait 1s"
150h:cancel( "soft", true) 150 linda:receive( 1, "yeah")
151 151
152-- wait until cancellation is effective. the lane will interrupt its loop and print the exit message 152 -- soft cancel, no awakening of waiting linda operations, should timeout
153waitCancellation( h, "done") 153 local a, b = h:cancel( "soft", 1, false)
154 -- cancellation should fail as the lane is still waiting on its linda
155 assert( a == false and b == "timeout")
156 waitCancellation( h, "waiting")
154 157
155-- do return end 158 -- soft cancel, this time awakens waiting linda operations, which returns cancel_error immediately, no timeout.
159 h:cancel( "soft", true)
156 160
157print "\n\n####################################################################\nbegin hook cancel test\n" 161 -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message
158h = lanes.gen( "*", protectedBody)( "get", 300000) 162 waitCancellation( h, "done")
159print "wait 2s" 163end
160linda:receive( 2, "yeah")
161 164
162-- count hook cancel after 3 instructions 165if #which_tests and which_tests.hook then
163h:cancel( "count", 300, 5.0) 166 print "\n\n####################################################################\nbegin hook cancel test\n"
167 h = lanes.gen( "*", protectedBody)( "get", 300000)
168 print "wait 2s"
169 linda:receive( 2, "yeah")
164 170
165-- wait until cancellation is effective. the lane will interrupt its loop and print the exit message 171 -- count hook cancel after some instruction instructions
166waitCancellation( h, "cancelled") 172 h:cancel( "count", 300, 5.0)
167 173
168print "\n\n####################################################################\nbegin hard cancel test\n" 174 -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message
169h = lanes.gen( "*", protectedBody)( "receive", nil) -- infinite timeout 175 waitCancellation( h, "cancelled")
176end
170 177
171-- wait 2s before cancelling the lane 178if #which_tests and which_tests.hard then
172print "wait 2s" 179 print "\n\n####################################################################\nbegin hard cancel test\n"
173linda:receive( 2, "yeah") 180 h = lanes.gen( "*", protectedBody)( "receive", nil) -- infinite timeout
174 181
175-- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it 182 -- wait 2s before cancelling the lane
176h:cancel() 183 print "wait 2s"
184 linda:receive( 2, "yeah")
177 185
178-- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error 186 -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it
179waitCancellation( h, "cancelled") 187 h:cancel()
180 188
181print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" 189 -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error
182h = lanes.gen( "*", laneBody)( "receive", nil) 190 waitCancellation( h, "cancelled")
191end
183 192
184-- wait 2s before cancelling the lane 193if #which_tests and which_tests.hard_unprotected then
185print "wait 2s" 194 print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n"
186linda:receive( 2, "yeah") 195 h = lanes.gen( "*", laneBody)( "receive", nil)
187 196
188-- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it 197 -- wait 2s before cancelling the lane
189h:cancel() 198 print "wait 2s"
199 linda:receive( 2, "yeah")
190 200
191-- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error 201 -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it
192waitCancellation( h, "cancelled") 202 h:cancel()
193 203
194print "\n\n####################################################################\nbegin kill cancel test\n" 204 -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error
195h = lanes.gen( "*", laneBody)( "busy", 50000000) -- start a pure Lua busy loop lane 205 waitCancellation( h, "cancelled")
206end
196 207
197-- wait 1/3s before cancelling the lane, before the busy loop can finish 208if #which_tests and which_tests.kill then
198print "wait 0.3s" 209 print "\n\n####################################################################\nbegin kill cancel test\n"
199linda:receive( 0.3, "yeah") 210 h = lanes.gen( "*", laneBody)( "busy", 50000000) -- start a pure Lua busy loop lane
200 211
201-- hard cancel with kill: the lane thread will be forcefully terminated 212 -- wait 1/3s before cancelling the lane, before the busy loop can finish
202h:cancel( true) 213 print "wait 0.3s"
214 linda:receive( 0.3, "yeah")
203 215
204-- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error 216 -- hard cancel with kill: the lane thread will be forcefully terminated. kill timeout is pthread-specific
205waitCancellation( h, "killed") 217 h:cancel( true, 1.0)
206 218
219 -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error
220 waitCancellation( h, "killed")
221end
207--#################################################################### 222--####################################################################
208 223
209print "\ndone" 224print "\ndone"