gitweb/lib - Very simple file based cache
[git/jnareb-git.git] / t / t9503 / test_cache_interface.pl
blob1a3f37404ba20690008103ca6c7ed0ee4cbc1878
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 done_testing();