diff --git a/lib/Filesys/Notify/Simple.pm b/lib/Filesys/Notify/Simple.pm index c88016d..24eca6e 100644 --- a/lib/Filesys/Notify/Simple.pm +++ b/lib/Filesys/Notify/Simple.pm @@ -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; @@ -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; @@ -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, diff --git a/t/empty_dir.t b/t/empty_dir.t new file mode 100644 index 0000000..6e61912 --- /dev/null +++ b/t/empty_dir.t @@ -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 $!; +} + + diff --git a/t/move.t b/t/move.t index 6779517..24ecb53 100644 --- a/t/move.t +++ b/t/move.t @@ -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"; @@ -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; diff --git a/t/non_existent_path.t b/t/non_existent_path.t index 5912580..ea87af8 100644 --- a/t/non_existent_path.t +++ b/t/non_existent_path.t @@ -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" }; diff --git a/t/rm_create.t b/t/rm_create.t index 55dfd37..0193161 100644 --- a/t/rm_create.t +++ b/t/rm_create.t @@ -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; @@ -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;