diff options
-rw-r--r-- | tests/cancel.lua | 191 |
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 @@ | |||
1 | local which_tests = {} | ||
2 | for k,v in ipairs{...} do | ||
3 | which_tests[v] = true | ||
4 | end | ||
5 | |||
1 | local lanes = require "lanes" .configure{ with_timers = false} | 6 | local lanes = require "lanes" .configure{ with_timers = false} |
2 | 7 | ||
3 | local linda = lanes.linda() | 8 | local linda = lanes.linda() |
4 | |||
5 | --#################################################################### | ||
6 | print "\n\n####################################################################\nbegin genlock & genatomic cancel test\n" | ||
7 | |||
8 | -- get a lock and a atomic operator | ||
9 | local lock = lanes.genlock( linda, "lock", 1) | ||
10 | local atomic = lanes.genatomic( linda, "atomic") | ||
11 | |||
12 | -- check that cancelled lindas give cancel_error as they should | ||
13 | linda:cancel() | ||
14 | assert( linda:get( "empty") == lanes.cancel_error) | ||
15 | assert( lanes.genlock( linda, "any", 1) == lanes.cancel_error) | ||
16 | assert( lanes.genatomic( linda, "any") == lanes.cancel_error) | ||
17 | |||
18 | -- check that lock and atomic functions return cancel_error if the linda was cancelled | ||
19 | assert( lock( 1) == lanes.cancel_error) | ||
20 | assert( lock( -1) == lanes.cancel_error) | ||
21 | assert( atomic( 1) == lanes.cancel_error) | ||
22 | |||
23 | -- reset the linda so that the other tests work | ||
24 | linda:cancel( "none") | ||
25 | linda:limit( "lock", -1) | ||
26 | linda:set( "lock") | ||
27 | linda:limit( "atomic", -1) | ||
28 | linda:set( "atomic") | ||
29 | |||
30 | -- a numeric value to read | 9 | -- a numeric value to read |
31 | linda:set( "val", 33.0) | 10 | linda:set( "val", 33.0) |
32 | 11 | ||
33 | print "test OK" | 12 | --#################################################################### |
13 | if #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" | ||
39 | end | ||
34 | --#################################################################### | 40 | --#################################################################### |
35 | 41 | ||
36 | local waitCancellation = function( h, expected_status) | 42 | local waitCancellation = function( h, expected_status) |
@@ -119,91 +125,100 @@ end | |||
119 | --#################################################################### | 125 | --#################################################################### |
120 | --#################################################################### | 126 | --#################################################################### |
121 | 127 | ||
122 | print "\n\n####################################################################\nbegin linda cancel test\n" | 128 | if #which_tests and which_tests.linda then |
123 | h = 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 | ||
125 | print "wait 1s" | 132 | print "wait 1s" |
126 | linda: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 |
129 | linda:cancel( "both") | 136 | linda:cancel( "both") |
130 | 137 | ||
131 | -- wait until cancellation is effective. | 138 | -- wait until cancellation is effective. |
132 | waitCancellation( 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 |
135 | linda:cancel( "none") | 142 | linda:cancel( "none") |
136 | 143 | end | |
137 | print "\n\n####################################################################\nbegin soft cancel test\n" | ||
138 | h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda | ||
139 | |||
140 | print "wait 1s" | ||
141 | linda:receive( 1, "yeah") | ||
142 | 144 | ||
143 | -- soft cancel, no awakening of waiting linda operations, should timeout | 145 | if #which_tests and which_tests.soft then |
144 | local 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 |
146 | assert( a == false and b == "timeout") | ||
147 | waitCancellation( h, "waiting") | ||
148 | 148 | ||
149 | -- soft cancel, this time awakens waiting linda operations, which returns cancel_error immediately, no timeout. | 149 | print "wait 1s" |
150 | h: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 |
153 | waitCancellation( 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 | ||
157 | print "\n\n####################################################################\nbegin hook cancel test\n" | 161 | -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message |
158 | h = lanes.gen( "*", protectedBody)( "get", 300000) | 162 | waitCancellation( h, "done") |
159 | print "wait 2s" | 163 | end |
160 | linda:receive( 2, "yeah") | ||
161 | 164 | ||
162 | -- count hook cancel after 3 instructions | 165 | if #which_tests and which_tests.hook then |
163 | h: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 |
166 | waitCancellation( h, "cancelled") | 172 | h:cancel( "count", 300, 5.0) |
167 | 173 | ||
168 | print "\n\n####################################################################\nbegin hard cancel test\n" | 174 | -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message |
169 | h = lanes.gen( "*", protectedBody)( "receive", nil) -- infinite timeout | 175 | waitCancellation( h, "cancelled") |
176 | end | ||
170 | 177 | ||
171 | -- wait 2s before cancelling the lane | 178 | if #which_tests and which_tests.hard then |
172 | print "wait 2s" | 179 | print "\n\n####################################################################\nbegin hard cancel test\n" |
173 | linda: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 |
176 | h: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 |
179 | waitCancellation( h, "cancelled") | 187 | h:cancel() |
180 | 188 | ||
181 | print "\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 |
182 | h = lanes.gen( "*", laneBody)( "receive", nil) | 190 | waitCancellation( h, "cancelled") |
191 | end | ||
183 | 192 | ||
184 | -- wait 2s before cancelling the lane | 193 | if #which_tests and which_tests.hard_unprotected then |
185 | print "wait 2s" | 194 | print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" |
186 | linda: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 |
189 | h: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 |
192 | waitCancellation( h, "cancelled") | 202 | h:cancel() |
193 | 203 | ||
194 | print "\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 |
195 | h = lanes.gen( "*", laneBody)( "busy", 50000000) -- start a pure Lua busy loop lane | 205 | waitCancellation( h, "cancelled") |
206 | end | ||
196 | 207 | ||
197 | -- wait 1/3s before cancelling the lane, before the busy loop can finish | 208 | if #which_tests and which_tests.kill then |
198 | print "wait 0.3s" | 209 | print "\n\n####################################################################\nbegin kill cancel test\n" |
199 | linda: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 |
202 | h: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 |
205 | waitCancellation( 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") | ||
221 | end | ||
207 | --#################################################################### | 222 | --#################################################################### |
208 | 223 | ||
209 | print "\ndone" | 224 | print "\ndone" |