X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=t%2Flei-refresh-mail-sync.t;h=ea83a513d5f882f2fc94ab4555f53301bac16aac;hb=refs%2Fheads%2Fmaster;hp=ff558277f8cc003d751c0f47a8aae4ce9e67f9c7;hpb=1323196d1df67db5cb47a171a60236049c1e5ce5;p=public-inbox.git diff --git a/t/lei-refresh-mail-sync.t b/t/lei-refresh-mail-sync.t index ff558277..0498a0c4 100644 --- a/t/lei-refresh-mail-sync.t +++ b/t/lei-refresh-mail-sync.t @@ -3,17 +3,22 @@ # License: AGPL-3.0+ use strict; use v5.10.1; use PublicInbox::TestCommon; require_mods(qw(lei)); +use File::Path qw(remove_tree); +require Socket; +use Fcntl qw(F_SETFD); + +pipe(my ($stop_r, $stop_w)) or xbail "pipe: $!"; +fcntl($stop_w, F_SETFD, 0) or xbail "F_SETFD: $!"; my $stop_daemon = sub { # needed since we don't have inotify + close $stop_w or xbail "close \$stop_w: $!"; lei_ok qw(daemon-pid); chomp(my $pid = $lei_out); $pid > 0 or xbail "bad pid: $pid"; kill('TERM', $pid) or xbail "kill: $!"; - for (0..10) { - tick; - kill(0, $pid) or last; - } - kill(0, $pid) and xbail "daemon still running (PID:$pid)"; + is(sysread($stop_r, my $buf, 1), 0, 'daemon stop pipe read EOF'); + pipe($stop_r, $stop_w) or xbail "pipe: $!"; + fcntl($stop_w, F_SETFD, 0) or xbail "F_SETFD: $!"; }; test_lei({ daemon_only => 1 }, sub { @@ -62,6 +67,88 @@ test_lei({ daemon_only => 1 }, sub { lei_ok 'inspect', "blob:$oid"; is_deeply(json_utf8->decode($lei_out), $exp1, 'replaced file noted again'); + + $stop_daemon->(); + + remove_tree($d); + lei_ok 'refresh-mail-sync', '--all'; + lei_ok 'inspect', "blob:$oid"; + is($lei_out, '{}', 'no known locations after "removal"'); + lei_ok 'ls-mail-sync'; + is($lei_out, '', 'no sync left when folder is gone'); + +SKIP: { + require_mods(qw(-imapd -nntpd Mail::IMAPClient Net::NNTP), 1); + require File::Copy; # stdlib + my $home = $ENV{HOME}; + my $srv; + my $cfg_path2 = "$home/cfg2"; + File::Copy::cp($cfg_path, $cfg_path2); + my $env = { PI_CONFIG => $cfg_path2 }; + my $sock_cls; + for my $x (qw(imapd)) { + my $s = tcp_server; + $sock_cls //= ref($s); + my $cmd = [ "-$x", '-W0', "--stdout=$home/$x.out", + "--stderr=$home/$x.err" ]; + my $opt = { 3 => $s, -CLOFORK => [ $stop_w ] }; + my $td = start_script($cmd, $env, $opt) or xbail("-$x"); + my $addr = tcp_host_port($s); + $srv->{$x} = { addr => $addr, td => $td, cmd => $cmd, s => $s }; + } + my $url = "imap://$srv->{imapd}->{addr}/t.v1.0"; + lei_ok 'import', $url, '+L:v1'; + lei_ok 'inspect', "blob:$oid"; + $before = json_utf8->decode($lei_out); + my @f = grep(m!\Aimap://;AUTH=ANONYMOUS\@\Q$srv->{imapd}->{addr}\E!, + keys %{$before->{'mail-sync'}}); + is(scalar(@f), 1, 'got IMAP folder') or xbail(\@f); + xsys([qw(git config), '-f', $cfg_path2, + qw(--unset publicinbox.t1.newsgroup)]) and + xbail "git config $?"; + $stop_daemon->(); # drop IMAP IDLE + $srv->{imapd}->{td}->kill('HUP'); + tick; # wait for HUP + lei_ok 'refresh-mail-sync', $url; + lei_ok 'inspect', "blob:$oid"; + my $after = json_utf8->decode($lei_out); + ok(!$after->{'mail-sync'}, 'no sync info for non-existent mailbox'); + lei_ok 'ls-mail-sync'; + unlike $lei_out, qr!^\Q$f[0]\E!, 'IMAP folder gone from mail_sync'; + + # simulate server downtime + $url = "imap://$srv->{imapd}->{addr}/t.v2.0"; + lei_ok 'import', $url, '+L:v2'; + + lei_ok 'inspect', "blob:$oid"; + $before = $lei_out; + delete $srv->{imapd}->{td}; # kill + join daemon + + my $pid = fork // xbail "fork"; + if ($pid == 0) { # dummy server to kill new connections + $SIG{TERM} = sub { POSIX::_exit(0) }; + $srv->{imapd}->{s}->blocking(1); + while (1) { + my $caddr = accept(my $c, $srv->{imapd}->{s}) // next; + shutdown($c, 2); + } + POSIX::_exit(0); + } + my $ar = PublicInbox::AutoReap->new($pid); + ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd'); + ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd'); + $ar->kill for qw(avoid sig wake miss-no signalfd or EVFILT_SIG); + $ar->join('TERM'); + + my $cmd = $srv->{imapd}->{cmd}; + my $s = $srv->{imapd}->{s}; + $s->blocking(0); + my $opt = { 3 => $s, -CLOFORK => [ $stop_w ] }; + $srv->{imapd}->{td} = start_script($cmd, $env, $opt) or xbail "@$cmd"; + lei_ok 'refresh-mail-sync', '--all'; + lei_ok 'inspect', "blob:$oid"; + is($lei_out, $before, 'no changes when server was down'); +}; # imapd+nntpd stuff }); done_testing;