From d8580e14fec64dd5bf3dd492a4811b290cbe4aec Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 3 Dec 2024 17:13:35 +0100 Subject: Internal rework of an enum bad practice usage --- tests/cancel.lua | 112 +++++++++++++++++++++++++++---------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) (limited to 'tests') diff --git a/tests/cancel.lua b/tests/cancel.lua index e65d794..9b65ad3 100644 --- a/tests/cancel.lua +++ b/tests/cancel.lua @@ -24,7 +24,7 @@ end local linda = lanes.linda() -- a numeric value to read -linda:set( "val", 33.0) +linda:set("val", 33.0) -- so that we can easily swap between lanes.gen and lanes.coro, to try stuff local generator = lanes.coro @@ -36,27 +36,27 @@ if not next(which_tests) or which_tests.genlock then print "\n\n####################################################################\nbegin genlock & genatomic cancel test\n" -- get a lock and a atomic operator - local lock = lanes.genlock( linda, "lock", 1) - local atomic = lanes.genatomic( linda, "atomic") + local lock = lanes.genlock(linda, "lock", 1) + local atomic = lanes.genatomic(linda, "atomic") local check_returned_cancel_error = function(_status, _err) assert(_status == nil and _err == lanes.cancel_error) end -- check that cancelled lindas give cancel_error as they should linda:cancel() - check_returned_cancel_error(linda:set( "empty", 42)) - check_returned_cancel_error(linda:get( "empty")) - check_returned_cancel_error(linda:send( "empty", 42)) - check_returned_cancel_error(linda:receive( "empty")) - check_returned_cancel_error(linda:limit( "empty", 5)) - check_returned_cancel_error(linda:restrict( "empty", "set/get")) - assert( lanes.genlock( linda, "any", 1) == lanes.cancel_error) - assert( lanes.genatomic( linda, "any") == lanes.cancel_error) + check_returned_cancel_error(linda:set("empty", 42)) + check_returned_cancel_error(linda:get("empty")) + check_returned_cancel_error(linda:send("empty", 42)) + check_returned_cancel_error(linda:receive("empty")) + check_returned_cancel_error(linda:limit("empty", 5)) + check_returned_cancel_error(linda:restrict("empty", "set/get")) + assert(lanes.genlock(linda, "any", 1) == lanes.cancel_error) + assert(lanes.genatomic(linda, "any") == lanes.cancel_error) -- check that lock and atomic functions return cancel_error if the linda was cancelled - assert( lock( 1) == lanes.cancel_error) - assert( lock( -1) == lanes.cancel_error) - assert( atomic( 1) == lanes.cancel_error) + assert(lock(1) == lanes.cancel_error) + assert(lock(-1) == lanes.cancel_error) + assert(atomic(1) == lanes.cancel_error) -- reset the linda so that the other tests work linda:cancel("none") @@ -70,62 +70,62 @@ end -- ################################################################################################## -local waitCancellation = function( h, expected_status) +local waitCancellation = function(h, expected_status) local l = lanes.linda() if expected_status ~= "running" then repeat - -- print( "lane status:", h.status) + -- print("lane status:", h.status) SLEEP(0.1) -- wait a bit until h.status ~= "running" end - print( "lane status:", h.status) - assert( h.status == expected_status, "lane status " .. h.status .. " (actual) ~= " .. expected_status .. " (expected)") + print("lane status:", h.status) + assert(h.status == expected_status, "lane status " .. h.status .. " (actual) ~= " .. expected_status .. " (expected)") print "test OK" end -local laneBody = function( mode_, payload_) +local laneBody = function(mode_, payload_) local name = "laneBody("..tostring(mode_)..","..tostring(payload_)..")" lane_threadname(name) - set_finalizer( function( err, stk) + set_finalizer(function(err, stk) if err == lanes.cancel_error then -- note that we don't get the cancel_error when running wrapped inside a protected call if it doesn't rethrow it - print( " laneBody after cancel" ) + print(" laneBody after cancel" ) elseif err then - print( " laneBody error: "..tostring(err)) + print(" laneBody error: "..tostring(err)) else print(" laneBody finalized") end end) - print( " entering " , name) + print(" entering " , name) repeat if mode_ == "receive" then -- linda mode - io.stdout:write( " lane calling receive() ... ") - local key, val = linda:receive( payload_, "boob") + io.stdout:write(" lane calling receive() ... ") + local key, val = linda:receive(payload_, "boob") print(tostring(key), val == lanes.cancel_error and "cancel_error" or tostring(val)) if val == lanes.cancel_error then break -- gracefully abort loop end elseif mode_ == "get" then -- busy wait mode getting data from the linda - io.stdout:write( " lane busy waiting ... ") + io.stdout:write(" lane busy waiting ... ") for i = 1, payload_ do -- force a non-jitable call - local _, a = linda:get( "val") + local _, a = linda:get("val") a = a * 2 end - print( "again?") + print("again?") elseif mode_ == "busy" then -- busy wait mode in pure Lua code - io.stdout:write( " lane busy waiting ... ") - local _, a = linda:get( "val") + io.stdout:write(" lane busy waiting ... ") + local _, a = linda:get("val") for i = 1, payload_ do a = a * 2 - a = math.sin( a) * math.sin( a) + math.cos( a) * math.cos( a) -- aka 1 + a = math.sin(a) * math.sin(a) + math.cos(a) * math.cos(a) -- aka 1 end - print( "again?") + print("again?") else error "no mode: raise an error" end @@ -133,23 +133,23 @@ local laneBody = function( mode_, payload_) print " lane shutting down after breaking out of loop" end -local protectedBody = function( ...) +local protectedBody = function(...) local ce = lanes.cancel_error - local errorHandler = function( _msg) + local errorHandler = function(_msg) -- forward the message to the main thread that will display it with a popup - print( " error handler got ", ce == _msg and "cancel_error"or tostring( _msg)) + print(" error handler got ", ce == _msg and "cancel_error" or tostring(_msg)) return _msg end -- Lua 5.1 doesn't pass additional xpcall arguments to the called function -- therefore we need to create a closure that has no arguments but pulls everything from its upvalue local params = {...} local unpack = table.unpack or unpack -- unpack for 5.1, table.unpack for 5.2+ - local paramLessClosure = function() laneBody(unpack( params)) end - local status, message = xpcall( paramLessClosure, errorHandler) + local paramLessClosure = function() laneBody(unpack(params)) end + local status, message = xpcall(paramLessClosure, errorHandler) if status == false then - print( " error handler rethrowing '" .. (ce == message and "cancel_error"or tostring( message)) .. "'") + print(" error handler rethrowing '" .. (ce == message and "cancel_error"or tostring(message)) .. "'") -- if the error isn't rethrown, the lane's finalizer won't get it - error( message) + error(message) end end @@ -159,20 +159,20 @@ end if not next(which_tests) or which_tests.linda then remaining_tests.linda = nil print "\n\n####################################################################\nbegin linda cancel test\n" - h = generator( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda + h = generator("*", laneBody)("receive", nil) -- start an infinite wait on the linda print "wait 1s" SLEEP(1) -- linda cancel: linda:receive() returns nil,cancel_error immediately print "cancelling" - linda:cancel( "both") + linda:cancel("both") -- wait until cancellation is effective. - waitCancellation( h, "done") + waitCancellation(h, "done") -- reset the linda so that the other tests work - linda:cancel( "none") + linda:cancel("none") end -- ################################################################################################## @@ -180,23 +180,23 @@ end if not next(which_tests) or which_tests.soft then remaining_tests.soft = nil print "\n\n####################################################################\nbegin soft cancel test\n" - h = generator( "*", protectedBody)( "receive") -- start an infinite wait on the linda + h = generator("*", protectedBody)("receive") -- start an infinite wait on the linda print "wait 1s" SLEEP(1) -- soft cancel, no awakening of waiting linda operations, should timeout - local a, b = h:cancel( "soft", 1, false) + local a, b = h:cancel("soft", 1, false) -- cancellation should fail as the lane is still waiting on its linda - assert( a == false and b == "timeout") - waitCancellation( h, "waiting") + assert(a == false and b == "timeout") + waitCancellation(h, "waiting") -- soft cancel, this time awakens waiting linda operations, which returns cancel_error immediately, no timeout. print "cancelling" - h:cancel( "soft", true) + h:cancel("soft", true) -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message - waitCancellation( h, "done") + waitCancellation(h, "done") end -- ################################################################################################## @@ -204,16 +204,16 @@ end if not next(which_tests) or which_tests.hook then remaining_tests.hook = nil print "\n\n####################################################################\nbegin hook cancel test\n" - h = generator( "*", protectedBody)( "get", 300000) + h = generator("*", protectedBody)("get", 300000) print "wait 2s" SLEEP(2) -- count hook cancel after some instruction instructions print "cancelling" - h:cancel( "line", 300, 5.0) + h:cancel("line", 300, 5.0) -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message - waitCancellation( h, "cancelled") + waitCancellation(h, "cancelled") end -- ################################################################################################## @@ -221,7 +221,7 @@ end if not next(which_tests) or which_tests.hard then remaining_tests.hard = nil print "\n\n####################################################################\nbegin hard cancel test\n" - h = lanes.gen( "*", protectedBody)( "receive", nil) -- infinite timeout + h = lanes.gen("*", protectedBody)("receive", nil) -- infinite timeout -- wait 2s before cancelling the lane print "wait 2s" @@ -232,7 +232,7 @@ if not next(which_tests) or which_tests.hard then h:cancel() -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error - waitCancellation( h, "cancelled") + waitCancellation(h, "cancelled") end -- ################################################################################################## @@ -240,7 +240,7 @@ end if not next(which_tests) or which_tests.hard_unprotected then remaining_tests.hard_unprotected = nil print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" - h = generator( "*", laneBody)( "receive", nil) + h = generator("*", laneBody)("receive", nil) -- wait 2s before cancelling the lane print "wait 2s" @@ -251,7 +251,7 @@ if not next(which_tests) or which_tests.hard_unprotected then h:cancel() -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error - waitCancellation( h, "cancelled") + waitCancellation(h, "cancelled") end -- ################################################################################################## -- cgit v1.2.3-55-g6feb