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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
#!/usr/bin/env perl
use strict;
use warnings;
use YAHC qw/
yahc_conn_state
yahc_retry_conn
yahc_reinit_conn
yahc_conn_errors
yahc_conn_attempt
yahc_conn_last_error
yahc_conn_attempts_left
/;
use FindBin;
use Test::More;
use Data::Dumper;
use Time::HiRes qw/time sleep/;
use lib "$FindBin::Bin/..";
use t::Utils;
unless ($ENV{TEST_LIVE}) {
plan skip_all => "Enable live testing by setting env: TEST_LIVE=1";
}
my (undef, $host, $port) = t::Utils::_start_plack_server_on_random_port();
my (undef, $ch_host, $ch_port) = t::Utils::_start_plack_server_on_random_port({ chunked => 1 });
my ($yahc, $yahc_storage) = YAHC->new;
for my $len (0, 1, 2, 8, 23, 345, 1024, 65535, 131072, 9812, 19874, 1473451, 10000000) {
my $body = t::Utils::_generate_sequence($len);
subtest "content_length_$len" => sub {
my $c = $yahc->request({
host => $host,
port => $port,
path => '/record',
body => $body,
head => [ 'Content-Type' => 'raw' ]
});
$yahc->run;
cmp_ok($c->{response}{body}, 'eq', $body, "We got expected body");
cmp_ok($c->{response}{head}{'content-length'}, '==', $len, "We got expected content-length");
cmp_ok($c->{response}{head}{'content-type'}, 'eq', 'raw', "We got expected content-type");
};
subtest "chunked_content_length_$len" => sub {
my $c = $yahc->request({
host => $ch_host,
port => $ch_port,
path => '/record',
body => $body,
head => [ 'Content-Type' => 'raw' ]
});
$yahc->run;
cmp_ok($c->{response}{body}, 'eq', $body, "We got expected body");
cmp_ok($c->{response}{head}{'content-type'}, 'eq', 'raw', "We got expected content-type");
};
}
subtest "callbacks" => sub {
my $init_callback;
my $connecting_callback;
my $connected_callback;
my $writing_callback;
my $reading_callback;
my $callback;
my $c = $yahc->request({
host => $host,
port => $port,
retries => 5,
request_timeout => 1,
init_callback => sub { $init_callback = 1 },
connecting_callback => sub { $connecting_callback = 1 },
connected_callback => sub { $connected_callback = 1 },
writing_callback => sub { $writing_callback = 1 },
reading_callback => sub { $reading_callback = 1 },
callback => sub { $callback = 1 },
});
$yahc->run;
ok !yahc_conn_last_error($c), "no errors";
if (yahc_conn_last_error($c)) {
diag Dumper(yahc_conn_errors($c));
}
ok $init_callback, "init_callback is called";
ok $connecting_callback, "connecting_callback is called";
ok $connected_callback, "connected_callback is called";
ok $writing_callback, "writing_callback is called";
ok $reading_callback, "reading_callback is called";
ok $callback, "callback is called";
};
subtest "connect_timeout" => sub {
my $c = $yahc->request({
host => $host,
port => $port,
connect_timeout => 0.1,
connecting_callback => sub { sleep 0.2 },
});
$yahc->run;
my $has_timeout = grep { $_->[0] & YAHC::Error::CONNECT_TIMEOUT() } @{ yahc_conn_errors($c) || []};
is($has_timeout, 1, "We got YAHC::Error::CONNECT_TIMEOUT()");
cmp_ok($c->{response}{status}, '!=', 200, "We didn't get a 200 OK response");
};
subtest "drain_timeout" => sub {
my $c = $yahc->request({
host => $host,
port => $port,
drain_timeout => 0.1,
writing_callback => sub { sleep 0.2 },
});
$yahc->run;
my $has_timeout = grep { $_->[0] & YAHC::Error::DRAIN_TIMEOUT() } @{ yahc_conn_errors($c) || []};
is($has_timeout, 1, "We got YAHC::Error::DRAIN_TIMEOUT()");
cmp_ok($c->{response}{status}, '!=', 200, "We didn't get a 200 OK response");
};
subtest "request_timeout" => sub {
my $c = $yahc->request({
host => $host,
port => $port,
request_timeout => 0.1,
reading_callback => sub { sleep 0.2 },
});
$yahc->run;
my $has_timeout = grep { $_->[0] & YAHC::Error::REQUEST_TIMEOUT() } @{ yahc_conn_errors($c) || [] };
is($has_timeout, 1, "We got YAHC::Error::REQUEST_TIMEOUT()");
cmp_ok($c->{response}{status}, '!=', 200, "We didn't get a 200 OK response");
};
subtest "lifetime_timeout" => sub {
my $c = $yahc->request({
host => $host,
port => $port,
lifetime_timeout => 0.1,
writing_callback => sub { sleep 0.2 },
});
$yahc->run;
my $has_timeout = grep { $_->[0] & YAHC::Error::LIFETIME_TIMEOUT() } @{ yahc_conn_errors($c) || [] };
is($has_timeout, 1, "We got YAHC::Error::LIFETIME_TIMEOUT()");
cmp_ok($c->{response}{status}, '!=', 200, "We didn't get a 200 OK response");
};
subtest "retry due to DNS error and connection error" => sub {
my $rnd_port = t::Utils::_generaete_random_port();
my $c = $yahc->request({
host => [
$host . "_non_existent:$port",
$host . "_non_existent:$port",
"127.0.0.1:$rnd_port",
"127.0.0.1:$rnd_port",
"$host:$port"
],
retries => 4,
});
$yahc->run;
cmp_ok($c->{response}{status}, '==', 200, "We got a 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
cmp_ok(yahc_conn_attempt($c), '==', 5, "We did 5 attempts");
};
subtest "retry two connections due to DNS error" => sub {
my $c1 = $yahc->request({
host => [ $host . "_non_existent", $host . "_non_existent", $host ],
port => $port,
retries => 2,
});
my $c2 = $yahc->request({
host => [ $host . "_non_existent", $host ],
port => $port,
retries => 1,
});
$yahc->run;
cmp_ok($c1->{response}{status}, '==', 200, "first request got a 200 OK response");
cmp_ok(yahc_conn_state($c1), '==', YAHC::State::COMPLETED(), "first request got COMPLETED state");
cmp_ok(yahc_conn_attempt($c1), '==', 3, "first request did 3 attempts");
cmp_ok($c2->{response}{status}, '==', 200, "second request got a 200 OK response");
cmp_ok(yahc_conn_state($c2), '==', YAHC::State::COMPLETED(), "second request got COMPLETED state");
cmp_ok(yahc_conn_attempt($c2), '==', 2, "second request did 2 attempts");
};
subtest "retry with backoff delay" => sub {
my $c = $yahc->request({
host => [ $host . "_non_existent", $host . "_non_existent_1", $host ],
port => $port,
retries => 2,
backoff_delay => 2,
request_timeout => 1,
});
my $start = time;
$yahc->run;
my $elapsed = time - $start;
cmp_ok($c->{response}{status}, '==', 200, "We got a 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
cmp_ok($elapsed, '>=', 4, "elapsed is greater than backoff_delay * retries")
};
subtest "manual retry with backoff delay" => sub {
my $c = $yahc->request({
host => [ $host, $host ],
port => $port,
retries => 1,
request_timeout => 1,
callback => sub {
my ($conn, $err) = @_;
yahc_retry_conn($conn, { backoff_delay => 2 })
if yahc_conn_attempts_left($conn) > 0;
}
});
my $start = time;
$yahc->run;
my $elapsed = time - $start;
cmp_ok($c->{response}{status}, '==', 200, "We got a 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
cmp_ok(yahc_conn_attempt($c), '==', 2, "request did two attempts");
cmp_ok($elapsed, '>=', 2, "elapsed is greater than 2 seconds")
};
subtest "retry with backoff delay due to timeout" => sub {
my $start = time;
my $c = $yahc->request({
host => [ $host, $host . '_non_existent', $host ],
port => $port,
retries => 2,
backoff_delay => 4,
connect_timeout => 0.5,
connecting_callback => sub {
sleep 1 if yahc_conn_attempt($_[0]) <= 1; # fail 1st and and 2nd attempts
},
});
$yahc->run;
my $elapsed = time - $start;
cmp_ok($c->{response}{status}, '==', 200, "We got a 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
cmp_ok($elapsed, '>=', 4, "elapsed is greater than backoff_delay");
my @errors = @{ yahc_conn_errors($c) || [] };
ok(@errors == 2, "We got two errors");
if (@errors == 2) {
ok($errors[0][0] & YAHC::Error::CONNECT_TIMEOUT(), "First error is CONNECT_TIMEOUT");
ok($errors[1][0] & YAHC::Error::CONNECT_ERROR(), "Second error is CONNECT_ERROR");
}
};
subtest "retry with backoff delay and lifetime timeout" => sub {
my $c = $yahc->request({
host => [ $host . "_non_existent", $host . "_non_existent_1", $host ],
port => $port,
retries => 2,
backoff_delay => 1,
request_timeout => 1,
lifetime_timeout => 4,
});
my $start = time;
$yahc->run;
my $elapsed = time - $start;
cmp_ok(int($elapsed), '<=', 4, "elapsed is smaller than lifetime");
cmp_ok($c->{response}{status}, '==', 200, "We didn't get 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
};
subtest "retry with backoff delay and lifetime timeout triggering lifetime timeout" => sub {
my $c = $yahc->request({
host => [ $host . "_non_existent", $host . "_non_existent_1", $host ],
port => $port,
retries => 2,
backoff_delay => 2,
request_timeout => 1,
lifetime_timeout => 4,
});
my $start = time;
$yahc->run;
my $elapsed = time - $start;
my ($err) = yahc_conn_last_error($c);
cmp_ok($err & YAHC::Error::LIFETIME_TIMEOUT(), '==', YAHC::Error::LIFETIME_TIMEOUT(), "We got lifetime timeout");
cmp_ok(int($elapsed), '<=', 4, "elapsed is smaller than lifetime");
cmp_ok($c->{response}{status}, '!=', 200, "We didn't get 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
};
subtest "reinitiaize connection" => sub {
my $first_attempt = 1;
my $c = $yahc->request({
host => $host . "_non_existent",
port => $port,
request_timeout => 1,
callback => sub {
my ($conn, $err) = @_;
yahc_reinit_conn($conn, { host => $host }) if $err && $first_attempt;
$first_attempt = 0;
},
});
$yahc->run;
ok !$first_attempt;
cmp_ok($c->{response}{status}, '==', 200, "We got a 200 OK response");
cmp_ok(yahc_conn_state($c), '==', YAHC::State::COMPLETED(), "We got COMPLETED state");
};
END { kill 'KILL', $_ foreach keys %{ t::Utils::_pids() } }
done_testing;
|