2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # end-to-end IMAP tests, see unit tests in t/imap.t, too
8 use PublicInbox::TestCommon;
9 use PublicInbox::Config;
10 use PublicInbox::Spawn qw(which);
11 require_mods(qw(DBD::SQLite Mail::IMAPClient Mail::IMAPClient::BodyStructure
12 Email::Address::XS||Mail::Address Parse::RecDescent));
13 my $imap_client = 'Mail::IMAPClient';
14 my $can_compress = $imap_client->can('compress');
15 if ($can_compress) { # hope this gets fixed upstream, soon
16 require PublicInbox::IMAPClient;
17 $imap_client = 'PublicInbox::IMAPClient';
20 require_ok 'PublicInbox::IMAP';
21 my $first_range = '0';
25 require_mods('Search::Xapian', 1);
30 push(@V, 2) if require_git('2.6', 1);
32 my ($tmpdir, $for_destroy) = tmpdir();
33 my $home = "$tmpdir/home";
34 local $ENV{HOME} = $home;
37 my $addr = "i$V\@example.com";
39 my $url = "http://example.com/i$V";
40 my $inboxdir = "$tmpdir/$name";
41 my $folder = "inbox.i$V";
42 my $cmd = ['-init', "-V$V", "-L$level", "--ng=$folder",
43 $name, $inboxdir, $url, $addr];
44 run_script($cmd) or BAIL_OUT("init $name");
46 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
47 'publicinboxmda.spamcheck', 'none') == 0 or
48 BAIL_OUT("config: $?");
50 open(my $fh, '<', 't/utf8.eml') or BAIL_OUT("open t/utf8.eml: $!");
51 my $env = { ORIGINAL_RECIPIENT => $addr };
52 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
53 BAIL_OUT('-mda delivery');
55 run_script(['-index', $inboxdir]) or BAIL_OUT("index $?");
58 my $sock = tcp_server();
59 my $err = "$tmpdir/stderr.log";
60 my $out = "$tmpdir/stdout.log";
61 my $cmd = [ '-imapd', '-W0', "--stdout=$out", "--stderr=$err" ];
62 my $td = start_script($cmd, undef, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
64 Server => $sock->sockhost,
65 Port => $sock->sockport,
68 my $mic = $imap_client->new(%mic_opt);
69 my $pre_login_capa = $mic->capability;
70 is(grep(/\AAUTH=ANONYMOUS\z/, @$pre_login_capa), 1,
71 'AUTH=ANONYMOUS advertised pre-login');
73 $mic->User('lorelei');
74 $mic->Password('Hunter2');
75 ok($mic->login && $mic->IsAuthenticated, 'LOGIN works');
76 my $post_login_capa = $mic->capability;
77 ok(join("\n", @$pre_login_capa) ne join("\n", @$post_login_capa),
78 'got different capabilities post-login');
80 $mic_opt{Authmechanism} = 'ANONYMOUS';
81 $mic_opt{Authcallback} = sub { '' };
82 $mic = $imap_client->new(%mic_opt);
83 ok($mic && $mic->login && $mic->IsAuthenticated, 'AUTHENTICATE ANONYMOUS');
84 my $post_auth_anon_capa = $mic->capability;
85 is_deeply($post_auth_anon_capa, $post_login_capa,
86 'auth anon has same capabilities');
88 ok($mic->noop, 'NOOP');
89 ok($mic->noop, 'NOOP (again)'); # for warnings
90 ok(!$mic->examine('foo') && ($e = $@), 'EXAMINE non-existent');
91 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
92 ok(!$mic->select('foo') && ($e = $@), 'EXAMINE non-existent');
93 like($e, qr/\bNO\b/, 'got a NO on EXAMINE for non-existent');
94 my $mailbox1 = "inbox.i1.$first_range";
95 ok($mic->select('inbox.i1'), 'SELECT on parent succeeds');
96 ok($mic->noop, 'NOOP while selected');
97 ok($mic->noop, 'NOOP again while selected'); # check warnings later
98 ok($mic->select($mailbox1), 'SELECT succeeds');
99 ok($mic->examine($mailbox1), 'EXAMINE succeeds');
100 my @raw = $mic->status($mailbox1, qw(Messages uidnext uidvalidity));
101 is(scalar(@raw), 2, 'got status response');
102 like($raw[0], qr/\A\*\x20STATUS\x20inbox\.i1\.$first_range\x20
103 \(MESSAGES\x20\d+\x20UIDNEXT\x20\d+\x20UIDVALIDITY\x20\d+\)\r\n/sx);
104 like($raw[1], qr/\A\S+ OK /, 'finished status response');
106 my @orig_list = @raw = $mic->list;
107 like($raw[0], qr/^\* LIST \(.*?\) "\." INBOX/,
109 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
110 is(scalar(@raw), scalar(@V) * 2 + 2, 'default LIST response');
111 @raw = $mic->list('', 'inbox.i1');
112 is(scalar(@raw), 2, 'limited LIST response');
113 like($raw[0], qr/^\* LIST \(.*?\) "\." INBOX/,
115 like($raw[-1], qr/^\S+ OK /, 'response ended with OK');
117 my $ret = $mic->search('all') or BAIL_OUT "SEARCH FAIL $@";
118 is_deeply($ret, [ 1 ], 'search all works');
119 $ret = $mic->search('uid 1') or BAIL_OUT "SEARCH FAIL $@";
120 is_deeply($ret, [ 1 ], 'search UID 1 works');
121 $ret = $mic->search('uid 1:1') or BAIL_OUT "SEARCH FAIL $@";
122 is_deeply($ret, [ 1 ], 'search UID 1:1 works');
123 $ret = $mic->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
124 is_deeply($ret, [ 1 ], 'search UID 1:* works');
125 $ret = $mic->search('DELETED') or BAIL_OUT "SEARCH FAIL $@";
126 is_deeply($ret, [], 'searching for DELETED returns nothing');
129 skip 'Xapian missing', 8 if $level eq 'basic';
130 my $x = $mic->search(qw(smaller 99999));
131 is_deeply($x, [1], 'SMALLER works with Xapian (hit)');
132 $x = $mic->search(qw(smaller 9));
133 is_deeply($x, [], 'SMALLER works with Xapian (miss)');
135 $x = $mic->search(qw(larger 99999));
136 is_deeply($x, [], 'LARGER works with Xapian (miss)');
137 $x = $mic->search(qw(larger 9));
138 is_deeply($x, [1], 'LARGER works with Xapian (hit)');
140 $x = $mic->search(qw(HEADER Message-ID testmessage@example.com));
141 is_deeply($x, [1], 'HEADER Message-ID works');
143 $x = $mic->search(qw(DRAFT HEADER Message-ID testmessage@example.com));
144 is_deeply($x, [], 'impossible (DRAFT) key filters out match');
146 $x = $mic->search(qw(HEADER Message-ID miss));
147 is_deeply($x, [], 'HEADER Message-ID can miss');
149 my @q = qw[OR HEADER Message-ID testmessage@example.com
150 (OR FROM Ryan (OR TO Joe CC Scott))];
151 $x = $mic->search(join(' ', @q));
152 is_deeply($x, [1], 'nested query works');
155 is_deeply(scalar $mic->flags('1'), [], '->flags works');
157 # RFC 3501 section 6.4.8 states:
158 # Also note that a UID range of 559:* always includes the
159 # UID of the last message in the mailbox, even if 559 is
160 # higher than any assigned UID value.
161 my $exp = $mic->fetch_hash(1, 'UID');
162 $ret = $mic->fetch_hash('559:*', 'UID');
163 is_deeply($ret, $exp, 'beginning range too big');
165 my @w; # Mail::IMAPClient hits a warning via overload
166 local $SIG{__WARN__} = sub { push @w, @_ };
167 $ret = $mic->fetch_hash(my $r = '559:558', 'UID');
168 is_deeply($ret, {}, "out-of-range UID FETCH $r");
169 @w = grep(!/\boverload\.pm\b/, @w);
170 is_deeply(\@w, [], 'no unexpected warning');
172 $ret = $mic->fetch_hash(my $r = '558:559', 'UID');
173 is_deeply($ret, {}, "out-of-range UID FETCH $r");
176 for my $r ('1:*', '1') {
177 $ret = $mic->fetch_hash($r, 'RFC822') or BAIL_OUT "FETCH $@";
178 is_deeply([keys %$ret], [1]);
179 like($ret->{1}->{RFC822}, qr/\r\n\r\nThis is a test/, 'read full');
181 # ensure Mail::IMAPClient behaves
182 my $str = $mic->message_string($r) or BAIL_OUT "->message_string: $@";
183 is($str, $ret->{1}->{RFC822}, '->message_string works as expected');
185 my $sz = $mic->fetch_hash($r, 'RFC822.size') or BAIL_OUT "FETCH $@";
186 is($sz->{1}->{'RFC822.SIZE'}, length($ret->{1}->{RFC822}),
189 $ret = $mic->fetch_hash($r, 'RFC822.HEADER') or BAIL_OUT "FETCH $@";
190 is_deeply([keys %$ret], [1]);
191 like($ret->{1}->{'RFC822.HEADER'},
192 qr/^Message-ID: <testmessage\@example\.com>/ms, 'read header');
194 $ret = $mic->fetch_hash($r, 'INTERNALDATE') or BAIL_OUT "FETCH $@";
195 is($ret->{1}->{'INTERNALDATE'}, '01-Jan-1970 00:00:00 +0000',
196 'internaldate matches');
197 ok(!$mic->fetch_hash($r, 'INFERNALDATE'), 'bogus attribute fails');
199 my $envelope = $mic->get_envelope($r) or BAIL_OUT("get_envelope: $@");
200 is($envelope->{bcc}, 'NIL', 'empty bcc');
201 is($envelope->{messageid}, '<testmessage@example.com>', 'messageid');
202 is(scalar @{$envelope->{to}}, 1, 'one {to} header');
203 # *sigh* too much to verify...
204 #use Data::Dumper; diag Dumper($envelope);
206 $ret = $mic->fetch_hash($r, 'FLAGS') or BAIL_OUT "FETCH $@";
207 is_deeply($ret->{1}->{FLAGS}, '', 'no flags');
209 $ret = $mic->fetch_hash($r, 'BODY[1]') or BAIL_OUT "FETCH $@";
210 like($ret->{1}->{'BODY[1]'}, qr/\AThis is a test message/, 'BODY[1]');
212 $ret = $mic->fetch_hash($r, 'BODY[1]<1>') or BAIL_OUT "FETCH $@";
213 like($ret->{1}->{'BODY[1]<1>'}, qr/\Ahis is a test message/,
216 $ret = $mic->fetch_hash($r, 'BODY[1]<2.3>') or BAIL_OUT "FETCH $@";
217 is($ret->{1}->{'BODY[1]<2>'}, "is ", 'BODY[1]<2.3>');
218 $ret = $mic->bodypart_string($r, 1, 3, 2) or
219 BAIL_OUT "bodypart_string $@";
220 is($ret, "is ", 'bodypart string');
222 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS.NOT (Message-ID)]')
223 or BAIL_OUT "FETCH $@";
224 $ret = $ret->{1}->{'BODY[HEADER.FIELDS.NOT (MESSAGE-ID)]'};
225 unlike($ret, qr/message-id/i, 'Message-ID excluded');
226 like($ret, qr/\r\n\r\n\z/s, 'got header end');
228 $ret = $mic->fetch_hash($r, 'BODY[HEADER.FIELDS (Message-ID)]')
229 or BAIL_OUT "FETCH $@";
230 is($ret->{1}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
231 'Message-ID: <testmessage@example.com>'."\r\n\r\n",
232 'got only Message-ID');
234 my $bs = $mic->get_bodystructure($r) or BAIL_OUT("bodystructure: $@");
235 ok($bs, 'got a bodystructure');
236 is(lc($bs->bodytype), 'text', '->bodytype');
237 is(lc($bs->bodyenc), '8bit', '->bodyenc');
239 ok($mic->has_capability('COMPRESS') ||
240 $mic->has_capability('COMPRESS=DEFLATE'), 'deflate cap');
242 skip 'Mail::IMAPClient too old for ->compress', 2 if !$can_compress;
243 my $c = $imap_client->new(%mic_opt);
244 ok($c && $c->compress, 'compress enabled');
245 ok($c->examine($mailbox1), 'EXAMINE succeeds after COMPRESS');
246 $ret = $c->search('uid 1:*') or BAIL_OUT "SEARCH FAIL $@";
247 is_deeply($ret, [ 1 ], 'search UID 1:* works after compression');
250 ok($mic->logout, 'logout works');
252 my $have_inotify = eval { require Linux::Inotify2; 1 };
254 my $pi_cfg = PublicInbox::Config->new;
255 $pi_cfg->each_inbox(sub {
257 my $env = { ORIGINAL_RECIPIENT => $ibx->{-primary_address} };
258 my $name = $ibx->{name};
259 my $ng = $ibx->{newsgroup};
260 my $mic = $imap_client->new(%mic_opt);
261 ok($mic && $mic->login && $mic->IsAuthenticated, "authed $name");
262 my $mb = "$ng.$first_range";
263 my $uidnext = $mic->uidnext($mb); # we'll fetch BODYSTRUCTURE on this
264 ok($uidnext, 'got uidnext for later fetch');
265 ok($mic->has_capability('IDLE'), "IDLE capa $name");
266 ok(!$mic->idle, "IDLE fails w/o SELECT/EXAMINE $name");
267 ok($mic->examine($mb), "EXAMINE $ng succeeds");
268 ok(my $idle_tag = $mic->idle, "IDLE succeeds on $ng");
270 open(my $fh, '<', 't/data/message_embed.eml') or BAIL_OUT("open: $!");
271 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
272 BAIL_OUT('-mda delivery');
273 my $t0 = Time::HiRes::time();
274 ok(my @res = $mic->idle_data(11), "IDLE succeeds on $ng");
275 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
276 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
278 my (@ino_info, $ino_fdinfo);
280 skip 'no inotify support', 1 unless $have_inotify;
281 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$td->{pid}/fd";
283 readlink($_) =~ /\binotify\b/
284 } glob("/proc/$td->{pid}/fd/*");
285 is(scalar(@ino), 1, 'only one inotify FD');
286 my $ino_fd = (split('/', $ino[0]))[-1];
287 $ino_fdinfo = "/proc/$td->{pid}/fdinfo/$ino_fd";
288 if (open my $fh, '<', $ino_fdinfo) {
290 @ino_info = grep(/^inotify wd:/, <$fh>);
291 ok(scalar(@ino_info), 'inotify has watches');
293 skip "$ino_fdinfo missing: $!", 1;
297 # ensure IDLE persists across HUP, w/o extra watches or FDs
298 $td->kill('HUP') or BAIL_OUT "failed to kill -imapd: $!";
299 for my $n (1..2) { # kick the event loop so we know HUP is done
300 my $m = $imap_client->new(%mic_opt);
301 ok($m->login && $m->IsAuthenticated && $m->logout,
302 "connection $n works after HUP");
305 open($fh, '<', 't/data/0001.patch') or BAIL_OUT("open: $!");
306 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or
307 BAIL_OUT('-mda delivery');
308 $t0 = Time::HiRes::time();
309 ok(@res = $mic->idle_data(11), "IDLE succeeds on $ng after HUP");
310 is(grep(/\A\* [0-9] EXISTS\b/, @res), 1, 'got EXISTS message');
311 ok((Time::HiRes::time() - $t0) < 10, 'IDLE client notified');
312 ok($mic->done($idle_tag), 'IDLE DONE');
313 my $bs = $mic->get_bodystructure($uidnext);
314 ok($bs, 'BODYSTRUCTURE ok for deeply nested');
315 $ret = $mic->fetch_hash($uidnext, 'BODY') or BAIL_OUT "FETCH $@";
316 ok($ret->{$uidnext}->{BODY}, 'got something in BODY');
318 # this matches dovecot behavior
319 $ret = $mic->fetch_hash($uidnext, 'BODY[1]') or BAIL_OUT "FETCH $@";
320 is($ret->{$uidnext}->{'BODY[1]'},
321 "testing embedded message harder\r\n", 'BODY[1]');
322 $ret = $mic->fetch_hash($uidnext, 'BODY[2]') or BAIL_OUT "FETCH $@";
323 like($ret->{$uidnext}->{'BODY[2]'},
324 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /, 'BODY[2]');
326 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.1]') or BAIL_OUT "FETCH $@";
327 is($ret->{$uidnext}->{'BODY[2.1.1]'},
328 "testing embedded message\r\n", 'BODY[2.1.1]');
330 $ret = $mic->fetch_hash($uidnext, 'BODY[2.1.2]') or BAIL_OUT "FETCH $@";
331 like($ret->{$uidnext}->{'BODY[2.1.2]'}, qr/\AFrom: /,
332 'BODY[2.1.2] tip matched');
333 like($ret->{$uidnext}->{'BODY[2.1.2]'},
334 # trailing CRLF may vary depending on MIME parser
335 qr/done_testing;(?:\r\n){1,2}\z/,
336 'BODY[2.1.2] tail matched');
338 $ret = $mic->fetch_hash("1:$uidnext", 'BODY[2.HEADER]') or
339 BAIL_OUT "2.HEADER $@";
340 like($ret->{$uidnext}->{'BODY[2.HEADER]'},
341 qr/\ADate: Sat, 18 Apr 2020 22:20:20 /,
342 '2.HEADER of message/rfc822');
344 $ret = $mic->fetch_hash($uidnext, 'BODY[2.MIME]') or
345 BAIL_OUT "2.MIME $@";
346 is($ret->{$uidnext}->{'BODY[2.MIME]'}, <<EOF, 'BODY[2.MIME]');
347 Content-Type: message/rfc822\r
348 Content-Disposition: attachment; filename="embed2x\.eml"\r
352 my @hits = $mic->search('SENTON' => '18-Apr-2020');
353 is_deeply(\@hits, [ $uidnext ], 'search with date condition works');
354 ok($mic->examine($ng), 'EXAMINE on dummy');
355 @hits = $mic->search('SENTSINCE' => '18-Apr-2020');
356 is_deeply(\@hits, [], 'search on dummy with condition works');
357 ok(!$mic->search('SENTSINCE' => '18-Abr-2020'), 'bad month fails');
359 my $ret = $mic->fetch_hash(2, 'RFC822');
361 'MSN FETCH on empty dummy will not trigger warnings, later');
364 # message sequence numbers :<
365 is($mic->Uid(0), 0, 'disable UID on '.ref($mic));
366 ok($mic->reconnect, 'reconnected');
367 $ret = $mic->fetch_hash('1,2:3', 'RFC822') or BAIL_OUT "FETCH $@";
368 is(scalar keys %$ret, 3, 'got all 3 messages with comma-separated sequence');
369 $ret = $mic->fetch_hash('1:*', 'RFC822') or BAIL_OUT "FETCH $@";
370 is(scalar keys %$ret, 3, 'got all 3 messages');
373 # do any clients use non-UID IMAP SEARCH?
374 skip 'Xapian missing', 2 if $level eq 'basic';
375 my $x = $mic->search('all');
376 is_deeply($x, [1, 2, 3], 'MSN SEARCH works before rm');
377 $x = $mic->search(qw(header subject embedded));
378 is_deeply($x, [2], 'MSN SEARCH on Subject works before rm');
382 my $rdr = { 0 => \($ret->{1}->{RFC822}) };
383 my $env = { HOME => $ENV{HOME} };
384 my @cmd = qw(-learn rm --all);
385 run_script(\@cmd, $env, $rdr) or BAIL_OUT('-learn rm');
389 # do any clients use non-UID IMAP SEARCH? We only ensure
390 # MSN "SEARCH" can return a result which can be retrieved
392 skip 'Xapian missing', 3 if $level eq 'basic';
393 my $x = $mic->search(qw(header subject embedded));
394 is(scalar(@$x), 1, 'MSN SEARCH on Subject works after rm');
396 skip 'previous test failed', 1 if !scalar(@$x);
397 $x = $mic->message_string($x->[0]);
398 is($x, $ret->{2}->{RFC822}, 'message 2 unchanged');
400 $x = $mic->search(qw(text embedded));
401 is(scalar(@$x), 1, 'MSN SEARCH on TEXT works after rm');
404 # FIXME? no EXPUNGE response, yet
405 my $r2 = $mic->fetch_hash('1:*', 'BODY.PEEK[]') or BAIL_OUT "FETCH $@";
406 is(scalar keys %$r2, 2, 'did not get all 3 messages');
407 is($r2->{2}->{'BODY[]'}, $ret->{2}->{RFC822}, 'message 2 unchanged');
408 is($r2->{3}->{'BODY[]'}, $ret->{3}->{RFC822}, 'message 3 unchanged');
409 $r2 = $mic->fetch_hash(2, 'BODY.PEEK[HEADER.FIELDS (message-id)]')
410 or BAIL_OUT "FETCH $@";
411 is($r2->{2}->{'BODY[HEADER.FIELDS (MESSAGE-ID)]'},
412 'Message-ID: <20200418222508.GA13918@dcvr>'."\r\n\r\n",
413 'BODY.PEEK[HEADER.FIELDS ...] drops .PEEK');
416 my @new_list = $mic->list;
417 # tag differs in [-1]
418 like($orig_list[-1], qr/\A\S+ OK List done\r\n/, 'orig LIST');
419 like($new_list[-1], qr/\A\S+ OK List done\r\n/, 'new LIST');
422 # TODO: not sure if sort order matters, imapd_refresh_finalize
423 # doesn't sort, hopefully clients don't care...
424 @new_list = sort @new_list;
425 @orig_list = sort @orig_list;
426 is_deeply(\@new_list, \@orig_list, 'LIST identical');
428 ok($mic->close, 'CLOSE works');
429 ok(!$mic->close, 'CLOSE not idempotent');
430 ok($mic->logout, 'logged out');
433 my $c = tcp_connect($sock);
435 like(<$c>, qr/\* OK/, 'got a greeting');
437 like(<$c>, qr/\A\* BAD Error in IMAP command/, 'empty line');
438 print $c "tagonly\r\n";
439 like(<$c>, qr/\Atagonly BAD Error in IMAP command/, 'tag-only line');
443 use_ok 'PublicInbox::Watch';
444 use_ok 'PublicInbox::InboxIdle';
445 require_git('1.8.5', 1) or
446 skip('git 1.8.5+ needed for --urlmatch', 4);
447 my $old_env = { HOME => $ENV{HOME} };
448 my $home = "$tmpdir/watch_home";
449 mkdir $home or BAIL_OUT $!;
450 mkdir "$home/.public-inbox" or BAIL_OUT $!;
451 local $ENV{HOME} = $home;
452 my $name = 'watchimap';
453 my $addr = "i1\@example.com";
454 my $url = "http://example.com/i1";
455 my $inboxdir = "$tmpdir/watchimap";
456 my $cmd = ['-init', '-V2', '-Lbasic', $name, $inboxdir, $url, $addr];
457 my ($ihost, $iport) = ($sock->sockhost, $sock->sockport);
458 my $imapurl = "imap://$ihost:$iport/inbox.i1.0";
459 run_script($cmd) or BAIL_OUT("init $name");
460 xsys(qw(git config), "--file=$home/.public-inbox/config",
461 "publicinbox.$name.watch",
462 $imapurl) == 0 or BAIL_OUT "git config $?";
463 my $cfg = PublicInbox::Config->new;
464 PublicInbox::DS->Reset;
465 my $ii = PublicInbox::InboxIdle->new($cfg);
466 my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
467 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
468 $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
469 my $watcherr = "$tmpdir/watcherr";
470 open my $err_wr, '>>', $watcherr or BAIL_OUT $!;
471 open my $err, '<', $watcherr or BAIL_OUT $!;
472 my $w = start_script(['-watch'], undef, { 2 => $err_wr });
474 diag 'waiting for initial fetch...';
475 PublicInbox::DS->EventLoop;
476 diag 'inbox unlocked on initial fetch, waiting for IDLE';
478 tick until (grep(/I: \S+ idling/, <$err>));
479 open my $fh, '<', 't/iso-2202-jp.eml' or BAIL_OUT $!;
480 $old_env->{ORIGINAL_RECIPIENT} = $addr;
481 ok(run_script([qw(-mda --no-precheck)], $old_env, { 0 => $fh }),
482 'delivered a message for IDLE to kick -watch');
483 diag 'waiting for IMAP IDLE wakeup';
484 PublicInbox::DS->SetPostLoopCallback(undef);
485 PublicInbox::DS->EventLoop;
486 diag 'inbox unlocked on IDLE wakeup';
488 # try again with polling
489 xsys(qw(git config), "--file=$home/.public-inbox/config",
490 'imap.PollInterval', 0.11) == 0
491 or BAIL_OUT "git config $?";
493 diag 'waiting for -watch reload + initial fetch';
494 tick until (grep(/I: will check/, <$err>));
496 open $fh, '<', 't/psgi_attach.eml' or BAIL_OUT $!;
497 ok(run_script([qw(-mda --no-precheck)], $old_env, { 0 => $fh }),
498 'delivered a message for -watch PollInterval');
500 diag 'waiting for PollInterval wakeup';
501 PublicInbox::DS->SetPostLoopCallback(undef);
502 PublicInbox::DS->EventLoop;
503 diag 'inbox unlocked (poll)';
506 is($?, 0, 'no error in exited -watch process');
508 $cfg->each_inbox(sub { shift->unsubscribe_unlock('ident') });
510 PublicInbox::DS->Reset;
512 my @err = grep(!/^I:/, <$err>);
513 is(@err, 0, 'no warnings/errors from -watch'.join(' ', @err));
515 if ($ENV{TEST_KILL_IMAPD}) { # not sure how reliable this test can be
516 xsys(qw(git config), "--file=$home/.public-inbox/config",
517 qw(--unset imap.PollInterval)) == 0
518 or BAIL_OUT "git config $?";
519 truncate($err_wr, 0) or BAIL_OUT $!;
521 $w = start_script(['-watch'], undef, { 2 => $err_wr });
523 tick until (grep(/I: \S+ idling/, <$err>));
524 diag 'killing imapd, waiting for CPU spins';
530 is($?, 0, 'no error in exited -watch process');
532 my $c = $t1[2] + $t1[3] - $t0[2] - $t0[3];
533 my $thresh = (0.9 * $delay);
534 diag "c=$c, threshold=$thresh";
535 ok($c < $thresh, 'did not burn much CPU');
536 is_deeply([grep(/ line \d+$/m, <$err>)], [],
537 'no backtraces from errors');
543 is($?, 0, 'no error in exited process') if !$ENV{TEST_KILL_IMAPD};
544 open my $fh, '<', $err or BAIL_OUT("open $err failed: $!");
545 my $eout = do { local $/; <$fh> };
546 unlike($eout, qr/wide/i, 'no Wide character warnings');
547 unlike($eout, qr/uninitialized/i, 'no uninitialized warnings');