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 my $json = PublicInbox::Config::json() or plan skip_all => 'JSON missing';
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 my ($home, $for_destroy) = tmpdir();
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
15 require_mods('IO::FDPass', 51);
16 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
17 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
18 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
20 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
21 is($err, '', 'no error from daemon-pid');
22 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
23 chomp(my $pid = $out);
24 ok(kill(0, $pid), 'pid is valid');
25 ok(-S $sock, 'sock created');
27 ok(!run_script([qw(lei)], undef, $opt), 'no args fails');
28 is($? >> 8, 1, '$? is 1');
29 is($out, '', 'nothing in stdout');
30 like($err, qr/^usage:/sm, 'usage in stderr');
32 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
34 ok(run_script(['lei', @$arg], undef, $opt), "lei @$arg");
35 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
36 is($err, '', "nothing in stderr (@$arg)");
39 ok(!run_script([qw(lei DBG-false)], undef, $opt), 'false(1) emulation');
40 is($? >> 8, 1, '$? set correctly');
41 is($err, '', 'no error from false(1) emulation');
43 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
45 ok(!run_script(['lei', @$arg], undef, $opt), "lei @$arg");
46 is($? >> 8, 1, '$? set correctly');
47 isnt($err, '', 'something in stderr');
48 is($out, '', 'nothing in stdout');
52 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
53 chomp(my $pid_again = $out);
54 is($pid, $pid_again, 'daemon-pid idempotent');
56 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
57 is($out, '', 'no output from daemon-stop');
58 is($err, '', 'no error from daemon-stop');
60 kill(0, $pid) or last;
63 ok(!-S $sock, 'sock gone');
64 ok(!kill(0, $pid), 'pid gone after stop');
66 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
67 chomp(my $new_pid = $out);
68 ok(kill(0, $new_pid), 'new pid is running');
69 ok(-S $sock, 'sock exists again');
70 unlink $sock or BAIL_OUT "unlink $!";
72 kill('CHLD', $new_pid) or last;
75 ok(!kill(0, $new_pid), 'daemon exits after unlink');
78 require_ok 'PublicInbox::LeiDaemon';