Merge remote-tracking branch 'origin/new/bug_7747'
[koha.git] / t / Cache.t
1 #!/usr/bin/perl
2
3 # Tests Koha::Cache and whichever type of cache is enabled (through Koha::Cache)
4
5 use strict;
6 use warnings;
7
8 use Test::More tests => 9;
9
10 BEGIN {
11         use_ok('Koha::Cache');
12         use_ok('C4::Context');
13 }
14
15 SKIP: {
16     my $cache = Koha::Cache->new ();
17
18     skip "Cache not enabled", 7 unless (Koha::Cache->is_cache_active() && defined $cache);
19
20     # test fetching an item that isnt in the cache
21     is( $cache->get_from_cache("not in here"), undef, "fetching item NOT in cache");
22
23     # test expiry time in cache
24     $cache->set_in_cache("timeout", "I AM DATA", 1); # expiry time of 1 second
25     sleep 1;
26     is( $cache->get_from_cache("timeout"), undef, "fetching expired item from cache");
27
28     # test fetching a valid, non expired, item from cache
29     $cache->set_in_cache("clear_me", "I AM MORE DATA", 1000); # overly large expiry time, clear below
30     $cache->set_in_cache("dont_clear_me", "I AM MORE DATA22", 1000); # overly large expiry time, clear below
31     is( $cache->get_from_cache("clear_me"), "I AM MORE DATA", "fetching valid item from cache");
32
33     # test clearing from cache
34     $cache->clear_from_cache("clear_me");
35     is( $cache->get_from_cache("clear_me"), undef, "fetching cleared item from cache");
36     is( $cache->get_from_cache("dont_clear_me"), "I AM MORE DATA22", "fetching valid item from cache (after clearing another item)");
37
38     #test flushing from cache
39     $cache->set_in_cache("flush_me", "testing 1 data");
40     $cache->flush_all;
41     is( $cache->get_from_cache("flush_me"), undef, "fetching flushed item from cache");
42     is( $cache->get_from_cache("dont_clear_me"), undef, "fetching flushed item from cache");
43 }