« blib/lib/POE/Test/Loops/wheel_run_size.pm
↑ Summary
blib/lib/POE/Test/Loops/z_rt39872_sigchld_stop.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Exercises the wheels commonly used with TCP 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 | |||
| 19 | use POE qw( | ||
| 20 | Component::Server::TCP | ||
| 21 | Wheel::ReadWrite | ||
| 22 | Filter::Line | ||
| 23 | Driver::SysRW | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $tcp_server_port; | ||
| 27 | |||
| 28 | use Test::More; | ||
| 29 | |||
| 30 | unless (-f "run_network_tests") { | ||
| 31 | plan skip_all => "Network access (and permission) required to run this test"; | ||
| 32 | } | ||
| 33 | |||
| 34 | plan tests => 9; | ||
| 35 | |||
| 36 | ############################################################################### | ||
| 37 | # A generic server session. | ||
| 38 | |||
| 39 | sub sss_new { | ||
| 40 | my ($socket, $peer_addr, $peer_port) = @_; | ||
| 41 | POE::Session->create( | ||
| 42 | inline_states => { | ||
| 43 | _start => \&sss_start, | ||
| 44 | _stop => \&sss_stop, | ||
| 45 | got_line => \&sss_line, | ||
| 46 | got_error => \&sss_error, | ||
| 47 | got_flush => \&sss_flush, | ||
| 48 | _child => sub { }, | ||
| 49 | }, | ||
| 50 | args => [ $socket, $peer_addr, $peer_port ], | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub sss_start { | ||
| 55 | my ($heap, $socket, $peer_addr, $peer_port) = @_[HEAP, ARG0..ARG2]; | ||
| 56 | |||
| 57 | # Swap the SocketFactory for the ReadWrite. This exercises a subtle | ||
| 58 | # bug in SocketFactory which should now be fixed. | ||
| 59 | $heap->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 60 | Handle => $socket, | ||
| 61 | Driver => POE::Driver::SysRW->new( BlockSize => 10 ), | ||
| 62 | Filter => POE::Filter::Line->new(), | ||
| 63 | InputEvent => 'got_line', | ||
| 64 | ErrorEvent => 'got_error', | ||
| 65 | FlushedEvent => 'got_flush', | ||
| 66 | ); | ||
| 67 | |||
| 68 | $heap->{wheel_id} = $heap->{wheel}->ID; | ||
| 69 | $heap->{test_two} = 1; | ||
| 70 | |||
| 71 | $heap->{flush_count} = 0; | ||
| 72 | $heap->{put_count} = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub sss_line { | ||
| 76 | my ($heap, $line) = @_[HEAP, ARG0]; | ||
| 77 | |||
| 78 | $line =~ tr/a-zA-Z/n-za-mN-ZA-M/; # rot13 | ||
| 79 | |||
| 80 | $heap->{wheel}->put($line); | ||
| 81 | $heap->{put_count}++; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub sss_error { | ||
| 85 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 86 | |||
| 87 | ok(!$errnum, "sss expecting errnum 0; got $errnum"); | ||
| 88 | $heap->{test_two} = 0 if $errnum; | ||
| 89 | |||
| 90 | delete $heap->{wheel}; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub sss_flush { | ||
| 94 | $_[HEAP]->{flush_count}++; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub sss_stop { | ||
| 98 | my $heap = $_[HEAP]; | ||
| 99 | ok($heap->{test_two}, "test two"); | ||
| 100 | ok($heap->{put_count} == $heap->{flush_count}, "flushed all put data"); | ||
| 101 | } | ||
| 102 | |||
| 103 | ############################################################################### | ||
| 104 | # A TCP socket client. | ||
| 105 | |||
| 106 | sub client_tcp_start { | ||
| 107 | my $heap = $_[HEAP]; | ||
| 108 | |||
| 109 | $heap->{wheel} = POE::Wheel::SocketFactory->new( | ||
| 110 | RemoteAddress => '127.0.0.1', | ||
| 111 | RemotePort => $tcp_server_port, | ||
| 112 | SuccessEvent => 'got_server', | ||
| 113 | FailureEvent => 'got_error', | ||
| 114 | ); | ||
| 115 | |||
| 116 | $heap->{socket_wheel_id} = $heap->{wheel}->ID; | ||
| 117 | $heap->{test_five} = 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub client_tcp_stop { | ||
| 121 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 122 | ok($heap->{test_five}, "test five"); | ||
| 123 | ok($heap->{test_seven}, "test seven"); | ||
| 124 | $_[KERNEL]->post( tcp_server => 'shutdown' ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub client_tcp_connected { | ||
| 128 | my ($heap, $server_socket) = @_[HEAP, ARG0]; | ||
| 129 | |||
| 130 | delete $heap->{wheel}; | ||
| 131 | $heap->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 132 | Handle => $server_socket, | ||
| 133 | Driver => POE::Driver::SysRW->new( BlockSize => 10 ), | ||
| 134 | Filter => POE::Filter::Line->new(), | ||
| 135 | InputEvent => 'got_line', | ||
| 136 | ErrorEvent => 'got_error', | ||
| 137 | FlushedEvent => 'got_flush', | ||
| 138 | ); | ||
| 139 | |||
| 140 | $heap->{readwrite_wheel_id} = $heap->{wheel}->ID; | ||
| 141 | $heap->{test_seven} = 1; | ||
| 142 | |||
| 143 | $heap->{flush_count} = 0; | ||
| 144 | $heap->{put_count} = 1; | ||
| 145 | $heap->{wheel}->put( '1: this is a test' ); | ||
| 146 | |||
| 147 | ok($heap->{wheel}->get_driver_out_octets() == 19, "buffered 19 octets"); | ||
| 148 | ok($heap->{wheel}->get_driver_out_messages() == 1, "buffered 1 message"); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub client_tcp_got_line { | ||
| 152 | my ($heap, $line) = @_[HEAP, ARG0]; | ||
| 153 | |||
| 154 | if ($line =~ s/^1: //) { | ||
| 155 | $heap->{put_count}++; | ||
| 156 | $heap->{wheel}->put( '2: ' . $line ); | ||
| 157 | } | ||
| 158 | elsif ($line =~ s/^2: //) { | ||
| 159 | ok($line eq 'this is a test', "received test message"); | ||
| 160 | delete $heap->{wheel}; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | sub client_tcp_got_error { | ||
| 165 | my ($heap, $operation, $errnum, $errstr, $wheel_id) = @_[HEAP, ARG0..ARG3]; | ||
| 166 | |||
| 167 | if ($wheel_id == $heap->{socket_wheel_id}) { | ||
| 168 | $heap->{test_five} = 0; | ||
| 169 | } | ||
| 170 | |||
| 171 | if ($wheel_id == $heap->{readwrite_wheel_id}) { | ||
| 172 | $heap->{test_seven} = 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | delete $heap->{wheel}; | ||
| 176 | warn "$operation error $errnum: $errstr"; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub client_tcp_got_flush { | ||
| 180 | $_[HEAP]->{flush_count}++; | ||
| 181 | } | ||
| 182 | |||
| 183 | ############################################################################### | ||
| 184 | # Start the TCP server and client. | ||
| 185 | |||
| 186 | POE::Component::Server::TCP->new( | ||
| 187 | Port => 0, | ||
| 188 | Address => '127.0.0.1', | ||
| 189 | Alias => 'tcp_server', | ||
| 190 | Acceptor => sub { | ||
| 191 | &sss_new(@_[ARG0..ARG2]); | ||
| 192 | # This next badness is just for testing. | ||
| 193 | my $sockname = $_[HEAP]->{listener}->getsockname(); | ||
| 194 | delete $_[HEAP]->{listener}; | ||
| 195 | |||
| 196 | my ($port, $addr) = sockaddr_in($sockname); | ||
| 197 | $addr = inet_ntoa($addr); | ||
| 198 | ok( | ||
| 199 | ($addr eq '127.0.0.1') && | ||
| 200 | ($port == $tcp_server_port), | ||
| 201 | "received connection" | ||
| 202 | ); | ||
| 203 | }, | ||
| 204 | Started => sub { | ||
| 205 | $tcp_server_port = ( | ||
| 206 | sockaddr_in($_[HEAP]->{listener}->getsockname()) | ||
| 207 | )[0]; | ||
| 208 | }, | ||
| 209 | ); | ||
| 210 | |||
| 211 | POE::Session->create( | ||
| 212 | inline_states => { | ||
| 213 | _start => \&client_tcp_start, | ||
| 214 | _stop => \&client_tcp_stop, | ||
| 215 | got_server => \&client_tcp_connected, | ||
| 216 | got_line => \&client_tcp_got_line, | ||
| 217 | got_error => \&client_tcp_got_error, | ||
| 218 | got_flush => \&client_tcp_got_flush, | ||
| 219 | } | ||
| 220 | ); | ||
| 221 | |||
| 222 | ### main loop | ||
| 223 | |||
| 224 | POE::Kernel->run(); | ||
| 225 | |||
| 226 | 1; |