Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/Filesys/Notify/Simple.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Filesys::Notify::Simple;
use strict;
use 5.008_001;
our $VERSION = '0.12';
our $interval = 2.0;

use Carp ();
use Cwd;
Expand Down Expand Up @@ -159,7 +160,8 @@ sub wait_timer {
my $cb = shift;
my @events;
while (1) {
sleep 2;
# sleep 0 is needed to fix sigalrm on windows!?
select undef, undef, undef, $interval && sleep 0;
my $new_fs = _full_scan(@path);
_compare_fs($fs, $new_fs, sub { push @events, { path => $_[0] } });
$fs = $new_fs;
Expand Down Expand Up @@ -201,7 +203,8 @@ sub _full_scan {
File::Find::finddepth({
wanted => sub {
my $fullname = $File::Find::fullname || File::Spec->rel2abs($File::Find::name);
$map{Cwd::realpath($File::Find::dir)}{$fullname} = _stat($fullname);
my $stat = $map{Cwd::realpath($File::Find::dir)}{$fullname} = _stat($fullname);
$map{$path}{$fullname} = $stat if $stat->{is_dir}; # keep track of directories
},
follow_fast => 1,
follow_skip => 2,
Expand Down
71 changes: 71 additions & 0 deletions t/empty_dir.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use strict;
use Filesys::Notify::Simple;
use Test::More;
use Test::SharedFork;
use File::Temp qw( tempdir );

use FindBin;

plan tests => 6;

my $dir = tempdir( DIR => "$FindBin::Bin/x" );
my $w = Filesys::Notify::Simple->new([ "$dir" ]);
$Filesys::Notify::Simple::interval = 0.5;

mkdir "$dir/root";


my $pid = fork;
if ($pid == 0) {
Test::SharedFork->child;
sleep 1;
note "mkdir subroot\n";
mkdir "$dir/subroot";
sleep 1;
note "mkdir subroot/deep\n";
mkdir "$dir/subroot/deep";
sleep 1;
note "mkdir subroot/deep/down\n";
mkdir "$dir/subroot/deep/down";
sleep 1;
note "rmdir subroot/deep/down\n";
rmdir "$dir/subroot/deep/down";
sleep 1;
note "rmdir subroot/deep\n";
rmdir "$dir/subroot/deep";
sleep 1;
note "rmdir subroot\n";
rmdir "$dir/subroot";
} elsif ($pid != 0) {
Test::SharedFork->parent;
my $event;
alarm 10;
note "wait mkdir subroot\n";
$w->wait(sub { $event = shift });
like $event->{path}, qr/subroot/;
alarm 10;
note "wait mkdir subroot/deep\n";
$w->wait(sub { $event = shift });
like $event->{path}, qr/subroot[\/\\]deep/;
alarm 10;
note "wait mkdir subroot/deep/down\n";
$w->wait(sub { $event = shift });
like $event->{path}, qr/subroot[\/\\]deep[\/\\]down/;
alarm 10;
note "wait rmdir subroot/deep/down\n";
$w->wait(sub { $event = shift });
like $event->{path}, qr/subroot[\/\\]deep[\/\\]down/;
alarm 10;
note "wait rmdir subroot/deep\n";
$w->wait(sub { $event = shift });
like $event->{path}, qr/subroot[\/\\]deep/;
alarm 10;
note "wait rmdir subroot\n";
$w->wait(sub { $event = shift });
like $event->{path}, qr/subroot/;
waitpid $pid, 0;
} else {
die $!;
}


5 changes: 3 additions & 2 deletions t/move.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plan tests => 2;

my $dir = tempdir( DIR => "$FindBin::Bin/x" );
my $w = Filesys::Notify::Simple->new([ "lib", "$dir" ]);
$Filesys::Notify::Simple::interval = 0.5;

my $test_file = "$dir/move_create.data";
my $test_file_to = "$dir/move_create.data.to";
Expand All @@ -18,11 +19,11 @@ my $test_file_to = "$dir/move_create.data.to";
my $pid = fork;
if ($pid == 0) {
Test::SharedFork->child;
sleep 3;
sleep 1;
open my $out, ">", $test_file;
print $out "foo" . time;
close $out;
sleep 3;
sleep 1;
rename($test_file => $test_file_to);
} elsif ($pid != 0) {
Test::SharedFork->parent;
Expand Down
1 change: 1 addition & 0 deletions t/non_existent_path.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $ENV{PERL_FNS_NO_OPT} = 1;
require Filesys::Notify::Simple;

my $fs = Filesys::Notify::Simple->new(["/xxx/nonexistent"]);
$Filesys::Notify::Simple::interval = 0.5;

eval {
$SIG{ALRM} = sub { die "Alarm\n" };
Expand Down
5 changes: 3 additions & 2 deletions t/rm_create.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use FindBin;
use File::Temp qw( tempdir );

my $dir = tempdir( DIR => "$FindBin::Bin/x" );
$Filesys::Notify::Simple::interval = 0.5;


plan tests => 2;
Expand All @@ -15,12 +16,12 @@ my $w = Filesys::Notify::Simple->new([ "lib", "$dir" ]);
my $pid = fork;
if ($pid == 0) {
Test::SharedFork->child;
sleep 3;
sleep 1;
my $test_file = "$dir/rm_create.data";
open my $out, ">", $test_file;
print $out "foo" . time;
close $out;
sleep 3;
sleep 1;
unlink $test_file;
} elsif ($pid != 0) {
Test::SharedFork->parent;
Expand Down