CHI/t/Driver.pm

filter: Statementstmt 84.6% 1045 / 1235 Branchbran 59.0% 65 / 110 Conditioncond 40.0% 8 / 20 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 92.9% 170 / 183 Podpod 0.0% 0 / 65 totaltotal 79.4% 1291 / 1626 SCAR 47.5
CC
79
Cov
82%
test_multiple_processes
186.5
test_serializers
39.5
test_discard_timeout
14.3
CRAP 116.0
« CHI/Stats.pm ↑ Summary CHI/Driver.pm »
1 package CHI::t::Driver;
2 9 $CHI::t::Driver::VERSION = '0.59';
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use CHI::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use CHI::Test::Util
7 9 ▶ qw(activate_test_logger cmp_bool is_between random_string skip_until);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use CHI::Util qw(can_load dump_one_line write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Spec::Functions qw(tmpdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Test::Warn;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use base qw(CHI::Test::Class);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Flags indicating what each test driver supports
20 435 ▶sub supports_clear { 1 }
Subroutine supports_clear: called CC=1 SCAR=0.0
Pod undocumented
21 7 ▶sub supports_expires_on_backend { 0 }
Subroutine supports_expires_on_backend: called CC=1 SCAR=0.0
Pod undocumented
22 8 ▶sub supports_get_namespaces { 1 }
Subroutine supports_get_namespaces: called CC=1 SCAR=0.0
Pod undocumented
23  
24 sub standard_keys_and_values : Test(startup) {
25 8 ▶ my ($self) = @_;
Subroutine standard_keys_and_values: called CC=1 SCAR=0.0
Pod undocumented
26  
27 8 my ( $keys_ref, $values_ref ) = $self->set_standard_keys_and_values();
28 8 $self->{keys} = $keys_ref;
29 8 $self->{values} = $values_ref;
30 8 $self->{keynames} = [ keys( %{$keys_ref} ) ];
31 8 $self->{key_count} = scalar( @{ $self->{keynames} } );
32 8 $self->{all_test_keys} = [ values(%$keys_ref), $self->extra_test_keys() ];
33 8 my $cache = $self->new_cache();
34 push(
35 8 @{ $self->{all_test_keys} },
36 8 $self->process_keys( $cache, @{ $self->{all_test_keys} } )
37 );
38 $self->{all_test_keys_hash} =
39 8 { map { ( $_, 1 ) } @{ $self->{all_test_keys} } };
40 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 sub kvpair {
43 175 ▶ my $self = shift;
Subroutine kvpair: called CC=4 SCAR=13.9
Pod undocumented
44 175 ▶ my $count = shift || 1;
Condition100%2/2
shift() || 1
l!l
30145
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
45  
46 return map {
47 175 (
48 $self->{keys}->{medium} . ( $_ == 1 ? '' : $_ ),
49 205 ▶ $self->{values}->{medium} . ( $_ == 1 ? '' : $_ )
Branch: 2 branches
BranchTrueFalseTotal
$_ == 1 ? : 175 30 205
$_ == 1 ? : 175 30 205
50 )
51 } ( 1 .. $count );
52 }
53  
54 sub setup : Test(setup) {
55 415 ▶ my $self = shift;
Subroutine setup: called CC=2 SCAR=7.1
Pod undocumented
56  
57 415 $self->{cache} = $self->new_cache();
58 415 ▶ $self->{cache}->clear() if $self->supports_clear();
Branch: 1 branch
BranchTrueFalseTotal
if $self->supports_clear 415 0 415
59 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 sub testing_driver_class {
62 383 ▶ my $self = shift;
Subroutine testing_driver_class: called CC=1 SCAR=0.0
Pod undocumented
63 383 my $class = ref($self);
64  
65 # By default, take the last part of the classname and use it as driver
66 383 my $driver_class = 'CHI::Driver::' . ( split( '::', $class ) )[-1];
67 383 return $driver_class;
68 }
69  
70 sub testing_chi_root_class {
71 446 ▶ return 'CHI';
Subroutine testing_chi_root_class: called CC=1 SCAR=0.0
Pod undocumented
72 }
73  
74 sub new_cache {
75 433 ▶ my $self = shift;
Subroutine new_cache: called CC=1 SCAR=0.0
Pod undocumented
76  
77 433 return $self->testing_chi_root_class->new( $self->new_cache_options(), @_ );
78 }
79  
80 sub new_cleared_cache {
81 94 ▶ my $self = shift;
Subroutine new_cleared_cache: called CC=1 SCAR=0.0
Pod undocumented
82  
83 94 my $cache = $self->new_cache(@_);
84 94 $cache->clear();
85 94 return $cache;
86 }
87  
88 sub new_cache_options {
89 720 ▶ my $self = shift;
Subroutine new_cache_options: called CC=1 SCAR=0.0
Pod undocumented
90  
91 return (
92 720 driver => '+' . $self->testing_driver_class(),
93 on_get_error => 'die',
94 on_set_error => 'die'
95 );
96 }
97  
98 sub set_standard_keys_and_values {
99 8 ▶ my $self = shift;
Subroutine set_standard_keys_and_values: called CC=2 SCAR=6.9
Pod undocumented
100  
101 8 my ( %keys, %values );
102 8 my @mixed_chars = ( 32 .. 48, 57 .. 65, 90 .. 97, 122 .. 126, 240 );
103  
104 %keys = (
105 'space' => ' ',
106 'newline' => "\n",
107 'char' => 'a',
108 'zero' => 0,
109 'one' => 1,
110 'medium' => 'medium',
111 320 'mixed' => join( "", map { chr($_) } @mixed_chars ),
112 8 'binary' => join( "", map { chr($_) } ( 129 .. 255 ) ),
113 'large' => scalar( 'ab' x 256 ),
114 'empty' => 'empty',
115 'arrayref' => [ 1, 2 ],
116 'hashref' => { foo => [ 'bar', 'baz' ] },
117 'utf8' => "Have \x{263a} a nice day",
118 );
119  
120 %values = map {
121 8 ▶ ( $_, ref( $keys{$_} ) ? $keys{$_} : scalar( reverse( $keys{$_} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
ref $keys{$_} ? : 16 88 104
122 } keys(%keys);
123 8 $values{empty} = '';
124  
125 8 return ( \%keys, \%values );
126 }
127  
128 # Extra keys (beyond the standard keys above) that we may use in these
129 # tests. We need to adhere to this for the benefit of drivers that don't
130 # support get_keys (like memcached) - they simulate get_keys(), clear(),
131 # etc. by using this fixed list of keys.
132 #
133 sub extra_test_keys {
134 8 ▶ my ($class) = @_;
Subroutine extra_test_keys: called CC=1 SCAR=0.0
Pod undocumented
135 return (
136 '', '2',
137 'medium2', 'foo',
138 'hashref', 'test_namespace_types',
139 "utf8", "encoded",
140 24 "binary", ( map { "done$_" } ( 0 .. 2 ) ),
141 8 ( map { "key$_" } ( 0 .. 20 ) )
142 );
143 }
144  
145 sub set_some_keys {
146 58 ▶ my ( $self, $c ) = @_;
Subroutine set_some_keys: called CC=2 SCAR=6.9
Pod undocumented
147  
148 58 foreach my $keyname ( @{ $self->{keynames} } ) {
149 754 $c->set( $self->{keys}->{$keyname}, $self->{values}->{$keyname} );
150 }
151 }
152  
153 sub test_encode : Tests {
154 8 ▶ my $self = shift;
Subroutine test_encode: called CC=1 SCAR=0.0
Pod undocumented
155 8 my $cache = $self->new_cleared_cache();
156  
157 8 my $utf8 = $self->{keys}->{utf8};
158 8 my $encoded = encode( utf8 => $utf8 );
159 8 my $binary_off = $self->{keys}->{binary};
160 8 my $binary_on = substr( $binary_off . $utf8, 0, length($binary_off) );
161  
162 8 ok( $binary_off eq $binary_on, "binary_off eq binary_on" );
163 8 ok( !Encode::is_utf8($binary_off), "!is_utf8(binary_off)" );
164 8 ok( Encode::is_utf8($binary_on), "is_utf8(binary_on)" );
165  
166 # Key maps to same thing whether encoded or non-encoded
167 #
168 8 my $value = time;
169 8 $cache->set( $utf8, $value );
170 8 is( $cache->get($utf8), $value, "get" );
171 8 is( $cache->get($encoded),
172 $value, "encoded and non-encoded map to same value" );
173  
174 # Key maps to same thing whether utf8 flag is off or on
175 #
176 # Commenting out for now - this is broken on FastMmap and
177 # DBI drivers (at least), and not entirely sure whether or
178 # with what priority we should demand this behavior.
179 #
180 8 if (0) {
181 $cache->set( $binary_off, $value );
182 is( $cache->get($binary_off), $value, "get binary_off" );
183 is( $cache->get($binary_on),
184 $value, "binary_off and binary_on map to same value" );
185 $cache->clear($binary_on);
186 ok( !$cache->get($binary_off), "cleared binary_off" ); #
187 }
188  
189 # Value is maintained as a utf8 or binary string, in scalar or in arrayref
190 8 $cache->set( "utf8", $utf8 );
191 8 is( $cache->get("utf8"), $utf8, "utf8 in scalar" );
192 8 $cache->set( "utf8", [$utf8] );
193 8 is( $cache->get("utf8")->[0], $utf8, "utf8 in arrayref" );
194  
195 8 $cache->set( "encoded", $encoded );
196 8 is( $cache->get("encoded"), $encoded, "encoded in scalar" );
197 8 $cache->set( "encoded", [$encoded] );
198 8 is( $cache->get("encoded")->[0], $encoded, "encoded in arrayref" );
199  
200 # Value retrieves as same thing whether stored with utf8 flag off or on
201 #
202 8 $cache->set( "binary", $binary_off );
203 8 is( $cache->get("binary"), $binary_on, "stored binary_off = binary_on" );
204 8 $cache->set( "binary", $binary_on );
205 8 is( $cache->get("binary"), $binary_off, "stored binary_on = binary_off" );
206 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
207  
208 sub test_simple : Tests {
209 12 ▶ my $self = shift;
Subroutine test_simple: called CC=2 SCAR=7.4
Pod undocumented
210 12 ▶ my $cache = shift || $self->{cache};
Condition33%1/3
shift() || $self->{'cache'}
l!l&&r!l&&!r
0120
MC/DC0%0/2
shift() || $self->{'cache'}
shift() $self->{'cache'}
Truth table33%1/3
A || B
A= shift()B= $self->{'cache'}
ABresult
1X1
011
000
211  
212 12 ok( $cache->set( $self->{keys}->{medium}, $self->{values}->{medium} ) );
213 10 is( $cache->get( $self->{keys}->{medium} ), $self->{values}->{medium} );
214 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 sub test_driver_class : Tests {
217 8 ▶ my $self = shift;
Subroutine test_driver_class: called CC=1 SCAR=0.0
Pod undocumented
218 8 my $cache = $self->{cache};
219  
220 8 isa_ok( $cache, 'CHI::Driver' );
221 8 isa_ok( $cache, $cache->driver_class );
222 8 can_ok( $cache, 'get', 'set', 'remove', 'clear', 'expire' );
223 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 sub test_key_types : Tests {
226 8 ▶ my $self = shift;
Subroutine test_key_types: called CC=4 SCAR=13.9
Pod undocumented
227 8 my $cache = $self->{cache};
228 8 $self->num_tests( $self->{key_count} * 9 + 1 );
229  
230 8 my @keys_set;
231 my $check_keys_set = sub {
232 216 ▶ my $desc = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
233 216 cmp_set( [ $cache->get_keys ], \@keys_set, "checking keys $desc" );
234 8 };
235  
236 8 $check_keys_set->("before sets");
237 8 foreach my $keyname ( @{ $self->{keynames} } ) {
238 104 my $key = $self->{keys}->{$keyname};
239 104 my $value = $self->{values}->{$keyname};
240 104 ok( !defined $cache->get($key), "miss for key '$keyname'" );
241 104 is( $cache->set( $key, $value ), $value, "set for key '$keyname'" );
242 104 push( @keys_set, $self->process_keys( $cache, $key ) );
243 104 $check_keys_set->("after set of key '$keyname'");
244 104 cmp_deeply( $cache->get($key), $value, "hit for key '$keyname'" );
245 }
246  
247 8 foreach my $keyname ( reverse @{ $self->{keynames} } ) {
248 104 my $key = $self->{keys}->{$keyname};
249 104 $cache->remove($key);
250 104 ok( !defined $cache->get($key),
251 "miss after remove for key '$keyname'" );
252 104 pop(@keys_set);
253 104 $check_keys_set->("after removal of key '$keyname'");
254 }
255  
256 # Confirm that transform_key is idempotent
257 #
258 8 foreach my $keyname ( @{ $self->{keynames} } ) {
259 104 my $key = $self->{keys}->{$keyname};
260 104 my $value = $self->{values}->{$keyname};
261 104 is(
262 $cache->transform_key( $cache->transform_key($key) ),
263 $cache->transform_key($key),
264 "transform_key is idempotent for '$keyname'"
265 );
266 104 $cache->clear();
267 104 $cache->set( $key, $value );
268 104 is( scalar( $cache->get_keys() ), 1, "exactly one key" );
269 104 cmp_deeply( $cache->get( ( $cache->get_keys )[0] ),
270 $value, "get with get_keys[0] got same value" );
271 }
272 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 sub test_deep_copy : Tests {
275 7 ▶ my $self = shift;
Subroutine test_deep_copy: called CC=2 SCAR=6.9
Pod undocumented
276 7 my $cache = $self->{cache};
277  
278 7 $self->set_some_keys($cache);
279 7 foreach my $keyname (qw(arrayref hashref)) {
280 14 my $key = $self->{keys}->{$keyname};
281 14 my $value = $self->{values}->{$keyname};
282 14 cmp_deeply( $cache->get($key), $value,
283 "get($key) returns original data structure" );
284 14 cmp_deeply( $cache->get($key), $cache->get($key),
285 "multiple get($key) return same data structure" );
286 14 isnt( $cache->get($key), $value,
287 "get($key) does not return original reference" );
288 14 isnt( $cache->get($key), $cache->get($key),
289 "multiple get($key) do not return same reference" );
290 }
291  
292 7 my $struct = { a => [ 1, 2 ], b => [ 4, 5 ] };
293 7 my $struct2 = dclone($struct);
294 7 $cache->set( 'hashref', $struct );
295 7 push( @{ $struct->{a} }, 3 );
296 7 delete( $struct->{b} );
297 7 cmp_deeply( $cache->get('hashref'),
298 $struct2,
299 "altering original set structure does not affect cached copy" );
300 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
301  
302 sub test_expires_immediately : Tests {
303 8 ▶ my $self = shift;
Subroutine test_expires_immediately: called CC=2 SCAR=15.1
Pod undocumented
304  
305 return 'author testing only - timing is unreliable'
306 8 ▶ unless ( $ENV{AUTHOR_TESTING} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTHOR_TESTING"} 8 0 8
307  
308 # expires_in default should be ignored
309 0 my $cache = $self->new_cache( expires_in => '1 hour' );
310  
311 # Expires immediately
312 my $test_expires_immediately = sub {
313 0 ▶ my ($set_option) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 0 my ( $key, $value ) = $self->kvpair();
315 0 my $desc = dump_one_line($set_option);
316 0 is( $cache->set( $key, $value, $set_option ), $value, "set ($desc)" );
317 0 is_between(
318 $cache->get_expires_at($key),
319 time() - 4,
320 time(), "expires_at ($desc)"
321 );
322 0 ok( $cache->exists_and_is_expired($key), "is_expired ($desc)" );
323 0 ok( !defined $cache->get($key), "immediate miss ($desc)" );
324 0 };
325 0 $test_expires_immediately->(0);
326 0 $test_expires_immediately->(-1);
327 0 $test_expires_immediately->("0 seconds");
328 0 $test_expires_immediately->("0 hours");
329 0 $test_expires_immediately->("-1 seconds");
330 0 $test_expires_immediately->( { expires_in => "0 seconds" } );
331 0 $test_expires_immediately->( { expires_at => time - 1 } );
332 0 $test_expires_immediately->("now");
333 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
334  
335 sub test_expires_shortly : Tests {
336 8 ▶ my $self = shift;
Subroutine test_expires_shortly: called CC=2 SCAR=15.5
Pod undocumented
337  
338 return 'author testing only - timing is unreliable'
339 8 ▶ unless ( $ENV{AUTHOR_TESTING} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTHOR_TESTING"} 8 0 8
340  
341 # expires_in default should be ignored
342 0 my $cache = $self->new_cache( expires_in => '1 hour' );
343  
344 # Expires shortly (real time)
345 my $test_expires_shortly = sub {
346 0 ▶ my ($set_option) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
347 0 my ( $key, $value ) = $self->kvpair();
348 0 my $desc = "set_option = " . dump_one_line($set_option);
349 0 my $start_time = time();
350 0 is( $cache->set( $key, $value, $set_option ), $value, "set ($desc)" );
351 0 is( $cache->get($key), $value, "hit ($desc)" );
352 0 is_between(
353 $cache->get_expires_at($key),
354 $start_time + 1,
355 $start_time + 8,
356 "expires_at ($desc)"
357 );
358 0 ok( !$cache->exists_and_is_expired($key), "not expired ($desc)" );
359 0 ok( $cache->is_valid($key), "valid ($desc)" );
360  
361 # Only bother sleeping and expiring for one of the variants
362 0 ▶ if ( $set_option eq "3 seconds" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set_option eq "3 seconds") 0 0 0
363 0 sleep(3);
364 0 ok( !defined $cache->get($key), "miss after 2 seconds ($desc)" );
365 0 ok( $cache->exists_and_is_expired($key), "is_expired ($desc)" );
366 0 ok( !$cache->is_valid($key), "invalid ($desc)" );
367 }
368 0 };
369 0 $test_expires_shortly->(3);
370 0 $test_expires_shortly->("3 seconds");
371 0 $test_expires_shortly->( { expires_at => time + 3 } );
372 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
373  
374 sub test_expires_later : Tests {
375 8 ▶ my $self = shift;
Subroutine test_expires_later: called CC=2 SCAR=15.5
Pod undocumented
376  
377 return 'author testing only - timing is unreliable'
378 8 ▶ unless ( $ENV{AUTHOR_TESTING} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTHOR_TESTING"} 8 0 8
379  
380 # expires_in default should be ignored
381 0 my $cache = $self->new_cache( expires_in => '1s' );
382  
383 # Expires later (test time)
384 my $test_expires_later = sub {
385 0 ▶ my ($set_option) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
386 0 my ( $key, $value ) = $self->kvpair();
387 0 my $desc = "set_option = " . dump_one_line($set_option);
388 0 is( $cache->set( $key, $value, $set_option ), $value, "set ($desc)" );
389 0 is( $cache->get($key), $value, "hit ($desc)" );
390 0 my $start_time = time();
391 0 is_between(
392 $cache->get_expires_at($key),
393 $start_time + 3580,
394 $start_time + 3620,
395 "expires_at ($desc)"
396 );
397 0 ok( !$cache->exists_and_is_expired($key), "not expired ($desc)" );
398 0 ok( $cache->is_valid($key), "valid ($desc)" );
399 0 local $CHI::Driver::Test_Time = $start_time + 3590;
400 0 ok( !$cache->exists_and_is_expired($key), "not expired ($desc)" );
401 0 ok( $cache->is_valid($key), "valid ($desc)" );
402 0 local $CHI::Driver::Test_Time = $start_time + 3610;
403 0 ok( !defined $cache->get($key), "miss after 1 hour ($desc)" );
404 0 ok( $cache->exists_and_is_expired($key), "is_expired ($desc)" );
405 0 ok( !$cache->is_valid($key), "invalid ($desc)" );
406 0 };
407 0 $test_expires_later->(3600);
408 0 $test_expires_later->("1 hour");
409 0 $test_expires_later->( { expires_at => time + 3600 } );
410 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
411  
412 sub test_expires_never : Tests {
413 8 ▶ my $self = shift;
Subroutine test_expires_never: called CC=1 SCAR=0.0
Pod undocumented
414 8 my $cache;
415  
416 # Expires never (will fail in 2037)
417 8 my ( $key, $value ) = $self->kvpair();
418 my $test_expires_never = sub {
419 16 ▶ my (@set_options) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
420 16 $cache->set( $key, $value, @set_options );
421 16 ok(
422 $cache->get_expires_at($key) >
423 time + Time::Duration::Parse::parse_duration('1 year'),
424 "expires never"
425 );
426 16 ok( !$cache->exists_and_is_expired($key), "not expired" );
427 16 ok( $cache->is_valid($key), "valid" );
428 8 };
429  
430 # never is default
431 8 $cache = $self->new_cache();
432 8 $test_expires_never->();
433  
434 # expires_in default should be ignored when never passed to set (RT #67970)
435 8 $cache = $self->new_cache( expires_in => '1s' );
436 8 $test_expires_never->('never');
437 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
438  
439 sub test_expires_defaults : Tests {
440 8 ▶ my $self = shift;
Subroutine test_expires_defaults: called CC=1 SCAR=0.0
Pod undocumented
441  
442 8 my $start_time = time();
443 8 local $CHI::Driver::Test_Time = $start_time;
444 8 my $cache;
445  
446 my $set_and_confirm_expires_at = sub {
447 32 ▶ my ( $expected_expires_at, $desc ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
448 32 my ( $key, $value ) = $self->kvpair();
449 32 $cache->set( $key, $value );
450 32 is( $cache->get_expires_at($key), $expected_expires_at, $desc );
451 32 $cache->clear();
452 8 };
453  
454 8 $cache = $self->new_cache( expires_in => 10 );
455 8 $set_and_confirm_expires_at->(
456 $start_time + 10,
457 "after expires_in constructor option"
458 );
459 8 $cache->expires_in(20);
460 8 $set_and_confirm_expires_at->( $start_time + 20,
461 "after expires_in method" );
462  
463 8 $cache = $self->new_cache( expires_at => $start_time + 30 );
464 8 $set_and_confirm_expires_at->(
465 $start_time + 30,
466 "after expires_at constructor option"
467 );
468 8 $cache->expires_at( $start_time + 40 );
469 8 $set_and_confirm_expires_at->( $start_time + 40,
470 "after expires_at method" );
471 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
472  
473 sub test_expires_manually : Tests {
474 8 ▶ my $self = shift;
Subroutine test_expires_manually: called CC=1 SCAR=0.0
Pod undocumented
475 8 my $cache = $self->{cache};
476  
477 8 my ( $key, $value ) = $self->kvpair();
478 8 my $desc = "expires manually";
479 8 $cache->set( $key, $value );
480 8 is( $cache->get($key), $value, "hit ($desc)" );
481 8 $cache->expire($key);
482 8 ok( !defined $cache->get($key), "miss after expire ($desc)" );
483 8 ok( !$cache->is_valid($key), "invalid after expire ($desc)" );
484 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
485  
486 sub test_expires_conditionally : Tests {
487 8 ▶ my $self = shift;
Subroutine test_expires_conditionally: called CC=1 SCAR=0.0
Pod undocumented
488 8 my $cache = $self->{cache};
489  
490 # Expires conditionally
491 my $test_expires_conditionally = sub {
492 32 ▶ my ( $code, $cond_desc, $expect_expire ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
493  
494 32 my ( $key, $value ) = $self->kvpair();
495 32 my $desc = "expires conditionally ($cond_desc)";
496 32 $cache->set( $key, $value );
497 32 ▶ is(
Branch: 1 branch
BranchTrueFalseTotal
$expect_expire ? : 16 16 32
498 $cache->get( $key, expire_if => $code ),
499 $expect_expire ? undef : $value,
500 "get result ($desc)"
501 );
502  
503 32 is( $cache->get($key), $value, "hit after expire_if ($desc)" );
504  
505 8 };
506 8 my $time = time();
507 8 ▶ $test_expires_conditionally->( sub { 1 }, 'true', 1 );
Subroutine __ANON__: called CC=1 SCAR=0.0
508 8 ▶ $test_expires_conditionally->( sub { 0 }, 'false', 0 );
Subroutine __ANON__: called CC=1 SCAR=0.0
509 $test_expires_conditionally->(
510 9 ▶ sub { $_[0]->created_at >= $time },
Subroutine __ANON__: called CC=1 SCAR=0.0
511 8 'created_at >= now', 1
512 );
513 $test_expires_conditionally->(
514 8 ▶ sub { $_[0]->created_at < $time },
Subroutine __ANON__: called CC=1 SCAR=0.0
515 8 'created_at < now', 0
516 );
517 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
518  
519 sub test_expires_variance : Tests {
520 8 ▶ my $self = shift;
Subroutine test_expires_variance: called CC=8 SCAR=20.8
Pod undocumented
521 8 my $cache = $self->{cache};
522  
523 8 my $start_time = time();
524 8 my $expires_at = $start_time + 10;
525 8 my ( $key, $value ) = $self->kvpair();
526 8 $cache->set( $key, $value,
527 { expires_at => $expires_at, expires_variance => 0.5 } );
528 8 is( $cache->get_object($key)->expires_at(),
529 $expires_at, "expires_at = $start_time" );
530 8 is(
531 $cache->get_object($key)->early_expires_at(),
532 $start_time + 5,
533 "early_expires_at = $start_time + 5"
534 );
535  
536 8 my %expire_count;
537 8 ▶ for ( my $time = $start_time + 3 ; $time <= $expires_at + 1 ; $time++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time <= $expires_at + 1) 72 8 80
538 72 local $CHI::Driver::Test_Time = $time;
539 72 ▶ for ( my $i = 0 ; $i < 100 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 100) 7200 72 7272
540 7200 ▶ if ( !defined $cache->get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache->get($key)) 3143 4057 7200
541 3143 $expire_count{$time}++;
542 }
543 }
544 }
545 8 ▶ for ( my $time = $start_time + 3 ; $time <= $start_time + 5 ; $time++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time <= $start_time + 5) 24 8 32
546 24 ok( !$expire_count{$time}, "got no expires at $time" );
547 }
548 8 ▶ for ( my $time = $start_time + 7 ; $time <= $start_time + 8 ; $time++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time <= $start_time + 8) 16 8 24
549 16 ▶ ok( $expire_count{$time} > 0 && $expire_count{$time} < 100,
Condition33%1/3
$expire_count{$time} > 0 && $expire_count{$time} < 100
!ll&&!rl&&r
0016
MC/DC0%0/2
$expire_count{$time} > 0 && $expire_count{$time} < 100
$expire_count{$time} > 0 $expire_count{$time} < 100
Truth table33%1/3
A && B
A= $expire_count{$time} > 0B= $expire_count{$time} < 100
ABresult
0X0
100
111
550 "got some expires at $time" );
551 }
552 8 ▶ for ( my $time = $expires_at ; $time <= $expires_at + 1 ; $time++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time <= $expires_at + 1) 16 8 24
553 16 ok( $expire_count{$time} == 100, "got all expires at $time" );
554 }
555 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
556  
557 sub test_not_in_cache : Tests {
558 8 ▶ my $self = shift;
Subroutine test_not_in_cache: called CC=1 SCAR=0.0
Pod undocumented
559 8 my $cache = $self->{cache};
560  
561 8 ok( !defined $cache->get_object('not in cache') );
562 8 ok( !defined $cache->get_expires_at('not in cache') );
563 8 ok( !$cache->is_valid('not in cache') );
564 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
565  
566 sub test_serialize : Tests {
567 7 ▶ my $self = shift;
Subroutine test_serialize: called CC=5 SCAR=16.1
Pod undocumented
568 7 my $cache = $self->{cache};
569 7 $self->num_tests( $self->{key_count} );
570  
571 7 $self->set_some_keys($cache);
572 7 foreach my $keyname ( @{ $self->{keynames} } ) {
573 91 ▶ my $expect_transformed =
Branch: 2 branches
BranchTrueFalseTotal
$keyname eq 'arrayref' || $keyname eq 'hashref' ? : 14 77 91
$keyname eq 'utf8' ? : 7 70 77
Condition100%3/3
$keyname eq "arrayref" or $keyname eq "hashref"
l!l&&r!l&&!r
7777
MC/DC100%2/2
$keyname eq "arrayref" or $keyname eq "hashref"
$keyname eq "arrayref" $keyname eq "hashref"
Truth table100%3/3
A or B
A= $keyname eq "arrayref"B= $keyname eq "hashref"
ABresult
1X1
011
000
574 ( $keyname eq 'arrayref' || $keyname eq 'hashref' ) ? 1
575 : ( $keyname eq 'utf8' ) ? 2
576 : 0;
577 is(
578 91 $cache->get_object( $self->{keys}->{$keyname} )->_is_transformed(),
579 $expect_transformed,
580 "is_transformed = $expect_transformed ($keyname)"
581 );
582 }
583 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
584  
585 {
586 package DummySerializer;
587 9 $DummySerializer::VERSION = '0.59';
588 ▶sub serialize { }
Subroutine serialize: not called CC=1 SCAR=0.0
589 ▶ sub deserialize { }
Subroutine deserialize: not called CC=1 SCAR=0.0
590 }
591  
592 9 sub test_serializers : Tests {
593 7 ▶ my ($self) = @_;
Subroutine test_serializers: called CC=7 SCAR=36.8
Pod undocumented
594  
595 7 ▶ unless ( can_load('Data::Serializer') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("Data::Serializer")) 7 0 7
596 7 $self->num_tests(1);
597 7 return 'Data::Serializer not installed';
598 }
599  
600 0 my @modes = (qw(string hash object));
601 0 my @variants = (qw(Storable Data::Dumper YAML));
602 0 @variants = grep { can_load($_) } @variants;
603 0 ok( scalar(@variants), "some variants ok" );
604  
605 0 my $initial_count = 5;
606 0 my $test_key_types_count = $self->{key_count};
607 0 my $test_count = $initial_count +
608 scalar(@variants) * scalar(@modes) * ( 1 + $test_key_types_count );
609  
610 0 my $cache1 = $self->new_cache();
611 0 isa_ok( $cache1->serializer, 'CHI::Serializer::Storable' );
612 0 my $cache2 = $self->new_cache();
613 0 is( $cache1->serializer, $cache2->serializer,
614 'same serializer returned from two objects' );
615  
616 throws_ok(
617 sub {
618 0 ▶ $self->new_cache( serializer => [1] );
Subroutine __ANON__: not called CC=1 SCAR=6.9
619 },
620 0 qr/Validation failed for|isa check for ".*?" failed/,
621 "invalid serializer"
622 );
623 lives_ok(
624 0 ▶ sub { $self->new_cache( serializer => bless( {}, 'DummySerializer' ) ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
625 ,
626 0 "valid dummy serializer"
627 );
628  
629 0 foreach my $mode (@modes) {
630 0 foreach my $variant (@variants) {
631 0 ▶ my $serializer_param = (
Branch: 2 branches
BranchTrueFalseTotal
$mode eq 'string' ? : 0 0 0
$mode eq 'hash' ? : 0 0 0
632 $mode eq 'string' ? $variant
633 : $mode eq 'hash' ? { serializer => $variant }
634 : Data::Serializer->new( serializer => $variant )
635 );
636 0 my $cache = $self->new_cache( serializer => $serializer_param );
637 0 is( $cache->serializer->serializer,
638 $variant, "serializer = $variant, mode = $mode" );
639 0 $self->{cache} = $cache;
640  
641 0 foreach my $keyname ( @{ $self->{keynames} } ) {
642 0 my $key = $self->{keys}->{$keyname};
643 0 my $value = $self->{values}->{$keyname};
644 0 $cache->set( $key, $value );
645 0 cmp_deeply( $cache->get($key), $value,
646 "hit for key '$keyname'" );
647 }
648  
649 0 $self->num_tests($test_count);
650 }
651 }
652 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
653  
654 sub test_namespaces : Tests {
655 8 ▶ my $self = shift;
Subroutine test_namespaces: called CC=3 SCAR=11.2
Pod undocumented
656 8 my $cache = $self->{cache};
657  
658 8 my $cache0 = $self->new_cache();
659 8 is( $cache0->namespace, 'Default', 'namespace defaults to "Default"' );
660  
661 8 my ( $ns1, $ns2, $ns3 ) = ( 'ns1', 'ns2', 'ns3' );
662 my ( $cache1, $cache1a, $cache2, $cache3 ) =
663 8 map { $self->new_cache( namespace => $_ ) } ( $ns1, $ns1, $ns2, $ns3 );
664 cmp_deeply(
665 8 [ map { $_->namespace } ( $cache1, $cache1a, $cache2, $cache3 ) ],
666 [ $ns1, $ns1, $ns2, $ns3 ],
667 'cache->namespace()'
668 );
669 8 $self->set_some_keys($cache1);
670 8 cmp_deeply(
671 $cache1->dump_as_hash(),
672 $cache1a->dump_as_hash(),
673 'cache1 and cache1a are same cache'
674 );
675 8 cmp_deeply( [ $cache2->get_keys() ],
676 [], 'cache2 empty after setting keys in cache1' );
677 8 $cache3->set( $self->{keys}->{medium}, 'different' );
678 is(
679 $cache1->get('medium'),
680 $self->{values}->{medium},
681 8 'cache1{medium} = medium'
682 );
683 8 is( $cache3->get('medium'), 'different', 'cache1{medium} = different' );
684  
685 8 ▶ if ( $self->supports_get_namespaces() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->supports_get_namespaces) { } 8 0 8
686  
687 # get_namespaces may or may not automatically include empty namespaces
688 8 cmp_deeply(
689 [ $cache1->get_namespaces() ],
690 supersetof( $ns1, $ns3 ),
691 "get_namespaces contains $ns1 and $ns3"
692 );
693  
694 8 foreach my $c ( $cache0, $cache1, $cache1a, $cache2, $cache3 ) {
695 40 cmp_set(
696 [ $cache->get_namespaces() ],
697 [ $c->get_namespaces() ],
698 'get_namespaces the same regardless of which cache asks'
699 );
700 }
701 }
702 else {
703 throws_ok(
704 0 ▶ sub { $cache1->get_namespaces() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
705 0 qr/not supported/,
706 "get_namespaces not supported"
707 );
708 0 SKIP: { skip "get_namespaces not supported", 5 }
709 }
710 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
711  
712 sub test_persist : Tests {
713 8 ▶ my $self = shift;
Subroutine test_persist: called CC=1 SCAR=0.0
Pod undocumented
714 8 my $cache = $self->{cache};
715  
716 8 my $hash;
717 {
718 8 my $cache1 = $self->new_cache();
719 8 $self->set_some_keys($cache1);
720 8 $hash = $cache1->dump_as_hash();
721 }
722 8 my $cache2 = $self->new_cache();
723 8 cmp_deeply(
724 $hash,
725 $cache2->dump_as_hash(),
726 'cache persisted between cache object creations'
727 );
728 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
729  
730 sub test_multi : Tests {
731 8 ▶ my $self = shift;
Subroutine test_multi: called CC=1 SCAR=0.0
Pod undocumented
732 8 my $cache = $self->{cache};
733  
734 my ( $keys, $values, $keynames ) =
735 8 ( $self->{keys}, $self->{values}, $self->{keynames} );
736  
737 8 my @ordered_keys = map { $keys->{$_} } @{$keynames};
738 my @ordered_values =
739 8 map { $values->{$_} } @{$keynames};
740 my %ordered_scalar_key_values =
741 88 map { ( $keys->{$_}, $values->{$_} ) }
742 8 grep { !ref( $keys->{$_} ) } @{$keynames};
743  
744 8 cmp_deeply( $cache->get_multi_arrayref( ['foo'] ),
745 [undef], "get_multi_arrayref before set" );
746  
747 8 $cache->set_multi( \%ordered_scalar_key_values );
748 8 $cache->set( $keys->{arrayref}, $values->{arrayref} );
749 8 $cache->set( $keys->{hashref}, $values->{hashref} );
750  
751 8 cmp_deeply( $cache->get_multi_arrayref( \@ordered_keys ),
752 \@ordered_values, "get_multi_arrayref" );
753 8 cmp_deeply( $cache->get( $ordered_keys[0] ),
754 $ordered_values[0], "get one after set_multi" );
755 8 cmp_deeply(
756 $cache->get_multi_arrayref( [ reverse @ordered_keys ] ),
757 [ reverse @ordered_values ],
758 "get_multi_arrayref"
759 );
760 cmp_deeply(
761 8 $cache->get_multi_hashref( [ grep { !ref($_) } @ordered_keys ] ),
762 \%ordered_scalar_key_values, "get_multi_hashref" );
763 8 cmp_set(
764 [ $cache->get_keys ],
765 [ $self->process_keys( $cache, @ordered_keys ) ],
766 "get_keys after set_multi"
767 );
768  
769 8 $cache->remove_multi( \@ordered_keys );
770 8 cmp_deeply(
771 $cache->get_multi_arrayref( \@ordered_keys ),
772 [ (undef) x scalar(@ordered_values) ],
773 "get_multi_arrayref after remove_multi"
774 );
775 8 cmp_set( [ $cache->get_keys ], [], "get_keys after remove_multi" );
776 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
777  
778 sub test_multi_no_keys : Tests {
779 8 ▶ my $self = shift;
Subroutine test_multi_no_keys: called CC=1 SCAR=0.0
Pod undocumented
780 8 my $cache = $self->{cache};
781  
782 8 cmp_deeply( $cache->get_multi_arrayref( [] ),
783 [], "get_multi_arrayref (no args)" );
784 8 cmp_deeply( $cache->get_multi_hashref( [] ),
785 {}, "get_multi_hashref (no args)" );
786 8 ▶ lives_ok { $cache->set_multi( {} ) } "set_multi (no args)";
Subroutine __ANON__: called CC=1 SCAR=0.0
787 8 ▶ lives_ok { $cache->remove_multi( [] ) } "remove_multi (no args)";
Subroutine __ANON__: called CC=1 SCAR=0.0
788 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
789  
790 sub test_l1_cache : Tests {
791 6 ▶ my $self = shift;
Subroutine test_l1_cache: called CC=2 SCAR=6.9
Pod undocumented
792 6 my @keys = map { "key$_" } ( 0 .. 2 );
793 6 my @values = map { "value$_" } ( 0 .. 2 );
794 6 my ( $cache, $l1_cache );
795  
796 6 ▶ return "skipping - no support for clear" unless $self->supports_clear();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->supports_clear 0 6 6
797  
798 my $test_l1_cache = sub {
799  
800 12 ▶ is( $l1_cache->subcache_type, "l1_cache", "subcache_type = l1_cache" );
Subroutine __ANON__: called CC=1 SCAR=0.0
801  
802 # Get on cache should populate l1 cache
803 #
804 12 $cache->set( $keys[0], $values[0] );
805 12 $l1_cache->clear();
806 12 ok( !$l1_cache->get( $keys[0] ), "l1 miss after clear" );
807 12 is( $cache->get( $keys[0] ),
808 $values[0], "primary hit after primary set" );
809 12 is( $l1_cache->get( $keys[0] ), $values[0],
810 "l1 hit after primary get" );
811  
812 # Primary cache should be reading l1 cache first
813 #
814 12 $l1_cache->set( $keys[0], $values[1] );
815 12 is( $cache->get( $keys[0] ),
816 $values[1], "got new value set explicitly in l1 cache" );
817 12 $l1_cache->remove( $keys[0] );
818 12 is( $cache->get( $keys[0] ), $values[0], "got old value again" );
819  
820 12 $cache->clear();
821 12 ok( !$cache->get( $keys[0] ), "miss after clear" );
822 12 ok( !$l1_cache->get( $keys[0] ), "miss after clear" );
823  
824 # get_multi_* - one from l1 cache, one from primary cache, one miss
825 #
826 12 $cache->set( $keys[0], $values[0] );
827 12 $cache->set( $keys[1], $values[1] );
828 12 $l1_cache->remove( $keys[0] );
829 12 $l1_cache->set( $keys[1], $values[2] );
830  
831 12 cmp_deeply(
832 $cache->get_multi_arrayref( [ $keys[0], $keys[1], $keys[2] ] ),
833 [ $values[0], $values[2], undef ],
834 "get_multi_arrayref"
835 );
836 12 cmp_deeply(
837 $cache->get_multi_hashref( [ $keys[0], $keys[1], $keys[2] ] ),
838 {
839 $keys[0] => $values[0],
840 $keys[1] => $values[2],
841 $keys[2] => undef
842 },
843 "get_multi_hashref"
844 );
845  
846 12 $self->_test_logging_with_l1_cache( $cache, $l1_cache );
847  
848 12 $self->_test_common_subcache_features( $cache, $l1_cache, 'l1_cache' );
849 6 };
850  
851 # Test with current cache in primary position...
852 #
853 6 $cache =
854 $self->new_cache( l1_cache => { driver => 'Memory', datastore => {} } );
855 6 $l1_cache = $cache->l1_cache;
856 6 isa_ok( $cache, $self->testing_driver_class, 'cache' );
857 6 isa_ok( $l1_cache, 'CHI::Driver::Memory', 'l1_cache' );
858 6 $test_l1_cache->();
859  
860 # and in l1 position
861 #
862 6 $cache = $self->testing_chi_root_class->new(
863 driver => 'Memory',
864 datastore => {},
865 l1_cache => { $self->new_cache_options() }
866 );
867 6 $l1_cache = $cache->l1_cache;
868 6 isa_ok( $cache, 'CHI::Driver::Memory', 'cache' );
869 6 isa_ok( $l1_cache, $self->testing_driver_class, 'l1_cache' );
870 6 $test_l1_cache->();
871 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
872  
873 sub test_mirror_cache : Tests {
874 6 ▶ my $self = shift;
Subroutine test_mirror_cache: called CC=2 SCAR=6.9
Pod undocumented
875 6 my ( $cache, $mirror_cache );
876 6 my ( $key, $value, $key2, $value2 ) = $self->kvpair(2);
877  
878 6 ▶ return "skipping - no support for clear" unless $self->supports_clear();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->supports_clear 0 6 6
879  
880 my $test_mirror_cache = sub {
881  
882 12 ▶ is( $mirror_cache->subcache_type, "mirror_cache" );
Subroutine __ANON__: called CC=1 SCAR=0.0
883  
884 # Get on either cache should not populate the other, and should not be able to see
885 # mirror keys from regular cache
886 #
887 12 $cache->set( $key, $value );
888 12 $mirror_cache->remove($key);
889 12 $cache->get($key);
890 12 ok( !$mirror_cache->get($key), "key not in mirror_cache" );
891  
892 12 $mirror_cache->set( $key2, $value2 );
893 12 ok( !$cache->get($key2), "key2 not in cache" );
894  
895 12 $self->_test_logging_with_mirror_cache( $cache, $mirror_cache );
896  
897 12 $self->_test_common_subcache_features( $cache, $mirror_cache,
898 'mirror_cache' );
899 6 };
900  
901 my $file_cache_options = sub {
902 12 ▶ my $root_dir =
Subroutine __ANON__: called CC=1 SCAR=0.0
903 tempdir( "chi-test-mirror-cache-XXXX", TMPDIR => 1, CLEANUP => 1 );
904 12 return ( driver => 'File', root_dir => $root_dir, depth => 3 );
905 6 };
906  
907 # Test with current cache in primary position...
908 #
909 6 $cache = $self->new_cache( mirror_cache => { $file_cache_options->() } );
910 6 $mirror_cache = $cache->mirror_cache;
911 6 isa_ok( $cache, $self->testing_driver_class );
912 6 isa_ok( $mirror_cache, 'CHI::Driver::File' );
913 6 $test_mirror_cache->();
914  
915 # and in mirror position
916 #
917 6 $cache =
918 $self->testing_chi_root_class->new( $file_cache_options->(),
919 mirror_cache => { $self->new_cache_options() } );
920 6 $mirror_cache = $cache->mirror_cache;
921 6 isa_ok( $cache, 'CHI::Driver::File' );
922 6 isa_ok( $mirror_cache, $self->testing_driver_class );
923 6 $test_mirror_cache->();
924 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
925  
926 sub test_subcache_overridable_params : Tests {
927 7 ▶ my ($self) = @_;
Subroutine test_subcache_overridable_params: called CC=1 SCAR=0.0
Pod undocumented
928  
929 7 my $cache;
930 warning_like {
931 7 ▶ $cache = $self->new_cache(
Subroutine __ANON__: called CC=1 SCAR=0.0
932 l1_cache => {
933 driver => 'Memory',
934 on_get_error => 'log',
935 datastore => {},
936 expires_variance => 0.5,
937 serializer => 'Foo'
938 }
939 );
940 }
941 7 qr/cannot override these keys/, "non-overridable subcache keys";
942 7 is( $cache->l1_cache->expires_variance, $cache->expires_variance );
943 7 is( $cache->l1_cache->serializer, $cache->serializer );
944 7 is( $cache->l1_cache->on_set_error, $cache->on_set_error );
945 7 is( $cache->l1_cache->on_get_error, 'log' );
946 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
947  
948 # Run logging tests for a cache with an l1_cache
949 #
950 sub _test_logging_with_l1_cache {
951 12 ▶ my ( $self, $cache ) = @_;
Subroutine _test_logging_with_l1_cache: called CC=1 SCAR=0.0
952  
953 12 $cache->clear();
954 12 my $log = activate_test_logger();
955 12 my ( $key, $value ) = $self->kvpair();
956  
957 12 my $driver = $cache->label;
958  
959 12 my $miss_not_in_cache = 'MISS \(not in cache\)';
960 12 my $miss_expired = 'MISS \(expired\)';
961  
962 12 my $start_time = time();
963  
964 12 $cache->get($key);
965 12 $log->contains_ok(
966 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_not_in_cache/
967 );
968 12 $log->contains_ok(
969 qr/cache get for .* key='$key', cache='.*l1.*', time='[-\d]+ms': $miss_not_in_cache/
970 );
971 12 $log->empty_ok();
972  
973 12 $cache->set( $key, $value, 81 );
974 12 $log->contains_ok(
975 qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='$driver', time='[-\d]+ms'/
976 );
977  
978 12 $log->contains_ok(
979 qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='.*l1.*', time='[-\d]+ms'/
980 );
981 12 $log->empty_ok();
982  
983 12 $cache->get($key);
984 12 $log->contains_ok(
985 qr/cache get for .* key='$key', cache='.*l1.*', time='[-\d]+ms': HIT/);
986 12 $log->empty_ok();
987  
988 12 local $CHI::Driver::Test_Time = $start_time + 120;
989 12 $cache->get($key);
990 12 $log->contains_ok(
991 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_expired/
992 );
993 12 $log->contains_ok(
994 qr/cache get for .* key='$key', cache='.*l1.*', time='[-\d]+ms': $miss_expired/
995 );
996 12 $log->empty_ok();
997  
998 12 $cache->remove($key);
999 12 $cache->get($key);
1000 12 $log->contains_ok(
1001 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_not_in_cache/
1002 );
1003 12 $log->contains_ok(
1004 qr/cache get for .* key='$key', cache='.*l1.*', time='[-\d]+ms': $miss_not_in_cache/
1005 );
1006 12 $log->empty_ok();
1007 }
1008  
1009 sub _test_logging_with_mirror_cache {
1010 12 ▶ my ( $self, $cache ) = @_;
Subroutine _test_logging_with_mirror_cache: called CC=1 SCAR=0.0
1011  
1012 12 $cache->clear();
1013 12 my $log = activate_test_logger();
1014 12 my ( $key, $value ) = $self->kvpair();
1015  
1016 12 my $driver = $cache->label;
1017  
1018 12 my $miss_not_in_cache = 'MISS \(not in cache\)';
1019 12 my $miss_expired = 'MISS \(expired\)';
1020  
1021 12 my $start_time = time();
1022  
1023 12 $cache->get($key);
1024 12 $log->contains_ok(
1025 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_not_in_cache/
1026 );
1027 12 $log->empty_ok();
1028  
1029 12 $cache->set( $key, $value, 81 );
1030 12 $log->contains_ok(
1031 qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='$driver', time='[-\d]+ms'/
1032 );
1033  
1034 12 $log->contains_ok(
1035 qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='.*mirror.*', time='[-\d]+ms'/
1036 );
1037 12 $log->empty_ok();
1038  
1039 12 $cache->get($key);
1040 12 $log->contains_ok(
1041 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': HIT/);
1042 12 $log->empty_ok();
1043  
1044 12 local $CHI::Driver::Test_Time = $start_time + 120;
1045 12 $cache->get($key);
1046 12 $log->contains_ok(
1047 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_expired/
1048 );
1049 12 $log->empty_ok();
1050  
1051 12 $cache->remove($key);
1052 12 $cache->get($key);
1053 12 $log->contains_ok(
1054 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_not_in_cache/
1055 );
1056 12 $log->empty_ok();
1057 }
1058  
1059 # Run tests common to l1_cache and mirror_cache
1060 #
1061 sub _test_common_subcache_features {
1062 24 ▶ my ( $self, $cache, $subcache, $subcache_type ) = @_;
Subroutine _test_common_subcache_features: called CC=2 SCAR=6.9
1063 24 my ( $key, $value, $key2, $value2 ) = $self->kvpair(2);
1064  
1065 24 for ( $cache, $subcache ) { $_->clear() }
1066  
1067 # Test informational methods
1068 #
1069 24 ok( !$cache->is_subcache, "is_subcache - false" );
1070 24 ok( $subcache->is_subcache, "is_subcache - true" );
1071 24 ok( $cache->has_subcaches, "has_subcaches - true" );
1072 24 ok( !$subcache->has_subcaches, "has_subcaches - false" );
1073 24 ok( !$cache->can('parent_cache'), "parent_cache - cannot" );
1074 24 is( $subcache->parent_cache, $cache, "parent_cache - defined" );
1075 24 ok( !$cache->can('subcache_type'), "subcache_type - cannot" );
1076 24 is( $subcache->subcache_type, $subcache_type, "subcache_type - defined" );
1077 24 cmp_deeply( $cache->subcaches, [$subcache], "subcaches - defined" );
1078 24 ok( !$subcache->can('subcaches'), "subcaches - cannot" );
1079 24 is( $cache->$subcache_type, $subcache, "$subcache_type - defined" );
1080 24 ok( !$subcache->can($subcache_type), "$subcache_type - cannot" );
1081  
1082 # Test that sets and various kinds of removals and expirations are distributed to both
1083 # the primary cache and the subcache
1084 #
1085 24 my ( $test_remove_method, $confirm_caches_empty,
1086 $confirm_caches_populated );
1087 $test_remove_method = sub {
1088 72 ▶ my ( $desc, $remove_code ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1089 72 $desc = "testing $desc";
1090  
1091 72 $confirm_caches_empty->("$desc: before set");
1092  
1093 72 $cache->set( $key, $value );
1094 72 $cache->set( $key2, $value2 );
1095 72 $confirm_caches_populated->("$desc: after set");
1096 72 $remove_code->();
1097  
1098 72 $confirm_caches_empty->("$desc: before set_multi");
1099 72 $cache->set_multi( { $key => $value, $key2 => $value2 } );
1100 72 $confirm_caches_populated->("$desc: after set_multi");
1101 72 $remove_code->();
1102  
1103 72 $confirm_caches_empty->("$desc: before return");
1104 24 };
1105  
1106 $confirm_caches_empty = sub {
1107 216 ▶ my ($desc) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1108 216 ok( !defined( $cache->get($key) ),
1109 "primary cache is not populated with '$key' - $desc" );
1110 216 ok( !defined( $subcache->get($key) ),
1111 "subcache is not populated with '$key' - $desc" );
1112 216 ok( !defined( $cache->get($key2) ),
1113 "primary cache is not populated #2 with '$key2' - $desc" );
1114 216 ok( !defined( $subcache->get($key2) ),
1115 "subcache is not populated #2 with '$key2' - $desc" );
1116 24 };
1117  
1118 $confirm_caches_populated = sub {
1119 144 ▶ my ($desc) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1120 144 is( $cache->get($key), $value,
1121 "primary cache is populated with '$key' - $desc" );
1122 144 is( $subcache->get($key),
1123 $value, "subcache is populated with '$key' - $desc" );
1124 144 is( $cache->get($key2), $value2,
1125 "primary cache is populated with '$key2' - $desc" );
1126 144 is( $subcache->get($key2),
1127 $value2, "subcache is populated with '$key2' - $desc" );
1128 24 };
1129  
1130 $test_remove_method->(
1131 48 ▶ 'remove', sub { $cache->remove($key); $cache->remove($key2) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1132 24 );
1133 $test_remove_method->(
1134 48 ▶ 'expire', sub { $cache->expire($key); $cache->expire($key2) }
Subroutine __ANON__: called CC=1 SCAR=0.0
1135 24 );
1136 24 ▶ $test_remove_method->( 'clear', sub { $cache->clear() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1137 }
1138  
1139 sub _verify_cache_is_cleared {
1140 25 ▶ my ( $self, $cache, $desc ) = @_;
Subroutine _verify_cache_is_cleared: called CC=2 SCAR=6.9
1141  
1142 25 cmp_deeply( [ $cache->get_keys ], [], "get_keys ($desc)" );
1143 25 is( scalar( $cache->get_keys ), 0, "scalar(get_keys) = 0 ($desc)" );
1144 25 ▶ while ( my ( $keyname, $key ) = each( %{ $self->{keys} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"keys"};}) 325 25 350
1145 325 ok( !defined $cache->get($key),
1146 "key '$keyname' no longer defined ($desc)" );
1147 }
1148 }
1149  
1150 sub process_keys {
1151 128 ▶ my ( $self, $cache, @keys ) = @_;
Subroutine process_keys: called CC=1 SCAR=0.0
Pod undocumented
1152 128 $self->process_key( $cache, 'foo' );
1153 128 return map { $self->process_key( $cache, $_ ) } @keys;
1154 }
1155  
1156 sub process_key {
1157 808 ▶ my ( $self, $cache, $key ) = @_;
Subroutine process_key: called CC=1 SCAR=0.0
Pod undocumented
1158 808 return $cache->unescape_key(
1159 $cache->escape_key( $cache->transform_key($key) ) );
1160 }
1161  
1162 sub test_clear : Tests {
1163 8 ▶ my $self = shift;
Subroutine test_clear: called CC=2 SCAR=7.3
Pod undocumented
1164 8 my $cache = $self->new_cache( namespace => 'name' );
1165 8 my $cache2 = $self->new_cache( namespace => 'other' );
1166 8 my $cache3 = $self->new_cache( namespace => 'name' );
1167 8 $self->num_tests( $self->{key_count} * 2 + 5 );
1168  
1169 8 ▶ if ( $self->supports_clear() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->supports_clear) { } 8 0 8
1170 8 $self->set_some_keys($cache);
1171 8 $self->set_some_keys($cache2);
1172 8 $cache->clear();
1173  
1174 8 $self->_verify_cache_is_cleared( $cache, 'cache after clear' );
1175 8 $self->_verify_cache_is_cleared( $cache3, 'cache3 after clear' );
1176 cmp_set(
1177 [ $cache2->get_keys ],
1178 8 [ $self->process_keys( $cache2, values( %{ $self->{keys} } ) ) ],
1179 'cache2 untouched by clear'
1180 );
1181 }
1182 else {
1183 throws_ok(
1184 0 ▶ sub { $cache->clear() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1185 0 qr/not supported/,
1186 "clear not supported"
1187 );
1188 0 SKIP: { skip "clear not supported", 9 }
1189 }
1190 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1191  
1192 sub test_logging : Tests {
1193 6 ▶ my $self = shift;
Subroutine test_logging: called CC=1 SCAR=0.0
Pod undocumented
1194 6 my $cache = $self->{cache};
1195  
1196 6 my $log = activate_test_logger();
1197 6 my ( $key, $value ) = $self->kvpair();
1198  
1199 6 my $driver = $cache->label;
1200  
1201 6 my $miss_not_in_cache = 'MISS \(not in cache\)';
1202 6 my $miss_expired = 'MISS \(expired\)';
1203  
1204 6 my $start_time = time();
1205  
1206 6 $cache->get($key);
1207 6 $log->contains_ok(
1208 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_not_in_cache/
1209 );
1210 6 $log->empty_ok();
1211  
1212 6 $cache->set( $key, $value );
1213 6 $log->contains_ok(
1214 qr/cache set for .* key='$key', size=\d+, expires='never', cache='$driver', time='[-\d]+ms'/
1215 );
1216 6 $log->empty_ok();
1217 6 $cache->set( $key, $value, 81 );
1218 6 $log->contains_ok(
1219 qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='$driver', time='[-\d]+ms'/
1220 );
1221 6 $log->empty_ok();
1222  
1223 6 $cache->get($key);
1224 6 $log->contains_ok(
1225 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': HIT/);
1226 6 $log->empty_ok();
1227  
1228 6 local $CHI::Driver::Test_Time = $start_time + 120;
1229 6 $cache->get($key);
1230 6 $log->contains_ok(
1231 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_expired/
1232 );
1233 6 $log->empty_ok();
1234  
1235 6 $cache->remove($key);
1236 6 $cache->get($key);
1237 6 $log->contains_ok(
1238 qr/cache get for .* key='$key', cache='$driver', time='[-\d]+ms': $miss_not_in_cache/
1239 );
1240 6 $log->empty_ok();
1241 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1242  
1243 sub test_stats : Tests {
1244 6 ▶ my $self = shift;
Subroutine test_stats: called CC=3 SCAR=23.3
Pod undocumented
1245  
1246 return 'author testing only - possible differences between JSON versions'
1247 6 ▶ unless ( $ENV{AUTHOR_TESTING} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTHOR_TESTING"} 6 0 6
1248  
1249 0 my $stats = $self->testing_chi_root_class->stats;
1250 0 $stats->enable();
1251  
1252 0 my ( $key, $value ) = $self->kvpair();
1253 0 my $start_time = time();
1254  
1255 0 my $cache;
1256 0 $cache = $self->new_cache( namespace => 'Foo' );
1257 0 $cache->get($key);
1258 0 $cache->set( $key, $value, 80 );
1259 0 $cache->get($key);
1260 0 local $CHI::Driver::Test_Time = $start_time + 120;
1261 0 $cache->get($key);
1262 0 $cache->remove($key);
1263 0 $cache->get($key);
1264  
1265 0 $cache = $self->new_cache( namespace => 'Bar' );
1266 0 $cache->set( $key, scalar( $value x 3 ) );
1267 0 $cache->set( $key, $value );
1268  
1269 0 $cache = $self->new_cache( namespace => 'Baz' );
1270 0 ▶ my $code = sub { usleep(100000); scalar( $value x 5 ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1271 0 $cache->compute( $key, undef, $code );
1272 0 $cache->compute( $key, undef, $code );
1273 0 $cache->compute( $key, undef, $code );
1274  
1275 0 my $log = activate_test_logger();
1276 0 my $label = $cache->label;
1277 0 $log->empty_ok();
1278 0 $stats->flush();
1279 0 $log->contains_ok(
1280 qr/CHI stats: {"absent_misses":2,"end_time":\d+,"expired_misses":1,"get_time_ms":\d+,"hits":1,"label":"$label","namespace":"Foo","root_class":"CHI","set_key_size":6,"set_time_ms":\d+,"set_value_size":20,"sets":1,"start_time":\d+}/
1281 );
1282 0 $log->contains_ok(
1283 qr/CHI stats: {"end_time":\d+,"label":"$label","namespace":"Bar","root_class":"CHI","set_key_size":12,"set_time_ms":\d+,"set_value_size":52,"sets":2,"start_time":\d+}/
1284 );
1285 0 $log->contains_ok(
1286 qr/CHI stats: {"absent_misses":1,"compute_time_ms":\d+,"computes":1,"end_time":\d+,"get_time_ms":\d+,"hits":2,"label":"$label","namespace":"Baz","root_class":"CHI","set_key_size":6,"set_time_ms":\d+,"set_value_size":44,"sets":1,"start_time":\d+}/
1287 );
1288 0 $log->empty_ok();
1289  
1290 0 my @logs = (
1291 'CHI stats: {"root_class":"CHI","namespace":"Foo","label":"File","start_time":1338404896,"end_time":1338404899,"hits":3,"sets":5,"set_time_ms":10}',
1292 'CHI stats: {"root_class":"CHI","namespace":"Foo","label":"File","start_time":1338404896,"end_time":1338404899,"hits":1,"sets":7,"set_time_ms":14}',
1293 'CHI stats: {"root_class":"CHI","namespace":"Bar","label":"File","start_time":1338404896,"end_time":1338404899,"hits":4,"sets":9,"set_time_ms":18}',
1294 'CHI stats: {"root_class":"CHI","namespace":"Foo","label":"File","start_time":1338404896,"end_time":1338404899,"sets":3,"set_time_ms":6}',
1295 'CHI stats: {"root_class":"CHI","namespace":"Foo","label":"File","start_time":1338404896,"end_time":1338404899,"hits":8}',
1296 'CHI stats: {"root_class":"CHI","namespace":"Foo","label":"Memory","start_time":1338404896,"end_time":1338404899,"sets":2,"set_time_ms":4}',
1297 'CHI stats: {"root_class":"CHI","namespace":"Bar","label":"File","start_time":1338404896,"end_time":1338404899,"hits":10,"sets":1,"set_time_ms":2}',
1298 'CHI stats: {"root_class":"CHI","namespace":"Bar","label":"File","start_time":1338404896,"end_time":1338404899,"hits":3,"set_errors":2}',
1299 );
1300 0 my $log_dir = tempdir( "chi-test-stats-XXXX", TMPDIR => 1, CLEANUP => 1 );
1301 0 write_file( "$log_dir/log1", join( "\n", splice( @logs, 0, 4 ) ) . "\n" );
1302 0 write_file( "$log_dir/log2", join( "\n", @logs ) );
1303 0 ▶ open( my $fh2, "<", "$log_dir/log2" ) or die "cannot open $log_dir/log2";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh2, "<", "$log_dir/log2" or die "cannot open $log_dir/log2" 0 0 0
1304 0 my $results = $stats->parse_stats_logs( "$log_dir/log1", $fh2 );
1305 0 close($fh2);
1306 0 cmp_deeply(
1307 $results,
1308 Test::Deep::bag(
1309 {
1310 avg_set_time_ms => '2',
1311 gets => 12,
1312 hit_rate => '1',
1313 hits => 12,
1314 label => 'File',
1315 namespace => 'Foo',
1316 root_class => 'CHI',
1317 set_time_ms => 30,
1318 sets => 15
1319 },
1320 {
1321 avg_set_time_ms => '2',
1322 gets => 17,
1323 hit_rate => '1',
1324 hits => 17,
1325 label => 'File',
1326 namespace => 'Bar',
1327 root_class => 'CHI',
1328 set_errors => 2,
1329 set_time_ms => 20,
1330 sets => 10
1331 },
1332 {
1333 avg_set_time_ms => '2',
1334 label => 'Memory',
1335 namespace => 'Foo',
1336 root_class => 'CHI',
1337 set_time_ms => 4,
1338 sets => 2
1339 },
1340 {
1341 avg_set_time_ms => '2',
1342 hits => '29',
1343 label => 'TOTALS',
1344 namespace => 'TOTALS',
1345 root_class => 'TOTALS',
1346 set_errors => '2',
1347 set_time_ms => 54,
1348 sets => 27
1349 }
1350 ),
1351 'parse_stats_logs'
1352 );
1353 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1354  
1355 sub test_cache_object : Tests {
1356 8 ▶ my $self = shift;
Subroutine test_cache_object: called CC=1 SCAR=0.0
Pod undocumented
1357 8 my $cache = $self->{cache};
1358 8 my ( $key, $value ) = $self->kvpair();
1359 8 my $start_time = time();
1360 8 $cache->set( $key, $value, { expires_at => $start_time + 10 } );
1361 8 is_between( $cache->get_object($key)->created_at,
1362 $start_time, $start_time + 2 );
1363 8 is_between( $cache->get_object($key)->get_created_at,
1364 $start_time, $start_time + 2 );
1365 8 is( $cache->get_object($key)->expires_at, $start_time + 10 );
1366 8 is( $cache->get_object($key)->get_expires_at, $start_time + 10 );
1367  
1368 8 local $CHI::Driver::Test_Time = $start_time + 50;
1369 8 $cache->set( $key, $value );
1370 8 is_between(
1371 $cache->get_object($key)->created_at,
1372 $start_time + 50,
1373 $start_time + 52
1374 );
1375 8 is_between(
1376 $cache->get_object($key)->get_created_at,
1377 $start_time + 50,
1378 $start_time + 52
1379 );
1380 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1381  
1382 sub test_size_awareness : Tests {
1383 7 ▶ my $self = shift;
Subroutine test_size_awareness: called CC=1 SCAR=0.0
Pod undocumented
1384 7 my ( $key, $value ) = $self->kvpair();
1385  
1386 7 ok( !$self->new_cleared_cache()->is_size_aware(),
1387 "not size aware by default" );
1388 7 ok( $self->new_cleared_cache( is_size_aware => 1 )->is_size_aware(),
1389 "is_size_aware turns on size awareness" );
1390 7 ok( $self->new_cleared_cache( max_size => 10 )->is_size_aware(),
1391 "max_size turns on size awareness" );
1392  
1393 7 my $cache = $self->new_cleared_cache( is_size_aware => 1 );
1394 7 is( $cache->get_size(), 0, "size is 0 for empty" );
1395 7 $cache->set( $key, $value );
1396 7 is_about( $cache->get_size, 20, "size is about 20 with one value" );
1397 7 $cache->set( $key, scalar( $value x 5 ) );
1398 7 is_about( $cache->get_size, 45, "size is 45 after overwrite" );
1399 7 $cache->set( $key, scalar( $value x 5 ) );
1400 7 is_about( $cache->get_size, 45, "size is still 45 after same overwrite" );
1401 7 $cache->set( $key, scalar( $value x 2 ) );
1402 7 is_about( $cache->get_size, 26, "size is 26 after overwrite" );
1403 7 $cache->remove($key);
1404 7 is( $cache->get_size, 0, "size is 0 again after removing key" );
1405 7 $cache->set( $key, $value );
1406 7 is_about( $cache->get_size, 20, "size is about 20 with one value" );
1407 7 $cache->clear();
1408 7 is( $cache->get_size, 0, "size is 0 again after clear" );
1409  
1410 7 my $time = time() + 10;
1411 7 $cache->set( $key, $value, { expires_at => $time } );
1412 7 is( $cache->get_expires_at($key),
1413 $time, "set options respected by size aware cache" );
1414 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1415  
1416 sub test_max_size : Tests {
1417 7 ▶ my $self = shift;
Subroutine test_max_size: called CC=3 SCAR=11.0
Pod undocumented
1418  
1419 7 is( $self->new_cache( max_size => '30k' )->max_size,
1420 30 * 1024, 'max_size parsing' );
1421  
1422 7 my $cache = $self->new_cleared_cache( max_size => 99 );
1423 7 ok( $cache->is_size_aware, "is size aware when max_size specified" );
1424 7 my $value_20 = 'x' x 6;
1425  
1426 7 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 35 7 42
1427 35 $cache->set( "key$i", $value_20 );
1428 }
1429 7 ▶ for ( my $i = 0 ; $i < 10 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 70 7 77
1430 70 $cache->set( "key" . int( rand(10) ), $value_20 );
1431 70 is_between( $cache->get_size, 60, 99,
1432 "after iteration $i, size = " . $cache->get_size );
1433 70 is_between( scalar( $cache->get_keys ),
1434 3, 5, "after iteration $i, keys = " . scalar( $cache->get_keys ) );
1435 }
1436 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1437  
1438 sub test_max_size_with_l1_cache : Tests {
1439 8 ▶ my $self = shift;
Subroutine test_max_size_with_l1_cache: called CC=5 SCAR=16.1
Pod undocumented
1440  
1441 8 my $cache = $self->new_cleared_cache(
1442 l1_cache => { driver => 'Memory', datastore => {}, max_size => 99 } );
1443 8 my $l1_cache = $cache->l1_cache;
1444 8 ok( $l1_cache->is_size_aware, "is size aware when max_size specified" );
1445 8 my $value_20 = 'x' x 6;
1446  
1447 8 my @keys = map { "key$_" } ( 0 .. 9 );
1448 8 my @shuffle_keys = shuffle(@keys);
1449 8 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 40 8 48
1450 40 $cache->set( "key$i", $value_20 );
1451 }
1452 8 ▶ for ( my $i = 0 ; $i < 10 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 80 8 88
1453 80 my $key = $shuffle_keys[$i];
1454 80 $cache->set( $key, $value_20 );
1455 80 is_between( $l1_cache->get_size, 60, 99,
1456 "after iteration $i, size = " . $l1_cache->get_size );
1457 80 is_between( scalar( $l1_cache->get_keys ),
1458 3, 5,
1459 "after iteration $i, keys = " . scalar( $l1_cache->get_keys ) );
1460 }
1461 8 cmp_deeply( [ sort $cache->get_keys ],
1462 \@keys, "primary cache still has all keys" );
1463  
1464 # Now test population by writeback
1465 8 $l1_cache->clear();
1466 8 is( $l1_cache->get_size, 0, "l1 size is 0 after clear" );
1467 8 ▶ for ( my $i = 0 ; $i < 5 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 40 8 48
1468 40 $cache->get("key$i");
1469 }
1470 8 ▶ for ( my $i = 0 ; $i < 10 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 80 8 88
1471 80 my $key = $shuffle_keys[$i];
1472 80 $cache->get($key);
1473 80 is_between( $l1_cache->get_size, 60, 99,
1474 "after iteration $i, size = " . $l1_cache->get_size );
1475 80 is_between( scalar( $l1_cache->get_keys ),
1476 3, 5,
1477 "after iteration $i, keys = " . scalar( $l1_cache->get_keys ) );
1478 }
1479 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1480  
1481 sub test_custom_discard_policy : Tests {
1482 7 ▶ my $self = shift;
Subroutine test_custom_discard_policy: called CC=3 SCAR=11.0
Pod undocumented
1483 7 my $value_20 = 'x' x 6;
1484 my $highest_first = sub {
1485 70 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1486 70 my @sorted_keys = sort( $c->get_keys );
1487 70 ▶ return sub { pop(@sorted_keys) };
Subroutine __ANON__: called CC=1 SCAR=0.0
1488 7 };
1489 7 my $cache = $self->new_cleared_cache(
1490 is_size_aware => 1,
1491 discard_policy => $highest_first
1492 );
1493 7 ▶ for ( my $j = 0 ; $j < 10 ; $j += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 10) 35 7 42
1494 35 $cache->clear();
1495 35 ▶ for ( my $i = 0 ; $i < 10 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 350 35 385
1496 350 my $k = ( $i + $j ) % 10;
1497 350 $cache->set( "key$k", $value_20 );
1498 }
1499 35 $cache->discard_to_size(100);
1500 cmp_set(
1501 [ $cache->get_keys ],
1502 35 [ map { "key$_" } ( 0 .. 4 ) ],
1503 "5 lowest"
1504 );
1505 35 $cache->discard_to_size(20);
1506 35 cmp_set( [ $cache->get_keys ], ["key0"], "1 lowest" );
1507 }
1508 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1509  
1510 sub test_discard_timeout : Tests {
1511 8 ▶ my $self = shift;
Subroutine test_discard_timeout: called CC=4 SCAR=26.6
Pod undocumented
1512 8 ▶ return 'author testing only' unless ( $ENV{AUTHOR_TESTING} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTHOR_TESTING"} 8 0 8
1513  
1514 my $bad_policy = sub {
1515 0 ▶ return sub { '1' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1516 0 };
1517 0 my $cache = $self->new_cleared_cache(
1518 is_size_aware => 1,
1519 discard_policy => $bad_policy
1520 );
1521 0 ▶ ok( defined( $cache->discard_timeout ) && $cache->discard_timeout > 1,
Condition0%0/3
defined $cache->discard_timeout && $cache->discard_timeout > 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cache->discard_timeout && $cache->discard_timeout > 1
defined $cache->discard_timeout $cache->discard_timeout > 1
Truth table0%0/3
A && B
A= defined $cache->discard_timeoutB= $cache->discard_timeout > 1
ABresult
0X0
100
111
1522 "positive discard timeout" );
1523 0 $cache->discard_timeout(1);
1524 0 is( $cache->discard_timeout, 1, "can set timeout" );
1525 0 my $start_time = time;
1526 0 $cache->set( 2, 2 );
1527 0 ▶ throws_ok { $cache->discard_to_size(0) } qr/discard timeout .* reached/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1528 0 ▶ ok(
Condition0%0/3
time >= $start_time && time <= $start_time + 4
!ll&&!rl&&r
000
MC/DC0%0/2
time >= $start_time && time <= $start_time + 4
time >= $start_time time <= $start_time + 4
Truth table0%0/3
A && B
A= time >= $start_timeB= time <= $start_time + 4
ABresult
0X0
100
111
1529 time >= $start_time && time <= $start_time + 4,
1530 sprintf(
1531 "time (%d) is between %d and %d",
1532 time, $start_time, $start_time + 4
1533 )
1534 );
1535 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1536  
1537 sub test_size_awareness_with_subcaches : Tests {
1538 7 ▶ my $self = shift;
Subroutine test_size_awareness_with_subcaches: called CC=1 SCAR=0.0
Pod undocumented
1539  
1540 7 my ( $cache, $l1_cache );
1541 my $set_values = sub {
1542 21 ▶ my $value_20 = 'x' x 6;
Subroutine __ANON__: called CC=2 SCAR=6.9
1543 21 ▶ for ( my $i = 0 ; $i < 20 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 20) 420 21 441
1544 420 $cache->set( "key$i", $value_20 );
1545 }
1546 21 $l1_cache = $cache->l1_cache;
1547 7 };
1548 my $is_size_aware = sub {
1549 28 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1550 28 my $label = $c->label;
1551  
1552 28 ok( $c->is_size_aware, "$label is size aware" );
1553 28 my $max_size = $c->max_size;
1554 28 ok( $max_size > 0, "$label has max size" );
1555 28 is_between( $c->get_size, $max_size - 40,
1556 $max_size, "$label size = " . $c->get_size );
1557 28 is_between(
1558 scalar( $c->get_keys ),
1559 ( $max_size + 1 ) / 20 - 2,
1560 ( $max_size + 1 ) / 20,
1561 "$label keys = " . scalar( $c->get_keys )
1562 );
1563 7 };
1564 my $is_not_size_aware = sub {
1565 7 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1566 7 my $label = $c->label;
1567  
1568 7 ok( !$c->is_size_aware, "$label is not size aware" );
1569 7 is( $c->get_keys, 20, "$label keys = 20" );
1570 7 };
1571  
1572 7 $cache = $self->new_cleared_cache(
1573 l1_cache => { driver => 'Memory', datastore => {}, max_size => 99 } );
1574 7 $set_values->();
1575 7 $is_not_size_aware->($cache);
1576 7 $is_size_aware->($l1_cache);
1577  
1578 7 $cache = $self->new_cleared_cache(
1579 l1_cache => { driver => 'Memory', datastore => {}, max_size => 99 },
1580 max_size => 199
1581 );
1582 7 $set_values->();
1583 7 $is_size_aware->($cache);
1584 7 $is_size_aware->($l1_cache);
1585  
1586 7 $cache = $self->new_cleared_cache(
1587 l1_cache => { driver => 'Memory', datastore => {} },
1588 max_size => 199
1589 );
1590 7 $set_values->();
1591 7 $is_size_aware->($cache);
1592  
1593 # Cannot call is_not_size_aware because the get_keys check will
1594 # fail. Keys will be removed from the l1_cache when they are removed
1595 # from the main cache, even though l1_cache does not have a max
1596 # size. Not sure if this is the correct behavior, but for now, we're not
1597 # going to test it. Normally, l1 caches will be more size limited than
1598 # their parent caches.
1599 #
1600 7 ok( !$l1_cache->is_size_aware, $l1_cache->label . " is not size aware" );
1601 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1602  
1603 sub is_about {
1604 35 ▶ my ( $value, $expected, $msg ) = @_;
Subroutine is_about: called CC=2 SCAR=7.4
Pod undocumented
1605  
1606 35 my $margin = int( $expected * 0.1 );
1607 35 ▶ if ( abs( $value - $expected ) <= $margin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $value - $expected <= $margin) { } 35 0 35
1608 35 pass($msg);
1609 }
1610 else {
1611 0 fail("$msg - got $value, expected $expected");
1612 }
1613 }
1614  
1615 sub test_busy_lock : Tests {
1616 8 ▶ my $self = shift;
Subroutine test_busy_lock: called CC=1 SCAR=0.0
Pod undocumented
1617 8 my $cache = $self->{cache};
1618  
1619 8 my ( $key, $value ) = $self->kvpair();
1620 8 my @bl = ( busy_lock => '30 sec' );
1621 8 my $start_time = time();
1622  
1623 8 local $CHI::Driver::Test_Time = $start_time;
1624 8 $cache->set( $key, $value, 100 );
1625 8 local $CHI::Driver::Test_Time = $start_time + 90;
1626 8 is( $cache->get( $key, @bl ), $value, "hit before expiration" );
1627 8 is(
1628 $cache->get_expires_at($key),
1629 $start_time + 100,
1630 "expires_at before expiration"
1631 );
1632 8 local $CHI::Driver::Test_Time = $start_time + 110;
1633 8 ok( !defined( $cache->get( $key, @bl ) ), "miss after expiration" );
1634 8 is(
1635 $cache->get_expires_at($key),
1636 $start_time + 140,
1637 "expires_at after busy lock"
1638 );
1639 8 is( $cache->get( $key, @bl ), $value, "hit after busy lock" );
1640 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1641  
1642 sub test_obj_ref : Tests {
1643 8 ▶ my $self = shift;
Subroutine test_obj_ref: called CC=1 SCAR=0.0
Pod undocumented
1644  
1645 # Make sure obj_ref works in conjunction with subcaches too
1646 8 my $cache =
1647 $self->new_cache( l1_cache => { driver => 'Memory', datastore => {} } );
1648 8 my $obj;
1649 8 my ( $key, $value ) = ( 'medium', [ a => 5, b => 6 ] );
1650  
1651 my $validate_obj = sub {
1652 16 ▶ isa_ok( $obj, 'CHI::CacheObject' );
Subroutine __ANON__: called CC=1 SCAR=0.0
1653 16 is( $obj->key, $key, "keys match" );
1654 16 cmp_deeply( $obj->value, $value, "values match" );
1655 8 };
1656  
1657 8 $cache->get( $key, obj_ref => \$obj );
1658 8 ok( !defined($obj), "obj not defined on miss" );
1659 8 $cache->set( $key, $value, { obj_ref => \$obj } );
1660 8 $validate_obj->();
1661 8 undef $obj;
1662 8 ok( !defined($obj), "obj not defined before get" );
1663 8 $cache->get( $key, obj_ref => \$obj );
1664 8 $validate_obj->();
1665 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1666  
1667 sub test_metacache : Tests {
1668 8 ▶ my $self = shift;
Subroutine test_metacache: called CC=1 SCAR=0.0
Pod undocumented
1669 8 my $cache = $self->{cache};
1670  
1671 8 ok( !defined( $cache->{metacache} ), "metacache is lazy" );
1672 8 $cache->metacache->set( 'foo', 5 );
1673 8 ok( defined( $cache->{metacache} ), "metacache autovivified" );
1674 8 is( $cache->metacache->get('foo'), 5 );
1675 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1676  
1677 sub test_scalar_return_values : Tests {
1678 7 ▶ my $self = shift;
Subroutine test_scalar_return_values: called CC=1 SCAR=0.0
Pod undocumented
1679 7 my $cache = $self->{cache};
1680  
1681 my $check = sub {
1682 35 ▶ my ($code) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1683 35 my $scalar_result = $code->();
1684 35 my @list = $code->();
1685 35 cmp_deeply( \@list, [$scalar_result] );
1686 7 };
1687  
1688 7 ▶ $check->( sub { $cache->fetch('a') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1689 7 ▶ $check->( sub { $cache->get('a') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1690 7 ▶ $check->( sub { $cache->set( 'a', 5 ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1691 7 ▶ $check->( sub { $cache->fetch('a') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1692 7 ▶ $check->( sub { $cache->get('a') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1693 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1694  
1695 sub test_no_leak : Tests {
1696 8 ▶ my ($self) = @_;
Subroutine test_no_leak: called CC=2 SCAR=7.1
Pod undocumented
1697  
1698 8 my $weakref;
1699 {
1700 8 my $cache = $self->new_cache();
1701 8 $weakref = $cache;
1702 8 weaken($weakref);
1703 8 ▶ ok( defined($weakref) && $weakref->isa('CHI::Driver'),
Condition33%1/3
defined $weakref && $weakref->isa('CHI::Driver')
!ll&&!rl&&r
008
MC/DC0%0/2
defined $weakref && $weakref->isa('CHI::Driver')
defined $weakref $weakref->isa('CHI::Driver')
Truth table33%1/3
A && B
A= defined $weakrefB= $weakref->isa('CHI::Driver')
ABresult
0X0
100
111
1704 "weakref is defined" );
1705 }
1706 8 ok( !defined($weakref), "weakref is no longer defined - cache was freed" );
1707 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1708  
1709 {
1710 9 package My::CHI;
1711 9 $My::CHI::VERSION = '0.59';
1712 9 our @ISA = qw(CHI);
1713 }
1714  
1715 sub test_driver_properties : Tests {
1716 8 ▶ my $self = shift;
Subroutine test_driver_properties: called CC=1 SCAR=0.0
Pod undocumented
1717 8 my $cache = $self->{cache};
1718  
1719 8 is( $cache->chi_root_class, 'CHI', 'chi_root_class=CHI' );
1720 8 my $cache2 = My::CHI->new( $self->new_cache_options() );
1721 8 is( $cache2->chi_root_class, 'My::CHI', 'chi_root_class=My::CHI' );
1722 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1723  
1724 sub test_missing_params : Tests {
1725 8 ▶ my $self = shift;
Subroutine test_missing_params: called CC=2 SCAR=6.9
Pod undocumented
1726 8 my $cache = $self->{cache};
1727  
1728 # These methods require a key
1729 8 foreach my $method (
1730 qw(get get_object get_expires_at exists_and_is_expired is_valid set expire compute get_multi_arrayref get_multi_hashref set_multi remove_multi)
1731 )
1732 {
1733 throws_ok(
1734 96 ▶ sub { $cache->$method() },
Subroutine __ANON__: called CC=1 SCAR=0.0
1735 96 qr/must specify key/,
1736 "$method throws error when no key passed"
1737 );
1738 }
1739 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1740  
1741 sub test_compute : Tests {
1742 8 ▶ my $self = shift;
Subroutine test_compute: called CC=3 SCAR=11.0
Pod undocumented
1743 8 my $cache = $self->{cache};
1744  
1745 # Test current arg order and pre-0.40 arg order
1746 8 foreach my $iter ( 0 .. 1 ) {
1747 16 my $count = 5;
1748 16 my $expire_time = time + 10;
1749 16 ▶ my @args1 = ( { expires_at => $expire_time }, sub { $count++ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1750 my @args2 = (
1751 {
1752 18 ▶ expire_if => sub { 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
1753 },
1754 16 ▶ sub { $count++ }
Subroutine __ANON__: called CC=1 SCAR=0.0
1755 16 );
1756 16 ▶ if ($iter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter) 8 8 16
1757 8 @args1 = reverse(@args1);
1758 8 @args2 = reverse(@args2);
1759 }
1760 16 $cache->clear;
1761 16 is( $cache->get('foo'), undef, "miss" );
1762 16 is( $cache->compute( 'foo', @args1 ), 5, "compute - 5" );
1763 16 is( $cache->get('foo'), 5, "hit - 5" );
1764 16 is( $cache->get_object('foo')->expires_at, $expire_time,
1765 "expire time" );
1766 16 is( $cache->compute( 'foo', @args2 ), 6, "compute - 6" );
1767 16 is( $cache->get('foo'), 6, "hit - 6" );
1768 }
1769  
1770 # Test wantarray
1771 8 $cache->clear();
1772 my $compute_list = sub {
1773 12 ▶ $cache->compute( 'foo', {}, sub { ( int( rand(10000) ) ) x 5 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1774 8 };
1775 8 my @list1 = $compute_list->();
1776 8 my @list2 = $compute_list->();
1777 8 is( scalar(@list1), 5, "list has 5 items" );
1778 8 cmp_deeply( \@list1, \@list2, "lists are the same" );
1779 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1780  
1781 sub test_compress_threshold : Tests {
1782 7 ▶ my $self = shift;
Subroutine test_compress_threshold: called CC=1 SCAR=0.0
Pod undocumented
1783 7 my $cache = $self->{cache};
1784  
1785 7 my $s0 = 'x' x 180;
1786 7 my $s1 = 'x' x 200;
1787 7 $cache->set( 'key0', $s0 );
1788 7 $cache->set( 'key1', $s1 );
1789 7 is_between( $cache->get_object('key0')->size, 180, 220 );
1790 7 is_between( $cache->get_object('key1')->size, 200, 240 );
1791  
1792 7 my $cache2 = $self->new_cache( compress_threshold => 190 );
1793 7 $cache2->set( 'key0', $s0 );
1794 7 $cache2->set( 'key1', $s1 );
1795 7 is_between( $cache2->get_object('key0')->size, 180, 220 );
1796 7 ok( $cache2->get_object('key1')->size < 100 );
1797 7 is( $cache2->get('key0'), $s0 );
1798 7 is( $cache2->get('key1'), $s1 );
1799 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1800  
1801 sub test_expires_on_backend : Tests {
1802 8 ▶ my $self = shift;
Subroutine test_expires_on_backend: called CC=4 SCAR=13.9
Pod undocumented
1803  
1804 8 ▶ return "skipping - no support for expires_on_backend"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->supports_expires_on_backend 7 1 8
1805 unless $self->supports_expires_on_backend();
1806 1 foreach my $expires_on_backend ( 0, 1 ) {
1807 2 my $cache =
1808 $self->new_cache( expires_on_backend => $expires_on_backend );
1809 2 $cache->set( 'key0', 5, '2s' );
1810 2 $cache->set( 'key1', 6, { expires_at => time + 2 } );
1811 2 is( $cache->get('key0'), 5, 'hit key0 before expire' );
1812 2 is( $cache->get('key1'), 6, 'hit key1 before expire' );
1813 2 sleep(3);
1814 2 ok( !defined( $cache->get('key0') ), 'miss key0 after expire' );
1815 2 ok( !defined( $cache->get('key1') ), 'miss key1 after expire' );
1816  
1817 2 ▶ if ($expires_on_backend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_on_backend) { } 1 1 2
1818 1 ok(
1819 !defined( $cache->get_object('key0') ),
1820 'cannot get_object(key0) after expire'
1821 );
1822 1 ok(
1823 !defined( $cache->get_object('key1') ),
1824 'cannot get_object(key1) after expire'
1825 );
1826 }
1827 else {
1828 1 ok(
1829 $cache->get_object('key0')->is_expired(),
1830 'can get_object(key0) after expire'
1831 );
1832 1 ok(
1833 $cache->get_object('key1')->is_expired(),
1834 'can get_object(key1) after expire'
1835 );
1836 }
1837 }
1838 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1839  
1840 sub test_append : Tests {
1841 6 ▶ my $self = shift;
Subroutine test_append: called CC=1 SCAR=0.0
Pod undocumented
1842 6 my $cache = $self->{cache};
1843 my ( $key, $value ) =
1844 6 ( $self->{keys}->{arrayref}, $self->{values}->{medium} );
1845  
1846 # Appending to non-existent key has no effect
1847 #
1848 6 $cache->append( $key, $value );
1849 6 ok( !$cache->get($key) );
1850  
1851 6 ok( $cache->set( $key, $value ) );
1852 6 $cache->append( $key, $value );
1853 6 is( $cache->get($key), $value . $value );
1854 6 $cache->append( $key, $value );
1855 6 is( $cache->get($key), $value . $value . $value );
1856 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1857  
1858 sub test_add : Tests {
1859 8 ▶ my $self = shift;
Subroutine test_add: called CC=1 SCAR=0.0
Pod undocumented
1860 8 my $cache = $self->{cache};
1861 my ( $key, $value ) =
1862 8 ( $self->{keys}->{arrayref}, $self->{values}->{medium} );
1863  
1864 8 my $t = time();
1865  
1866 8 $cache->add( $key, $value, { expires_at => $t + 100 } );
1867 8 is( $cache->get($key), $value, "get" );
1868 8 is( $cache->get_object($key)->expires_at, $t + 100, "expires_at" );
1869  
1870 8 $cache->add( $key, $value . $value, { expires_at => $t + 200 } );
1871 8 is( $cache->get($key), $value, "get (after add)" );
1872 8 is( $cache->get_object($key)->expires_at,
1873 $t + 100, "expires_at (after add)" );
1874  
1875 8 $cache->remove($key);
1876 8 $cache->add( $key, $value . $value, { expires_at => $t + 200 } );
1877 8 is( $cache->get($key), $value . $value, "get (after expire and add)" );
1878 8 is( $cache->get_object($key)->expires_at,
1879 $t + 200, "expires_at (after expire and add)" );
1880 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1881  
1882 sub test_replace : Tests {
1883 8 ▶ my $self = shift;
Subroutine test_replace: called CC=1 SCAR=0.0
Pod undocumented
1884 8 my $cache = $self->{cache};
1885 my ( $key, $value ) =
1886 8 ( $self->{keys}->{arrayref}, $self->{values}->{medium} );
1887  
1888 8 my $t = time();
1889  
1890 8 $cache->replace( $key, $value, { expires_at => $t + 100 } );
1891 8 ok( !$cache->get_object($key), "get" );
1892  
1893 8 $cache->set( $key, $value . $value, { expires_at => $t + 200 } );
1894 8 $cache->replace( $key, $value, { expires_at => $t + 100 } );
1895 8 is( $cache->get($key), $value, "get (after replace)" );
1896 8 is( $cache->get_object($key)->expires_at,
1897 $t + 100, "expires_at (after replace)" );
1898 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1899  
1900 sub test_max_key_length : Tests {
1901 6 ▶ my $self = shift;
Subroutine test_max_key_length: called CC=3 SCAR=11.0
Pod undocumented
1902  
1903 # Test max_key_length and also that key does not get transformed twice in mirror_cache
1904 #
1905 6 my $mirror_store = {};
1906 6 my $cache = $self->new_cleared_cache(
1907 max_key_length => 10,
1908 mirror_cache => { driver => 'Memory', datastore => $mirror_store }
1909 );
1910  
1911 6 foreach my $keyname ( 'medium', 'large' ) {
1912 my ( $key, $value ) =
1913 12 ( $self->{keys}->{$keyname}, $self->{values}->{$keyname} );
1914 12 $cache->set( $key, $value );
1915 12 is( $cache->get($key), $value, $keyname );
1916 12 is( $cache->mirror_cache->get($key), $value, $keyname );
1917 12 ▶ if ( $keyname eq 'medium' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyname eq 'medium') { } 6 6 12
1918 6 is( $cache->get_object($key)->key(), $key, "medium key stored" );
1919 }
1920 else {
1921 6 isnt( $cache->get_object($key)->key(), $key, "md5 key stored" );
1922 6 is( length( $cache->get_object($key)->key() ),
1923 32, "md5 key stored" );
1924 }
1925 }
1926 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1927  
1928 # Test that cache does not get corrupted with multiple concurrent processes writing
1929 #
1930 sub test_multiple_processes : Tests {
1931 5 ▶ my $self = shift;
Subroutine test_multiple_processes: called CC=14 SCAR=52.3
Pod undocumented
1932 5 ▶ return "author test only" unless $ENV{AUTHOR_TESTING};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"AUTHOR_TESTING"} 5 0 5
1933 0 ▶ return "does not pass on file driver"
Branch: 1 branch
BranchTrueFalseTotal
if $self->new_cache->short_driver_name eq "File" 0 0 0
1934 if $self->new_cache->short_driver_name eq 'File';
1935  
1936 0 my ( @values, @pids, %valid_values );
1937 0 my $shared_key = $self->{keys}->{medium};
1938 0 my $num_procs = 4;
1939  
1940 0 local $SIG{CHLD} = 'IGNORE';
1941  
1942 # Each child continuously writes a unique 10000 byte string to a single shared key
1943 #
1944 my $child_action = sub {
1945 0 ▶ my $p = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1946 0 my $value = $values[$p];
1947 0 my $child_cache = $self->new_cache();
1948  
1949 0 sleep(1); # Wait for parent to be ready
1950 0 my $child_end_time = time() + 5;
1951 0 ▶ while ( time < $child_end_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $child_end_time) 0 0 0
1952 0 $child_cache->set( $shared_key, $value );
1953 }
1954 0 $child_cache->set( "done$p", 1 );
1955 0 };
1956  
1957 0 foreach my $p ( 0 .. $num_procs ) {
1958 0 $values[$p] = random_string(10000);
1959 0 $valid_values{ $values[$p] } = $p;
1960 0 ▶ if ( my $pid = fork() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 0 0 0
1961 0 $pids[$p] = $pid;
1962 }
1963 else {
1964 0 $child_action->($p);
1965 0 exit;
1966 }
1967 }
1968  
1969 # Parent continuously gets shared key, makes sure it is one of the valid values.
1970 # Loop until we see done flag for each child process, or until 10 secs pass.
1971 # At end make sure we saw each process's value once.
1972 #
1973 0 my ( %seen, $error );
1974 0 my $parent_end_time = time() + 10;
1975 0 my $parent_cache = $self->new_cache();
1976 0 ▶ while ( !$error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
1977 0 ▶ for ( my $i = 0 ; $i < 100 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 100) 0 0 0
1978 0 my $value = $parent_cache->get($shared_key);
1979 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
1980 0 ▶ if ( defined( my $p = $valid_values{$value} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $p = $valid_values{$value})) { } 0 0 0
1981 0 $seen{$p} = 1;
1982 }
1983 else {
1984 0 $error = "got invalid value '$value' from shared key";
1985 0 last;
1986 }
1987 }
1988 }
1989 0 ▶ if ( !grep { !$parent_cache->get("done$_") } ( 0 .. $num_procs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not $parent_cache->get("done$_");} 0 .. $num_procs) 0 0 0
1990 0 last;
1991 }
1992 0 ▶ if ( time() >= $parent_end_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time >= $parent_end_time) 0 0 0
1993 0 $error = "did not see all done flags after 10 secs";
1994 }
1995 }
1996  
1997 0 ▶ if ( !$error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
1998 0 ▶ if ( my ($p) = grep { !$seen{$_} } ( 0 .. $num_procs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($p) = grep({not $seen{$_};} 0 .. $num_procs)) 0 0 0
1999 0 $error = "never saw value from process $p";
2000 }
2001 }
2002  
2003 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
2004 0 ok( 0, $error );
2005 }
2006 else {
2007 0 ok( 1, "passed" );
2008 }
2009 9 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
2010  
2011 9 1;
« CHI/Stats.pm ↑ Summary CHI/Driver.pm »