]> Sergey Matveev's repositories - public-inbox.git/blob - t/fake_inotify.t
kqnotify|fake_inotify: detect Maildir write ops
[public-inbox.git] / t / fake_inotify.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 FakeInotify 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 use_ok 'PublicInbox::FakeInotify';
11 my $MIN_FS_TICK = 0.011; # for low-res CONFIG_HZ=100 systems
12 my ($tmpdir, $for_destroy) = tmpdir();
13 mkdir "$tmpdir/new" or BAIL_OUT "mkdir: $!";
14 open my $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
15 close $fh or BAIL_OUT "close: $!";
16
17 my $fi = PublicInbox::FakeInotify->new;
18 my $mask = PublicInbox::FakeInotify::MOVED_TO_OR_CREATE();
19 my $hit = [];
20 my $cb = sub { push @$hit, map { $_->fullname } @_ };
21 my $w = $fi->watch("$tmpdir/new", $mask, $cb);
22
23 select undef, undef, undef, $MIN_FS_TICK;
24 rename("$tmpdir/tst", "$tmpdir/new/tst") or BAIL_OUT "rename: $!";
25 $fi->poll;
26 is_deeply($hit, ["$tmpdir/new/tst"], 'rename(2) detected');
27
28 @$hit = ();
29 select undef, undef, undef, $MIN_FS_TICK;
30 open $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
31 close $fh or BAIL_OUT "close: $!";
32 link("$tmpdir/tst", "$tmpdir/new/link") or BAIL_OUT "link: $!";
33 $fi->poll;
34 is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected');
35
36 $w->cancel;
37 @$hit = ();
38 select undef, undef, undef, $MIN_FS_TICK;
39 link("$tmpdir/new/tst", "$tmpdir/new/link2") or BAIL_OUT "link: $!";
40 $fi->poll;
41 is_deeply($hit, [], 'link(2) not detected after cancel');
42
43 PublicInbox::DS->Reset;
44
45 done_testing;