From e8d818bf0da2b1b2f296e17112cafe8b9f6f4d8a Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 28 Mar 2024 11:42:21 +0100 Subject: tweak cancel.lua test file to be able to cherry-pick tests with command line arguments --- tests/cancel.lua | 191 ++++++++++++++++++++++++++++++------------------------- 1 file 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 @@ +local which_tests = {} +for k,v in ipairs{...} do + which_tests[v] = true +end + local lanes = require "lanes" .configure{ with_timers = false} local linda = lanes.linda() - ---#################################################################### -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") - --- check that cancelled lindas give cancel_error as they should -linda:cancel() -assert( linda:get( "empty") == lanes.cancel_error) -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) - --- reset the linda so that the other tests work -linda:cancel( "none") -linda:limit( "lock", -1) -linda:set( "lock") -linda:limit( "atomic", -1) -linda:set( "atomic") - -- a numeric value to read linda:set( "val", 33.0) -print "test OK" +--#################################################################### +if #which_tests and 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") + + -- check that cancelled lindas give cancel_error as they should + linda:cancel() + assert( linda:get( "empty") == lanes.cancel_error) + 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) + + -- reset the linda so that the other tests work + linda:cancel( "none") + linda:limit( "lock", -1) + linda:set( "lock") + linda:limit( "atomic", -1) + linda:set( "atomic") + + print "test OK" +end --#################################################################### local waitCancellation = function( h, expected_status) @@ -119,91 +125,100 @@ end --#################################################################### --#################################################################### -print "\n\n####################################################################\nbegin linda cancel test\n" -h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda +if #which_tests and which_tests.linda then + print "\n\n####################################################################\nbegin linda cancel test\n" + h = lanes.gen( "*", laneBody)( "receive", nil) -- start an infinite wait on the linda -print "wait 1s" -linda:receive( 1, "yeah") + print "wait 1s" + linda:receive( 1, "yeah") --- linda cancel: linda:receive() returns cancel_error immediately -linda:cancel( "both") + -- linda cancel: linda:receive() returns cancel_error immediately + linda:cancel( "both") --- wait until cancellation is effective. -waitCancellation( h, "done") + -- wait until cancellation is effective. + waitCancellation( h, "done") --- reset the linda so that the other tests work -linda:cancel( "none") - -print "\n\n####################################################################\nbegin soft cancel test\n" -h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda - -print "wait 1s" -linda:receive( 1, "yeah") + -- reset the linda so that the other tests work + linda:cancel( "none") +end --- soft cancel, no awakening of waiting linda operations, should timeout -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") +if #which_tests and which_tests.soft then + print "\n\n####################################################################\nbegin soft cancel test\n" + h = lanes.gen( "*", protectedBody)( "receive") -- start an infinite wait on the linda --- soft cancel, this time awakens waiting linda operations, which returns cancel_error immediately, no timeout. -h:cancel( "soft", true) + print "wait 1s" + linda:receive( 1, "yeah") --- wait until cancellation is effective. the lane will interrupt its loop and print the exit message -waitCancellation( h, "done") + -- soft cancel, no awakening of waiting linda operations, should timeout + 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") --- do return end + -- soft cancel, this time awakens waiting linda operations, which returns cancel_error immediately, no timeout. + h:cancel( "soft", true) -print "\n\n####################################################################\nbegin hook cancel test\n" -h = lanes.gen( "*", protectedBody)( "get", 300000) -print "wait 2s" -linda:receive( 2, "yeah") + -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message + waitCancellation( h, "done") +end --- count hook cancel after 3 instructions -h:cancel( "count", 300, 5.0) +if #which_tests and which_tests.hook then + print "\n\n####################################################################\nbegin hook cancel test\n" + h = lanes.gen( "*", protectedBody)( "get", 300000) + print "wait 2s" + linda:receive( 2, "yeah") --- wait until cancellation is effective. the lane will interrupt its loop and print the exit message -waitCancellation( h, "cancelled") + -- count hook cancel after some instruction instructions + h:cancel( "count", 300, 5.0) -print "\n\n####################################################################\nbegin hard cancel test\n" -h = lanes.gen( "*", protectedBody)( "receive", nil) -- infinite timeout + -- wait until cancellation is effective. the lane will interrupt its loop and print the exit message + waitCancellation( h, "cancelled") +end --- wait 2s before cancelling the lane -print "wait 2s" -linda:receive( 2, "yeah") +if #which_tests and which_tests.hard then + print "\n\n####################################################################\nbegin hard cancel test\n" + h = lanes.gen( "*", protectedBody)( "receive", nil) -- infinite timeout --- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it -h:cancel() + -- wait 2s before cancelling the lane + print "wait 2s" + linda:receive( 2, "yeah") --- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error -waitCancellation( h, "cancelled") + -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it + h:cancel() -print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" -h = lanes.gen( "*", laneBody)( "receive", nil) + -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error + waitCancellation( h, "cancelled") +end --- wait 2s before cancelling the lane -print "wait 2s" -linda:receive( 2, "yeah") +if #which_tests and which_tests.hard_unprotected then + print "\n\n####################################################################\nbegin hard cancel test with unprotected lane body\n" + h = lanes.gen( "*", laneBody)( "receive", nil) --- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it -h:cancel() + -- wait 2s before cancelling the lane + print "wait 2s" + linda:receive( 2, "yeah") --- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error -waitCancellation( h, "cancelled") + -- hard cancel: the lane will be interrupted from inside its current linda:receive() and won't return from it + h:cancel() -print "\n\n####################################################################\nbegin kill cancel test\n" -h = lanes.gen( "*", laneBody)( "busy", 50000000) -- start a pure Lua busy loop lane + -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error + waitCancellation( h, "cancelled") +end --- wait 1/3s before cancelling the lane, before the busy loop can finish -print "wait 0.3s" -linda:receive( 0.3, "yeah") +if #which_tests and which_tests.kill then + print "\n\n####################################################################\nbegin kill cancel test\n" + h = lanes.gen( "*", laneBody)( "busy", 50000000) -- start a pure Lua busy loop lane --- hard cancel with kill: the lane thread will be forcefully terminated -h:cancel( true) + -- wait 1/3s before cancelling the lane, before the busy loop can finish + print "wait 0.3s" + linda:receive( 0.3, "yeah") --- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error -waitCancellation( h, "killed") + -- hard cancel with kill: the lane thread will be forcefully terminated. kill timeout is pthread-specific + h:cancel( true, 1.0) + -- wait until cancellation is effective. the lane will be stopped by the linda operation throwing an error + waitCancellation( h, "killed") +end --#################################################################### print "\ndone" -- cgit v1.2.3-55-g6feb