1#!/usr/bin/perl -T
2
3use lib '.'; use lib 't';
4use SATest; sa_t_init("spamd_kill_restart_rr");
5
6use Test::More;
7plan skip_all => "Spamd tests disabled" if $SKIP_SPAMD_TESTS;
8plan skip_all => "Long running tests disabled" unless conf_bool('run_long_tests');
9plan skip_all => "Tests don't work on windows" if $RUNNING_ON_WINDOWS;
10plan tests => 93;
11
12use File::Spec;
13
14# ---------------------------------------------------------------------------
15
16my $pid_file = "log/spamd.pid";
17my($pid1, $pid2);
18
19tstlocalrules("
20      use_auto_whitelist 0
21  ");
22
23dbgprint "Starting spamd...\n";
24start_spamd("-L --round-robin -r ${pid_file}");
25sleep 1;
26
27for $retry (0 .. 9) {
28  ok ($pid1 = read_from_pidfile($pid_file));
29  ok (-e $pid_file) or warn "$pid_file is not there before SIGINT";
30  ok (!-z $pid_file) or warn "$pid_file is empty before SIGINT";
31  ok ($pid1 != 0);
32  dbgprint "killing spamd at pid $pid1, loop try $retry...\n";
33
34  # now, wait for the PID file to change or disappear; the real order
35  # is [SIGINT, unlink, exec, create] but due to race conditions under
36  # load we could have missed the unlink, exec, create part.
37
38  dbgprint "Waiting for PID file to change...\n";
39  wait_for_file_to_change_or_disappear($pid_file, 20, sub {
40          $pid1 and kill ('INT', $pid1);
41        });
42
43  # in the SIGINT case, the file will not change -- it will be unlinked
44  ok (!-e $pid_file);
45
46  # override this so the old logs are still visible and the new
47  # spamd will be started even though stop_spamd() was not called
48  $spamd_pid = 0;
49
50  dbgprint "starting new spamd, loop try $retry...\n";
51  my $startat = time;
52  start_spamd("-D -L --round-robin -r ${pid_file}");
53
54  dbgprint "Waiting for spamd at pid $pid1 to restart...\n";
55  wait_for_file_to_appear ($pid_file, 40);
56  ok (-e $pid_file) or warn "$pid_file does not exist post restart; started at $startat, gave up at ".time;
57
58  ok (!-z $pid_file) or warn "$pid_file is empty post restart";
59  ok ($pid2 = read_from_pidfile($pid_file));
60
61  dbgprint "Looking for new spamd at pid $pid2...\n";
62  ok ($pid2 != 0 and kill (0, $pid2));
63
64  $pid1 = $pid2;
65}
66
67  dbgprint "A little time to settle...\n";
68  sleep 2;
69
70  dbgprint "Checking GTUBE...\n";
71  %patterns = (
72    q{ X-Spam-Flag: YES } => 'flag',
73    q{ GTUBE }            => 'gtube',
74  );
75  ok (spamcrun ("< data/spam/gtube.eml", \&patterns_run_cb));
76  ok_all_patterns;
77
78
79dbgprint "Stopping spamd...\n";
80stop_spamd;
81
82
83