| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # POE::XS::Loop::Poll wasn't handling errors correctly, this was | ||
| 5 | # particularly noticeable for connect() failures, so check connection | ||
| 6 | # failures are handled correctly | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | |||
| 10 | use Test::More; | ||
| 11 | |||
| 12 | unless (-f "run_network_tests") { | ||
| 13 | plan skip_all => "Network access (and permission) required to run this test"; | ||
| 14 | } | ||
| 15 | |||
| 16 | # MSWin32+ActiveState 5.6.1 and 5.10.1 always time out. And if we remove the | ||
| 17 | # delay, then the OS never times out. 5.8.0 seems to work fine. Since this | ||
| 18 | # behavior seems to come and go, we're skipping it for all versions of MSWin32. | ||
| 19 | if ($^O eq 'MSWin32' and not $ENV{POE_DANTIC}) { | ||
| 20 | plan skip_all => "This test fails for various versions of MSWin32 perl"; | ||
| 21 | } | ||
| 22 | |||
| 23 | plan tests => 3; | ||
| 24 | |||
| 25 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 26 | |||
| 27 | BEGIN { | ||
| 28 | package | ||
| 29 | POE::Kernel; | ||
| 30 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 31 | } | ||
| 32 | |||
| 33 | use POE qw( Wheel::ReadWrite Component::Client::TCP ); | ||
| 34 | |||
| 35 | # Dynamically find an unused port for the failure-to-connect test. | ||
| 36 | # Listen on the port, but accept no connections there. | ||
| 37 | |||
| 38 | my $unused_port; | ||
| 39 | { | ||
| 40 | use IO::Socket::INET; | ||
| 41 | my $reserved = IO::Socket::INET->new( | ||
| 42 | LocalAddr => '127.0.0.1', | ||
| 43 | ReuseAddr => 0, | ||
| 44 | ); | ||
| 45 | if (defined $reserved) { | ||
| 46 | $unused_port = (sockaddr_in(getsockname($reserved)))[0]; | ||
| 47 | pass("found unused port: $unused_port"); | ||
| 48 | } else { | ||
| 49 | fail("found unused port error: $@"); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | # Timeout. | ||
| 54 | |||
| 55 | POE::Session->create( | ||
| 56 | inline_states => { | ||
| 57 | _start => sub { | ||
| 58 | $poe_kernel->alias_set('watcher'); | ||
| 59 | $_[HEAP]{alarm} = $poe_kernel->delay_set(timeout => 10); | ||
| 60 | }, | ||
| 61 | timeout => sub { | ||
| 62 | $poe_kernel->post(client => 'shutdown'); | ||
| 63 | fail("timeout for connection"); | ||
| 64 | }, | ||
| 65 | shutdown => sub { | ||
| 66 | $poe_kernel->alarm_remove($_[HEAP]{alarm}); | ||
| 67 | }, | ||
| 68 | _stop => sub { }, # Pacify assertions. | ||
| 69 | } | ||
| 70 | ); | ||
| 71 | |||
| 72 | # Test connection failure. | ||
| 73 | |||
| 74 | POE::Component::Client::TCP->new( | ||
| 75 | RemotePort => $unused_port, | ||
| 76 | RemoteAddress => '127.0.0.1', | ||
| 77 | Alias => 'client', | ||
| 78 | Connected => sub { | ||
| 79 | fail("should have failed to connect"); | ||
| 80 | }, | ||
| 81 | ConnectError => sub { | ||
| 82 | $poe_kernel->post(watcher => 'shutdown'); | ||
| 83 | pass("expected connection failure occurred"); | ||
| 84 | }, | ||
| 85 | ServerInput => sub { | ||
| 86 | warn "ServerInput called unexpectedly\n"; | ||
| 87 | }, | ||
| 88 | ); | ||
| 89 | |||
| 90 | # Run the tests. | ||
| 91 | |||
| 92 | POE::Kernel->run(); | ||
| 93 | |||
| 94 | pass("run() returned successfully"); | ||
| 95 | |||
| 96 | 1; |