1#!/usr/bin/perl
2
3# Test close() on connections.
4
5use warnings;
6use strict;
7use lib qw(./mylib ../mylib);
8use Test::More tests => 4;
9
10sub POE::Kernel::ASSERT_DEFAULT () { 1 }
11
12use POE;
13use POE::Component::Client::Keepalive;
14use POE::Component::Resolver;
15use Socket qw(AF_INET);
16
17use TestServer;
18my $server_port = TestServer->spawn(0);
19
20POE::Session->create(
21  inline_states => {
22    _child            => sub { },
23    _start            => \&start,
24    _stop             => sub { },
25    got_first_conn    => \&got_first_conn,
26    got_second_conn   => \&got_second_conn,
27    nothing           => sub { },
28  }
29);
30
31sub start {
32  my $heap = $_[HEAP];
33
34  $heap->{others} = 0;
35
36  $heap->{cm} = POE::Component::Client::Keepalive->new(
37    keep_alive   => 1,
38    max_open     => 1,
39    max_per_host => 1,
40    resolver     => POE::Component::Resolver->new(af_order => [ AF_INET ]),
41  );
42
43  $heap->{cm}->allocate(
44    scheme  => "http",
45    addr    => "localhost",
46    port    => $server_port,
47    event   => "got_first_conn",
48    context => "first",
49  );
50
51}
52
53sub got_first_conn {
54  my ($kernel, $heap, $stuff) = @_[KERNEL, HEAP, ARG0];
55
56  my $conn = $stuff->{connection};
57  my $which = $stuff->{context};
58  ok(!defined($stuff->{from_cache}), "$which uses a new connection");
59  ok(defined($conn), "first request honored asynchronously");
60
61
62  $conn->start( InputEvent => 'nothing' );
63
64  $conn->close();
65
66  undef $conn;
67
68  $heap->{cm}->allocate(
69    scheme => "http",
70    addr => "localhost",
71    port => $server_port,
72    event => "got_second_conn",
73    context => "second"
74  );
75}
76
77
78sub got_second_conn {
79  my ($kernel, $heap, $stuff) = @_[KERNEL, HEAP, ARG0];
80
81  my $conn  = $stuff->{connection};
82  my $which = $stuff->{context};
83  ok(defined($conn), "$which request honored asynchronously");
84  ok(!defined ($stuff->{from_cache}), "$which uses a new connection");
85
86  TestServer->shutdown();
87  $_[HEAP]->{cm}->shutdown();
88
89}
90
91POE::Kernel->run();
92exit;
93