]> Sergey Matveev's repositories - public-inbox.git/blob - t/fake_inotify.t
11dac117f0d14907eef1a3a21e0ae4a618fafadc
[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 $w = $fi->watch("$tmpdir/new", $mask);
20
21 select undef, undef, undef, $MIN_FS_TICK;
22 rename("$tmpdir/tst", "$tmpdir/new/tst") or BAIL_OUT "rename: $!";
23 my @events = map { $_->fullname } $fi->read;
24 is_deeply(\@events, ["$tmpdir/new/tst"], 'rename(2) detected');
25
26 select undef, undef, undef, $MIN_FS_TICK;
27 open $fh, '>', "$tmpdir/tst" or BAIL_OUT "open: $!";
28 close $fh or BAIL_OUT "close: $!";
29 link("$tmpdir/tst", "$tmpdir/new/link") or BAIL_OUT "link: $!";
30 @events = map { $_->fullname } $fi->read;
31 is_deeply(\@events, ["$tmpdir/new/link"], 'link(2) detected');
32
33 $w->cancel;
34 select undef, undef, undef, $MIN_FS_TICK;
35 link("$tmpdir/new/tst", "$tmpdir/new/link2") or BAIL_OUT "link: $!";
36 @events = map { $_->fullname } $fi->read;
37 is_deeply(\@events, [], 'link(2) not detected after cancel');
38
39 PublicInbox::DS->Reset;
40
41 done_testing;