]> Sergey Matveev's repositories - public-inbox.git/blob - t/kqnotify.t
kqnotify|fake_inotify: detect Maildir write ops
[public-inbox.git] / t / kqnotify.t
1 #!perl -w
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 #
5 # Ensure KQNotify can pick up rename(2) and link(2) operations
6 # used by Maildir writing tools
7 use strict;
8 use Test::More;
9 use PublicInbox::TestCommon;
10 plan skip_all => 'KQNotify is only for *BSD systems' if $^O !~ /bsd/;
11 require_mods('IO::KQueue');
12 use_ok 'PublicInbox::KQNotify';
13 my ($tmpdir, $for_destroy) = tmpdir();
14 mkdir "$tmpdir/new" or BAIL_OUT "mkdir: $!";
15 open my $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
16 close $fh or BAIL_OUT "close: $!";
17
18 my $kqn = PublicInbox::KQNotify->new;
19 my $mask = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
20 my $hit = [];
21 my $cb = sub { push @$hit, map { $_->fullname } @_ };
22 my $w = $kqn->watch("$tmpdir/new", $mask, $cb);
23
24 rename("$tmpdir/tst", "$tmpdir/new/tst") or BAIL_OUT "rename: $!";
25 $kqn->poll;
26 is_deeply($hit, ["$tmpdir/new/tst"], 'rename(2) detected (via NOTE_EXTEND)');
27
28 @$hit = ();
29 open $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
30 close $fh or BAIL_OUT "close: $!";
31 link("$tmpdir/tst", "$tmpdir/new/link") or BAIL_OUT "link: $!";
32 $kqn->poll;
33 is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected (via NOTE_WRITE)');
34
35 $w->cancel;
36 @$hit = ();
37 link("$tmpdir/new/tst", "$tmpdir/new/link2") or BAIL_OUT "link: $!";
38 $kqn->poll;
39 is_deeply($hit, [], 'link(2) not detected after cancel');
40
41 done_testing;