summaryrefslogtreecommitdiff
path: root/cpan/Memoize/t/expfile.t
blob: d6dc08f07fe5545449bf7a379487cfeace6e10e2 (plain)
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
use strict; use warnings;
use Memoize;
use lib 't/lib';

my $n = 0;
$|=1;

if ($ENV{PERL_MEMOIZE_TESTS_FAST_ONLY}) {
  print "1..0 # Skipped: Slow tests disabled\n";
  exit 0;
}

print "1..12\n";
# (1)
++$n; print "ok $n\n";

my $READFILE_CALLS = 0;
my $FILE = './TESTFILE';

sub writefile {
  my $FILE = shift;
  open F, "> $FILE" or die "Couldn't write temporary file $FILE: $!";
  print F scalar(localtime), "\n";
  close F;
}

sub readfile {
  $READFILE_CALLS++;
  my $FILE = shift;
  open F, "< $FILE" or die "Couldn't write temporary file $FILE: $!";
  my $data = <F>;
  close F;
  $data;
}

require ExpireFile;
# (2)
++$n; print "ok $n\n";

tie my %cache => 'ExpireFile';
memoize 'readfile',
    SCALAR_CACHE => [HASH => \%cache],
    LIST_CACHE => 'FAULT'
    ;

# (3)
++$n; print "ok $n\n";

# (4)
writefile($FILE);
++$n; print "ok $n\n";
sleep 4;

# (5-6)
my $t1 = readfile($FILE);
++$n; print "ok $n\n";
++$n; print ((($READFILE_CALLS == 1) ? '' : 'not '), "ok $n\n");

# (7-9)
my $t2 = readfile($FILE);
++$n; print "ok $n\n";  
++$n; print ((($READFILE_CALLS == 1) ? '' : 'not '), "ok $n\n");
++$n; print ((($t1 eq $t2) ? '' : 'not '), "ok $n\n");

# (10-12)
sleep 4;
writefile($FILE);
my $t3 = readfile($FILE);
++$n; print "ok $n\n";
++$n; print ((($READFILE_CALLS == 2) ? '' : 'not '), "ok $n\n");
++$n; print ((($t1 ne $t3) ? '' : 'not '), "ok $n\n");

END { 1 while unlink $FILE || () }