lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / lei-watch.t
blob8ad50d13aa024e99ed0c8459f50ad1a6857be488
1 #!perl -w
2 # Copyright 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;
5 use File::Path qw(make_path remove_tree);
6 use PublicInbox::IO qw(write_file);
7 plan skip_all => "TEST_FLAKY not enabled for $0" if !$ENV{TEST_FLAKY};
8 require_mods('lei');
9 my $have_fast_inotify = eval { require PublicInbox::Inotify } ||
10         eval { require IO::KQueue };
12 $have_fast_inotify or
13         diag("$0 IO::KQueue or inotify missing, test will be slow");
15 my ($ro_home, $cfg_path) = setup_public_inboxes;
16 test_lei(sub {
17         my ($md, $mh1, $mh2) = map { "$ENV{HOME}/$_" } qw(md mh1 mh2);
18         my $cfg_f = "$ENV{HOME}/.config/lei/config";
19         my $md2 = $md.'2';
20         lei_ok 'ls-watch';
21         is($lei_out, '', 'nothing in ls-watch, yet');
23         my ($ino_fdinfo, $ino_contents);
24         SKIP: {
25                 $have_fast_inotify && $^O eq 'linux' or
26                         skip 'Linux/inotify-only internals check', 1;
27                 lei_ok 'daemon-pid'; chomp(my $pid = $lei_out);
28                 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$pid/fd";
29                 my @ino = grep {
30                         (readlink($_) // '') =~ /\binotify\b/
31                 } glob("/proc/$pid/fd/*");
32                 is(scalar(@ino), 1, 'only one inotify FD');
33                 my $ino_fd = (split('/', $ino[0]))[-1];
34                 $ino_fdinfo = "/proc/$pid/fdinfo/$ino_fd";
35                 open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
36                 $ino_contents = [ <$fh> ];
37         }
39         if (0) { # TODO
40                 my $url = 'imaps://example.com/foo.bar.0';
41                 lei_ok([qw(add-watch --state=pause), $url], undef, {});
42                 lei_ok 'ls-watch';
43                 is($lei_out, "$url\n", 'ls-watch shows added watch');
44                 ok(!lei(qw(add-watch --state=pause), 'bogus'.$url),
45                         'bogus URL rejected');
46         }
48         # first, make sure tag-ro works
49         make_path("$md/new", "$md/cur", "$md/tmp", $mh1, $mh2);
50         lei_ok qw(add-watch --state=tag-ro), $md;
51         lei_ok 'ls-watch';
52         like($lei_out, qr/^\Qmaildir:$md\E$/sm, 'maildir shown');
53         lei_ok qw(q mid:testmessage@example.com -o), $md, '-I', "$ro_home/t1";
54         my @f = glob("$md/cur/*:2,");
55         is(scalar(@f), 1, 'got populated maildir with one result');
57         rename($f[0], "$f[0]S") or xbail "rename $!"; # set (S)een
58         tick($have_fast_inotify ? 0.2 : 2.2); # always needed for 1 CPU systems
59         lei_ok qw(note-event done); # flushes immediately (instead of 5s)
61         lei_ok qw(q mid:testmessage@example.com -o), $md2, '-I', "$ro_home/t1";
62         my @f2 = glob("$md2/*/*");
63         is(scalar(@f2), 1, 'got one result');
64         like($f2[0], qr/S\z/, 'seen set from rename') or diag explain(\@f2);
65         my $e2 = eml_load($f2[0]);
66         my $e1 = eml_load("$f[0]S");
67         is_deeply($e2, $e1, 'results match');
69         SKIP: {
70                 $ino_fdinfo or skip 'Linux/inotify-only watch check', 1;
71                 open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
72                 my $cmp = [ <$fh> ];
73                 ok(scalar(@$cmp) > scalar(@$ino_contents),
74                         'inotify has Maildir watches');
75         }
77         lei_ok 'rm-watch', $md;
78         lei_ok 'ls-watch', \'refresh watches';
79         is($lei_out, '', 'no watches left');
81         lei_ok 'add-watch', $md2;
82         remove_tree($md2);
83         lei_ok 'rm-watch', "maildir:".$md2, \'with maildir: prefix';
84         lei_ok 'ls-watch', \'refresh watches';
85         is($lei_out, '', 'no watches left');
87         lei_ok 'add-watch', $md;
88         remove_tree($md);
89         lei_ok 'rm-watch', $md, \'absolute path w/ missing dir';
90         lei_ok 'ls-watch', \'refresh watches';
91         is($lei_out, '', 'no watches left');
93         SKIP: {
94                 $ino_fdinfo or skip 'Linux/inotify-only removal removal', 1;
95                 open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
96                 my $cmp = [ <$fh> ];
97                 is_xdeeply($cmp, $ino_contents, 'inotify Maildir watches gone');
98         };
100         write_file '>', "$mh1/.mh_sequences";
101         lei_ok qw(add-watch --state=tag-ro), $mh1, "mh:$mh2";
102         lei_ok 'ls-watch', \'refresh watches';
103         like $lei_out, qr/^\Qmh:$mh1\E$/sm, 'MH 1 shown';
104         like $lei_out, qr/^\Qmh:$mh2\E$/sm, 'MH 2 shown';
107 done_testing;