Bug 16185: t/db_dependent/Reports_Guided.t is failing
[koha.git] / t / Cache.t
blob8adaf1c92782e8844e82ee61fe05ed0fcd83d7fe
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Test::More tests => 37;
22 my $destructorcount = 0;
24 BEGIN {
25 use_ok('Koha::Cache');
26 use_ok('Koha::Cache::Object');
27 use_ok('C4::Context');
30 SKIP: {
31 # Set a special namespace for testing, to avoid breaking
32 # if test is run with a different user than Apache's.
33 $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
34 my $cache = Koha::Cache->get_instance();
36 skip "Cache not enabled", 33
37 unless ( $cache->is_cache_active() && defined $cache );
39 # test fetching an item that isnt in the cache
40 is( $cache->get_from_cache("not in here"),
41 undef, "fetching item NOT in cache" );
43 # test expiry time in cache
44 $cache->set_in_cache( "timeout", "I AM DATA", 1 ); # expiry time of 1 second
45 sleep 2;
46 $cache->flush_L1_cache();
47 is( $cache->get_from_cache("timeout"),
48 undef, "fetching expired item from cache" );
50 # test fetching a valid, non expired, item from cache
51 $cache->set_in_cache( "clear_me", "I AM MORE DATA", 1000 )
52 ; # overly large expiry time, clear below
53 $cache->set_in_cache( "dont_clear_me", "I AM MORE DATA22", 1000 )
54 ; # overly large expiry time, clear below
55 is(
56 $cache->get_from_cache("clear_me"),
57 "I AM MORE DATA",
58 "fetching valid item from cache"
61 # test clearing from cache
62 $cache->clear_from_cache("clear_me");
63 is( $cache->get_from_cache("clear_me"),
64 undef, "fetching cleared item from cache" );
65 is(
66 $cache->get_from_cache("dont_clear_me"),
67 "I AM MORE DATA22",
68 "fetching valid item from cache (after clearing another item)"
71 #test flushing from cache
72 $cache->set_in_cache( "flush_me", "testing 1 data" );
73 $cache->flush_all;
74 is( $cache->get_from_cache("flush_me"),
75 undef, "fetching flushed item from cache" );
76 is( $cache->get_from_cache("dont_clear_me"),
77 undef, "fetching flushed item from cache" );
79 my $constructorcount = 0;
80 my $myscalar = $cache->create_scalar(
82 'key' => 'myscalar',
83 'timeout' => 1,
84 'allowupdate' => 1,
85 'unset' => 1,
86 'constructor' => sub { return ++$constructorcount; },
87 'destructor' => sub { return ++$destructorcount; },
90 ok( defined($myscalar), 'Created tied scalar' );
91 is( $$myscalar, 1, 'Constructor called to first initialize' );
92 $cache->flush_L1_cache();
93 is( $$myscalar, 1, 'Data retrieved from cache' );
94 $cache->flush_L1_cache();
95 sleep 2;
96 is( $$myscalar, 2, 'Constructor called again when timeout reached' );
97 $$myscalar = 5;
98 is( $$myscalar, 5, 'Stored new value to cache' );
99 is( $constructorcount, 2, 'Constructor not called after storing value' );
100 undef $myscalar;
102 is( $cache->get_from_cache("myscalar"),
103 undef, 'Item removed from cache on destruction' );
105 my %hash = ( 'key' => 'value' );
107 my $myhash = $cache->create_hash(
109 'key' => 'myhash',
110 'timeout' => 1,
111 'allowupdate' => 1,
112 'unset' => 1,
113 'constructor' => sub { return { %hash }; },
117 ok(defined $myhash, 'Created tied hash');
119 is($myhash->{'key'}, 'value', 'Found expected value in hash');
120 ok(exists $myhash->{'key'}, 'Exists works');
121 $myhash->{'key2'} = 'surprise';
122 is($myhash->{'key2'}, 'surprise', 'Setting hash member worked');
123 $hash{'key2'} = 'nosurprise';
124 sleep 2;
125 $cache->flush_L1_cache();
126 is($myhash->{'key2'}, 'nosurprise', 'Cache change caught');
129 my $foundkeys = 0;
130 foreach my $key (keys %{$myhash}) {
131 $foundkeys++;
134 is($foundkeys, 2, 'Found expected 2 keys when iterating through hash');
136 isnt(scalar %{$myhash}, undef, 'scalar knows the hash is not empty');
138 $hash{'anotherkey'} = 'anothervalue';
140 sleep 2;
141 $cache->flush_L1_cache();
143 ok(exists $myhash->{'anotherkey'}, 'Cache reset properly');
145 delete $hash{'anotherkey'};
146 delete $myhash->{'anotherkey'};
148 ok(!exists $myhash->{'anotherkey'}, 'Key successfully deleted');
150 undef %hash;
151 %{$myhash} = ();
153 is(scalar %{$myhash}, 0, 'hash cleared');
155 $hash{'key'} = 'value';
156 is($myhash->{'key'}, 'value', 'retrieved value after clearing cache');
158 # UTF8 testing
159 my $utf8_str = "A Møøse once bit my sister";
160 $cache->set_in_cache('utf8_1', $utf8_str);
161 is($cache->get_from_cache('utf8_1'), $utf8_str, 'Simple 8-bit UTF8 correctly retrieved');
162 $utf8_str = "\x{20ac}"; # €
163 $cache->set_in_cache('utf8_1', $utf8_str);
164 my $utf8_res = $cache->get_from_cache('utf8_1');
165 # This'll ensure that we're getting a unicode string back, rather than
166 # a couple of bytes.
167 is(length($utf8_res), 1, 'UTF8 string length correct');
168 # ...and that it's really the character we intend
169 is(ord($utf8_res), 8364, 'UTF8 string value correct');
171 # Make sure the item will be deep copied
172 # Scalar
173 my $item = "just a simple scalar";
174 $cache->set_in_cache('test_deep_copy', $item);
175 my $item_from_cache = $cache->get_from_cache('test_deep_copy');
176 $item_from_cache = "a modified scalar";
177 is( $cache->get_from_cache('test_deep_copy'), 'just a simple scalar', 'A scalar will not be modified in the cache if get from the cache' );
178 # Array
179 my @item = qw( an array ref );
180 $cache->set_in_cache('test_deep_copy_array', \@item);
181 $item_from_cache = $cache->get_from_cache('test_deep_copy_array');
182 @$item_from_cache = qw( another array ref );
183 is_deeply( $cache->get_from_cache('test_deep_copy_array'), [ qw ( an array ref ) ], 'An array will be deep copied');
184 $item_from_cache = $cache->get_from_cache('test_deep_copy_array', { unsafe => 1 });
185 @$item_from_cache = qw( another array ref );
186 is_deeply( $cache->get_from_cache('test_deep_copy_array'), [ qw ( another array ref ) ], 'An array will not be deep copied if the unsafe flag is set');
187 # Hash
188 my %item = ( a => 'hashref' );
189 $cache->set_in_cache('test_deep_copy_hash', \%item);
190 $item_from_cache = $cache->get_from_cache('test_deep_copy_hash');
191 %$item_from_cache = ( another => 'hashref' );
192 is_deeply( $cache->get_from_cache('test_deep_copy_hash'), { a => 'hashref' }, 'A hash will be deep copied');
193 $item_from_cache = $cache->get_from_cache('test_deep_copy_hash', { unsafe => 1});
194 %$item_from_cache = ( another => 'hashref' );
195 is_deeply( $cache->get_from_cache('test_deep_copy_hash'), { another => 'hashref' }, 'A hash will not be deep copied if the unsafe flag is set');
198 END {
199 SKIP: {
200 $ENV{ MEMCACHED_NAMESPACE } = 'unit_tests';
201 my $cache = Koha::Cache->get_instance();
202 skip "Cache not enabled", 1
203 unless ( $cache->is_cache_active() );
204 is( $destructorcount, 1, 'Destructor run exactly once' );
205 # cleanup temporary file
206 my $tmp_file = $cache->{ fastmmap_cache }->{ share_file };
207 unlink $tmp_file if defined $tmp_file;