blib/lib/POE/Test/Loops/wheel_tail.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 253 CC 7 SCAR 14.6
CC
7
Cov
0%
client_tcp_got_error
12.0
sss_block
6.0
sss_error
6.0
CRAP 4.3
1 #!/usr/bin/perl -w
2 # vim: ts=2 sw=2 expandtab
3  
4 # Exercises Wheel::FollowTail, Wheel::ReadWrite, and Filter::Block.
5 # TODO - Needs tests for Seek and SeekBack.
6  
7 use strict;
8 use lib qw(./mylib ../mylib);
9 use Socket;
10  
11 sub POE::Kernel::ASSERT_DEFAULT () { 1 }
12 sub POE::Kernel::CATCH_EXCEPTIONS () { 0 }
13  
14 BEGIN {
15 package
16 POE::Kernel;
17 use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
18 }
19  
20 use Test::More;
21  
22 plan tests => 10;
23  
24 use POE qw(
25 Component::Server::TCP
26 Wheel::FollowTail
27 Wheel::ReadWrite
28 Wheel::SocketFactory
29 Filter::Line
30 Filter::Block
31 Driver::SysRW
32 );
33  
34 sub DEBUG () { 0 }
35  
36 my $tcp_server_port;
37 my $max_send_count = 10; # expected to be even
38  
39 ###############################################################################
40 # A generic server session.
41  
42 sub sss_new {
43 my ($socket, $peer_addr, $peer_port) = @_;
44 POE::Session->create(
45 inline_states => {
46 _start => \&sss_start,
47 _stop => \&sss_stop,
48 got_error => \&sss_error,
49 got_block => \&sss_block,
50 ev_timeout => sub {
51 DEBUG and warn "=== handle tail got timeout";
52 delete $_[HEAP]->{wheel};
53 },
54 },
55 args => [ $socket, $peer_addr, $peer_port ],
56 );
57 }
58  
59 sub sss_start {
60 my ($heap, $socket, $peer_addr, $peer_port) = @_[HEAP, ARG0..ARG2];
61  
62 delete $heap->{wheel};
63 $heap->{wheel} = POE::Wheel::FollowTail->new(
64 Handle => $socket,
65 Driver => POE::Driver::SysRW->new( BlockSize => 24 ),
66 Filter => POE::Filter::Block->new( BlockSize => 16 ),
67 InputEvent => 'got_block_nonexistent',
68 ErrorEvent => 'got_error_nonexistent',
69 );
70  
71 # Test event changing.
72 $heap->{wheel}->event(
73 InputEvent => 'got_block',
74 ErrorEvent => 'got_error',
75 );
76  
77 $heap->{test_two} = 1;
78 $heap->{wheel_id} = $heap->{wheel}->ID;
79 $heap->{read_count} = 0;
80 }
81  
82 sub sss_block {
83 my ($kernel, $heap, $block) = @_[KERNEL, HEAP, ARG0];
84 DEBUG and warn "=== handle tail got block ($block)";
85  
86 if ($block eq 'DONEDONEDONEDONE') {
87 $kernel->delay( ev_timeout => 1 );
88 return;
89 }
90  
91 $heap->{read_count}++;
92 $kernel->delay( ev_timeout => 10 );
93 }
94  
95 sub sss_error {
96 my ($heap, $syscall, $errnum, $errstr, $wheel_id) = @_[HEAP, ARG0..ARG3];
97 DEBUG and warn "=== handle tail got $syscall error $errnum: $errstr";
98 $_[HEAP]->{test_two} = 0 if $errnum;
99 }
100  
101 sub sss_stop {
102 my $heap = $_[HEAP];
103 DEBUG and warn "=== handle tail stopped";
104 ok($heap->{test_two}, "handle tail test two");
105 is(
106 $heap->{read_count}, $max_send_count,
107 "handle tail read everything we were sent " .
108 "did($heap->{read_count}) wanted($max_send_count)"
109 );
110 }
111  
112 ###############################################################################
113 # A TCP socket client.
114  
115 sub client_tcp_start {
116 my $heap = $_[HEAP];
117  
118 DEBUG and warn "=== client tcp started";
119  
120 $heap->{wheel} = POE::Wheel::SocketFactory->new(
121 RemoteAddress => '127.0.0.1',
122 RemotePort => $tcp_server_port,
123 SuccessEvent => 'got_server_nonexistent',
124 FailureEvent => 'got_error_nonexistent',
125 );
126  
127 # Test event changing.
128 $heap->{wheel}->event(
129 SuccessEvent => 'got_server',
130 FailureEvent => 'got_error',
131 );
132  
133 $heap->{socketfactory_wheel_id} = $heap->{wheel}->ID;
134 $heap->{test_three} = 1;
135 }
136  
137 sub client_tcp_stop {
138 my $heap =$_[HEAP];
139 ok(
140 $heap->{test_three},
141 "test three"
142 );
143 ok(
144 $heap->{put_count} == $max_send_count,
145 "sent everything we should"
146 );
147  
148 my $sent_count = $_[HEAP]->{put_count} / 2;
149 ok(
150 $heap->{flush_count} == $sent_count,
151 "flushed what we sent (flush=$heap->{flush_count}; sent=$sent_count)"
152 );
153 ok(
154 $heap->{test_six},
155 "test six"
156 );
157 }
158  
159 sub client_tcp_connected {
160 my ($kernel, $heap, $server_socket) = @_[KERNEL, HEAP, ARG0];
161  
162 delete $heap->{wheel};
163 $heap->{wheel} = POE::Wheel::ReadWrite->new(
164 Handle => $server_socket,
165 Driver => POE::Driver::SysRW->new( BlockSize => 32 ),
166 Filter => POE::Filter::Block->new( BlockSize => 16 ),
167 ErrorEvent => 'got_error_nonexistent',
168 FlushedEvent => 'got_flush_nonexistent',
169 );
170  
171 DEBUG and warn "=== client tcp connected";
172  
173 # Test event changing.
174 $heap->{wheel}->event(
175 ErrorEvent => 'got_error',
176 FlushedEvent => 'got_flush',
177 );
178  
179 $heap->{test_six} = 1;
180 $heap->{readwrite_wheel_id} = $heap->{wheel}->ID;
181  
182 $heap->{flush_count} = 0;
183 $heap->{put_count} = 0;
184  
185 $kernel->yield( 'next_send' );
186 }
187  
188 sub client_tcp_next_send {
189 my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0];
190  
191 DEBUG and warn "=== client tcp got alarm";
192  
193 $heap->{wheel}->put( '0123456789ABCDEF0123456789ABCDEF' );
194 $heap->{put_count} += 2;
195  
196 # Trigger shutdown after the last test send.
197 if ($heap->{put_count} >= $max_send_count) {
198 $heap->{wheel}->put( 'DONEDONEDONEDONE' );
199 }
200 }
201  
202 sub client_tcp_got_error {
203 my ($heap, $operation, $errnum, $errstr, $wheel_id) = @_[HEAP, ARG0..ARG3];
204  
205 if ($wheel_id == $heap->{socketfactory_wheel_id}) {
206 $heap->{test_three} = 0;
207 }
208  
209 if ($wheel_id == $heap->{readwrite_wheel_id}) {
210 $heap->{test_six} = 0;
211 }
212  
213 delete $heap->{wheel};
214 warn "$operation error $errnum: $errstr";
215 }
216  
217 sub client_tcp_got_flush {
218 $_[HEAP]->{flush_count}++;
219 DEBUG and warn "=== client_tcp_got_flush";
220  
221 if ($_[HEAP]->{put_count} < $max_send_count) {
222 # Puts a little delay between puts.
223 $_[KERNEL]->delay( next_send => 0.100 );
224 }
225 else {
226 # Delays destruction until all data is out.
227 delete $_[HEAP]->{wheel};
228 }
229 }
230  
231 ###############################################################################
232 # Start the TCP server and client.
233  
234 SKIP: {
235 unless (-f "run_network_tests") {
236 skip "network access (and permission) required to run this test", 7;
237 }
238  
239 POE::Component::Server::TCP->new(
240 Port => 0,
241 Address => '127.0.0.1',
242 Acceptor => sub {
243 &sss_new(@_[ARG0..ARG2]);
244 # This next badness is just for testing.
245 my $sockname = $_[HEAP]->{listener}->getsockname();
246 delete $_[HEAP]->{listener};
247  
248 my ($port, $addr) = sockaddr_in($sockname);
249 $addr = inet_ntoa($addr);
250  
251 ok(
252 ($addr eq '127.0.0.1') && ($port == $tcp_server_port),
253 "received connection"
254 );
255 },
256 Started => sub {
257 $tcp_server_port = (
258 sockaddr_in($_[HEAP]->{listener}->getsockname())
259 )[0];
260 },
261 );
262  
263 POE::Session->create(
264 inline_states => {
265 _start => \&client_tcp_start,
266 _stop => \&client_tcp_stop,
267 got_server => \&client_tcp_connected,
268 got_error => \&client_tcp_got_error,
269 got_flush => \&client_tcp_got_flush,
270 next_send => \&client_tcp_next_send,
271 }
272 );
273 }
274  
275 ### Test a file that appears and disappears.
276  
277 SKIP: {
278 if (($^O eq 'MSWin32' or $^O eq 'cygwin') and not $ENV{POE_DANTIC}) {
279 skip "Can't test file reset on $^O because the OS locks opened files", 2;
280 }
281  
282 POE::Session->create(
283 inline_states => {
284 _start => sub {
285 my ($kernel, $heap) = @_[KERNEL, HEAP];
286  
287 unlink "./test-tail-file";
288 $heap->{wheel} = POE::Wheel::FollowTail->new(
289 Filter => POE::Filter::Line->new(Literal => "\n"),
290 Filename => "./test-tail-file",
291 InputEvent => "got_input",
292 ErrorEvent => "got_error",
293 ResetEvent => "got_reset",
294 IdleEvent => "create_file",
295 PollInterval => 0.500,
296 );
297 $heap->{sent_count} = 0;
298 $heap->{recv_count} = 0;
299 $heap->{reset_count} = 0;
300 DEBUG and warn "=== file tail start";
301 },
302  
303 create_file => sub {
304 open(FH, ">./test-tail-file") or die $!;
305 print FH "moo\n";
306 close FH;
307 DEBUG and warn "=== file tail create file";
308 $_[HEAP]->{sent_count}++;
309  
310 # Make file creation a one-shot occurrence.
311 $_[HEAP]->{wheel}->event( IdleEvent => undef );
312 },
313  
314 got_input => sub {
315 my ($kernel, $heap) = @_[KERNEL, HEAP];
316 $heap->{recv_count}++;
317  
318 DEBUG and warn "=== file tail input: $_[ARG0]\n";
319  
320 unlink "./test-tail-file";
321  
322 if ($heap->{recv_count} == 1) {
323 # Trigger a new one-shot file creation.
324 $heap->{wheel}->event( IdleEvent => 'create_file' );
325 return;
326 }
327  
328 delete $heap->{wheel};
329 },
330  
331 got_error => sub { warn "$_[ARG0] error $_[ARG1]: $_[ARG2]"; die },
332  
333 got_reset => sub {
334 DEBUG and warn "=== file tail got reset";
335 $_[HEAP]->{reset_count}++;
336 },
337  
338 _stop => sub {
339 DEBUG and warn "=== file tail stop";
340 my $heap = $_[HEAP];
341 ok(
342 ($heap->{sent_count} == $heap->{recv_count}) &&
343 ($heap->{sent_count} == 2),
344 "file tail sent and received everything we should " .
345 "sent($heap->{sent_count}) recv($heap->{recv_count}) wanted(2)"
346 );
347 is($heap->{reset_count}, 2, "file tail resets detected");
348 },
349 },
350 );
351 }
352  
353 ### main loop
354  
355 POE::Kernel->run();
356  
357 pass("run() returned successfully");
358  
359 1;