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
|
#!perl
use 5.14.1;
use warnings;
use HTTP::Status qw(HTTP_TOO_MANY_REQUESTS);
use HTTP::Response;
use Test::Fatal;
use Test::Spec;
BEGIN {
my $time = 1_500_000_000;
*CORE::GLOBAL::time = sub () {
my ( $caller ) = caller();
if($caller eq 'main' || $caller =~ /^Twitter::API/) {
return $time++;
} else {
return CORE::time();
}
};
}
use Twitter::API;
sub new_client {
my $limit = shift;
my $exception_info;
if(@_) {
$exception_info = shift;
} else {
$exception_info = [ HTTP_TOO_MANY_REQUESTS, 'Rate Limit Exceeded',
'x-rate-limit-reset' => time + 900 ];
}
my $remaining = $limit;
my $user_agent = mock();
$user_agent->stubs(request => sub {
if(!defined($remaining) || $remaining-- > 0 ) {
HTTP::Response->new(200, 'OK');
} else {
$remaining = $limit;
my ( $code, $reason, %headers ) = @$exception_info;
my $res = HTTP::Response->new($code, $reason);
for my $name (keys %headers) {
$res->header($name, $headers{$name});
}
$res
}
});
return Twitter::API->new_with_traits(
traits => 'RateLimiting',
consumer_key => 'key',
consumer_secret => 'secret',
access_token => 'token',
access_token_secret => 'token-secret',
user_agent => $user_agent,
);
}
describe RateLimiting => sub {
it 'should be unaffected if the rate limit is never hit' => sub {
my $client = new_client();
my $sleep = mock();
$sleep->expects('ping')->exactly(0);
$client->rate_limit_sleep_code(sub { $sleep->ping });
$client->get('foo');
$client->get('foo');
$client->get('foo');
$client->get('foo');
pass;
};
it 'should invoke the sleep callback if the rate limit is hit' => sub {
my $client = new_client(2);
my $n_calls = 0;
$client->rate_limit_sleep_code(sub { $n_calls++ });
$client->get('foo');
$client->get('foo');
is $n_calls, 0, 'sleep should not be called before the 3rd request';
$client->get('foo');
is $n_calls, 1, 'sleep should be called exactly once after the 3rd request';
$client->get('foo');
is $n_calls, 1, 'the rate limit should have reset for the 4th request';
};
it 'should not intercept any other 4xx errors' => sub {
my $client = new_client(2, [ 400, 'Unknown Error' ]);
my $n_calls = 0;
$client->rate_limit_sleep_code(sub { $n_calls++ });
$client->get('foo');
$client->get('foo');
like exception {
$client->get('foo');
}, qr/Unknown Error/;
is $n_calls, 0, 'sleep should not be called if a different 4xx error happens';
};
it 'should not intercept any 5xx errors' => sub {
my $client = new_client(2, [ 500, 'Internal Server Error' ]);
my $n_calls = 0;
$client->rate_limit_sleep_code(sub { $n_calls++ });
$client->get('foo');
$client->get('foo');
like exception {
$client->get('foo');
}, qr/Internal Server Error/;
is $n_calls, 0, 'sleep should not be called if a 5xx error happens';
};
it 'should be called with the correct amount of time to sleep' => sub {
my $reset_time = time + 900;
my $client = new_client(2, [
HTTP_TOO_MANY_REQUESTS, 'Rate Limit Exceeded',
'x-rate-limit-reset' => $reset_time,
]);
my $sleep_amount;
$client->rate_limit_sleep_code(sub {
( $sleep_amount ) = @_;
});
$client->get('foo');
$client->get('foo');
my $next_time = time + 1;
$client->get('foo');
is $sleep_amount, ($reset_time - $next_time), 'sleep should be called with the correct time interval';
};
};
runtests;
|