File: 210_live_timeout_connected.t

package info (click to toggle)
libyahc-perl 0.035-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 440 kB
  • sloc: perl: 3,661; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,686 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

use strict;
use warnings;

use YAHC qw/yahc_conn_errors/;
use Net::Ping;
use Test::More;
use Data::Dumper;
use Time::HiRes qw/time/;

unless ($ENV{TEST_LIVE}) {
    plan skip_all => "Enable live testing by setting env: TEST_LIVE=1";
}

my @errors;
my @elapsed;
my $generated = 0;
my $timeout = 0.5;

for my $attempt (1..10) {
    # find a ip and confirm it is not reachable.
    my $pinger = Net::Ping->new("tcp", 2);
    $pinger->port_number(80);

    my $ip;
    my $iter = 10;
    do {
        $ip = join ".", 172, (int(rand()*15+16)), int(rand()*250+1),  int(rand()*255+1);
    } while($iter-- > 0 && $pinger->ping($ip));

    next if $iter == 0;
    $generated = 1;
    pass "attempt $attempt ip generated = $ip";

    local $SIG{ALRM} = sub { BAIL_OUT('ALARM') };
    alarm(10); # 10 sec of timeout

    my ($yahc, $yahc_storage) = YAHC->new({
        account_for_signals => 1
    });

    my $start = time;
    my $conn = $yahc->request({
        host            => $ip,
        connect_timeout => $timeout,
    });

    $yahc->run(YAHC::State::CONNECTED);

    my $elps = time - $start;
    push @elapsed, $elps;
    pass("attempt $attempt elapsed " . sprintf("%.3fs", $elps));

    push @errors, grep {
       $_->[0] & YAHC::Error::CONNECT_TIMEOUT()
    } @{ yahc_conn_errors($conn) || [] };
    last if @errors;
}

plan skip_all => "Cannot randomly generate an unreachable IP." unless $generated;
ok($_ <= $timeout * 2, "elapsed is roughly same as timeout") for @elapsed;
ok(@errors > 0, <<TEXT
Got CONNECT_TIMEOUT errors. If you see this error it's not necessary a bug!
Most likely the test failed to find unavailable IP address.
TEXT
);

done_testing;