3 # Tests Koha::Cache and whichever type of cache is enabled (through Koha::Cache)
8 use Test
::More tests
=> 29;
10 my $destructorcount = 0;
13 use_ok
('Koha::Cache');
14 use_ok
('Koha::Cache::Object');
15 use_ok
('C4::Context');
19 my $cache = Koha
::Cache
->new();
21 skip
"Cache not enabled", 25
22 unless ( Koha
::Cache
->is_cache_active() && defined $cache );
24 # test fetching an item that isnt in the cache
25 is
( $cache->get_from_cache("not in here"),
26 undef, "fetching item NOT in cache" );
28 # test expiry time in cache
29 $cache->set_in_cache( "timeout", "I AM DATA", 1 ); # expiry time of 1 second
31 is
( $cache->get_from_cache("timeout"),
32 undef, "fetching expired item from cache" );
34 # test fetching a valid, non expired, item from cache
35 $cache->set_in_cache( "clear_me", "I AM MORE DATA", 1000 )
36 ; # overly large expiry time, clear below
37 $cache->set_in_cache( "dont_clear_me", "I AM MORE DATA22", 1000 )
38 ; # overly large expiry time, clear below
40 $cache->get_from_cache("clear_me"),
42 "fetching valid item from cache"
45 # test clearing from cache
46 $cache->clear_from_cache("clear_me");
47 is
( $cache->get_from_cache("clear_me"),
48 undef, "fetching cleared item from cache" );
50 $cache->get_from_cache("dont_clear_me"),
52 "fetching valid item from cache (after clearing another item)"
55 #test flushing from cache
56 $cache->set_in_cache( "flush_me", "testing 1 data" );
58 is
( $cache->get_from_cache("flush_me"),
59 undef, "fetching flushed item from cache" );
60 is
( $cache->get_from_cache("dont_clear_me"),
61 undef, "fetching flushed item from cache" );
63 my $constructorcount = 0;
64 my $myscalar = $cache->create_scalar(
70 'constructor' => sub { return ++$constructorcount; },
71 'destructor' => sub { return ++$destructorcount; },
74 ok
( defined($myscalar), 'Created tied scalar' );
75 is
( $$myscalar, 1, 'Constructor called to first initialize' );
76 is
( $$myscalar, 1, 'Data retrieved from cache' );
78 is
( $$myscalar, 2, 'Constructor called again when timeout reached' );
80 is
( $$myscalar, 5, 'Stored new value to cache' );
81 is
( $constructorcount, 2, 'Constructor not called after storing value' );
84 is
( $cache->get_from_cache("myscalar"),
85 undef, 'Item removed from cache on destruction' );
87 my %hash = ( 'key' => 'value' );
89 my $myhash = $cache->create_hash(
95 'constructor' => sub { return { %hash }; },
99 ok
(defined $myhash, 'Created tied hash');
101 is
($myhash->{'key'}, 'value', 'Found expected value in hash');
102 ok
(exists $myhash->{'key'}, 'Exists works');
103 $myhash->{'key2'} = 'surprise';
104 is
($myhash->{'key2'}, 'surprise', 'Setting hash member worked');
105 $hash{'key2'} = 'nosurprise';
107 is
($myhash->{'key2'}, 'nosurprise', 'Cache change caught');
111 foreach my $key (keys %{$myhash}) {
115 is
($foundkeys, 2, 'Found expected 2 keys when iterating through hash');
117 isnt
(scalar %{$myhash}, undef, 'scalar knows the hash is not empty');
119 $hash{'anotherkey'} = 'anothervalue';
123 ok
(exists $myhash->{'anotherkey'}, 'Cache reset properly');
125 delete $hash{'anotherkey'};
126 delete $myhash->{'anotherkey'};
128 ok
(!exists $myhash->{'anotherkey'}, 'Key successfully deleted');
133 is
(scalar %{$myhash}, 0, 'hash cleared');
135 $hash{'key'} = 'value';
136 is
($myhash->{'key'}, 'value', 'retrieved value after clearing cache');
141 skip
"Cache not enabled", 1
142 unless ( Koha
::Cache
->is_cache_active() );
143 is
( $destructorcount, 1, 'Destructor run exactly once' );