From 38465f9a556fd20760821d4c53dc5c1266b5ad09 Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Sat, 29 Apr 2023 22:11:25 -0700 Subject: runtests: spawn a new process for the test runner When the -j option is given, a new process is spawned in which the test programs are run and from which test servers are started. Only one process can be started at once, but this is sufficient to test that the infrastructure can isolate those functions in a new task. There should be no visible difference between the two modes at the moment. Ref: #10818 Closes #11064 --- tests/runner.pm | 92 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 15 deletions(-) (limited to 'tests/runner.pm') diff --git a/tests/runner.pm b/tests/runner.pm index 71e20ce58..8ea50de89 100644 --- a/tests/runner.pm +++ b/tests/runner.pm @@ -22,7 +22,15 @@ # ########################################################################### -# This module contains entry points to run a single test +# This module contains entry points to run a single test. runner_init +# determines whether they will run in a separate process or in the process of +# the caller. The relevant interface is asynchronous so it will work in either +# case. Program arguments are marshalled and then written to the end of a pipe +# (in controlleripccall) which is later read from and the arguments +# unmarshalled (in ipcrecv) before the desired function is called normally. +# The function return values are then marshalled and written into another pipe +# (again in ipcrecv) when is later read from and unmarshalled (in runnerar) +# before being returned to the caller. package runner; @@ -36,6 +44,7 @@ BEGIN { our @EXPORT = qw( checktestcmd prepro + readtestkeywords restore_test_env runner_init runnerac_clearlocks @@ -59,7 +68,6 @@ BEGIN { # these are for debugging only our @EXPORT_OK = qw( - readtestkeywords singletest_preprocess ); } @@ -99,6 +107,7 @@ use testutil qw( ####################################################################### # Global variables set elsewhere but used only by this package +# These may only be set *before* runner_init is called our $DBGCURL=$CURL; #"../src/.libs/curl"; # alternative for debugging our $valgrind_logfile="--log-file"; # the option name for valgrind >=3 our $valgrind_tool="--tool=memcheck"; @@ -121,7 +130,8 @@ my $controllerw; # pipe that controller writes to my $runnerr; # pipe that runner reads from my $runnerw; # pipe that runner writes to my $controllerr; # pipe that controller reads from - +my $multiprocess; # nonzero with a separate test runner process +my $onerunnerid; # a single runner ID # redirected stdout/stderr to these files sub stdoutfilename { @@ -140,12 +150,9 @@ sub stderrfilename { # runnerac_* functions # Called by controller sub runner_init { - my ($logdir)=@_; + my ($logdir, $jobs)=@_; - # Set this directory as ours - # TODO: This will need to be uncommented once there are multiple runners - #$LOGDIR = $logdir; - mkdir("$LOGDIR/$PIDDIR", 0777); + $multiprocess = !!$jobs; # enable memory debugging if curl is compiled with it $ENV{'CURL_MEMDEBUG'} = "$LOGDIR/$MEMDUMP"; @@ -161,8 +168,58 @@ sub runner_init { pipe $runnerr, $controllerw; pipe $controllerr, $runnerw; - # There is only one runner right now - return "singleton"; + if($multiprocess) { + # Create a separate process in multiprocess mode + my $child = fork(); + if(0 == $child) { + # TODO: set up a better signal handler + $SIG{INT} = 'IGNORE'; + $SIG{TERM} = 'IGNORE'; + + $onerunnerid = $$; + print "Runner $onerunnerid starting\n" if($verbose); + + # Here we are the child (runner). + close($controllerw); + close($controllerr); + + # Set this directory as ours + $LOGDIR = $logdir; + mkdir("$LOGDIR/$PIDDIR", 0777); + + # handle IPC calls + event_loop(); + + # Can't rely on logmsg here in case it's buffered + print "Runner $onerunnerid exiting\n" if($verbose); + exit 0; + } + + # Here we are the parent (controller). + close($runnerw); + close($runnerr); + + $onerunnerid = $child; + + } else { + # Create our pid directory + mkdir("$LOGDIR/$PIDDIR", 0777); + + # Don't create a separate process + $onerunnerid = "integrated"; + } + + return $onerunnerid; +} + +####################################################################### +# Loop to execute incoming IPC calls until the shutdown call +sub event_loop { + while () { + if(ipcrecv()) { + last; + } + } } ####################################################################### @@ -979,6 +1036,11 @@ sub runner_test_preprocess { loadtest("${TESTDIR}/test${testnum}"); readtestkeywords(); + ################################################################### + # Restore environment variables that were modified in a previous run. + # Test definition may instruct to (un)set environment vars. + restore_test_env(1); + ################################################################### # Start the servers needed to run this test case my ($why, $error) = singletest_startservers($testnum, \%testtimings); @@ -1115,10 +1177,10 @@ sub controlleripccall { # Send IPC call via pipe syswrite($controllerw, (pack "L", length($margs)) . $margs); - # Call the remote function - # TODO: this will eventually be done in a separate runner process - # kicked off by runner_init() - ipcrecv(); + if(!$multiprocess) { + # Call the remote function here in single process mode + ipcrecv(); + } } ################################################################### @@ -1140,7 +1202,7 @@ sub runnerar { my $resarrayref = thaw $buf; # First argument is runner ID - unshift @$resarrayref, "singleton"; + unshift @$resarrayref, $onerunnerid; return @$resarrayref; } -- cgit v1.2.1