File: 10_resolver.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 (125 lines) | stat: -rw-r--r-- 2,934 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl

# Test connection reuse.  Allocates a connection, frees it, and
# allocates another.  The second allocation should return right away
# because it is honored from the keep-alive pool.

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

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 $test_server_use_count = 0;

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

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

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

  $_[KERNEL]->alias_set ('WITH');
  $heap->{cm} = POE::Component::Client::Keepalive->new(
    resolver => POE::Component::Resolver->new(af_order => [ AF_INET ]),
  );

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

  ++$test_server_use_count;
}

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

  $_[KERNEL]->alias_set ('WITHOUT');
  $heap->{cm} = POE::Component::Client::Keepalive->new(
    resolver => POE::Component::Resolver->new(af_order => [ AF_INET ]),
  );

  $heap->{cm}->allocate(
    scheme  => "http",
    addr    => "localhost",
    port    => $server_port,
    event   => "got_conn",
    context => "second",
  );

  ++$test_server_use_count;
}

# TODO - I think this callback is polymorphic (first vs. second)
# bcause it has common code.  It's probably cleaner to implement two
# separate callbacks and some helpers to handle their commonalities.

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

  # The delete() ensures only one copy of the connection exists.
  my $connection = delete $response->{connection};
  my $which = $response->{context};

  if (defined $connection) {
    pass "$which request honored asynchronously";
  }
  else {
    fail(
      "$which request $response->{function} error $response->{error_num}: " .
      $response->{error_str}
    );
  }

  ok(
    (not defined $response->{'from_cache'}),
    "$which request not from cache"
  );

  if ($which eq 'first') {
    ok(1, "$which request from internal resolver");
  } elsif ($which eq 'second') {
    ok(1, "$which request from external resolver");
  }

  TestServer->shutdown() unless --$test_server_use_count;

  # need this so we don't get trace output about our session having
  # already died
  $connection = undef;
  # and this so we can terminate without having to go through the
  # idle polling period
  $heap->{cm}->shutdown;
  # and this so we terminate at all
  delete $heap->{cm};
}

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