gitweb/lib - capture output directly to cache entry file
[git/jnareb-git.git] / t / t9503 / test_cache_interface.pl
blob0b4b09fa6499d0703afb94e06396e3965cb91f16
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
4 use warnings;
5 use strict;
7 use Test::More;
9 # test source version
10 use lib $ENV{GITWEBLIBDIR} || "$ENV{GIT_BUILD_DIR}/gitweb/lib";
13 # Test creating a cache
15 BEGIN { use_ok('GitwebCache::SimpleFileCache'); }
16 diag("Using lib '$INC[0]'");
17 diag("Testing '$INC{'GitwebCache/SimpleFileCache.pm'}'");
19 my $cache = new_ok('GitwebCache::SimpleFileCache');
21 # Test that default values are defined
23 ok(defined $GitwebCache::SimpleFileCache::DEFAULT_CACHE_ROOT,
24 '$DEFAULT_CACHE_ROOT defined');
25 ok(defined $GitwebCache::SimpleFileCache::DEFAULT_CACHE_DEPTH,
26 '$DEFAULT_CACHE_DEPTH defined');
28 # Test accessors and default values for cache
30 SKIP: {
31 skip 'default values not defined', 3
32 unless ($GitwebCache::SimpleFileCache::DEFAULT_CACHE_ROOT &&
33 $GitwebCache::SimpleFileCache::DEFAULT_CACHE_DEPTH);
35 is($cache->get_namespace(), '', "default namespace is ''");
36 cmp_ok($cache->get_root(), 'eq', $GitwebCache::SimpleFileCache::DEFAULT_CACHE_ROOT,
37 "default cache root is '$GitwebCache::SimpleFileCache::DEFAULT_CACHE_ROOT'");
38 cmp_ok($cache->get_depth(), '==', $GitwebCache::SimpleFileCache::DEFAULT_CACHE_DEPTH,
39 "default cache depth is $GitwebCache::SimpleFileCache::DEFAULT_CACHE_DEPTH");
42 # Test the getting, setting, and removal of a cached value
43 # (Cache::Cache interface)
45 my $key = 'Test Key';
46 my $value = 'Test Value';
48 subtest 'Cache::Cache interface' => sub {
49 foreach my $method (qw(get set remove)) {
50 can_ok($cache, $method);
53 $cache->set($key, $value);
54 cmp_ok($cache->get_size($key), '>', 0, 'get_size after set, is greater than 0');
55 is($cache->get($key), $value, 'get after set, returns cached value');
56 $cache->remove($key);
57 ok(!defined($cache->get($key)), 'get after remove, is undefined');
59 eval { $cache->remove('Not-Existent Key'); };
60 ok(!$@, 'remove on non-existent key doesn\'t die');
61 diag($@) if $@;
63 done_testing();
66 # Test the getting and setting of a cached value
67 # (CHI interface)
69 my $call_count = 0;
70 sub get_value {
71 $call_count++;
72 return $value;
74 subtest 'CHI interface' => sub {
75 can_ok($cache, qw(compute));
77 is($cache->compute($key, \&get_value), $value, "compute 1st time (set) returns '$value'");
78 is($cache->compute($key, \&get_value), $value, "compute 2nd time (get) returns '$value'");
79 is($cache->compute($key, \&get_value), $value, "compute 3rd time (get) returns '$value'");
80 cmp_ok($call_count, '==', 1, 'get_value() is called once from compute');
82 done_testing();
85 # Test the getting and setting of a cached value
86 # (compute_fh interface)
88 $call_count = 0;
89 sub write_value {
90 my $fh = shift;
91 $call_count++;
92 print {$fh} $value;
94 sub compute_fh_output {
95 my ($cache, $key, $code_fh) = @_;
97 my ($fh, $filename) = $cache->compute_fh($key, $code_fh);
99 local $/ = undef;
100 return <$fh>;
102 subtest 'compute_fh interface' => sub {
103 can_ok($cache, qw(compute_fh));
105 $cache->remove($key);
106 is(compute_fh_output($cache, $key, \&write_value), $value,
107 "compute_fh 1st time (set) returns '$value'");
108 is(compute_fh_output($cache, $key, \&write_value), $value,
109 "compute_fh 2nd time (get) returns '$value'");
110 is(compute_fh_output($cache, $key, \&write_value), $value,
111 "compute_fh 3rd time (get) returns '$value'");
112 cmp_ok($call_count, '==', 1, 'write_value() is called once from compute_fh');
114 done_testing();
117 # Test cache expiration
119 subtest 'cache expiration' => sub {
120 $cache->set_expires_in(60*60*24); # set expire time to 1 day
121 cmp_ok($cache->get_expires_in(), '>', 0, '"expires in" is greater than 0');
122 is($cache->get($key), $value, 'get returns cached value (not expired in 1d)');
124 $cache->set_expires_in(-1); # set expire time to never expire
125 is($cache->get_expires_in(), -1, '"expires in" is set to never (-1)');
126 is($cache->get($key), $value, 'get returns cached value (not expired)');
128 $cache->set_expires_in(0);
129 is($cache->get_expires_in(), 0, '"expires in" is set to now (0)');
130 $cache->set($key, $value);
131 ok(!defined($cache->get($key)), 'cache is expired');
133 done_testing();
136 # Test assertions for adaptive cache expiration
138 my $load = 0.0;
139 sub load { return $load; }
140 my $expires_min = 10;
141 my $expires_max = 30;
142 $cache->set_expires_in(-1);
143 $cache->set_expires_min($expires_min);
144 $cache->set_expires_max($expires_max);
145 $cache->set_check_load(\&load);
146 subtest 'adaptive cache expiration' => sub {
147 cmp_ok($cache->get_expires_min(), '==', $expires_min,
148 '"expires min" set correctly');
149 cmp_ok($cache->get_expires_max(), '==', $expires_max,
150 '"expires max" set correctly');
152 $load = 0.0;
153 cmp_ok($cache->get_expires_in(), '>=', $expires_min,
154 '"expires in" bound from down for load=0');
155 cmp_ok($cache->get_expires_in(), '<=', $expires_max,
156 '"expires in" bound from up for load=0');
158 $load = 1_000;
159 cmp_ok($cache->get_expires_in(), '>=', $expires_min,
160 '"expires in" bound from down for heavy load');
161 cmp_ok($cache->get_expires_in(), '<=', $expires_max,
162 '"expires in" bound from up for heavy load');
164 done_testing();
167 $cache->set_expires_in(-1);
169 done_testing();