/
utils_request.t
90 lines (75 loc) · 1.69 KB
/
utils_request.t
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
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 11;
use CHI;
use File::Temp qw/ tempdir /;
my $dir = tempdir( CLEANUP => 1 );
###########################
# General module tests... #
###########################
my $module = 'PodBook::Utils::Request';
use_ok( $module );
my $obj = $module->new(
user_id => '127.0.0.1',
item_key => 'metacpan::EBook::MOBI',
item_type => 'mobi',
access_interval_limit => 0,
chi_ref => CHI->new( driver => 'File',
root_dir => $dir )
);
isa_ok($obj, $module);
# methods
can_ok($obj, 'new');
can_ok($obj, 'clear_cache');
can_ok($obj, 'is_cached');
# fresh cache
$obj->clear_cache();
if ($obj->is_cached()) {
fail('empty cache');
}
else {
pass('empty cache');
}
# write to cache
my $book = 'This is ASCII, even if a real book is binary...';
$obj->cache_book( $book, 1);
if ($obj->is_cached()) {
pass('cache hit');
}
else {
fail('cache hit');
}
# cache entry is expired
sleep(2);
if ($obj->is_cached()) {
fail('cache expired');
}
else {
pass('cache expired');
}
# check protection of heavy load from one user
$obj->{uid_expiration} = 1;
if ($obj->uid_is_allowed()) {
pass('user first request');
}
else {
fail('user first request');
}
# user is to fast!
if ($obj->uid_is_allowed()) {
fail('user block');
}
else {
pass('user block');
}
# user waited long enough
sleep 2;
if ($obj->uid_is_allowed()) {
pass('user second request');
}
else {
fail('user second request');
}
# AT THE END, CLEAR ALL THE CACHE FROM TESTING!
$obj->clear_cache();