public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob add477a44f5661ec9826aafb2e66ae01d7bf02c2 2057 bytes (raw)
$ git show HEAD:t/kqnotify.t	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
#!perl -w
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
#
# Ensure KQNotify can pick up rename(2) and link(2) operations
# used by Maildir writing tools
use v5.12;
use PublicInbox::TestCommon;
use autodie;
require_bsd;
require_mods('IO::KQueue');
use_ok 'PublicInbox::KQNotify';
my ($tmpdir, $for_destroy) = tmpdir();
mkdir "$tmpdir/new";

my $kqn = PublicInbox::KQNotify->new;
my $mask = PublicInbox::KQNotify::MOVED_TO_OR_CREATE();
my $w = $kqn->watch("$tmpdir/new", $mask);

open my $fh, '>', "$tmpdir/tst";
close $fh;
rename("$tmpdir/tst", "$tmpdir/new/tst");
my $hit = [ map { $_->fullname } $kqn->read ];
is_deeply($hit, ["$tmpdir/new/tst"],
		'rename(2) detected (via NOTE_EXTEND)')
		or diag explain($hit);

open $fh, '>', "$tmpdir/tst";
close $fh;
link("$tmpdir/tst", "$tmpdir/new/link");
my @read = map { $_->fullname } $kqn->read;
$hit = [ grep(m!/link$!, @read) ];
is_deeply($hit, ["$tmpdir/new/link"], 'link(2) detected (via NOTE_WRITE)')
	or diag explain(\@read);

{
	my $d = "$tmpdir/new/ANOTHER";
	mkdir $d;
	$hit = [ map { $_->fullname } $kqn->read ];
	is_xdeeply($hit, [ $d ], 'mkdir detected');
	rmdir $d;
	# TODO: should we always watch for directory removals?
}

$w->cancel;
link("$tmpdir/new/tst", "$tmpdir/new/link2");
$hit = [ map { $_->fullname } $kqn->read ];
is_deeply($hit, [], 'link(2) not detected after cancel');

# rearm:
my $GONE = PublicInbox::KQNotify::NOTE_DELETE() |
	PublicInbox::KQNotify::NOTE_REVOKE() |
	PublicInbox::KQNotify::NOTE_ATTRIB() |
	PublicInbox::KQNotify::NOTE_WRITE() |
	PublicInbox::KQNotify::NOTE_RENAME();
$w = $kqn->watch("$tmpdir/new", $mask|$GONE);
my @unlink = sort glob("$tmpdir/new/*");
unlink(@unlink);
$hit = [ sort(map { $_->fullname } $kqn->read) ];
is_xdeeply($hit, \@unlink, 'unlinked files match');

# this is unreliable on Dragonfly tmpfs (fixed post-6.4)
rmdir "$tmpdir/new";
$hit = [ sort(map { $_->fullname } $kqn->read) ];
is(scalar(@$hit), 1, 'detected self removal') or check_broken_tmpfs;

done_testing;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git