-
Notifications
You must be signed in to change notification settings - Fork 38
/
CacheTests.pm
558 lines (465 loc) · 15.1 KB
/
CacheTests.pm
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package CacheTests;
use v5.14;
use utf8;
use Foswiki();
use File::Spec();
use Foswiki::OopsException();
use Foswiki::PageCache();
use Benchmark qw(:hireswallclock);
use Moo;
use namespace::clean;
extends qw(FoswikiFnTestCase);
has testAction => ( is => 'rw', );
has testUri => ( is => 'rw', clearer => 1, );
has testPathInfo =>
( is => 'rw', clearer => 1, lazy => 1, default => sub { $_[0]->testUri }, );
has oldDbiDsn => ( is => 'rw', );
has oldCacheDsn => ( is => 'rw', );
has param_refresh => ( is => 'rw', );
sub fixture_groups {
my $this = shift;
my @page;
foreach my $dir (@INC) {
my $d = File::Spec->catdir( $dir, 'Foswiki', 'PageCache', 'DBI' );
if ( opendir( my $D, $d ) ) {
foreach my $alg ( readdir $D ) {
next if $alg =~ /^\./;
next unless $alg =~ /^(.*)\.pm$/;
$alg = $1;
next if ( grep { $_ eq $alg } @page );
my $dbcheckfn = "dbcheck_$alg";
no strict 'refs';
if ( defined &{$dbcheckfn} ) {
next unless &$dbcheckfn();
}
use strict 'refs';
if ( defined &{$alg} ) {
push( @page, $alg );
next;
}
if ( eval "require Foswiki::PageCache::DBI::$alg; 1;" ) {
no strict 'refs';
*{$alg} = sub {
my $this = shift;
$Foswiki::cfg{CacheManager} =
'Foswiki::PageCache::' . $alg;
};
use strict 'refs';
push( @page, $alg );
}
else {
print STDERR
"Cannot test Foswiki::PageCache::$alg\nCompilation error when trying to 'require' it\n $@";
}
}
closedir($D);
}
}
return (
\@page,
[ 'view', 'rest' ],
[ 'NoCompress', 'Compress' ],
[
'refresh_all', 'refresh_cache', 'refresh_fire', 'refresh_on',
'timing'
],
);
}
sub skip {
my ( $this, $test ) = @_;
return "simple test not compatible with rest transactions"
if ( defined $test && $test =~ m/verify_simple/ && $test =~ m/_rest_/ );
return undef;
}
sub dbcheckDBI {
my ( $dbn, $cfg ) = @_;
my $dbh;
eval {
require DBI;
my $dsn =
"dbi:$dbn:database="
. ( $Foswiki::cfg{Cache}{DBI}{$cfg}{Database} || 'foswiki' )
. ';host='
. ( $Foswiki::cfg{Cache}{DBI}{$cfg}{Host} || 'localhost' )
. (
$Foswiki::cfg{Cache}{DBI}{$cfg}{Port}
? ';port=' . $Foswiki::cfg{Cache}{DBI}{$cfg}{Port}
: ''
);
$dbh = DBI->connect(
$dsn,
$Foswiki::cfg{Cache}{DBI}{$cfg}{Username},
$Foswiki::cfg{Cache}{DBI}{$cfg}{Password},
{
PrintError => 0,
RaiseError => 1
}
);
$dbh->disconnect() if $dbh;
};
if ($@) {
print STDERR
"**** Could not use $cfg; is the database installed and configured?\n";
}
return ( $dbh && !$@ ) ? 1 : 0;
}
sub dbcheck_SQLite {
$Foswiki::cfg{Cache}{DSN} = "dbi:SQLite:dbname=generic.db";
return dbcheckDBI( 'SQLite', 'Generic' );
}
sub SQLite {
Foswiki::load_package('Foswiki::PageCache::DBI::SQLite');
$Foswiki::cfg{Cache}{Implementation} = 'Foswiki::PageCache::DBI::SQLite';
$Foswiki::cfg{Cache}{DBI}{SQLite}{Filename} =
"$Foswiki::cfg{WorkingDir}/${$}_sqlite.db";
$Foswiki::cfg{Cache}{Enabled} = 1;
}
sub dbcheck_PostgreSQL {
return dbcheckDBI( 'Pg', 'PostgreSQL' );
}
sub PostgreSQL {
Foswiki::load_package('Foswiki::PageCache::DBI::PostgreSQL');
$Foswiki::cfg{Cache}{Implementation} =
'Foswiki::PageCache::DBI::PostgreSQL';
$Foswiki::cfg{Cache}{Enabled} = 1;
}
sub dbcheck_MySQL {
return dbcheckDBI( 'mysql', 'MySQL' );
}
sub MySQL {
Foswiki::load_package('Foswiki::PageCache::DBI::MySQL');
$Foswiki::cfg{Cache}{Implementation} = 'Foswiki::PageCache::DBI::MySQL';
$Foswiki::cfg{Cache}{Enabled} = 1;
}
sub dbcheck_Generic {
return dbcheck_SQLite();
}
sub Generic {
$Foswiki::cfg{Cache}{DBI}{DSN} =
"dbi:SQLite:dbname=$Foswiki::cfg{WorkingDir}/${$}_generic.db";
Foswiki::load_package('Foswiki::PageCache::DBI::Generic');
$Foswiki::cfg{Cache}{Implementation} = 'Foswiki::PageCache::DBI::Generic';
$Foswiki::cfg{Cache}{Enabled} = 1;
}
sub Compress {
$Foswiki::cfg{HttpCompress} = 1;
$Foswiki::cfg{Cache}{Compress} = 1;
return;
}
sub NoCompress {
$Foswiki::cfg{HttpCompress} = 0;
$Foswiki::cfg{Cache}{Compress} = 0;
return;
}
sub view {
my $this = shift;
$this->testAction('view');
}
sub refresh_all {
my $this = shift;
$this->{param_refresh} = 'all';
}
sub refresh_on {
my $this = shift;
$this->{param_refresh} = 'on';
}
sub refresh_cache {
my $this = shift;
$this->{param_refresh} = 'cache';
}
sub refresh_fire {
my $this = shift;
$this->{param_refresh} = 'fire';
}
sub timing {
my $this = shift;
$this->{param_refresh} = '';
}
sub rest_handler {
return '';
}
sub rest {
my $this = shift;
$this->testAction('rest');
Foswiki::Func::registerRESTHandler(
'trial', \&rest_handler,
authenticate => 0,
validate => 0,
http_allow => 'GET',
);
$this->testPathInfo( '/' . __PACKAGE__ . '/trial' );
}
my %twistyIDs;
# Convert the random IDs into sequential ones, so that we have some hope of
# writing repeatable tests.
sub _mangleID {
my ($id) = @_;
my $mangledID = $twistyIDs{$id};
if ( not defined $mangledID ) {
$mangledID = scalar( keys(%twistyIDs) ) + 1;
$twistyIDs{$id} = $mangledID;
}
return $mangledID;
}
around set_up => sub {
my $orig = shift;
my $this = shift;
$Foswiki::cfg{Cache}{Enabled} = 0;
$orig->( $this, @_ );
$Foswiki::cfg{HttpCompress} = 0;
$Foswiki::cfg{Cache}{Compress} = 0;
$this->oldDbiDsn( $Foswiki::cfg{Cache}{DBI}{DSN} );
$this->oldCacheDsn( $Foswiki::cfg{Cache}{DSN} );
delete $this->app->env->{FOSWIKI_TEST_PATH_INFO};
delete $this->app->env->{FOSWIKI_TEST_ACTION};
$this->clear_testUri;
$this->clear_testPathInfo;
};
around tear_down => sub {
my $orig = shift;
my $this = shift;
$Foswiki::cfg{Cache}{DBI}{DSN} = $this->oldDbiDsn;
$Foswiki::cfg{Cache}{DSN} = $this->oldCacheDsn;
$orig->($this);
unlink("$Foswiki::cfg{WorkingDir}/${$}_sqlite.db");
unlink("$Foswiki::cfg{WorkingDir}/${$}_generic.db");
};
sub _clearCache {
my ( $this, $pathinfo ) = @_;
$this->app->cfg->data->{Cache}{Debug} = 1;
print STDERR "_clearCache entered\n";
$this->createNewFoswikiApp(
requestParams => {
initializer => {
#refresh => 'all',
skin => ['none'],
action => ['view'],
endPoint => '/Main/WebHome',
},
},
engineParams => {
simulate => 'cgi',
initialAttributes => {
uri => "/Main/WebHome",
path_info => '/Main/WebHome',
method => 'GET',
action => 'view',
},
},
context => { view => 1 },
user => $Foswiki::cfg{AdminUserLogin},
);
print STDERR "_clearcache App created\n";
my ( $one, $result, $stdout, $stderr ) = $this->capture(
sub {
return $this->app->handleRequest;
}
);
print STDERR "_clearcache exit\n";
print STDERR
"== ONE ===\n$one\n==========================================================================\n";
print STDERR
"== RESULT ===\n$result\n==========================================================================\n";
print STDERR
"== STDOUT ===\n$stdout\n==========================================================================\n";
print STDERR
"== STDERR ===\n$stderr\n==========================================================================\n";
return;
}
sub _runQuery {
my $this = shift;
$this->app->cfg->data->{Cache}{Debug} = 1;
$this->createNewFoswikiApp(
requestParams => {
initializer => {
skin => ['none'],
action => [ $this->testAction ],
endPoint => $this->testUri,
},
},
engineParams => {
simulate => 'cgi',
initialAttributes => {
uri => $this->testUri,
path_info => $this->testPathInfo,
method => 'GET',
action => $this->testAction,
},
},
context => { $this->testAction => 1 },
user => $this->test_user_login,
);
my ( $one, $result, $stdout, $stderr ) = $this->capture(
sub {
return $this->app->handleRequest;
}
);
return $one;
}
sub check {
my ( $this, $pathinfo ) = @_;
$this->app->cfg->data->{Cache}{Debug} = 1;
$this->createNewFoswikiApp(
requestParams => {
initializer => {
skin => ['none'],
action => [ $this->testAction ],
endPoint => $this->testUri,
},
},
engineParams => {
simulate => 'cgi',
initialAttributes => {
uri => $this->testUri,
path_info => $this->testPathInfo,
method => 'GET',
action => $this->testAction,
},
},
context => { $this->testAction => 1 },
user => $this->test_user_login,
);
# This first request should *not* be satisfied from the cache, but
# the cache should be populated with the result.
my $p1start = Benchmark->new();
my ( $one, $result, $stdout, $stderr ) = $this->capture(
sub {
return $this->app->handleRequest;
}
);
my $p1end = Benchmark->new();
print STDERR "P1: $stderr\n" if $stderr;
$this->createNewFoswikiApp(
requestParams => {
initializer => {
skin => ['none'],
action => [ $this->testAction ],
endPoint => $this->testUri,
},
},
engineParams => {
simulate => 'cgi',
initialAttributes => {
uri => $this->testUri,
path_info => $this->testPathInfo,
method => 'GET',
action => $this->testAction,
},
},
context => { $this->testAction => 1, },
user => $this->test_user_login,
);
}
# Check timing and general cache operation.
sub check_timing {
my ($this) = @_;
$Foswiki::cfg{Cache}{Debug} = 1;
$this->_clearCache();
# This first request should *not* be satisfied from the cache, but
# the cache should be populated with the result.
my $p1start = Benchmark->new();
my $one = $this->_runQuery();
my $p1end = Benchmark->new();
my $p2start = Benchmark->new();
my $two = $this->_runQuery();
my $p2end = Benchmark->new();
$this->assert( $one =~ s/\r//g, 'Failed to remove \r' );
$this->assert( $one =~ s/^(.*?)\n\n+//s, 'Failed to remove HTTP headers' );
my $one_head = $1;
$this->assert_does_not_match( qr/X-Foswiki-Pagecache/i, $one_head );
$this->assert( $two =~ s/\r//g, 'Failed to remove \r' );
$this->assert( $two =~ s/^(.*?)\n\n+//s, 'Failed to remove HTTP headers' );
my $two_heads = $1;
$this->assert_matches( qr/X-Foswiki-Pagecache: 1/i, $two_heads );
print STDERR "To cache: "
. timestr( timediff( $p1end, $p1start ) ) . "\n";
print STDERR "From cache: "
. timestr( timediff( $p2end, $p2start ) ) . "\n";
return if $one eq $two;
for ( $one, $two ) {
$this->assert(
s/value=['"]\??[a-fA-F0-9]{32}['"]/value=vkey/gs,
'Failed to replace all value=key with dummy key "vkey"'
);
$this->assert( s/([?;&]t=)\d+/${1}0/g,
'Failed to replace timestamp in page URL with dummy (0)' );
# Do *not* assert the removal of SERVERTIME; it is only present
# if the JQueryPlugin::FOSWIKI plugin is installed and enabled.
s/<meta[^>]*?foswiki\.SERVERTIME"[^>]*?>//gi;
# There may not be TWISTY usage; so no need to assert, but IDs need
# to be sequential and not random
%twistyIDs = ();
s/<(span|div)([^>]*?)(\d+?)(show|hide|toggle)([^>]*?)>/'<'.$1.$2._mangleID($3).$4.$5.'>'/ge;
# the two times are rarely different, but if they bridge a clock
# tick...
s/(<input[^>]*\bname="t"[^>]*\bvalue=")\d+("[^>]*>)/$1$2/is;
s/(<input[^>]*\bvalue=")\d+("[^>]*\bname="t"[^>]*>)/$1$2/is;
}
$this->assert_html_equals( $one, $two );
return;
}
sub check_refresh {
my $this = shift;
$this->_clearCache();
my $user =
( $this->{param_refresh} eq 'all' )
? $Foswiki::cfg{AdminUserLogin}
: $this->{test_user_login};
$Foswiki::cfg{Cache}{Debug} = 1;
$this->_runQuery();
# This first request should prime the cache
my $one = $this->_runQuery( $this->{param_refresh} );
# This second request should be satisfied from the cache
my $two = $this->_runQuery();
# This third request with refresh should not be satisfied from the cache
my $three = $this->_runQuery( $this->{param_refresh} );
$this->assert( $one =~ s/\r//g, 'Failed to remove \r' );
$this->assert( $one =~ s/^(.*?)\n\n+//s, 'Failed to remove HTTP headers' );
my $one_head = $1;
$this->assert_does_not_match( qr/X-Foswiki-Pagecache/i, $one_head );
$this->assert( $two =~ s/\r//g, 'Failed to remove \r' );
$this->assert( $two =~ s/^(.*?)\n\n+//s, 'Failed to remove HTTP headers' );
my $two_heads = $1;
$this->assert_matches( qr/X-Foswiki-Pagecache: 1/i, $two_heads );
$this->assert( $three =~ s/\r//g, 'Failed to remove \r' );
$this->assert( $three =~ s/^(.*?)\n\n+//s,
'Failed to remove HTTP headers' );
my $three_heads = $1;
if ( $this->{param_refresh} ) {
$this->assert_does_not_match( qr/X-Foswiki-Pagecache: 1/i,
$three_heads );
}
else {
$this->assert_matches( qr/X-Foswiki-Pagecache: 1/i, $three_heads );
}
return;
}
sub verify_simple {
my $this = shift;
$this->testUri('/');
#$this->check;
if ( $this->{param_refresh} ) {
$this->check_refresh;
}
else {
$this->check_timing;
}
}
sub verify_topic {
my $this = shift;
$this->testUri("/$Foswiki::cfg{SystemWebName}/FileAttribute");
$this->check;
}
sub verify_utf8_topic {
my $this = shift;
use utf8;
my $web = $this->test_web . "/Šňáĺľ";
my $topic = 'Šňá徊ťěř';
Foswiki::Func::createWeb($web);
my ($meta) = Foswiki::Func::readTopic( $web, $topic );
$meta->text($topic);
$meta->save();
$this->testUri( Encode::encode_utf8("/$web/$topic") );
$this->check;
}
1;