File: cache.t

package info (click to toggle)
libapache-asp-perl 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,108 kB
  • ctags: 830
  • sloc: perl: 6,033; php: 417; sh: 65; lisp: 22; makefile: 10
file content (171 lines) | stat: -rw-r--r-- 4,932 bytes parent folder | download | duplicates (7)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

use Apache::ASP::CGI;
use strict;
$SIG{__DIE__} = \&Carp::confess;

my @dbms = qw( MLDBM::Sync::SDBM_File DB_File GDBM_File );
my $dbm_ok;
for my $dbm ( @dbms ) {
    eval "use $dbm";
    if(! $@) {
	$dbm_ok = $dbm;
#	print STDERR $dbm_ok."\n";
	last;
    }
}
return unless $dbm_ok;

&Apache::ASP::CGI::do_self(
			   CacheSize => '1K',  # auto cleanup after test		  
			   CacheDB => $dbm_ok,
			   UseStrict => 1,
			   NoState => 1,
#			   Debug => -3,
			   # CacheDir can be set separately from StateDir
			   StateDir => '.state',
			   CacheDir => '.cache',
);

__END__

<% 
my $asp = $Server->{asp};
my $cache_lock = ".cache/cache/Response.lock";

my $reset_cache_counts = sub { map { $asp->{'cache_count_'.$_} = 0 } 
			       qw( fetch miss store expires last_modified_expires ) 
			       };
my $check_cache_counts = sub {
    my($error, %args) = @_;
    for my $key ( keys %args ) {
	my $asp_key = 'cache_count_'.$key;
	$t->eok($asp->{$asp_key} == $args{$key},
		"$error cache test: $asp_key is $asp->{$asp_key}, should be $args{$key}"
		);
	
    }
};

my $out_length = 2000;
my $script = qq[<\%\= 
		"1234" x 500
		%\>];

# BASIC
for(1..3) {
    my $out = $Response->TrapInclude({
	File => \$script,
	Cache => 1,
	Expires => 3600,
	LastModified => time()-10,
	Key => $0,
    });
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
}

&$check_cache_counts("BASIC", fetch => 2, miss => 1, store => 1);
&$reset_cache_counts;

$t->eok(-e $cache_lock, "Cache lock test");

# EXPIRES PAST
for(1..3) {
    my $out = $Response->TrapInclude({
	File => \$script,
	Cache => 1,
	Expires => -1,
	Key => $0,
    });
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
}
&$check_cache_counts("EXPIRES", expires => 3, store => 3);
&$reset_cache_counts;

# EXPIRES FUTURE, first is new, second should be cached, third should expire
for(1..3) {
    my $out = $Response->TrapInclude({
	File => \$script,
	Cache => 1,
	Expires => 2,
	Key => [ 'EXPIRES FUTURE' ],
    });
    if($_ == 2) { sleep 2; };
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
};
&$check_cache_counts("EXPIRES FUTURE", miss => 1, fetch => 1, expires => 1, store => 2);
&$reset_cache_counts;

# LAST MODIFIED EXPIRE/CACHE
for my $last_modified ( time + 10, Apache::ASP::Date::time2str(time + 10), time-10, Apache::ASP::Date::time2str(time-10) ) {
    my $out = $Response->TrapInclude({
	File => \$script,
	Cache => 1,
	Key => [ 'EXPIRES FUTURE' ],
	LastModified => $last_modified,
    });
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
}
&$check_cache_counts("LAST MODIFED EXPIRES", last_modified_expires => 2, store => 2, fetch => 2);
&$reset_cache_counts;

# CLEAR
for (1,0,1,0,1) {
    my $out = $Response->TrapInclude({
	File => \$script,
	Cache => 1,
	Key => [ 'EXPIRES FUTURE' ],
	Clear => $_,
    });
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
}
&$check_cache_counts("CLEAR", store => 3, fetch => 2);
&$reset_cache_counts;

# KEY
for (1,0,1,0,1) {
    my $out = $Response->TrapInclude({
	File => \$script,
	Cache => 1,
	Key => { 'KEY TEST' => $_ },
    });
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
}
&$check_cache_counts("CLEAR", miss => 2, store => 2, fetch => 3);
&$reset_cache_counts;

# NORMAL + RV
for my $arg (1,0,1,0,1,0,1) {
    my @rv = $Response->Include({
	File => 'cache_test.inc',
	Cache => 1,
    }, $arg, $arg);
    $Response->Debug("return values from cached include: ",@rv);
    $t->eok((grep($_ eq $arg, @rv)) == 2, "Return values from caching include");
    my $out = $Response->{BinaryRef};
    $$out =~ s/\s+//isg;
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
    $Response->Clear;
}
&$check_cache_counts("CLEAR", miss => 2, store => 2, fetch => 5);
&$reset_cache_counts;

# KEY CHECK 2
for my $arg ({ arg => 1 }, { arg => 1 }, { arg => 1 }, { arg => 2 }) {
    my @rv = $Response->Include({
	File => 'cache_test.inc',
	Cache => 1,
	Key => $arg
	}, $arg );
    my $out = $Response->{BinaryRef};
    $$out =~ s/\s+//isg;
    $t->eok(length($$out) == $out_length, "Output length from include should be $out_length, found: ".length($$out));
    $Response->Clear;
}
&$check_cache_counts("CLEAR", miss => 2, store => 2, fetch => 2);
&$reset_cache_counts;

$asp->{r}->register_cleanup(sub { -e $cache_lock && die("cache lock $cache_lock still exists after cleanup") });

%>