summaryrefslogtreecommitdiffstats
path: root/t/04-retry.t
blob: d304b5a5a2e842c304a14a5a5822fd118c45cd76 (plain)
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
use strict;
use warnings;
use utf8;

use FindBin ();
use lib "$FindBin::Bin/lib";

use Test::More;
use TestServer qw(se_address json new_client);

use Cepis;
use Time::HiRes ();

{
    my $server = TestServer->new(
        routes => {
            '/01001000' => [
                [ 500, json( { erro => 'interno' } ), {} ],
                [ 200, json( se_address() ),          {} ],
            ]
        }
    );
    my $client = new_client($server);

    is( $client->lookup('01001000')->localidade, 'São Paulo', 'a retry recovers' );
    is( $server->count('/01001000'), 2, 'it took two attempts' );
    $server->stop;
}

{
    my $server = TestServer->new(
        routes => { '/01001000' => [ 503, json( { erro => 'interno' } ), {} ] } );
    my $client = new_client($server);

    my $ok = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'exhausting the attempts throws' );
    isa_ok( $@, 'Cepis::Error::ServerError' );
    is( $@->status, 503, 'the status of the last attempt is exposed' );
    is( $server->count('/01001000'), 3, 'three attempts by default' );
    $server->stop;
}

{
    my $server = TestServer->new( routes => { '/01001000' => [ 500, '{}', {} ] } );
    my $client = new_client( $server, max_attempts => 1 );

    my $ok = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'max_attempts of 1 still throws' );
    is( $server->count('/01001000'), 1, 'and disables retrying' );
    $server->stop;
}

{
    my $server = TestServer->new(
        routes => { '/01001000' => [ 429, '{}', { 'Retry-After' => '7' } ] } );
    my $client = new_client($server);

    my $ok = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'a 429 throws' );
    isa_ok( $@, 'Cepis::Error::RateLimited' );
    cmp_ok( abs( $@->retry_after - 7 ), '<', 1e-9, 'Retry-After in seconds is read' );
    $server->stop;
}

{
    my @day   = qw(Sun Mon Tue Wed Thu Fri Sat);
    my @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    my @t     = gmtime( time + 4 );
    my $date  = sprintf '%s, %02d %s %04d %02d:%02d:%02d GMT', $day[ $t[6] ], $t[3],
        $month[ $t[4] ], $t[5] + 1900, $t[2], $t[1], $t[0];

    my $server = TestServer->new(
        routes => { '/01001000' => [ 429, '{}', { 'Retry-After' => $date } ] } );
    my $client = new_client($server);

    my $ok = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'a 429 with an HTTP date throws' );
    isa_ok( $@, 'Cepis::Error::RateLimited' );
    cmp_ok( $@->retry_after, '>',  0, 'the date form is parsed too' );
    cmp_ok( $@->retry_after, '<=', 5, 'and lands where it should' );
    $server->stop;
}

{
    my $server = TestServer->new(
        routes => { '/01001000' => [ 429, '{}', { 'Retry-After' => '3600' } ] } );
    my $client = new_client($server);

    my $started = Time::HiRes::time();
    my $ok      = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'an absurd Retry-After still throws' );
    cmp_ok( Time::HiRes::time() - $started, '<', 5, 'and is capped by backoff_max' );
    $server->stop;
}

{
    my $client = Cepis::Client->new(
        base_url     => 'http://127.0.0.1:1',
        max_attempts => 2,
        backoff_base => 0.001,
        backoff_max  => 0.01,
        timeout      => 1.0,
    );

    my $ok = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'nothing listening throws' );
    isa_ok( $@, 'Cepis::Error::Network' );
    like( "$@", qr/127\.0\.0\.1:1/, 'the message names the URL' );
}

{
    my $server = TestServer->new( routes => { '/01001000' => [ 500, '{}', {} ] } );
    my $client = new_client(
        $server,
        max_attempts  => 50,
        backoff_base  => 0.05,
        backoff_max   => 0.05,
        total_timeout => 0.3,
    );

    my $started = Time::HiRes::time();
    my $ok      = eval { $client->lookup('01001000'); 1 };
    ok( !$ok, 'the call still throws' );
    isa_ok( $@, 'Cepis::Error::ServerError' );
    cmp_ok( Time::HiRes::time() - $started, '<', 3, 'total_timeout bounds the whole call' );
    cmp_ok( $server->count('/01001000'), '<', 50, 'and cuts the attempts short' );
    $server->stop;
}

{
    my $server = TestServer->new(
        routes => {
            '/01001000' => [
                [ 502, '{}', {} ],
                [ 504, '{}', {} ],
                [ 200, json( se_address() ), {} ],
            ]
        }
    );
    my $client = new_client($server);

    is( $client->lookup('01001000')->uf, 'SP', '502 and 504 are retried too' );
    is( $server->count('/01001000'), 3, 'across three attempts' );
    $server->stop;
}

done_testing();