Bug 11629: (follow-up) Add message for librarian that status was updated
[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 => 29;
9
10 my $destructorcount = 0;
11
12 BEGIN {
13     use_ok('Koha::Cache');
14     use_ok('Koha::Cache::Object');
15     use_ok('C4::Context');
16 }
17
18 SKIP: {
19     my $cache = Koha::Cache->new();
20
21     skip "Cache not enabled", 25
22       unless ( Koha::Cache->is_cache_active() && defined $cache );
23
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" );
27
28     # test expiry time in cache
29     $cache->set_in_cache( "timeout", "I AM DATA", 1 ); # expiry time of 1 second
30     sleep 2;
31     is( $cache->get_from_cache("timeout"),
32         undef, "fetching expired item from cache" );
33
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
39     is(
40         $cache->get_from_cache("clear_me"),
41         "I AM MORE DATA",
42         "fetching valid item from cache"
43     );
44
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" );
49     is(
50         $cache->get_from_cache("dont_clear_me"),
51         "I AM MORE DATA22",
52         "fetching valid item from cache (after clearing another item)"
53     );
54
55     #test flushing from cache
56     $cache->set_in_cache( "flush_me", "testing 1 data" );
57     $cache->flush_all;
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" );
62
63     my $constructorcount = 0;
64     my $myscalar         = $cache->create_scalar(
65         {
66             'key'         => 'myscalar',
67             'timeout'     => 1,
68             'allowupdate' => 1,
69             'unset'       => 1,
70             'constructor' => sub { return ++$constructorcount; },
71             'destructor'  => sub { return ++$destructorcount; },
72         }
73     );
74     ok( defined($myscalar), 'Created tied scalar' );
75     is( $$myscalar, 1, 'Constructor called to first initialize' );
76     is( $$myscalar, 1, 'Data retrieved from cache' );
77     sleep 2;
78     is( $$myscalar, 2, 'Constructor called again when timeout reached' );
79     $$myscalar = 5;
80     is( $$myscalar,        5, 'Stored new value to cache' );
81     is( $constructorcount, 2, 'Constructor not called after storing value' );
82     undef $myscalar;
83
84     is( $cache->get_from_cache("myscalar"),
85         undef, 'Item removed from cache on destruction' );
86
87     my %hash = ( 'key' => 'value' );
88
89     my $myhash         = $cache->create_hash(
90         {
91             'key'         => 'myhash',
92             'timeout'     => 1,
93             'allowupdate' => 1,
94             'unset'       => 1,
95             'constructor' => sub { return { %hash }; },
96         }
97     );
98
99     ok(defined $myhash, 'Created tied hash');
100
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';
106     sleep 2;
107     is($myhash->{'key2'}, 'nosurprise', 'Cache change caught');
108
109
110     my $foundkeys = 0;
111     foreach my $key (keys %{$myhash}) {
112         $foundkeys++;
113     }
114
115     is($foundkeys, 2, 'Found expected 2 keys when iterating through hash');
116
117     isnt(scalar %{$myhash}, undef, 'scalar knows the hash is not empty');
118
119     $hash{'anotherkey'} = 'anothervalue';
120
121     sleep 2;
122
123     ok(exists $myhash->{'anotherkey'}, 'Cache reset properly');
124
125     delete $hash{'anotherkey'};
126     delete $myhash->{'anotherkey'};
127
128     ok(!exists $myhash->{'anotherkey'}, 'Key successfully deleted');
129
130     undef %hash;
131     %{$myhash} = ();
132
133     is(scalar %{$myhash}, 0, 'hash cleared');
134
135     $hash{'key'} = 'value';
136     is($myhash->{'key'}, 'value', 'retrieved value after clearing cache');
137 }
138
139 END {
140   SKIP: {
141         skip "Cache not enabled", 1
142           unless ( Koha::Cache->is_cache_active() );
143         is( $destructorcount, 1, 'Destructor run exactly once' );
144     }
145 }