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

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 200 CC 15 SCAR 21.3
CC
15
Cov
0%
BEGIN
30.0
server_unix_child
12.0
client_unix_got_line
12.0
CRAP 8.4
1 #!/usr/bin/perl -w
2 # vim: ts=2 sw=2 expandtab
3  
4 # Exercises the wheels commonly used with UNIX domain sockets.
5  
6 use strict;
7 use lib qw(./mylib ../mylib);
8  
9 sub POE::Kernel::ASSERT_DEFAULT () { 1 }
10  
11 BEGIN {
12 package
13 POE::Kernel;
14 use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
15 }
16  
17 use Socket;
18 use File::Temp qw(tempfile);
19  
20 # We can't test_setup(0, "reason") because that calls exit(). And Tk
21 # will croak if you call BEGIN { exit() }. And that croak will cause
22 # this test to FAIL instead of skip.
23 BEGIN {
24 my $error;
25 unless (-f 'run_network_tests') {
26 $error = "Network access (and permission) required to run this test";
27 }
28 elsif (($^O eq "MSWin32" or $^O eq "MacOS") and not $ENV{POE_DANTIC}) {
29 $error = "$^O does not support UNIX sockets";
30 }
31 elsif ($^O eq "cygwin" and not $ENV{POE_DANTIC}) {
32 $error = "UNIX sockets on $^O always block";
33 }
34  
35 if ($error) {
36 # Not using Test::More so we can avoid Tk::exit.
37 print "1..0 # Skip $error\n";
38 CORE::exit();
39 }
40 }
41  
42 use Test::More tests => 12;
43  
44 use POE qw(
45 Wheel::SocketFactory
46 Wheel::ReadWrite
47 Filter::Line
48 Driver::SysRW
49 );
50  
51 my (undef, $unix_server_socket) = tempfile('poe-usrv-XXXXXX');
52  
53 ###############################################################################
54 # A generic server session.
55  
56 sub sss_new {
57 my ($socket, $peer_addr, $peer_port) = @_;
58 POE::Session->create(
59 inline_states => {
60 _start => \&sss_start,
61 _stop => \&sss_stop,
62 got_line => \&sss_line,
63 got_error => \&sss_error,
64 got_flush => \&sss_flush,
65 },
66 args => [ $socket, $peer_addr, $peer_port ],
67 );
68 }
69  
70 sub sss_start {
71 my ($heap, $socket, $peer_addr, $peer_port) = @_[HEAP, ARG0..ARG2];
72  
73 delete $heap->{wheel};
74 $heap->{wheel} = POE::Wheel::ReadWrite->new(
75 Handle => $socket,
76 Driver => POE::Driver::SysRW->new( BlockSize => 10 ),
77 Filter => POE::Filter::Line->new(),
78 InputEvent => 'got_line',
79 ErrorEvent => 'got_error',
80 FlushedEvent => 'got_flush',
81 );
82  
83 $heap->{wheel_id} = $heap->{wheel}->ID;
84 $heap->{test_six} = 1;
85 $heap->{flush_count} = 0;
86 $heap->{put_count} = 0;
87 }
88  
89 sub sss_line {
90 my ($heap, $line) = @_[HEAP, ARG0];
91  
92 $line =~ tr/a-zA-Z/n-za-mN-ZA-M/; # rot13
93  
94 $heap->{wheel}->put($line);
95 $heap->{put_count}++;
96 }
97  
98 sub sss_error {
99 my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2];
100  
101 ok(!$errnum, "sss expecting errnum 0; got $errnum");
102 $heap->{test_six} = 0 if $errnum;
103  
104 delete $heap->{wheel};
105 }
106  
107 sub sss_flush {
108 $_[HEAP]->{flush_count}++;
109 }
110  
111 sub sss_stop {
112 my $heap = $_[HEAP];
113 ok($heap->{test_six}, "test six");
114 ok(
115 $_[HEAP]->{put_count} == $_[HEAP]->{flush_count},
116 "flushed everything we put"
117 );
118 }
119  
120 ###############################################################################
121 # A UNIX domain socket server.
122  
123 sub server_unix_start {
124 my $heap = $_[HEAP];
125  
126 unlink $unix_server_socket if -e $unix_server_socket;
127  
128 $heap->{wheel} = POE::Wheel::SocketFactory->new(
129 SocketDomain => PF_UNIX,
130 BindAddress => $unix_server_socket,
131 SuccessEvent => 'got_client',
132 FailureEvent => 'got_error',
133 );
134  
135 $heap->{client_count} = 0;
136 $heap->{test_two} = 1;
137 }
138  
139 sub server_unix_stop {
140 my $heap = $_[HEAP];
141  
142 delete $heap->{wheel};
143  
144 ok($heap->{test_two}, "test two");
145 ok($heap->{client_count} == 1, "only one client");
146  
147 unlink $unix_server_socket if -e $unix_server_socket;
148 }
149  
150 sub server_unix_answered {
151 $_[HEAP]->{client_count}++;
152 sss_new(@_[ARG0..ARG2]);
153 }
154  
155 sub server_unix_error {
156 my ($session, $heap, $operation, $errnum, $errstr, $wheel_id) =
157 @_[SESSION, HEAP, ARG0..ARG3];
158  
159 if ($wheel_id == $heap->{wheel}->ID) {
160 delete $heap->{wheel};
161 $heap->{test_two} = 0;
162 }
163  
164 warn $session->ID, " got $operation error $errnum: $errstr\n";
165 }
166  
167 # This arrives with 'lose' when a server session has closed.
168 sub server_unix_child {
169 if ($_[ARG0] eq 'create') {
170 $_[HEAP]->{child} = $_[ARG1];
171 }
172 if ($_[ARG0] eq 'lose') {
173 delete $_[HEAP]->{wheel};
174 ok(
175 $_[ARG1] == $_[HEAP]->{child},
176 "lost expected child session"
177 );
178 }
179 }
180  
181 ###############################################################################
182 # A UNIX domain socket client.
183  
184 sub client_unix_start {
185 my $heap = $_[HEAP];
186  
187 $heap->{wheel} = POE::Wheel::SocketFactory->new(
188 SocketDomain => PF_UNIX,
189 RemoteAddress => $unix_server_socket,
190 SuccessEvent => 'got_server',
191 FailureEvent => 'got_error',
192 );
193  
194 $heap->{socket_wheel_id} = $heap->{wheel}->ID;
195 $heap->{test_three} = 1;
196 }
197  
198 sub client_unix_stop {
199 my $heap = $_[HEAP];
200 ok($heap->{test_three}, "test three");
201 ok($heap->{test_four}, "test four");
202 }
203  
204 sub client_unix_connected {
205 my ($heap, $server_socket) = @_[HEAP, ARG0];
206  
207 delete $heap->{wheel};
208 $heap->{wheel} = POE::Wheel::ReadWrite->new(
209 Handle => $server_socket,
210 Driver => POE::Driver::SysRW->new( BlockSize => 10 ),
211 Filter => POE::Filter::Line->new(),
212 InputEvent => 'got_line',
213 ErrorEvent => 'got_error',
214 FlushedEvent => 'got_flush',
215 );
216  
217 $heap->{readwrite_wheel_id} = $heap->{wheel}->ID;
218 $heap->{test_four} = 1;
219 $heap->{flush_count} = 0;
220 $heap->{put_count} = 1;
221 $heap->{wheel}->put( '1: this is a test' );
222  
223 ok(
224 $heap->{wheel}->get_driver_out_octets() == 19,
225 "buffered 19 octets"
226 );
227 ok(
228 $heap->{wheel}->get_driver_out_messages() == 1,
229 "buffered 1 message"
230 );
231 }
232  
233 sub client_unix_got_line {
234 my ($heap, $line) = @_[HEAP, ARG0];
235  
236 if ($line =~ s/^1: //) {
237 $heap->{put_count}++;
238 $heap->{wheel}->put( '2: ' . $line );
239 }
240 elsif ($line =~ s/^2: //) {
241 ok(
242 $line eq 'this is a test',
243 "received expected text"
244 );
245 delete $heap->{wheel};
246 }
247 }
248  
249 sub client_unix_got_error {
250 my ($session, $heap, $operation, $errnum, $errstr, $wheel_id) =
251 @_[SESSION, HEAP, ARG0..ARG3];
252  
253 if ($wheel_id == $heap->{socket_wheel_id}) {
254 $heap->{test_three} = 0;
255 }
256  
257 if ($wheel_id == $heap->{readwrite_wheel_id}) {
258 $heap->{test_four} = 0;
259 }
260  
261 delete $heap->{wheel};
262 warn $session->ID, " caught $operation error $errnum: $errstr";
263 }
264  
265 sub client_unix_got_flush {
266 $_[HEAP]->{flush_count}++;
267 }
268  
269 ### Start the UNIX domain server and client.
270  
271 POE::Session->create(
272 inline_states => {
273 _start => \&server_unix_start,
274 _stop => \&server_unix_stop,
275 _child => \&server_unix_child,
276 got_client => \&server_unix_answered,
277 got_error => \&server_unix_error,
278 }
279 );
280  
281 POE::Session->create(
282 inline_states => {
283 _start => \&client_unix_start,
284 _stop => \&client_unix_stop,
285 got_server => \&client_unix_connected,
286 got_line => \&client_unix_got_line,
287 got_error => \&client_unix_got_error,
288 got_flush => \&client_unix_got_flush
289 }
290 );
291  
292 ### main loop
293  
294 POE::Kernel->run();
295  
296 pass("run() returned normally");
297  
298 1;