describing the need of and a first implementation of the merged accessor and a test
[rersyncrecent.git] / t / 02-operation.t
blob202bdcdbc1dd85c7ee9fec4e7d782dcb4d5b53de
1 use Test::More;
2 use strict;
3 my $tests;
4 BEGIN { $tests = 0 }
5 use lib "lib";
6 use File::Basename qw(dirname);
7 use File::Copy qw(cp);
8 use File::Path qw(mkpath rmtree);
9 use File::Rsync::Mirror::Recentfile;
10 use Storable;
11 use Time::HiRes qw(time sleep);
12 use YAML::Syck;
14 my $root_from = "t/ta";
15 my $root_to = "t/tb";
16 rmtree [$root_from, $root_to];
19     my @intervals;
20     BEGIN {
21         $tests += 13;
22         @intervals = qw( 2s 4s 8s 16s 32s Z );
23     }
24     is 6, scalar @intervals, "array has 6 elements: @intervals";
25     my $rf0 = File::Rsync::Mirror::Recentfile->new
26         (
27          aggregator     => [@intervals[1..$#intervals]],
28          interval       => $intervals[0],
29          localroot      => $root_from,
30          rsync_options  => {
31                             compress          => 0,
32                             links             => 1,
33                             times             => 1,
34                             checksum          => 0,
35                            },
36         );
37     for my $iv (@intervals) {
38         for my $i (0..3) {
39             my $file = sprintf
40                 (
41                  "%s/A%s-%02d",
42                  $root_from,
43                  $iv,
44                  $i,
45                 );
46             mkpath dirname $file;
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");
51         }
52     }
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);
58     }
59     $rf0->write_recent($recent_events);
60     $rf0->aggregate;
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
68     }
69     open my $fh, ">", "$root_from/finissage" or die "Could not open: $!";
70     print $fh "fin";
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");
74     $rf0->aggregate;
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
78         # truncate them
79         ok($iv eq "Z" || 1700 > $filesize, "file $iv has good size[$filesize]");
80     }
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;
90     is (
91         92,
92         $recent_events_cnt,
93         "found $recent_events_cnt events",
94        );
95     $rf->interval("10s");
96     $rf->localroot($root_from);
97     $rf->comment("produced during the test 02-operation.t");
98     $rf->aggregator([qw(30s 1m 2m 1h Z)]);
99     $rf->verbose(1);
100     my $start = Time::HiRes::time;
101     for my $e (@$recent_events) {
102         for my $pass (0,1) {
103             my $file = sprintf
104                 (
105                  "%s/%s",
106                  $pass==0 ? $root_from : $root_to,
107                  $e->{path},
108                 );
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': $!";
113             if ($pass==0) {
114                 $rf->update($file,$e->{type});
115             }
116         }
117     }
118     $rf->aggregate;
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;
122     sleep 1.5;
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");
128     $rf->aggregate;
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;
137     for my $i (0..30) {
138         my $file = sprintf
139             (
140              "%s/secscnt%03d",
141              $root_from,
142              $i % 12,
143             );
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");
148         $rf->aggregate;
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;
156     }
160     BEGIN { $tests += 2 }
161     my $rf = File::Rsync::Mirror::Recentfile->new
162         (
163          filenameroot   => "RECENT",
164          interval       => q(1m),
165          localroot      => $root_to,
166          max_rsync_errors  => 0,
167          remote_dir     => $root_from,
168          verbose        => 1,
169          rsync_options  => {
170                             compress          => 0,
171                             links             => 1,
172                             times             => 1,
173                             # not available in rsync 3.0.3: 'omit-dir-times'  => 1,
174                             checksum          => 0,
175                            },
176         );
177     my $somefile_epoch;
178     for my $pass (0,1) {
179         my $success;
180         if (0 == $pass) {
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);
186         }
187         ok($success, "mirrored with success");
188     }
191 rmtree [$root_from, $root_to];
193 BEGIN { plan tests => $tests }
195 # Local Variables:
196 # mode: cperl
197 # cperl-indent-level: 4
198 # End: