lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / kqnotify.t
blobadd477a44f5661ec9826aafb2e66ae01d7bf02c2
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # Ensure KQNotify can pick up rename(2) and link(2) operations
6 # used by Maildir writing tools
7 use v5.12;
8 use PublicInbox::TestCommon;
9 use autodie;
10 require_bsd;
11 require_mods('IO::KQueue');
12 use_ok 'PublicInbox::KQNotify';
13 my ($tmpdir, $for_destroy) = tmpdir();
14 mkdir "$tmpdir/new";
16 my $kqn = PublicInbox::KQNotify->new;
17 my $mask = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
18 my $w = $kqn->watch("$tmpdir/new", $mask);
20 open my $fh, '>', "$tmpdir/tst";
21 close $fh;
22 rename("$tmpdir/tst", "$tmpdir/new/tst");
23 my $hit = [ map { $_->fullname } $kqn->read ];
24 is_deeply($hit, ["$tmpdir/new/tst"],
25                 'rename(2) detected (via NOTE_EXTEND)')
26                 or diag explain($hit);
28 open $fh, '>', "$tmpdir/tst";
29 close $fh;
30 link("$tmpdir/tst", "$tmpdir/new/link");
31 my @read = map { $_->fullname } $kqn->read;
32 $hit = [ grep(m!/link$!, @read) ];
33 is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected (via NOTE_WRITE)')
34         or diag explain(\@read);
37         my $d = "$tmpdir/new/ANOTHER";
38         mkdir $d;
39         $hit = [ map { $_->fullname } $kqn->read ];
40         is_xdeeply($hit, [ $d ], 'mkdir detected');
41         rmdir $d;
42         # TODO: should we always watch for directory removals?
45 $w->cancel;
46 link("$tmpdir/new/tst", "$tmpdir/new/link2");
47 $hit = [ map { $_->fullname } $kqn->read ];
48 is_deeply($hit, [], 'link(2) not detected after cancel');
50 # rearm:
51 my $GONE = PublicInbox::KQNotify::NOTE_DELETE() |
52         PublicInbox::KQNotify::NOTE_REVOKE() |
53         PublicInbox::KQNotify::NOTE_ATTRIB() |
54         PublicInbox::KQNotify::NOTE_WRITE() |
55         PublicInbox::KQNotify::NOTE_RENAME();
56 $w = $kqn->watch("$tmpdir/new", $mask|$GONE);
57 my @unlink = sort glob("$tmpdir/new/*");
58 unlink(@unlink);
59 $hit = [ sort(map { $_->fullname } $kqn->read) ];
60 is_xdeeply($hit, \@unlink, 'unlinked files match');
62 # this is unreliable on Dragonfly tmpfs (fixed post-6.4)
63 rmdir "$tmpdir/new";
64 $hit = [ sort(map { $_->fullname } $kqn->read) ];
65 is(scalar(@$hit), 1, 'detected self removal') or check_broken_tmpfs;
67 done_testing;