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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
|
#!/usr/bin/perl -w
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print. (It may become useful if
# the test is moved to ./t subdirectory.) Remember that all the tests
# except the first are done twice--once with Storable, and once with
# Data::Dumper. The $TEST_SET_SIZE is the number of unique tests, not
# counting the trivial first test. @PERSISTENCE_MECHANISMS is an array
# containing all the supported persistence mechanisms
use vars qw($TEST_SET_SIZE @PERSISTENCE_MECHANISMS);
BEGIN
{
$| = 1;
$TEST_SET_SIZE = 25;
@PERSISTENCE_MECHANISMS = qw(Data::Dumper Storable);
# The test set is repeated once for each implementation, plus the
# first test
my $last_test_to_print =
(($TEST_SET_SIZE) * ($#PERSISTENCE_MECHANISMS + 1)) + 1;
print "1..$last_test_to_print\n";
}
END {print "not ok 1\n" unless $loaded;}
use File::Cache qw($sSUCCESS $sFAILURE);
$loaded = 1;
print "ok 1\n";
######################### End of black magic.
use strict;
my $sTEST_CACHE_KEY = "/tmp/TSTC";
my $sTEST_NAMESPACE = "TestCache";
my $sMAX_SIZE = 1000;
my $sTEST_USERNAME = "web";
my $sTEST_CACHE_DEPTH = 3;
# Run all remaining tests for each implementation
my $test_set_number = 0;
foreach my $implementation (@PERSISTENCE_MECHANISMS)
{
$test_set_number++;
my $test_set_start = $TEST_SET_SIZE * ($test_set_number - 1) + 2;
my $test_set_end = $TEST_SET_SIZE * $test_set_number + 1;
# Only do the tests if the persistence mechanism module is present
if (eval "require $implementation")
{
do_tests($implementation, $test_set_start);
}
else
{
skip_tests($test_set_start, $test_set_end);
}
}
sub skip_tests
{
my ($start,$end) = @_;
for (my $i = $start; $i <= $end;$i++)
{
print "ok $i # skip\n";
}
}
sub do_tests
{
my ($implementation,$test_number_start) = @_;
print "--> Testing $implementation implementation\n";
# Test creation of a cache object
my $test = $test_number_start;
my $cache1 = new File::Cache( { cache_key => $sTEST_CACHE_KEY,
namespace => $sTEST_NAMESPACE,
max_size => $sMAX_SIZE,
auto_remove_stale => 0,
username => $sTEST_USERNAME,
filemode => 0770,
implementation => $implementation,
cache_depth => $sTEST_CACHE_DEPTH } );
if ($cache1)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the setting of a scalar in the cache
$test++;
my $seed_value = "Hello World";
my $key = 'key1';
my $status = $cache1->set($key, $seed_value);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of a scalar from the cache
$test++;
my $val1_retrieved = $cache1->get($key);
if ($val1_retrieved eq $seed_value)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the setting of a blessed object from the cache
$test++;
my $key2 = 'key2';
$status = $cache1->set($key2, $cache1);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of a blessed object from the cache
$test++;
my $cache1_retrieved = $cache1->get($key2);
$val1_retrieved = $cache1_retrieved->get($key);
if ($val1_retrieved eq $seed_value)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of the scalar from a subprocess
$test++;
my $pid = fork( );
if ( not defined $pid )
{
die( "Error forking\n" );
}
elsif ( $pid == 0 )
{
test_subprocess_get( $sTEST_CACHE_KEY,
$sTEST_NAMESPACE,
$sTEST_USERNAME,
$sTEST_CACHE_DEPTH,
$implementation,
$key,
$seed_value,
$test );
exit( 1 );
}
else
{
sleep( 1 );
}
# Test checking the memory consumption of the cache
$test++;
my $size = File::Cache::SIZE($sTEST_CACHE_KEY);
if ($size > 0)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test clearing the cache's namespace
$test++;
$status = $cache1->clear();
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the max_size limit
# Intentionally add more data to the cache than fits in max_size
$test++;
my $string = 'abcdefghij';
my $start_size = $cache1->size();
$cache1->set('initial_value', $string);
my $end_size = $cache1->size();
my $string_size = $end_size - $start_size;
my $cache_item = 0;
# This should take the cache to nearly the edge
while (($cache1->size() + $string_size) < $sMAX_SIZE)
{
$cache1->set("item:$cache_item", $string);
$cache_item++;
}
# This should put it over the top
$cache1->set("item:$cache_item", $string);
if ($cache1->size > $sMAX_SIZE)
{
print "not ok $test\n";
}
else
{
print "ok $test\n";
}
# Test the getting of a scalar after the clearing of a cache
$test++;
my $val2_retrieved = $cache1->get($key);
if ($val2_retrieved)
{
print "not ok $test\n";
}
else
{
print "ok $test\n";
}
# Test the setting of a scalar in the cache with a immediate timeout
$test++;
$status = $cache1->set($key, $seed_value, 0);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of a scalar from the cache that should have timed
# out immediately
$test++;
my $val3_retrieved = $cache1->get($key);
if ($val3_retrieved)
{
print "not ok $test\n";
}
else
{
print "ok $test\n";
}
# Test the getting of the expired scalar using get_stale
$test++;
my $val3_stale_retrieved = $cache1->get_stale($key);
if ($val3_stale_retrieved)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the setting of a scalar in the cache with a timeout in the
# near future
$test++;
$status = $cache1->set($key, $seed_value, 2);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of a scalar from the cache that should not have
# timed out yet (unless the system is *really* slow)
$test++;
my $val4_retrieved = $cache1->get($key);
if ($val4_retrieved eq $seed_value)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of a scalar from the cache that should have timed out
$test++;
sleep(3);
my $val5_retrieved = $cache1->get($key);
if ($val5_retrieved)
{
print "not ok $test\n";
}
else
{
print "ok $test\n";
}
# Test purging the cache's namespace
$test++;
$status = $cache1->purge();
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test getting the creation time of the cache entry
$test++;
my $timed_key = 'timed key';
my $creation_time = time();
my $expires_in = 1000;
$cache1->set($timed_key, $seed_value, $expires_in);
# Delay a bit
sleep(2);
# Let's expect no more than 1 second delay between the creation of
# the cache entry and our saving of the time.
my $cached_creation_time = $cache1->get_creation_time($timed_key);
my $creation_time_delta = $creation_time - $cached_creation_time;
if ($creation_time_delta <= 1)
{
$status = 1;
}
else
{
$status = 0;
}
if ($status)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test getting the expiration time of the cache entry
$test++;
my $expected_expiration_time =
$cache1->get_creation_time($timed_key) + $expires_in;
my $actual_expiration_time = $cache1->get_expiration_time($timed_key);
$status = $expected_expiration_time == $actual_expiration_time;
if ($status)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test PURGING of a cache object
$test++;
$status = File::Cache::PURGE($sTEST_CACHE_KEY);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the removal of a cached file
$test++;
$status = $sSUCCESS;
my $remove_key = "foo";
my $remove_value = "bar";
$cache1->set($remove_key, $remove_value);
$cache1->get($remove_key) eq $remove_value or
$status = $sFAILURE;
$cache1->remove($remove_key) or
$status = $sFAILURE;
if (defined $cache1->get($remove_key))
{
$status = $sFAILURE;
}
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test CLEARING of a cache object
$test++;
$status = File::Cache::CLEAR($sTEST_CACHE_KEY);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test directories not created unless needed
$test++;
File::Cache::CLEAR($sTEST_CACHE_KEY);
if (-e $sTEST_CACHE_KEY)
{
print "not ok $test\n";
}
$cache1 = new File::Cache( { cache_key => $sTEST_CACHE_KEY,
implementation => $implementation,
namespace => $sTEST_NAMESPACE } );
opendir(DIR, $sTEST_CACHE_KEY) or
croak("Couldn't open directory $sTEST_CACHE_KEY: $!");
my @dirents = readdir(DIR);
closedir DIR;
my @files = grep { $_ !~ /^(\.|\.\.|.description)$/ } @dirents;
if (!@files)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
File::Cache::CLEAR($sTEST_CACHE_KEY);
# Test the setting of a binary scalar in the cache
$test++;
$cache1 = new File::Cache( { cache_key => $sTEST_CACHE_KEY,
implementation => $implementation,
namespace => $sTEST_NAMESPACE } );
# Make a string of all possible ASCII characters
$seed_value = '';
for (my $i = 0; $i < 256 ; $i++)
{
$seed_value .= chr($i);
}
my $binary_key = 'key1';
$status = $cache1->set($binary_key, $seed_value);
if ($status == $sSUCCESS)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
# Test the getting of a binary scalar from the cache
$test++;
my $val6_retrieved = $cache1->get($binary_key);
if ($val6_retrieved eq $seed_value)
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
File::Cache::CLEAR($sTEST_CACHE_KEY);
}
sub test_subprocess_get
{
my ( $cache_key, $namespace, $username, $cache_depth, $implementation,
$key, $expected_value, $test ) = @_;
$cache_key or
die( 'cache_key required' );
$namespace or
die( 'namespace required' );
$username or
die( 'username required' );
$cache_depth or
die( 'cache_depth required' );
$implementation or
die( 'implementation required' );
$key or
die( 'key required' );
$expected_value or
die( 'expected_value required' );
$test or
die( 'test required' );
my $cache = new File::Cache( { cache_key => $cache_key,
namespace => $namespace,
username => $username,
implementation => $implementation,
cache_depth => $cache_depth } ) or
die("Couldn't create cache");
my $value = $cache->get($key) or
die( "Couldn't get object at $key" );
if ( $value eq $expected_value )
{
print "ok $test\n";
}
else
{
print "not ok $test\n";
}
}
1;
|