2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
6 use File::Path qw(remove_tree);
10 pipe(my ($stop_r, $stop_w)) or xbail "pipe: $!";
11 fcntl($stop_w, F_SETFD, 0) or xbail "F_SETFD: $!";
13 my $stop_daemon = sub { # needed since we don't have inotify
14 close $stop_w or xbail "close \$stop_w: $!";
15 lei_ok qw(daemon-pid);
16 chomp(my $pid = $lei_out);
17 $pid > 0 or xbail "bad pid: $pid";
18 kill('TERM', $pid) or xbail "kill: $!";
19 is(sysread($stop_r, my $buf, 1), 0, 'daemon stop pipe read EOF');
20 pipe($stop_r, $stop_w) or xbail "pipe: $!";
21 fcntl($stop_w, F_SETFD, 0) or xbail "F_SETFD: $!";
24 test_lei({ daemon_only => 1 }, sub {
25 my $d = "$ENV{HOME}/d";
26 my ($ro_home, $cfg_path) = setup_public_inboxes;
27 lei_ok qw(daemon-pid);
28 lei_ok qw(add-external), "$ro_home/t2";
29 lei_ok qw(q mid:testmessage@example.com -o), "Maildir:$d";
30 my (@o) = glob("$d/*/*");
31 scalar(@o) == 1 or xbail('multiple results', \@o);
32 my ($bn0) = ($o[0] =~ m!/([^/]+)\z!);
34 my $oid = '9bf1002c49eb075df47247b74d69bcd555e23422';
35 lei_ok 'inspect', "blob:$oid";
36 my $before = json_utf8->decode($lei_out);
37 my $exp0 = { 'mail-sync' => { "maildir:$d" => [ $bn0 ] } };
38 is_deeply($before, $exp0, 'inspect shows expected');
42 $dst =~ s/:2,.*\z// and $dst =~ s!/cur/!/new/! and
43 rename($o[0], $dst) or xbail "rename($o[0] => $dst): $!";
45 lei_ok 'inspect', "blob:$oid";
46 is_deeply(json_utf8->decode($lei_out),
47 $before, 'inspect unchanged immediately after restart');
48 lei_ok 'refresh-mail-sync', '--all';
49 lei_ok 'inspect', "blob:$oid";
50 my ($bn1) = ($dst =~ m!/([^/]+)\z!);
51 my $exp1 = { 'mail-sync' => { "maildir:$d" => [ $bn1 ] } };
52 is_deeply(json_utf8->decode($lei_out), $exp1,
53 'refresh-mail-sync updated location');
56 rename($dst, "$d/unwatched") or xbail "rename $dst out-of-the-way $!";
58 lei_ok 'refresh-mail-sync', $d;
59 lei_ok 'inspect', "blob:$oid";
60 is($lei_out, '{}', 'no known locations after "removal"');
61 lei_ok 'refresh-mail-sync', "Maildir:$d";
64 rename("$d/unwatched", $dst) or xbail "rename $dst back";
66 lei_ok 'refresh-mail-sync', "Maildir:$d";
67 lei_ok 'inspect', "blob:$oid";
68 is_deeply(json_utf8->decode($lei_out), $exp1,
69 'replaced file noted again');
74 lei_ok 'refresh-mail-sync', '--all';
75 lei_ok 'inspect', "blob:$oid";
76 is($lei_out, '{}', 'no known locations after "removal"');
77 lei_ok 'ls-mail-sync';
78 is($lei_out, '', 'no sync left when folder is gone');
81 require_mods(qw(-imapd -nntpd Mail::IMAPClient Net::NNTP), 1);
82 require File::Copy; # stdlib
83 my $home = $ENV{HOME};
85 my $cfg_path2 = "$home/cfg2";
86 File::Copy::cp($cfg_path, $cfg_path2);
87 my $env = { PI_CONFIG => $cfg_path2 };
89 for my $x (qw(imapd)) {
91 $sock_cls //= ref($s);
92 my $cmd = [ "-$x", '-W0', "--stdout=$home/$x.out",
93 "--stderr=$home/$x.err" ];
94 my $opt = { 3 => $s, -CLOFORK => [ $stop_w ] };
95 my $td = start_script($cmd, $env, $opt) or xbail("-$x");
96 my $addr = tcp_host_port($s);
97 $srv->{$x} = { addr => $addr, td => $td, cmd => $cmd, s => $s };
99 my $url = "imap://$srv->{imapd}->{addr}/t.v1.0";
100 lei_ok 'import', $url, '+L:v1';
101 lei_ok 'inspect', "blob:$oid";
102 $before = json_utf8->decode($lei_out);
103 my @f = grep(m!\Aimap://;AUTH=ANONYMOUS\@\Q$srv->{imapd}->{addr}\E!,
104 keys %{$before->{'mail-sync'}});
105 is(scalar(@f), 1, 'got IMAP folder') or xbail(\@f);
106 xsys([qw(git config), '-f', $cfg_path2,
107 qw(--unset publicinbox.t1.newsgroup)]) and
108 xbail "git config $?";
109 $stop_daemon->(); # drop IMAP IDLE
110 $srv->{imapd}->{td}->kill('HUP');
112 lei_ok 'refresh-mail-sync', $url;
113 lei_ok 'inspect', "blob:$oid";
114 my $after = json_utf8->decode($lei_out);
115 ok(!$after->{'mail-sync'}, 'no sync info for non-existent mailbox');
116 lei_ok 'ls-mail-sync';
117 unlike $lei_out, qr!^\Q$f[0]\E!, 'IMAP folder gone from mail_sync';
119 # simulate server downtime
120 $url = "imap://$srv->{imapd}->{addr}/t.v2.0";
121 lei_ok 'import', $url, '+L:v2';
123 lei_ok 'inspect', "blob:$oid";
125 delete $srv->{imapd}->{td}; # kill + join daemon
127 my $pid = fork // xbail "fork";
128 if ($pid == 0) { # dummy server to kill new connections
129 $SIG{TERM} = sub { POSIX::_exit(0) };
130 $srv->{imapd}->{s}->blocking(1);
132 my $caddr = accept(my $c, $srv->{imapd}->{s}) // next;
137 my $ar = PublicInbox::AutoReap->new($pid);
138 ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd');
139 ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd');
141 local $SIG{CHLD} = sub { $ar->join('TERM'); undef $ar };
143 eval { $ar->kill and tick(0.01) }
144 } while (defined($ar));
147 my $cmd = $srv->{imapd}->{cmd};
148 my $s = $srv->{imapd}->{s};
150 my $opt = { 3 => $s, -CLOFORK => [ $stop_w ] };
151 $srv->{imapd}->{td} = start_script($cmd, $env, $opt) or xbail "@$cmd";
152 lei_ok 'refresh-mail-sync', '--all';
153 lei_ok 'inspect', "blob:$oid";
154 is($lei_out, $before, 'no changes when server was down');
155 }; # imapd+nntpd stuff