You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by jm...@apache.org on 2007/03/14 16:38:33 UTC

svn commit: r518187 - in /spamassassin/trunk/t: SATest.pm spamd_hup.t spamd_kill_restart.t spamd_kill_restart_rr.t

Author: jm
Date: Wed Mar 14 08:38:32 2007
New Revision: 518187

URL: http://svn.apache.org/viewvc?view=rev&rev=518187
Log:
refactor some shared code from spamd_hup.t, spamd_kill_restart.t and spamd_kill_restart_rr.t into SATest.pm; add new more-raceproof spamd_hup logic to other two tests, too

Modified:
    spamassassin/trunk/t/SATest.pm
    spamassassin/trunk/t/spamd_hup.t
    spamassassin/trunk/t/spamd_kill_restart.t
    spamassassin/trunk/t/spamd_kill_restart_rr.t

Modified: spamassassin/trunk/t/SATest.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/SATest.pm?view=diff&rev=518187&r1=518186&r2=518187
==============================================================================
--- spamassassin/trunk/t/SATest.pm (original)
+++ spamassassin/trunk/t/SATest.pm Wed Mar 14 08:38:32 2007
@@ -757,4 +757,74 @@
   }
 }
 
+sub wait_for_file_to_change_or_disappear {
+  my ($f, $timeout, $action) = @_;
+
+  my $lastmod = (-M $f);
+
+  $action->();
+
+  my $timeout = 20;
+  my $wait = 0;
+  my $newlastmod;
+  do {
+    sleep (int($wait++ / 4) + 1) if $timeout > 0;
+    $timeout--;
+    $newlastmod = (-M $f);
+  } while((-e $f) && defined($newlastmod) &&
+                $newlastmod == $lastmod && $timeout);
+}
+
+sub wait_for_file_to_appear {
+  my ($f, $timeout) = @_;
+
+  # note that the wait period increases the longer it takes,
+  # 20 retries works out to a total of 60 seconds
+  my $timeout = 20;
+  my $wait = 0;
+  do {
+    sleep (int($wait++ / 4) + 1) if $timeout > 0;
+    $timeout--;
+  } while((!-e $f || -z $f) && $timeout);
+}
+
+sub read_from_pidfile {
+  my $f = shift;
+  my $npid = 0;
+  my $retries = 5;
+
+  do {
+    if ($retries != 5) {
+      sleep 1;
+      warn "retrying read of pidfile $f, due to short/nonexistent read: ".
+            "retry $retries";
+    }
+    $retries--;
+
+    if (!open (PID, "<".$f)) {
+      warn "Could not open pid file ${f}: $!\n";     # and retry
+      next;
+    }
+
+    $npid = <PID>;
+    if (defined $npid) { chomp $npid; }
+    close(PID);
+
+    if (!$npid || $npid < 1) {
+      warn "failed to read anything sensible from $f, retrying read";
+      $npid = 0;
+      next;
+    }
+    if (!kill (0, $npid)) {
+      warn "failed to kill -0 $npid, retrying read";
+      $npid = 0;
+    }
+
+  } until ($npid > 1 or $retries == 0);
+
+  return $npid;
+}
+
+sub dbgprint { print STDOUT "[".time()."] ".$_[0]; }
+
 1;

Modified: spamassassin/trunk/t/spamd_hup.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/spamd_hup.t?view=diff&rev=518187&r1=518186&r2=518187
==============================================================================
--- spamassassin/trunk/t/spamd_hup.t (original)
+++ spamassassin/trunk/t/spamd_hup.t Wed Mar 14 08:38:32 2007
@@ -13,62 +13,42 @@
 # ---------------------------------------------------------------------------
 
 my $pid_file = "log/spamd.pid";
-
 my($pid1, $pid2);
 
