diff options
Diffstat (limited to 'tests/cancel.lua')
-rw-r--r-- | tests/cancel.lua | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/tests/cancel.lua b/tests/cancel.lua index e39ad98..b75d085 100644 --- a/tests/cancel.lua +++ b/tests/cancel.lua | |||
@@ -9,6 +9,11 @@ end | |||
9 | 9 | ||
10 | local lanes = require "lanes" .configure{ with_timers = false} | 10 | local lanes = require "lanes" .configure{ with_timers = false} |
11 | 11 | ||
12 | local SLEEP = function(...) | ||
13 | local k, v = lanes.sleep(...) | ||
14 | assert(k == nil and v == "timeout") | ||
15 | end | ||
16 | |||
12 | local linda = lanes.linda() | 17 | local linda = lanes.linda() |
13 | -- a numeric value to read | 18 | -- a numeric value to read |
14 | linda:set( "val", 33.0) | 19 | linda:set( "val", 33.0) |
@@ -51,7 +56,7 @@ local waitCancellation = function( h, expected_status) | |||
51 | if expected_status ~= "running" then | 56 | if expected_status ~= "running" then |
52 | repeat | 57 | repeat |
53 | -- print( "lane status:", h.status) | 58 | -- print( "lane status:", h.status) |
54 | l:receive( 0.1, "yeah") -- wait a bit | 59 | SLEEP(0.1) -- wait a bit |
55 | until h.status ~= "running" | 60 | until h.status ~= "running" |
56 | end | 61 | end |
57 | print( "lane status:", h.status) | 62 | print( "lane status:", h.status) |
@@ -80,8 +85,8 @@ local laneBody = function( mode_, payload_) | |||
80 | -- linda mode | 85 | -- linda mode |
81 | io.stdout:write( " lane calling receive() ... ") | 86 | io.stdout:write( " lane calling receive() ... ") |
82 | local key, val = linda:receive( payload_, "boob") | 87 | local key, val = linda:receive( payload_, "boob") |
83 | print( lanes.cancel_error == key and "cancel_error" or tostring( key), tostring( val)) | 88 | print(tostring(key), val == lanes.cancel_error and "cancel_error" or tostring(val)) |
84 | if key == lanes.cancel_error then | 89 | if val == lanes.cancel_error then |
85 | break -- gracefully abort loop | 90 | break -- gracefully abort loop |
86 | end | 91 | end |
87 | elseif mode_ == "get" then | 92 | elseif mode_ == "get" then |
@@ -138,9 +143,9 @@ if not next(which_tests) or which_tests.linda then | |||
138 | h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda | 143 | h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda |
139 | 144 | ||
140 | print "wait 1s" | 145 | print "wait 1s" |
141 | linda:receive( 1, "yeah") | 146 | SLEEP(1) |
142 | 147 | ||
143 | -- linda cancel: linda:receive() returns cancel_error immediately | 148 | -- linda cancel: linda:receive() returns nil,cancel_error immediately |
144 | print "cancelling" | 149 | print "cancelling" |
145 | linda:cancel( "both") | 150 | linda:cancel( "both") |
146 | 151 | ||
@@ -159,7 +164,7 @@ if not next(which_tests) or which_tests.soft then | |||
159 | h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda | 164 | h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda |
160 | 165 | ||
161 | print "wait 1s" | 166 | print "wait 1s" |
162 | linda:receive( 1, "yeah") | 167 | SLEEP(1) |
163 | 168 | ||
164 | -- soft cancel, no awakening of waiting linda operations, should timeout | 169 | -- soft cancel, no awakening of waiting linda operations, should timeout |
165 | local a, b = h:cancel( "soft", 1, false) | 170 | local a, b = h:cancel( "soft", 1, false) |
@@ -182,7 +187,7 @@ if not next(which_tests) or which_tests.hook then | |||
182 | print "\n\n####################################################################\nbegin hook cancel test\n" | 187 | print "\n\n####################################################################\nbegin hook cancel test\n" |
183 | h = lanes.gen( "*", protectedBody)( "get", 300000) | 188 | h = lanes.gen( "*", protectedBody)( "get", 300000) |
184 | print "wait 2s" | 189 | print "wait 2s" |
185 | linda:receive( 2, "yeah") | 190 | SLEEP(2) |
186 | 191 | ||
187 | -- count hook cancel after some instruction instructions | 192 | -- count hook cancel after some instruction instructions |
188 | print "cancelling" | 193 | print "cancelling" |
@@ -201,7 +206,7 @@ if not next(which_tests) or which_tests.hard then | |||
201 | 206 | ||
202 | -- wait 2s before cancelling the lane | 207 | -- wait 2s before cancelling the lane |
203 | print "wait 2s" | 208 | print "wait 2s" |
204 | linda:receive( 2, "yeah") | 209 | SLEEP(2) |
205 | 210 | ||
206 | -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it | 211 | -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it |
207 | print "cancelling" | 212 | print "cancelling" |
@@ -220,7 +225,7 @@ if not next(which_tests) or which_tests.hard_unprotected then | |||
220 | 225 | ||
221 | -- wait 2s before cancelling the lane | 226 | -- wait 2s before cancelling the lane |
222 | print "wait 2s" | 227 | print "wait 2s" |
223 | linda:receive( 2, "yeah") | 228 | SLEEP(2) |
224 | 229 | ||
225 | -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it | 230 | -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it |
226 | print "cancelling" | 231 | print "cancelling" |