1
2#########################
3
4use Test::More tests => 19;
5BEGIN { use_ok('Cache::FastMmap') };
6use Data::Dumper;
7use strict;
8
9#########################
10
11# Test maintaining write back of expired items after get
12
13# Test a backing store just made of a local hash
14my %BackingStore = ();
15
16my $FC = Cache::FastMmap->new(
17  serializer => '',
18  init_file => 1,
19  num_pages => 1,
20  page_size => 8192,
21  context => \%BackingStore,
22  write_cb => sub { $_[0]->{$_[1]} = $_[2]; },
23  delete_cb => sub { delete $_[0]->{$_[1]} },
24  write_action => 'write_back'
25);
26
27my $epoch = time;
28my $now = $epoch;
29Cache::FastMmap::_set_time_override($now);
30
31ok( defined $FC );
32
33ok( $FC->set('foo', '123abc', 1), 'store item 1');
34ok( $FC->set('bar', '456def', 2), 'store item 2');
35ok( $FC->set('baz', '789ghi', 3), 'store item 3');
36is( $FC->get('foo'), '123abc',  "get item 1");
37is( $FC->get('bar'), '456def',  "get item 2");
38is( $FC->get('baz'), '789ghi',  "get item 3");
39
40$now = $epoch+1;
41Cache::FastMmap::_set_time_override($now);
42
43is( $FC->get('foo'), undef,     "get item 1 after sleep 1");
44is( $FC->get('bar'), '456def',  "get item 2 after sleep 1");
45is( $FC->get('baz'), '789ghi',  "get item 3 after sleep 1");
46
47$FC->empty(1);
48
49ok( eq_hash(\%BackingStore, { foo => '123abc' }), "items match expire 1" );
50
51$FC->expire('baz');
52$FC->expire('dummy');
53
54ok( eq_hash(\%BackingStore, { foo => '123abc', baz => '789ghi' }), "items match explicit expire" );
55
56is( $FC->get('baz'), undef,  "get item 3 after explicit expire");
57
58$now = $epoch+2;
59Cache::FastMmap::_set_time_override($now);
60
61is( $FC->get('foo'), undef,  "get item 1 after sleep 2");
62is( $FC->get('bar'), undef,  "get item 2 after sleep 2");
63is( $FC->get('baz'), undef,  "get item 3 after sleep 2");
64
65$FC->empty(1);
66
67ok( eq_hash(\%BackingStore, { foo => '123abc', bar => '456def', baz => '789ghi' }), "items match expire 2");
68
69$FC->remove('foo');
70
71ok( eq_hash(\%BackingStore, { bar => '456def', baz => '789ghi' }), "items match remove 1");
72
73
74