sub
my_sleep {
my
(
$s
) =
@_
;
::diag(
"$$: Sleep $s..."
);
sleep
$s
;
::diag(
"$$: continue"
);
}
*::my_sleep = \
&my_sleep
;
sub
tests
{
my
(
$PORT
,
$KA_MAX
,
$S
) =
@_
;
my_sleep
$S
;
my
$MAX
= 2;
my
@UA
;
my
$CC
= LWP::ConnCache->new();
my
@C
;
my
@CID
;
foreach
my
$n
( 0..
$MAX
) {
$UA
[
$n
] = LWP::UserAgent->new;
$UA
[
$n
]->conn_cache(
$CC
);
my
$resp
=
$UA
[
$n
]->request(
$req
);
push
@C
,
$CC
->get_connections;
push
@CID
,
$resp
->header(
'X-CID'
);
is_index(
$resp
);
}
my_sleep 1;
$CC
->prune;
::is(
$CC
->get_connections(
'http'
), 1,
"They all shared the same connection"
);
::is_deeply( \
@C
, [ (
$C
[0] ) x (
$MAX
+1) ],
" ... and same protocol object"
);
::is_deeply( \
@CID
, [ (
$CID
[0] ) x (
$MAX
+1) ],
" ... and same connection ID"
);
foreach
my
$ua
(
@UA
) {
my
$resp
=
$ua
->request(
$req
);
my
@new
=
$CC
->get_connections;
if
(
@new
) {
push
@C
,
@new
;
}
else
{
push
@C
, 0;
}
push
@CID
,
$resp
->header(
'X-CID'
);
is_honk(
$resp
);
}
my_sleep 1;
$CC
->prune;
::is(
$CC
->get_connections(
'http'
), 1,
"They all shared the same connection"
);
my
$want
= [ (
$CID
[0] ) x (
$KA_MAX
+1),
(
$CID
[
$KA_MAX
+1] ) x (
@CID
-(
$KA_MAX
+1) ) ];
::is_deeply( \
@CID
,
$want
,
" ... and same connection ID"
)
or
die
Dumper {
CID
=>\
@CID
,
want
=>
$want
};
$want
= [ (
$C
[0] ) x (
$KA_MAX
), 0,
(
$C
[
$KA_MAX
+1] ) x (
@C
-(
$KA_MAX
+1) ) ];
::is_deeply( \
@C
,
$want
,
" ... and shared protocol objects"
)
or
die
Dumper {
CID
=>\
@CID
,
C
=>\
@C
,
want
=>
$want
};
my
$sharedC
=
$C
[-1];
my
$sharedCID
=
$CID
[-1];
my_sleep
$S
+1;
$CC
->prune;
::is(
$CC
->get_connections(
'http'
), 0,
"They all timed out"
)
or
die
"MAKE IT SO"
;
@C
= ();
@CID
= ();
foreach
my
$ua
(
@UA
) {
my
$resp
=
$ua
->request(
$req
);
push
@C
,
$CC
->get_connections;
push
@CID
,
$resp
->header(
'X-CID'
);
is_honk(
$resp
);
}
my_sleep 1;
$CC
->prune;
::is(
$CC
->get_connections(
'http'
), 1,
"They all shared the same connection"
);
::is_deeply( \
@C
, [ (
$C
[0] ) x (0+
@C
) ],
" ... and same protocol object"
);
::isnt(
$C
[0],
$sharedC
,
" ... but it is new"
);
::is_deeply( \
@CID
, [ (
$CID
[0] ) x (
$MAX
+1) ],
" ... and same connection ID"
);
::isnt(
$CID
[0],
$sharedCID
,
" ... but it is new"
);
@C
= ();
foreach
my
$n
( 0 .. (3
*$MAX
) ) {
unless
(
$UA
[
$n
] ) {
$UA
[
$n
] = LWP::UserAgent->new;
$UA
[
$n
]->conn_cache(
$CC
);
}
my
$resp
=
$UA
[
$n
]->request(
$req
);
push
@C
,
$CC
->get_connections;
is_bonk2(
$resp
);
}
$CC
->prune;
::is(
$CC
->get_connections(
'http'
), 1,
"There are 1 active connection"
);
::is_deeply( \
@C
, [ ( (
$C
[0] ) x 3), ( (
$C
[3] ) x 2) ],
" ... but 3 were used, 3 conns max"
);
}
sub
is_index
{
my
(
$resp
) =
@_
;
::ok(
$resp
->is_success,
"got index"
) or
die
"resp="
, Dumper
$resp
;
my
$content
=
$resp
->content;
::ok(
$content
=~ /this is top/,
"got top index"
);
}
sub
is_honk
{
my
(
$resp
) =
@_
;
::ok(
$resp
->is_success,
"got honk"
) or
die
"resp="
, Dumper
$resp
;
my
$content
=
$resp
->content;
::ok(
$content
=~ /this is honk/,
"got honk"
);
}
sub
is_bonk2
{
my
(
$resp
) =
@_
;
::ok(
$resp
->is_success,
"got bonk2"
) or
die
"resp="
, Dumper
$resp
;
my
$content
=
$resp
->content;
::ok(
$content
=~ /This,
my
friend/,
"got bonk2"
) or
die
"content=$content"
;
}
1;