-print "[".time."] Starting spamd...\n";
+dbgprint "Starting spamd...\n";
 start_spamd("-L -r ${pid_file}");
 sleep 1;
 
 for $retry (0 .. 9) {
-  ok ($pid1 = get_pid());
-  print "[".time."] HUPing spamd at pid $pid1, loop try $retry...\n";
+  ok ($pid1 = read_from_pidfile($pid_file));
   ok (-e $pid_file) or warn "$pid_file is not there before SIGHUP";
   ok (!-z $pid_file) or warn "$pid_file is empty before SIGHUP";
-
-  my $lastmod = (-M $pid_file);
-  ok ($pid1 != 0 and kill ('HUP', $pid1));
+  ok ($pid1 != 0);
+  dbgprint "HUPing spamd at pid $pid1, loop try $retry...\n";
 
   # now, wait for the PID file to change or disappear; the real order
   # is [SIGHUP, unlink, exec, create] but due to race conditions under
   # load we could have missed the unlink, exec, create part.
 
-  print "[".time."] Waiting for PID file to change...\n";
-  {
-    my $timeout = 20;
-    my $wait = 0;
-    my $newlastmod;
-    do {
-      sleep (int($wait++ / 4) + 1) if $timeout > 0;
-      $timeout--;
-      $newlastmod = (-M $pid_file);
-    } while((-e $pid_file) && defined($newlastmod) &&
-                  $newlastmod == $lastmod && $timeout);
-  }
-
-  print "[".time."] Waiting for spamd at pid $pid1 to restart...\n";
-  # note that the wait period increases the longer it takes,
-  # 20 retries works out to a total of 60 seconds
-  {
-    my $timeout = 20;
-    my $wait = 0;
-    do {
-      sleep (int($wait++ / 4) + 1) if $timeout > 0;
-      $timeout--;
-    } while((!-e $pid_file || -z $pid_file) && $timeout);
-  }
+  dbgprint "Waiting for PID file to change...\n";
+  wait_for_file_to_change_or_disappear($pid_file, 20, sub {
+          $pid1 and kill ('HUP', $pid1);
+        });
+
+  dbgprint "Waiting for spamd at pid $pid1 to restart...\n";
+  wait_for_file_to_appear ($pid_file, 20);
   ok (-e $pid_file) or warn "$pid_file does not exist post restart";
   ok (!-z $pid_file) or warn "$pid_file is empty post restart";
 
-  ok ($pid2 = get_pid($pid1));
-  print "[".time."] Looking for new spamd at pid $pid2...\n";
+  ok ($pid2 = read_from_pidfile($pid_file));
+  dbgprint "Looking for new spamd at pid $pid2...\n";
   #ok ($pid2 != $pid1);     # no longer guaranteed with SIGHUP
   ok ($pid2 != 0 and kill (0, $pid2));
 
-  print "[".time."] A little time to settle...\n";
+  dbgprint "A little time to settle...\n";
   sleep 2;
 
-  print "[".time."] Checking GTUBE...\n";
+  dbgprint "Checking GTUBE...\n";
   %patterns = (
     q{ X-Spam-Flag: YES } => 'flag',
     q{ GTUBE }            => 'gtube',
@@ -80,43 +60,6 @@
 }
 
 
-print "[".time."] Stopping spamd...\n";
+dbgprint "Stopping spamd...\n";
 stop_spamd;
-
-
-sub get_pid {
-  my($opid, $npid) = (@_, 0, 0);
-
-  my $retries = 5;
-  do {
-    if ($retries != 5) {
-      sleep 1;
-      warn "retrying read of pidfile $pid_file, due to short/nonexistent read: ".
-            "retry $retries";
-    }
-    $retries--;
-
-    if (!open (PID, "<".$pid_file)) {
-      warn "Could not open pid file ${pid_file}: $!\n";     # and retry
-      next;
-    }
-
-    $npid = <PID>;
-    if (defined $npid) { chomp $npid; }
-    close(PID);
-
-    if (!$npid || $npid < 1) {
-      warn "failed to read anything sensible from $pid_file, retrying read";
-      $npid = 0;
-      next;
-    }
-    if (!kill (0, $npid)) {
-      warn "failed to kill -0 $npid, retrying read";
-      $npid = 0;
-    }
-
-  } until ($npid > 1 or $retries == 0);
-
-  return $npid;
-}
 

Modified: spamassassin/trunk/t/spamd_kill_restart.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/spamd_kill_restart.t?view=diff&rev=518187&r1=518186&r2=518187
==============================================================================
--- spamassassin/trunk/t/spamd_kill_restart.t (original)
+++ spamassassin/trunk/t/spamd_kill_restart.t Wed Mar 14 08:38:32 2007
@@ -4,7 +4,7 @@
 use SATest; sa_t_init("spamd_kill_restart");
 use constant TEST_ENABLED => !$SKIP_SPAMD_TESTS && !$RUNNING_ON_WINDOWS;
 
-use Test; BEGIN { plan tests => (TEST_ENABLED? 73 : 0) };
+use Test; BEGIN { plan tests => (TEST_ENABLED? 93 : 0) };
 
 use File::Spec;
 
@@ -13,59 +13,53 @@
 # ---------------------------------------------------------------------------
 
 my $pid_file = "log/spamd.pid";
-
 my($pid1, $pid2);
 
-sub dbgprint { print STDOUT "[".time()."] ".$_[0]; }
-
 dbgprint "Starting spamd...\n";
 start_spamd("-L -r ${pid_file}");
 sleep 1;
 
 for $retry (0 .. 9) {
-  ok ($pid1 = get_pid());
+  ok ($pid1 = read_from_pidfile($pid_file));
+  ok (-e $pid_file) or warn "$pid_file is not there before SIGINT";
+  ok (!-z $pid_file) or warn "$pid_file is empty before SIGINT";
+  ok ($pid1 != 0);
   dbgprint "killing spamd at pid $pid1, loop try $retry...\n";
-  ok ($pid1 != 0 and kill ('INT', $pid1));
 
-# ensure we wait for the exit to happen; under load, we could
-# still be waiting at this point for the spamd to receive the
-# signal
-
-  dbgprint "Waiting for spamd at pid $pid1 to exit...\n";
-  my $timeout = 20;
-  do {
-    # no increase in the timeout here
-    sleep (1) if $timeout > 0;
-    $timeout--;
-  } while(-e $pid_file && $timeout);
+  # now, wait for the PID file to change or disappear; the real order
+  # is [SIGINT, unlink, exec, create] but due to race conditions under
+  # load we could have missed the unlink, exec, create part.
+
+  dbgprint "Waiting for PID file to change...\n";
+  wait_for_file_to_change_or_disappear($pid_file, 20, sub {
+          $pid1 and kill ('INT', $pid1);
+        });
+
+  # in the SIGINT case, the file will not change -- it will be unlinked
   ok (!-e $pid_file);
 
-# override these so the old logs are still visible and the new
-# spamd will be started even though stop_spamd() was not called
+  # override this so the old logs are still visible and the new
+  # spamd will be started even though stop_spamd() was not called
   $spamd_pid = 0;
 
   dbgprint "starting new spamd, loop try $retry...\n";
   start_spamd("-D -L -r ${pid_file}");
-  ok ($pid1 = get_pid());
 
   dbgprint "Waiting for spamd at pid $pid1 to restart...\n";
-# note that the wait period increases the longer it takes,
-# 20 retries works out to a total of 60 seconds
-  $timeout = 20;
-  my $wait = 0;
-  do {
-    sleep (int($wait++ / 4) + 1) if $timeout > 0;
-    $timeout--;
-  } while(!-e $pid_file && $timeout);
-  ok (-e $pid_file);
+  wait_for_file_to_appear ($pid_file, 20);
+  ok (-e $pid_file) or warn "$pid_file does not exist post restart";
+  ok (!-z $pid_file) or warn "$pid_file is empty post restart";
+  ok ($pid2 = read_from_pidfile($pid_file));
 
-  ok ($pid2 = get_pid($pid1));
   dbgprint "Looking for new spamd at pid $pid2...\n";
   ok ($pid2 != 0 and kill (0, $pid2));
 
   $pid1 = $pid2;
 }
 
+  dbgprint "A little time to settle...\n";
+  sleep 2;
+
   dbgprint "Checking GTUBE...\n";
   %patterns = (
     q{ X-Spam-Flag: YES } => 'flag',
@@ -78,22 +72,4 @@
 dbgprint "Stopping spamd...\n";
 stop_spamd;
 
-
-sub get_pid {
-  my($opid, $npid) = (@_, 0, 0);
-  #my $timeout = 5;
-  #do {
-  #  sleep 1;
-  #  $timeout--;
-
-    if (open (PID, "< ${pid_file}")) {
-      $npid = <PID>;
-      chomp $npid;
-      close(PID);
-    } else {
-      die "Could not open pid file ${pid_file}: $!\n";
-    }
-  #} until ($npid != $opid or $timeout == 0);
-  return $npid;
-}
 

Modified: spamassassin/trunk/t/spamd_kill_restart_rr.t
URL: http://svn.apache.org/viewvc/spamassassin/trunk/t/spamd_kill_restart_rr.t?view=diff&rev=518187&r1=518186&r2=518187
==============================================================================
--- spamassassin/trunk/t/spamd_kill_restart_rr.t (original)
+++ spamassassin/trunk/t/spamd_kill_restart_rr.t Wed Mar 14 08:38:32 2007
@@ -4,7 +4,7 @@
 use SATest; sa_t_init("spamd_kill_restart_rr");
 use constant TEST_ENABLED => !$SKIP_SPAMD_TESTS && !$RUNNING_ON_WINDOWS;
 
-use Test; BEGIN { plan tests => (TEST_ENABLED? 73 : 0) };
+use Test; BEGIN { plan tests => (TEST_ENABLED? 93 : 0) };
 
 use File::Spec;
 
@@ -13,59 +13,53 @@
 # ---------------------------------------------------------------------------
 
 my $pid_file = "log/spamd.pid";
-
 my($pid1, $pid2);
 
-sub dbgprint { print STDOUT "[".time()."] ".$_[0]; }
-
 dbgprint "Starting spamd...\n";
 start_spamd("-L --round-robin -r ${pid_file}");
 sleep 1;
 
 for $retry (0 .. 9) {
-  ok ($pid1 = get_pid());
+  ok ($pid1 = read_from_pidfile($pid_file));
+  ok (-e $pid_file) or warn "$pid_file is not there before SIGINT";
+  ok (!-z $pid_file) or warn "$pid_file is empty before SIGINT";
+  ok ($pid1 != 0);
   dbgprint "killing spamd at pid $pid1, loop try $retry...\n";
-  ok ($pid1 != 0 and kill ('INT', $pid1));
 
-# ensure we wait for the exit to happen; under load, we could
-# still be waiting at this point for the spamd to receive the
-# signal
-
-  dbgprint "Waiting for spamd at pid $pid1 to exit...\n";
-  my $timeout = 20;
-  do {
-    # no increase in the timeout here
-    sleep (1) if $timeout > 0;
-    $timeout--;
-  } while(-e $pid_file && $timeout);
+  # now, wait for the PID file to change or disappear; the real order
+  # is [SIGINT, unlink, exec, create] but due to race conditions under
+  # load we could have missed the unlink, exec, create part.
+
+  dbgprint "Waiting for PID file to change...\n";
+  wait_for_file_to_change_or_disappear($pid_file, 20, sub {
+          $pid1 and kill ('INT', $pid1);
+        });
+
+  # in the SIGINT case, the file will not change -- it will be unlinked
   ok (!-e $pid_file);
 
-# override these so the old logs are still visible and the new
-# spamd will be started even though stop_spamd() was not called
+  # override this so the old logs are still visible and the new
+  # spamd will be started even though stop_spamd() was not called
   $spamd_pid = 0;
 
   dbgprint "starting new spamd, loop try $retry...\n";
   start_spamd("-D -L --round-robin -r ${pid_file}");
-  ok ($pid1 = get_pid());
 
   dbgprint "Waiting for spamd at pid $pid1 to restart...\n";
-# note that the wait period increases the longer it takes,
-# 20 retries works out to a total of 60 seconds
-  $timeout = 20;
-  my $wait = 0;
-  do {
-    sleep (int($wait++ / 4) + 1) if $timeout > 0;
-    $timeout--;
-  } while(!-e $pid_file && $timeout);
-  ok (-e $pid_file);
+  wait_for_file_to_appear ($pid_file, 20);
+  ok (-e $pid_file) or warn "$pid_file does not exist post restart";
+  ok (!-z $pid_file) or warn "$pid_file is empty post restart";
+  ok ($pid2 = read_from_pidfile($pid_file));
 
-  ok ($pid2 = get_pid($pid1));
   dbgprint "Looking for new spamd at pid $pid2...\n";
   ok ($pid2 != 0 and kill (0, $pid2));
 
   $pid1 = $pid2;
 }
 
+  dbgprint "A little time to settle...\n";
+  sleep 2;
+
   dbgprint "Checking GTUBE...\n";
   %patterns = (
     q{ X-Spam-Flag: YES } => 'flag',
@@ -78,22 +72,4 @@
 dbgprint "Stopping spamd...\n";
 stop_spamd;
 
-
-sub get_pid {
-  my($opid, $npid) = (@_, 0, 0);
-  #my $timeout = 5;
-  #do {
-  #  sleep 1;
-  #  $timeout--;
-
-    if (open (PID, "< ${pid_file}")) {
-      $npid = <PID>;
-      chomp $npid;
-      close(PID);
-    } else {
-      die "Could not open pid file ${pid_file}: $!\n";
-    }
-  #} until ($npid != $opid or $timeout == 0);
-  return $npid;
-}