File: 51_rate_limit.t

package info (click to toggle)
libnet-twitter-perl 4.01043-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 844 kB
  • sloc: perl: 6,703; makefile: 13
file content (45 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download | duplicates (3)
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
#!perl
use warnings;
use strict;
use Test::More;
use JSON::MaybeXS qw/encode_json/;
use lib qw(t/lib);
use Net::Twitter;

eval 'use TestUA';
plan skip_all => 'LWP::UserAgent 5.819 required' if $@;

my $nt = Net::Twitter->new(ssl => 0, traits => [qw/API::REST RateLimit/]);

my $reset = time + 1800;
my $t = TestUA->new(1, $nt->ua);
$t->response->content(encode_json({
    remaining_hits        => 75,
    reset_time_in_seconds => $reset,
    hourly_limit          => 150,
}));

is   $nt->rate_limit,     150,    'rate_limit';
is   $nt->rate_remaining, 75,     'rate_remaining';
is   $nt->rate_reset,     $reset, 'rate_reset';
like $t->request->uri, qr/rate_limit_status/, 'rate_limit_status called';

# HACK! Test approxmate values
my $ratio = $nt->rate_ratio;
ok   $ratio > 0.9 && $ratio < 1.1, 'rate_ratio is about 1.0';

my $until = $nt->until_rate(2.0);
ok   $until > 890 && $until < 910, 'until_rate(2.0) is about 900';


# test clock mismatch
$t->response->content(encode_json({
    remaining_hits        => 10,
    reset_time_in_seconds => $nt->_rate_limit_status->{rate_reset} = time - 10,
    hourly_limit          => 150,
}));

ok   $nt->rate_reset >= time, 'clock fudged';
is   $nt->rate_remaining, 10, 'forced a rate_limit_status call';

done_testing;