6 use File::Basename qw(dirname);
8 use File::Path qw(mkpath rmtree);
9 use File::Rsync::Mirror::Recentfile;
11 use Time::HiRes qw(time sleep);
14 my $root_from = "t/ta";
16 rmtree [$root_from, $root_to];
22 @intervals = qw( 2s 4s 8s 16s 32s Z );
24 is 6, scalar @intervals, "array has 6 elements: @intervals";
25 my $rf0 = File::Rsync::Mirror::Recentfile->new
27 aggregator => [@intervals[1..$#intervals]],
28 interval => $intervals[0],
29 localroot => $root_from,
37 for my $iv (@intervals) {
47 open my $fh, ">", $file or die "Could not open '$file': $!";
48 print $fh time, ":", $file, "\n";
49 close $fh or die "Could not close '$file': $!";
50 $rf0->update($file,"new");
53 my $recent_events = $rf0->recent_events;
54 # faking internals as if the contents were wide-spread in time
55 for my $evi (0..$#$recent_events) {
56 my $ev = $recent_events->[$evi];
57 $ev->{epoch} -= 2**($evi*.25);
59 $rf0->write_recent($recent_events);
61 for my $iv (@intervals) {
62 my $rf = "$root_from/RECENT-$iv.yaml";
63 my $filesize = -s $rf;
64 # now they have 1700+ bytes because they were merged for the
65 # first time ever and could not be truncated for this reason.
66 ok(1700 < $filesize, "file $iv has good size[$filesize]");
67 utime 0, 0, $rf; # so that the next aggregate isn't skipped
69 open my $fh, ">", "$root_from/finissage" or die "Could not open: $!";
71 close $fh or die "Could not close: $!";
72 $rf0->update("$root_from/finissage","new");
73 $rf0 = File::Rsync::Mirror::Recentfile->new_from_file("$root_from/RECENT-2s.yaml");
75 for my $iv (@intervals) {
76 my $filesize = -s "$root_from/RECENT-$iv.yaml";
77 # now they have <1700 bytes because the second aggregate could
79 ok($iv eq "Z" || 1700 > $filesize, "file $iv has good size[$filesize]");
83 rmtree [$root_from, $root_to];
86 BEGIN { $tests += 38 }
87 my $rf = File::Rsync::Mirror::Recentfile->new_from_file("t/RECENT-6h.yaml");
88 my $recent_events = $rf->recent_events;
89 my $recent_events_cnt = scalar @$recent_events;
93 "found $recent_events_cnt events",
96 $rf->localroot($root_from);
97 $rf->comment("produced during the test 02-operation.t");
98 $rf->aggregator([qw(30s 1m 2m 1h Z)]);
100 my $start = Time::HiRes::time;
101 for my $e (@$recent_events) {
106 $pass==0 ? $root_from : $root_to,
109 mkpath dirname $file;
110 open my $fh, ">", $file or die "Could not open '$file': $!";
111 print $fh time, ":", $file, "\n";
112 close $fh or die "Could not close '$file': $!";
114 $rf->update($file,$e->{type});
119 my $took = Time::HiRes::time - $start;
120 ok $took > 0, "creating the tree and aggregate took $took seconds";
121 my $dagg1 = $rf->_debug_aggregate;
123 my $file_from = "$root_from/anotherfilefromtesting";
124 open my $fh, ">", $file_from or die "Could not open: $!";
125 print $fh time, ":", $file_from;
126 close $fh or die "Could not close: $!";
127 $rf->update($file_from,"new");
129 my $dagg2 = $rf->_debug_aggregate;
130 ok($dagg1->[0][1] < $dagg2->[0][1], "The 10s file size larger: $dagg1->[0][1] < $dagg2->[0][1]");
131 ok($dagg1->[1][2] < $dagg2->[1][2], "The 1m file timestamp larger: $dagg1->[1][2] < $dagg2->[1][2]");
132 is $dagg1->[2][1], $dagg2->[2][1], "The 2m file size unchanged";
133 is $dagg1->[3][2], $dagg2->[3][2], "The 1h file timestamp unchanged";
134 ok -l "t/ta/RECENT.recent", "found the symlink";
135 my $have_slept = my $have_worked = 0;
136 $start = Time::HiRes::time;
144 open my $fh, ">", $file or die "Could not open '$file': $!";
145 print $fh time, ":", $file, "\n";
146 close $fh or die "Could not close '$file': $!";
147 $rf->update($file,"new");
149 my $rf2 = File::Rsync::Mirror::Recentfile->new_from_file("$root_from/RECENT-30s.yaml");
150 my $rece = $rf2->recent_events;
151 my $rececnt = @$rece;
152 my $span = $rece->[0]{epoch} - $rece->[-1]{epoch};
153 $have_worked = Time::HiRes::time - $start - $have_slept;
154 ok($rececnt > 0 && $span < 30, "i[$i] cnt[$rececnt] span[$span] worked[$have_worked]");
155 $have_slept += Time::HiRes::sleep 0.99;
160 BEGIN { $tests += 2 }
161 my $rf = File::Rsync::Mirror::Recentfile->new
163 filenameroot => "RECENT",
165 localroot => $root_to,
166 max_rsync_errors => 0,
167 remote_dir => $root_from,
173 # not available in rsync 3.0.3: 'omit-dir-times' => 1,
181 $success = $rf->mirror;
182 my $re = $rf->recent_events;
183 $somefile_epoch = $re->[24]{epoch};
184 } elsif (1 == $pass) {
185 $success = $rf->mirror(after => $somefile_epoch);
187 ok($success, "mirrored with success");
191 rmtree [$root_from, $root_to];
193 BEGIN { plan tests => $tests }
197 # cperl-indent-level: 4