2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 require_mods(qw(json DBD::SQLite Search::Xapian));
10 my ($home, $for_destroy) = tmpdir();
11 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 require_mods('IO::FDPass', 51);
15 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
16 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
17 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
19 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
20 is($err, '', 'no error from daemon-pid');
21 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
22 chomp(my $pid = $out);
23 ok(kill(0, $pid), 'pid is valid');
24 ok(-S $sock, 'sock created');
26 ok(!run_script([qw(lei)], undef, $opt), 'no args fails');
27 is($? >> 8, 1, '$? is 1');
28 is($out, '', 'nothing in stdout');
29 like($err, qr/^usage:/sm, 'usage in stderr');
31 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
33 ok(run_script(['lei', @$arg], undef, $opt), "lei @$arg");
34 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
35 is($err, '', "nothing in stderr (@$arg)");
38 ok(!run_script([qw(lei DBG-false)], undef, $opt), 'false(1) emulation');
39 is($? >> 8, 1, '$? set correctly');
40 is($err, '', 'no error from false(1) emulation');
42 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
44 ok(!run_script(['lei', @$arg], undef, $opt), "lei @$arg");
45 is($? >> 8, 1, '$? set correctly');
46 isnt($err, '', 'something in stderr');
47 is($out, '', 'nothing in stdout');
51 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
52 chomp(my $pid_again = $out);
53 is($pid, $pid_again, 'daemon-pid idempotent');
55 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
56 is($out, '', 'no output from daemon-stop');
57 is($err, '', 'no error from daemon-stop');
59 kill(0, $pid) or last;
62 ok(!-S $sock, 'sock gone');
63 ok(!kill(0, $pid), 'pid gone after stop');
65 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
66 chomp(my $new_pid = $out);
67 ok(kill(0, $new_pid), 'new pid is running');
68 ok(-S $sock, 'sock exists again');
69 unlink $sock or BAIL_OUT "unlink $!";
71 kill('CHLD', $new_pid) or last;
74 ok(!kill(0, $new_pid), 'daemon exits after unlink');
77 require_ok 'PublicInbox::LeiDaemon';