« blib/lib/POE/Test/Loops/z_steinert_signal_integrity.pm
↑ Summary
blib/lib/POE/Test/Loops/z_rt39872_sigchld.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Exercises the ListenAccept wheel. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use lib qw(./mylib ../mylib); | ||
| 8 | use IO::Socket; | ||
| 9 | |||
| 10 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | package | ||
| 14 | POE::Kernel; | ||
| 15 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 16 | } | ||
| 17 | |||
| 18 | use Test::More; | ||
| 19 | use POE qw(Wheel::ListenAccept Wheel::SocketFactory); | ||
| 20 | |||
| 21 | unless (-f "run_network_tests") { | ||
| 22 | plan skip_all => "Network access (and permission) required to run this test"; | ||
| 23 | } | ||
| 24 | |||
| 25 | use constant CONNECTOR_COUNT => 5; | ||
| 26 | plan tests => 2; | ||
| 27 | |||
| 28 | my $bound_port; | ||
| 29 | |||
| 30 | ### A listening session. | ||
| 31 | sub listener_start { | ||
| 32 | my $heap = $_[HEAP]; | ||
| 33 | |||
| 34 | my $listening_socket = IO::Socket::INET->new( | ||
| 35 | LocalAddr => '127.0.0.1', | ||
| 36 | #LocalPort => 0, # 0 is the default, and as a bonus this works on MSWin32+ActiveState 5.6.1 | ||
| 37 | Listen => 5, | ||
| 38 | Proto => 'tcp', | ||
| 39 | Reuse => 'yes', | ||
| 40 | ); | ||
| 41 | |||
| 42 | if (defined $listening_socket) { | ||
| 43 | pass("created listening socket"); | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | fail("created listening socket error: $@"); | ||
| 47 | fail("listening socket accepted connections"); | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | $bound_port = (sockaddr_in(getsockname($listening_socket)))[0]; | ||
| 52 | |||
| 53 | $heap->{listener_wheel} = POE::Wheel::ListenAccept->new( | ||
| 54 | Handle => $listening_socket, | ||
| 55 | AcceptEvent => 'got_connection_nonexistent', | ||
| 56 | ErrorEvent => 'got_error_nonexistent' | ||
| 57 | ); | ||
| 58 | |||
| 59 | $heap->{listener_wheel}->event( | ||
| 60 | AcceptEvent => 'got_connection', | ||
| 61 | ErrorEvent => 'got_error' | ||
| 62 | ); | ||
| 63 | |||
| 64 | $heap->{accept_count} = 0; | ||
| 65 | $_[KERNEL]->delay( got_timeout => 10 ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub listener_stop { | ||
| 69 | if (defined $bound_port) { | ||
| 70 | ok( | ||
| 71 | $_[HEAP]->{accept_count} == CONNECTOR_COUNT, | ||
| 72 | "listening socket accepted connections" | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | sub listener_got_connection { | ||
| 78 | if (++$_[HEAP]->{accept_count} == CONNECTOR_COUNT) { | ||
| 79 | $_[KERNEL]->delay( got_timeout => 1 ); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub listener_got_error { | ||
| 84 | delete $_[HEAP]->{listener_wheel}; | ||
| 85 | $_[KERNEL]->delay( got_timeout => 1 ); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub listener_got_timeout { | ||
| 89 | delete $_[HEAP]->{listener_wheel}; | ||
| 90 | } | ||
| 91 | |||
| 92 | ### A connecting session. | ||
| 93 | sub connector_start { | ||
| 94 | $_[HEAP]->{connector_wheel} = POE::Wheel::SocketFactory->new( | ||
| 95 | RemoteAddress => '127.0.0.1', | ||
| 96 | RemotePort => $bound_port, | ||
| 97 | SuccessEvent => 'got_connection', | ||
| 98 | FailureEvent => 'got_error', | ||
| 99 | ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub connector_got_connection { | ||
| 103 | delete $_[HEAP]->{connector_wheel}; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub connector_got_error { | ||
| 107 | delete $_[HEAP]->{connector_wheel}; | ||
| 108 | } | ||
| 109 | |||
| 110 | ### Main loop. | ||
| 111 | |||
| 112 | POE::Session->create( | ||
| 113 | inline_states => { | ||
| 114 | _start => \&listener_start, | ||
| 115 | _stop => \&listener_stop, | ||
| 116 | got_connection => \&listener_got_connection, | ||
| 117 | got_error => \&listener_got_error, | ||
| 118 | got_timeout => \&listener_got_timeout, | ||
| 119 | } | ||
| 120 | ); | ||
| 121 | |||
| 122 | if (defined $bound_port) { | ||
| 123 | foreach (1..CONNECTOR_COUNT) { | ||
| 124 | POE::Session->create( | ||
| 125 | inline_states => { | ||
| 126 | _start => \&connector_start, | ||
| 127 | got_connection => \&connector_got_connection, | ||
| 128 | got_error => \&connector_got_error, | ||
| 129 | _stop => sub { }, # Pacify assertions. | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | $poe_kernel->run(); | ||
| 136 | |||
| 137 | 1; |