File: 51_reiss_reuse.t

package info (click to toggle)
libpoe-component-client-keepalive-perl 0.2720-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 276 kB
  • sloc: perl: 2,465; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 2,168 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl

# Regression test for a bug which occured because a loop
# that would look for existing free connections would reuse
# a connection without removing it from a list of connections
# that can be closed if we have neared the connection limit.

use warnings;
use strict;
use lib qw(./mylib ../mylib);
use Test::More tests => 5;

sub POE::Kernel::ASSERT_DEFAULT () { 1 }

use POE;
use POE::Component::Client::Keepalive;
use POE::Component::Resolver;
use Socket qw(AF_INET);

use TestServer;
my $server_port  = TestServer->spawn(0);
my $another_port = TestServer->spawn(0);

POE::Session->create(
  inline_states => {
    _child           => sub { },
    _start           => \&start,
    _stop            => sub { },
    got_another_conn => \&got_another_conn,
    got_conn         => \&got_conn,
    got_error        => \&got_error,
  }
);

sub start {
  my $heap = $_[HEAP];

  $heap->{cm} = POE::Component::Client::Keepalive->new(
    max_open     => 2,
    max_per_host => 2,
    resolver     => POE::Component::Resolver->new(af_order => [ AF_INET ]),
  );

  $heap->{conn_count} = 0;

  for (1..3) {
    $heap->{cm}->allocate(
      scheme  => "http",
      addr    => "localhost",
      port    => $server_port,
      event   => "got_conn",
      context => "first/$_",
    );
  }
}

sub got_conn {
  my ($heap, $stuff) = @_[HEAP, ARG0];

  my $conn  = delete $stuff->{connection};
  my $which = $stuff->{context};

  if (defined $conn) {
    pass "$which request established asynchronously";
  }
  else {
    fail(
      "$which request $stuff->{function} error $stuff->{error_num}: " .
      $stuff->{error_str}
    );
  }

  $conn = undef;
  if (++$heap->{request_count} == 1) {
    $heap->{cm}->allocate(
      scheme => "http",
      addr => "localhost",
      port => $server_port,
      event => "got_conn",
      context => "second-a"
    );
    $heap->{cm}->allocate(
      scheme => "http",
      addr => "localhost",
      port => $another_port,
      event => "got_conn",
      context => "second-b"
    );
  }

  if ($heap->{request_count} == 5) {
    TestServer->shutdown();
    $heap->{cm}->shutdown();
  }
}

POE::Kernel->run();
exit;