line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
###################################################################### |
2
|
|
|
|
|
|
|
# $Id: SizeAwareCacheTester.pm,v 1.11 2002/04/07 17:04:46 dclinton Exp $ |
3
|
|
|
|
|
|
|
# Copyright (C) 2001-2003 DeWitt Clinton All Rights Reserved |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Software distributed under the License is distributed on an "AS |
6
|
|
|
|
|
|
|
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or |
7
|
|
|
|
|
|
|
# implied. See the License for the specific language governing |
8
|
|
|
|
|
|
|
# rights and limitations under the License. |
9
|
|
|
|
|
|
|
###################################################################### |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
package Cache::SizeAwareCacheTester; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
1145
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
14
|
2
|
|
|
2
|
|
9
|
use Cache::BaseCacheTester; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
33
|
|
15
|
2
|
|
|
2
|
|
9
|
use Cache::Cache; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
90
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
11
|
use vars qw( @ISA ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1298
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
@ISA = qw ( Cache::BaseCacheTester ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub test |
23
|
|
|
|
|
|
|
{ |
24
|
2
|
|
|
2
|
1
|
13
|
my ( $self, $cache ) = @_; |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
|
|
7
|
$self->_test_one( $cache ); |
27
|
2
|
|
|
|
|
11
|
$self->_test_two( $cache ); |
28
|
2
|
|
|
|
|
12
|
$self->_test_three( $cache ); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Test the limit_size( ) method, which should automatically purge the |
33
|
|
|
|
|
|
|
# first object added (with the closer expiration time) |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _test_one |
36
|
|
|
|
|
|
|
{ |
37
|
2
|
|
|
2
|
|
154
|
my ( $self, $cache ) = @_; |
38
|
|
|
|
|
|
|
|
39
|
2
|
50
|
|
|
|
11
|
$cache or |
40
|
|
|
|
|
|
|
croak( "cache required" ); |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
9
|
$cache->clear( ); |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
9
|
my $empty_size = $cache->size( ); |
45
|
|
|
|
|
|
|
|
46
|
2
|
50
|
|
|
|
21
|
( $empty_size == 0 ) ? |
47
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$empty_size == 0' ); |
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
3
|
my $first_key = 'Key 1'; |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
5
|
my $first_expires_in = '10'; |
52
|
|
|
|
|
|
|
|
53
|
2
|
|
|
|
|
3
|
my $value = $self; |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
10
|
$cache->set( $first_key, $value, $first_expires_in ); |
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
8
|
my $first_size = $cache->size( ); |
58
|
|
|
|
|
|
|
|
59
|
2
|
50
|
|
|
|
13
|
( $first_size > $empty_size ) ? |
60
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$first_size > $empty_size' ); |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
3
|
my $size_limit = $first_size; |
63
|
|
|
|
|
|
|
|
64
|
2
|
|
|
|
|
5
|
my $second_key = 'Key 2'; |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
6
|
my $second_expires_in = $first_expires_in * 2; |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
9
|
$cache->set( $second_key, $value, $second_expires_in ); |
69
|
|
|
|
|
|
|
|
70
|
2
|
|
|
|
|
8
|
my $second_size = $cache->size( ); |
71
|
|
|
|
|
|
|
|
72
|
2
|
50
|
|
|
|
14
|
( $second_size > $first_size ) ? |
73
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$second_size > $first_size' ); |
74
|
|
|
|
|
|
|
|
75
|
2
|
|
|
|
|
13
|
$cache->limit_size( $size_limit ); |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
24
|
my $first_value = $cache->get( $first_key ); |
78
|
|
|
|
|
|
|
|
79
|
2
|
50
|
|
|
|
24
|
( not defined $first_value ) ? |
80
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( 'not defined $first_value' ); |
81
|
|
|
|
|
|
|
|
82
|
2
|
|
|
|
|
10
|
my $third_size = $cache->size( ); |
83
|
|
|
|
|
|
|
|
84
|
2
|
50
|
|
|
|
13
|
( $third_size <= $size_limit ) ? |
85
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$third_size <= $size_limit' ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Test the limit_size method when a number of objects can expire |
91
|
|
|
|
|
|
|
# simultaneously |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _test_two |
94
|
|
|
|
|
|
|
{ |
95
|
2
|
|
|
2
|
|
4
|
my ( $self, $cache ) = @_; |
96
|
|
|
|
|
|
|
|
97
|
2
|
50
|
|
|
|
11
|
$cache or |
98
|
|
|
|
|
|
|
croak( "cache required" ); |
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
9
|
$cache->clear( ); |
101
|
|
|
|
|
|
|
|
102
|
2
|
|
|
|
|
11
|
my $empty_size = $cache->size( ); |
103
|
|
|
|
|
|
|
|
104
|
2
|
50
|
|
|
|
18
|
( $empty_size == 0 ) ? |
105
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$empty_size == 0' ); |
106
|
|
|
|
|
|
|
|
107
|
2
|
|
|
|
|
4
|
my $value = "A very short string"; |
108
|
|
|
|
|
|
|
|
109
|
2
|
|
|
|
|
5
|
my $first_key = 'Key 0'; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
4
|
my $first_expires_in = 20; |
112
|
|
|
|
|
|
|
|
113
|
2
|
|
|
|
|
3
|
my $start = time; |
114
|
2
|
|
|
|
|
9
|
$cache->set( $first_key, $value, $first_expires_in ); |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
|
|
|
6
|
my $first_size = $cache->size( ); |
117
|
|
|
|
|
|
|
|
118
|
2
|
50
|
|
|
|
14
|
( $first_size > $empty_size ) ? |
119
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$first_size > $empty_size' ); |
120
|
|
|
|
|
|
|
|
121
|
2
|
|
|
|
|
7
|
my $second_expires_in = $first_expires_in / 2; |
122
|
|
|
|
|
|
|
|
123
|
2
|
|
|
|
|
5
|
my $num_keys = 5; |
124
|
|
|
|
|
|
|
|
125
|
2
|
|
|
|
|
10
|
for ( my $i = 1; $i <= $num_keys; $i++ ) |
126
|
|
|
|
|
|
|
{ |
127
|
10
|
|
|
|
|
28
|
my $key = 'Key ' . $i; |
128
|
|
|
|
|
|
|
|
129
|
10
|
|
|
|
|
10001552
|
sleep ( 1 ); |
130
|
|
|
|
|
|
|
|
131
|
10
|
|
|
|
|
194
|
$cache->set( $key, $value, $second_expires_in ); |
132
|
|
|
|
|
|
|
} |
133
|
2
|
|
|
|
|
5
|
my $second_inserted = time; |
134
|
|
|
|
|
|
|
|
135
|
2
|
|
|
|
|
14
|
my $second_size = $cache->size( ); |
136
|
|
|
|
|
|
|
|
137
|
2
|
50
|
|
|
|
15
|
if (time - $start < $first_expires_in ) { |
138
|
2
|
50
|
|
|
|
19
|
( $second_size > $first_size ) ? |
139
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$second_size > $first_size' ); |
140
|
|
|
|
|
|
|
} else { |
141
|
0
|
|
|
|
|
0
|
$self->skip( '$second_size > $first_size (not finished in ' . |
142
|
|
|
|
|
|
|
$first_expires_in . ' s)'); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
2
|
|
|
|
|
6
|
my $size_limit = $first_size; |
146
|
|
|
|
|
|
|
|
147
|
2
|
|
|
|
|
11
|
$cache->limit_size( $size_limit ); |
148
|
|
|
|
|
|
|
|
149
|
2
|
|
|
|
|
20
|
my $third_size = $cache->size( ); |
150
|
|
|
|
|
|
|
|
151
|
2
|
50
|
|
|
|
22
|
( $third_size <= $size_limit ) ? |
152
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$third_size <= $size_limit' ); |
153
|
|
|
|
|
|
|
|
154
|
2
|
|
|
|
|
17
|
my $first_value = $cache->get( $first_key ); |
155
|
|
|
|
|
|
|
|
156
|
2
|
50
|
|
|
|
21
|
if (time - $start >= $first_expires_in) { |
|
|
50
|
|
|
|
|
|
157
|
0
|
|
|
|
|
0
|
$self->skip( '$first_value eq $value (not finished in ' . |
158
|
|
|
|
|
|
|
$first_expires_in . ' s)'); |
159
|
|
|
|
|
|
|
} elsif ($second_inserted + $second_expires_in >= |
160
|
|
|
|
|
|
|
$start + $first_expires_in) { |
161
|
0
|
|
|
|
|
0
|
$self->skip( '$first_value eq $value (second key insterted to late, ' . |
162
|
|
|
|
|
|
|
'so first key had expiration time before the second one, ' . |
163
|
|
|
|
|
|
|
'thus the first key was removed when limit cache size'); |
164
|
|
|
|
|
|
|
} else { |
165
|
2
|
50
|
|
|
|
19
|
( $first_value eq $value ) ? |
166
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$first_value eq $value' ); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Test the max_size( ) method, which should keep the cache under |
173
|
|
|
|
|
|
|
# the given size |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub _test_three |
176
|
|
|
|
|
|
|
{ |
177
|
2
|
|
|
2
|
|
4
|
my ( $self, $cache ) = @_; |
178
|
|
|
|
|
|
|
|
179
|
2
|
50
|
|
|
|
8
|
$cache or |
180
|
|
|
|
|
|
|
croak( "cache required" ); |
181
|
|
|
|
|
|
|
|
182
|
2
|
|
|
|
|
9
|
$cache->clear( ); |
183
|
|
|
|
|
|
|
|
184
|
2
|
|
|
|
|
14
|
my $empty_size = $cache->size( ); |
185
|
|
|
|
|
|
|
|
186
|
2
|
50
|
|
|
|
24
|
( $empty_size == 0 ) ? |
187
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$empty_size == 0' ); |
188
|
|
|
|
|
|
|
|
189
|
2
|
|
|
|
|
5
|
my $first_key = 'Key 1'; |
190
|
|
|
|
|
|
|
|
191
|
2
|
|
|
|
|
7
|
my $value = $self; |
192
|
|
|
|
|
|
|
|
193
|
2
|
|
|
|
|
13
|
$cache->set( $first_key, $value ); |
194
|
|
|
|
|
|
|
|
195
|
2
|
|
|
|
|
11
|
my $first_size = $cache->size( ); |
196
|
|
|
|
|
|
|
|
197
|
2
|
50
|
|
|
|
17
|
( $first_size > $empty_size ) ? |
198
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$first_size > $empty_size' ); |
199
|
|
|
|
|
|
|
|
200
|
2
|
|
|
|
|
5
|
my $max_size = $first_size; |
201
|
|
|
|
|
|
|
|
202
|
2
|
|
|
|
|
11
|
$cache->set_max_size( $max_size ); |
203
|
|
|
|
|
|
|
|
204
|
2
|
|
|
|
|
5
|
my $second_key = 'Key 2'; |
205
|
|
|
|
|
|
|
|
206
|
2
|
|
|
|
|
8
|
$cache->set( $second_key, $value ); |
207
|
|
|
|
|
|
|
|
208
|
2
|
|
|
|
|
12
|
my $second_size = $cache->size( ); |
209
|
|
|
|
|
|
|
|
210
|
2
|
50
|
|
|
|
19
|
( $second_size <= $max_size ) ? |
211
|
|
|
|
|
|
|
$self->ok( ) : $self->not_ok( '$second_size <= $max_size' ); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
1; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
__END__ |