Debian Codegraph @

t/90_regression/whjackson-followtail.t

raw symbols SCIP
1 #!/usr/bin/perl
2 # vim: ts=2 sw=2 filetype=perl expandtab
3
4 # This regression test verifies what happens when the following
5 # happens in between two polls of a log file:
6 #
7 # 1. A log file is rolled by being renamed out of the way.
8 # 2. The new log is created by appending to the original file location.
9 #
10 # The desired result is the first log lines are fetched to completion
11 # before the new log is opened. No data is lost in this case.
12
13 use strictscip-perl cpan POE 1.370 strict/;
14 use warningsscip-perl cpan POE 1.370 warnings/;
15
16 sub POE::Kernel::ASSERT_DEFAULTscip-perl cpan POE 1.370 main/`POE::Kernel::ASSERT_DEFAULT`(). () { 1 }
17
18 use Test::Morescip-perl cpan POE 1.370 Test/More/;
19 use POEscip-perl cpan POE 1.370 POE/ qw(Wheel::FollowTail);
20 use POE::Test::Sequencescip-perl cpan POE 1.370 POE/Test/Sequence/;
21
22 use constantscip-perl cpan POE 1.370 constant/ LOGscip-perl cpan POE 1.370 main/LOG(). => 'test_log';
23 use constantscip-perl cpan POE 1.370 constant/ OLD_LOGscip-perl cpan POE 1.370 main/OLD_LOG(). => 'test_log.1';
24
25 # TODO - Perhaps POE::Wheel::FollowTail should close its file at the
26 # end of a poll and reopen it at the start of the next? At least on
27 # silly systems like DOS^H^H^HWindows?
28
29 {
30 openscip-perl cpan POE 1.370 main/open(). my $fh, '>>', LOG or diescip-perl cpan POE 1.370 main/die(). "open failed: $!";
31 unless (renamescip-perl cpan POE 1.370 main/rename(). LOG, OLD_LOG) {
32 planscip-perl cpan POE 1.370 main/plan(). skip_all => "$^O cannot rename files that are open";
33 }
34 closescip-perl cpan POE 1.370 main/close(). $fh;
35 unlinkscip-perl cpan POE 1.370 main/unlink(). LOG, OLD_LOG;
36 }
37
38 my $sequence = POE::Test::Sequence->newscip-perl cpan POE 1.370 POE/Test/Sequence/new().(
39 sequence => [
40 [ got_start_event => 0, sub {
41 $_scip-perl cpan POE 1.370 main/`@_`.[HEAP]{wheel} = POE::Wheel::FollowTail->newscip-perl cpan POE 1.370 POE/Wheel/FollowTail/new().(
42 InputEvent => 'input_event',
43 ResetEvent => 'reset_event',
44 IdleEvent => 'idle_event',
45 Filename => LOG,
46 PollInterval => 1,
47 );
48 }
49 ],
50 [ got_idle_event => 0, sub { append_to_logscip-perl cpan POE 1.370 main/append_to_log().("a") } ],
51 [ did_log_append => "a", undef ],
52 [ got_reset_event => 0, undef ], # Initial open is a reset.
53 [ got_input_event => "a", undef ],
54 [ got_idle_event => 0, sub {
55 append_to_logscip-perl cpan POE 1.370 main/append_to_log().("b");
56 roll_logscip-perl cpan POE 1.370 main/roll_log().();
57 append_to_logscip-perl cpan POE 1.370 main/append_to_log().("c");
58 }
59 ],
60 [ did_log_append => "b", undef ],
61 [ did_log_roll => 0, undef ],
62 [ did_log_append => "c", undef ],
63 [ got_input_event => "b", undef ],
64 [ got_reset_event => 0, undef ],
65 [ got_input_event => "c", sub { append_to_logscip-perl cpan POE 1.370 main/append_to_log().("d") } ],
66 [ did_log_append => "d", undef ],
67 [ got_input_event => "d", sub { deletescip-perl cpan POE 1.370 main/delete(). $_scip-perl cpan POE 1.370 main/`@_`.[HEAP]{wheel} } ],
68 [ got_stop_event => 0, sub {
69 # Clean up test log files, if we can.
70 unlinkscip-perl cpan POE 1.370 main/unlink(). LOG or diescip-perl cpan POE 1.370 main/die(). "unlink failed: $!";
71 unlinkscip-perl cpan POE 1.370 main/unlink(). OLD_LOG or diescip-perl cpan POE 1.370 main/die(). "unlink failed: $!";
72 }
73 ],
74 ],
75 );
76
77 planscip-perl cpan POE 1.370 main/plan(). tests => $sequence->test_countscip-perl cpan POE 1.370 POE/Test/Sequence/test_count().();
78
79 POE::Session->createscip-perl cpan POE 1.370 POE/Session/create().(
80 inline_states => {
81 _start => sub { goto $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("got_start_event", 0) },
82 _stop => sub { goto $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("got_stop_event", 0) },
83 input_event => sub { goto $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("got_input_event", $_scip-perl cpan POE 1.370 main/`@_`.[ARG0]) },
84 reset_event => sub { goto $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("got_reset_event", 0) },
85 idle_event => sub { goto $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("got_idle_event", 0) },
86 }
87 );
88
89 POE::Kernel->runscip-perl cpan POE 1.370 POE/Kernel/run().();
90 exitscip-perl cpan POE 1.370 main/exit().;
91
92 # Helpers.
93
94 sub roll_logscip-perl cpan POE 1.370 main/roll_log(). {
95 $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("did_log_roll", 0);
96 renamescip-perl cpan POE 1.370 main/rename(). LOG, OLD_LOG or diescip-perl cpan POE 1.370 main/die(). "rename failed: $!";
97 return;
98 }
99
100 sub append_to_logscip-perl cpan POE 1.370 main/append_to_log(). {
101 my ($line) = @_scip-perl cpan POE 1.370 main/`@_`.;
102
103 $sequence->nextscip-perl cpan POE 1.370 POE/Test/Sequence/next().("did_log_append", $line);
104
105 openscip-perl cpan POE 1.370 main/open(). my $fh, '>>', LOG or diescip-perl cpan POE 1.370 main/die(). "open failed: $!";
106 print {$fh} "$line\n";
107 closescip-perl cpan POE 1.370 main/close(). $fh or diescip-perl cpan POE 1.370 main/die(). "close failed: $!";
108
109 return;
110 }
111
112 1;