1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);
use Carp;
$SIG{__DIE__} = sub { confess @_; };
# This test suite requires total accuracy in ordering of removals over a short
# time period, so a higher resolution timer is required.
eval { require Time::HiRes }
or plan skip_all => 'Time::HiRes is required for this test.';
Time::HiRes->export('Cache::File', 'time');
Time::HiRes->export('Cache::File::Entry', 'time');
plan tests => 22;
require_ok('Cache::File');
my $tempdir = tempdir(CLEANUP => 1);
my $cache = Cache::File->new(
cache_root => $tempdir,
size_limit => 10
);
is(ref($cache->removal_strategy()), 'Cache::RemovalStrategy::LRU',
'Default removal strategy set to LRU');
my $entry1 = $cache->entry('testkey');
my $entry2 = $cache->entry('testkey2');
my $entry3 = $cache->entry('testkey3');
# Test that entry1 is removed when entry2 overfills cache
$entry1->set('012345678'); # 9 bytes
ok($entry1->exists(), 'Entry added');
is($cache->size(), 9, 'Cache size correct');
$entry2->set('0123456'); # 7 bytes
ok($entry2->exists(), 'Second entry added');
ok(!$entry1->exists(), 'First entry removed');
is($cache->size(), 7, 'Cache size correct');
# Test that readding entry1 overfills cache and removes entry2
$entry1->set('012345678'); # 9 bytes
ok($entry1->exists(), 'First entry added');
ok(!$entry2->exists(), 'Second entry removed');
is($cache->size(), 9, 'Cache size correct');
# Test that entry1 is removed after entry2 & entry3 are added and overfill cache
$entry1->remove();
is($cache->size(), 0, 'Cache size correct');
$entry1->set('0123'); # 4 bytes
ok($entry1->exists(), 'First entry added');
$entry2->set('0123'); # 4 bytes
ok($entry1->exists(), 'Second entry added');
is($cache->size(), 8, 'Cache size correct');
$entry3->set('01234'); # 5 bytes
ok($entry3->exists(), 'Third entry added');
ok(!$entry1->exists(), 'First entry removed');
ok($entry2->exists(), 'Second entry remains');
is($cache->size(), 9, 'Cache size correct');
# Test that entry2 is removed after entry1 is used (LRU)
$entry1->remove();
$entry2->remove();
$entry3->remove();
$entry1->set('0123'); # 4 bytes
$entry2->set('0123'); # 4 bytes
$entry1->get();
$entry3->set('0123'); # 4 bytes
ok($entry3->exists(), 'Third entry added');
ok($entry1->exists(), 'First entry remains');
ok(!$entry2->exists(), 'Second entry removed');
is($cache->size(), 8, 'Cache size correct');
|