/usr/share/perl5/CHI/t/Driver.pm is in libchi-perl 0.50-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 | package CHI::t::Driver;
BEGIN {
$CHI::t::Driver::VERSION = '0.50';
}
use strict;
use warnings;
use CHI::Test;
use CHI::Test::Util
qw(activate_test_logger cmp_bool is_between random_string skip_until);
use CHI::Util qw(can_load dump_one_line write_file);
use Encode;
use File::Spec::Functions qw(tmpdir);
use File::Temp qw(tempdir);
use List::Util qw(shuffle);
use Scalar::Util qw(weaken);
use Storable qw(dclone);
use Test::Warn;
use base qw(CHI::Test::Class);
# Flags indicating what each test driver supports
sub supports_clear { 1 }
sub supports_expires_on_backend { 0 }
sub supports_get_namespaces { 1 }
sub standard_keys_and_values : Test(startup) {
my ($self) = @_;
my ( $keys_ref, $values_ref ) = $self->set_standard_keys_and_values();
$self->{keys} = $keys_ref;
$self->{values} = $values_ref;
$self->{keynames} = [ keys( %{$keys_ref} ) ];
$self->{key_count} = scalar( @{ $self->{keynames} } );
$self->{all_test_keys} = [ values(%$keys_ref), $self->extra_test_keys() ];
my $cache = $self->new_cache();
push(
@{ $self->{all_test_keys} },
$self->process_keys( $cache, @{ $self->{all_test_keys} } )
);
$self->{all_test_keys_hash} =
{ map { ( $_, 1 ) } @{ $self->{all_test_keys} } };
}
sub kvpair {
my $self = shift;
my $count = shift || 1;
return map {
(
$self->{keys}->{medium} . ( $_ == 1 ? '' : $_ ),
$self->{values}->{medium} . ( $_ == 1 ? '' : $_ )
)
} ( 1 .. $count );
}
sub setup : Test(setup) {
my $self = shift;
$self->{cache} = $self->new_cache();
$self->{cache}->clear() if $self->supports_clear();
}
sub testing_driver_class {
my $self = shift;
my $class = ref($self);
# By default, take the last part of the classname and use it as driver
my $driver_class = 'CHI::Driver::' . ( split( '::', $class ) )[-1];
return $driver_class;
}
sub testing_chi_root_class {
return 'CHI';
}
sub new_cache {
my $self = shift;
return $self->testing_chi_root_class->new( $self->new_cache_options(), @_ );
}
sub new_cleared_cache {
my $self = shift;
my $cache = $self->new_cache(@_);
$cache->clear();
return $cache;
}
sub new_cache_options {
my $self = shift;
return (
driver_class => $self->testing_driver_class(),
on_get_error => 'die',
on_set_error => 'die'
);
}
sub set_standard_keys_and_values {
my $self = shift;
my ( %keys, %values );
my @mixed_chars = ( 32 .. 48, 57 .. 65, 90 .. 97, 122 .. 126, 240 );
%keys = (
'space' => ' ',
'newline' => "\n",
'char' => 'a',
'zero' => 0,
'one' => 1,
'medium' => 'medium',
'mixed' => join( "", map { chr($_) } @mixed_chars ),
'binary' => join( "", map { chr($_) } ( 129 .. 255 ) ),
'large' => scalar( 'ab' x 256 ),
'empty' => 'empty',
'arrayref' => [ 1, 2 ],
'hashref' => { foo => [ 'bar', 'baz' ] },
'utf8' => "Have \x{263a} a nice day",
);
%values = map {
( $_, ref( $keys{$_} ) ? $keys{$_} : scalar( reverse( $keys{$_} ) ) )
} keys(%keys);
$values{empty} = '';
return ( \%keys, \%values );
}
# Extra keys (beyond the standard keys above) that we may use in these
# tests. We need to adhere to this for the benefit of drivers that don't
# support get_keys (like memcached) - they simulate get_keys(), clear(),
# etc. by using this fixed list of keys.
#
sub extra_test_keys {
my ($class) = @_;
return (
'', '2',
'medium2', 'foo',
'hashref', 'test_namespace_types',
"utf8", "encoded",
"binary", ( map { "done$_" } ( 0 .. 2 ) ),
( map { "key$_" } ( 0 .. 20 ) )
);
}
sub set_some_keys {
my ( $self, $c ) = @_;
foreach my $keyname ( @{ $self->{keynames} } ) {
$c->set( $self->{keys}->{$keyname}, $self->{values}->{$keyname} );
}
}
sub test_encode : Tests {
my $self = shift;
my $cache = $self->new_cleared_cache();
my $utf8 = $self->{keys}->{utf8};
my $encoded = encode( utf8 => $utf8 );
my $binary_off = $self->{keys}->{binary};
my $binary_on = substr( $binary_off . $utf8, 0, length($binary_off) );
ok( $binary_off eq $binary_on, "binary_off eq binary_on" );
ok( !Encode::is_utf8($binary_off), "!is_utf8(binary_off)" );
ok( Encode::is_utf8($binary_on), "is_utf8(binary_on)" );
# Key maps to same thing whether encoded or non-encoded
#
my $value = time;
$cache->set( $utf8, $value );
is( $cache->get($utf8), $value, "get" );
is( $cache->get($encoded), $value,
"encoded and non-encoded map to same value" );
# Key maps to same thing whether utf8 flag is off or on
#
# Commenting out for now - this is broken on FastMmap and
# DBI drivers (at least), and not entirely sure whether or
# with what priority we should demand this behavior.
#
if (0) {
$cache->set( $binary_off, $value );
is( $cache->get($binary_off), $value, "get binary_off" );
is( $cache->get($binary_on),
$value, "binary_off and binary_on map to same value" );
$cache->clear($binary_on);
ok( !$cache->get($binary_off), "cleared binary_off" ); #
}
# Value is maintained as a utf8 or binary string, in scalar or in arrayref
$cache->set( "utf8", $utf8 );
is( $cache->get("utf8"), $utf8, "utf8 in scalar" );
$cache->set( "utf8", [$utf8] );
is( $cache->get("utf8")->[0], $utf8, "utf8 in arrayref" );
$cache->set( "encoded", $encoded );
is( $cache->get("encoded"), $encoded, "encoded in scalar" );
$cache->set( "encoded", [$encoded] );
is( $cache->get("encoded")->[0], $encoded, "encoded in arrayref" );
# Value retrieves as same thing whether stored with utf8 flag off or on
#
$cache->set( "binary", $binary_off );
is( $cache->get("binary"), $binary_on, "stored binary_off = binary_on" );
$cache->set( "binary", $binary_on );
is( $cache->get("binary"), $binary_off, "stored binary_on = binary_off" );
}
sub test_simple : Tests {
my $self = shift;
my $cache = shift || $self->{cache};
ok( $cache->set( $self->{keys}->{medium}, $self->{values}->{medium} ) );
is( $cache->get( $self->{keys}->{medium} ), $self->{values}->{medium} );
}
sub test_driver_class : Tests {
my $self = shift;
my $cache = $self->{cache};
isa_ok( $cache, 'CHI::Driver' );
isa_ok( $cache, $cache->driver_class );
can_ok( $cache, 'get', 'set', 'remove', 'clear', 'expire' );
}
sub test_key_types : Tests {
my $self = shift;
my $cache = $self->{cache};
$self->num_tests( $self->{key_count} * 9 + 1 );
my @keys_set;
my $check_keys_set = sub {
my $desc = shift;
cmp_set( [ $cache->get_keys ], \@keys_set, "checking keys $desc" );
};
$check_keys_set->("before sets");
foreach my $keyname ( @{ $self->{keynames} } ) {
my $key = $self->{keys}->{$keyname};
my $value = $self->{values}->{$keyname};
ok( !defined $cache->get($key), "miss for key '$keyname'" );
is( $cache->set( $key, $value ), $value, "set for key '$keyname'" );
push( @keys_set, $self->process_keys( $cache, $key ) );
$check_keys_set->("after set of key '$keyname'");
cmp_deeply( $cache->get($key), $value, "hit for key '$keyname'" );
}
foreach my $keyname ( reverse @{ $self->{keynames} } ) {
my $key = $self->{keys}->{$keyname};
$cache->remove($key);
ok( !defined $cache->get($key),
"miss after remove for key '$keyname'" );
pop(@keys_set);
$check_keys_set->("after removal of key '$keyname'");
}
# Confirm that transform_key is idempotent
#
foreach my $keyname ( @{ $self->{keynames} } ) {
my $key = $self->{keys}->{$keyname};
my $value = $self->{values}->{$keyname};
is(
$cache->transform_key( $cache->transform_key($key) ),
$cache->transform_key($key),
"transform_key is idempotent for '$keyname'"
);
$cache->clear();
$cache->set( $key, $value );
is( scalar( $cache->get_keys() ), 1, "exactly one key" );
cmp_deeply( $cache->get( ( $cache->get_keys )[0] ),
$value, "get with get_keys[0] got same value" );
}
}
sub test_deep_copy : Tests {
my $self = shift;
my $cache = $self->{cache};
$self->set_some_keys($cache);
foreach my $keyname (qw(arrayref hashref)) {
my $key = $self->{keys}->{$keyname};
my $value = $self->{values}->{$keyname};
cmp_deeply( $cache->get($key), $value,
"get($key) returns original data structure" );
cmp_deeply( $cache->get($key), $cache->get($key),
"multiple get($key) return same data structure" );
isnt( $cache->get($key), $value,
"get($key) does not return original reference" );
isnt( $cache->get($key), $cache->get($key),
"multiple get($key) do not return same reference" );
}
my $struct = { a => [ 1, 2 ], b => [ 4, 5 ] };
my $struct2 = dclone($struct);
$cache->set( 'hashref', $struct );
push( @{ $struct->{a} }, 3 );
delete( $struct->{b} );
cmp_deeply( $cache->get('hashref'),
$struct2,
"altering original set structure does not affect cached copy" );
}
sub test_expires_immediately : Tests {
my $self = shift;
return 'author testing only - timing is unreliable'
unless ( $ENV{AUTHOR_TESTING} );
# expires_in default should be ignored
my $cache = $self->new_cache( expires_in => '1 hour' );
# Expires immediately
my $test_expires_immediately = sub {
my ($set_option) = @_;
my ( $key, $value ) = $self->kvpair();
my $desc = dump_one_line($set_option);
is( $cache->set( $key, $value, $set_option ), $value, "set ($desc)" );
is_between(
$cache->get_expires_at($key),
time() - 4,
time(), "expires_at ($desc)"
);
ok( $cache->exists_and_is_expired($key), "is_expired ($desc)" );
ok( !defined $cache->get($key), "immediate miss ($desc)" );
};
$test_expires_immediately->(0);
$test_expires_immediately->(-1);
$test_expires_immediately->("0 seconds");
$test_expires_immediately->("0 hours");
$test_expires_immediately->("-1 seconds");
$test_expires_immediately->( { expires_in => "0 seconds" } );
$test_expires_immediately->( { expires_at => time - 1 } );
$test_expires_immediately->("now");
}
sub test_expires_shortly : Tests {
my $self = shift;
return 'author testing only - timing is unreliable'
unless ( $ENV{AUTHOR_TESTING} );
# expires_in default should be ignored
my $cache = $self->new_cache( expires_in => '1 hour' );
# Expires shortly (real time)
my $test_expires_shortly = sub {
my ($set_option) = @_;
my ( $key, $value ) = $self->kvpair();
my $desc = "set_option = " . dump_one_line($set_option);
my $start_time = time();
is( $cache->set( $key, $value, $set_option ), $value, "set ($desc)" );
is( $cache->get($key), $value, "hit ($desc)" );
is_between(
$cache->get_expires_at($key),
$start_time + 1,
$start_time + 8,
"expires_at ($desc)"
);
ok( !$cache->exists_and_is_expired($key), "not expired ($desc)" );
ok( $cache->is_valid($key), "valid ($desc)" );
# Only bother sleeping and expiring for one of the variants
if ( $set_option eq "3 seconds" ) {
sleep(3);
ok( !defined $cache->get($key), "miss after 2 seconds ($desc)" );
ok( $cache->exists_and_is_expired($key), "is_expired ($desc)" );
ok( !$cache->is_valid($key), "invalid ($desc)" );
}
};
$test_expires_shortly->(3);
$test_expires_shortly->("3 seconds");
$test_expires_shortly->( { expires_at => time + 3 } );
}
sub test_expires_later : Tests {
my $self = shift;
return 'author testing only - timing is unreliable'
unless ( $ENV{AUTHOR_TESTING} );
# expires_in default should be ignored
my $cache = $self->new_cache( expires_in => '1s' );
# Expires later (test time)
my $test_expires_later = sub {
my ($set_option) = @_;
my ( $key, $value ) = $self->kvpair();
my $desc = "set_option = " . dump_one_line($set_option);
is( $cache->set( $key, $value, $set_option ), $value, "set ($desc)" );
is( $cache->get($key), $value, "hit ($desc)" );
my $start_time = time();
is_between(
$cache->get_expires_at($key),
$start_time + 3580,
$start_time + 3620,
"expires_at ($desc)"
);
ok( !$cache->exists_and_is_expired($key), "not expired ($desc)" );
ok( $cache->is_valid($key), "valid ($desc)" );
local $CHI::Driver::Test_Time = $start_time + 3590;
ok( !$cache->exists_and_is_expired($key), "not expired ($desc)" );
ok( $cache->is_valid($key), "valid ($desc)" );
local $CHI::Driver::Test_Time = $start_time + 3610;
ok( !defined $cache->get($key), "miss after 1 hour ($desc)" );
ok( $cache->exists_and_is_expired($key), "is_expired ($desc)" );
ok( !$cache->is_valid($key), "invalid ($desc)" );
};
$test_expires_later->(3600);
$test_expires_later->("1 hour");
$test_expires_later->( { expires_at => time + 3600 } );
}
sub test_expires_never : Tests {
my $self = shift;
my $cache;
# Expires never (will fail in 2037)
my ( $key, $value ) = $self->kvpair();
my $test_expires_never = sub {
my (@set_options) = @_;
$cache->set( $key, $value, @set_options );
ok(
$cache->get_expires_at($key) >
time + Time::Duration::Parse::parse_duration('1 year'),
"expires never"
);
ok( !$cache->exists_and_is_expired($key), "not expired" );
ok( $cache->is_valid($key), "valid" );
};
# never is default
$cache = $self->new_cache();
$test_expires_never->();
# expires_in default should be ignored when never passed to set (RT #67970)
$cache = $self->new_cache( expires_in => '1s' );
$test_expires_never->('never');
}
sub test_expires_defaults : Tests {
my $self = shift;
my $start_time = time();
local $CHI::Driver::Test_Time = $start_time;
my $cache;
my $set_and_confirm_expires_at = sub {
my ( $expected_expires_at, $desc ) = @_;
my ( $key, $value ) = $self->kvpair();
$cache->set( $key, $value );
is( $cache->get_expires_at($key), $expected_expires_at, $desc );
$cache->clear();
};
$cache = $self->new_cache( expires_in => 10 );
$set_and_confirm_expires_at->(
$start_time + 10,
"after expires_in constructor option"
);
$cache->expires_in(20);
$set_and_confirm_expires_at->( $start_time + 20,
"after expires_in method" );
$cache = $self->new_cache( expires_at => $start_time + 30 );
$set_and_confirm_expires_at->(
$start_time + 30,
"after expires_at constructor option"
);
$cache->expires_at( $start_time + 40 );
$set_and_confirm_expires_at->( $start_time + 40,
"after expires_at method" );
}
sub test_expires_manually : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $key, $value ) = $self->kvpair();
my $desc = "expires manually";
$cache->set( $key, $value );
is( $cache->get($key), $value, "hit ($desc)" );
$cache->expire($key);
ok( !defined $cache->get($key), "miss after expire ($desc)" );
ok( !$cache->is_valid($key), "invalid after expire ($desc)" );
}
sub test_expires_conditionally : Tests {
my $self = shift;
my $cache = $self->{cache};
# Expires conditionally
my $test_expires_conditionally = sub {
my ( $code, $cond_desc, $expect_expire ) = @_;
my ( $key, $value ) = $self->kvpair();
my $desc = "expires conditionally ($cond_desc)";
$cache->set( $key, $value );
is(
$cache->get( $key, expire_if => $code ),
$expect_expire ? undef : $value,
"get result ($desc)"
);
is( $cache->get($key), $value, "hit after expire_if ($desc)" );
};
my $time = time();
$test_expires_conditionally->( sub { 1 }, 'true', 1 );
$test_expires_conditionally->( sub { 0 }, 'false', 0 );
$test_expires_conditionally->(
sub { $_[0]->created_at >= $time },
'created_at >= now', 1
);
$test_expires_conditionally->(
sub { $_[0]->created_at < $time },
'created_at < now', 0
);
}
sub test_expires_variance : Tests {
my $self = shift;
my $cache = $self->{cache};
my $start_time = time();
my $expires_at = $start_time + 10;
my ( $key, $value ) = $self->kvpair();
$cache->set( $key, $value,
{ expires_at => $expires_at, expires_variance => 0.5 } );
is( $cache->get_object($key)->expires_at(),
$expires_at, "expires_at = $start_time" );
is(
$cache->get_object($key)->early_expires_at(),
$start_time + 5,
"early_expires_at = $start_time + 5"
);
my %expire_count;
for ( my $time = $start_time + 3 ; $time <= $expires_at + 1 ; $time++ ) {
local $CHI::Driver::Test_Time = $time;
for ( my $i = 0 ; $i < 100 ; $i++ ) {
if ( !defined $cache->get($key) ) {
$expire_count{$time}++;
}
}
}
for ( my $time = $start_time + 3 ; $time <= $start_time + 5 ; $time++ ) {
ok( !$expire_count{$time}, "got no expires at $time" );
}
for ( my $time = $start_time + 7 ; $time <= $start_time + 8 ; $time++ ) {
ok( $expire_count{$time} > 0 && $expire_count{$time} < 100,
"got some expires at $time" );
}
for ( my $time = $expires_at ; $time <= $expires_at + 1 ; $time++ ) {
ok( $expire_count{$time} == 100, "got all expires at $time" );
}
}
sub test_not_in_cache : Tests {
my $self = shift;
my $cache = $self->{cache};
ok( !defined $cache->get_object('not in cache') );
ok( !defined $cache->get_expires_at('not in cache') );
ok( !$cache->is_valid('not in cache') );
}
sub test_serialize : Tests {
my $self = shift;
my $cache = $self->{cache};
$self->num_tests( $self->{key_count} );
$self->set_some_keys($cache);
foreach my $keyname ( @{ $self->{keynames} } ) {
my $expect_transformed =
( $keyname eq 'arrayref' || $keyname eq 'hashref' ) ? 1
: ( $keyname eq 'utf8' ) ? 2
: 0;
is(
$cache->get_object( $self->{keys}->{$keyname} )->_is_transformed(),
$expect_transformed,
"is_transformed = $expect_transformed ($keyname)"
);
}
}
{
package DummySerializer;
BEGIN {
$DummySerializer::VERSION = '0.50';
}
sub serialize { }
sub deserialize { }
}
sub test_serializers : Tests {
my ($self) = @_;
unless ( can_load('Data::Serializer') ) {
$self->num_tests(1);
return 'Data::Serializer not installed';
}
my @modes = (qw(string hash object));
my @variants = (qw(Storable Data::Dumper YAML));
@variants = grep { can_load($_) } @variants;
ok( scalar(@variants), "some variants ok" );
my $initial_count = 5;
my $test_key_types_count = $self->{key_count};
my $test_count = $initial_count +
scalar(@variants) * scalar(@modes) * ( 1 + $test_key_types_count );
my $cache1 = $self->new_cache();
isa_ok( $cache1->serializer, 'CHI::Serializer::Storable' );
my $cache2 = $self->new_cache();
is( $cache1->serializer, $cache2->serializer,
'same serializer returned from two objects' );
throws_ok(
sub {
$self->new_cache( serializer => [1] );
},
qr/Validation failed for/,
"invalid serializer"
);
lives_ok(
sub { $self->new_cache( serializer => bless( {}, 'DummySerializer' ) ) }
,
"valid dummy serializer"
);
foreach my $mode (@modes) {
foreach my $variant (@variants) {
my $serializer_param = (
$mode eq 'string' ? $variant
: $mode eq 'hash' ? { serializer => $variant }
: Data::Serializer->new( serializer => $variant )
);
my $cache = $self->new_cache( serializer => $serializer_param );
is( $cache->serializer->serializer,
$variant, "serializer = $variant, mode = $mode" );
$self->{cache} = $cache;
foreach my $keyname ( @{ $self->{keynames} } ) {
my $key = $self->{keys}->{$keyname};
my $value = $self->{values}->{$keyname};
$cache->set( $key, $value );
cmp_deeply( $cache->get($key), $value,
"hit for key '$keyname'" );
}
$self->num_tests($test_count);
}
}
}
sub test_namespaces : Tests {
my $self = shift;
my $cache = $self->{cache};
my $cache0 = $self->new_cache();
is( $cache0->namespace, 'Default', 'namespace defaults to "Default"' );
my ( $ns1, $ns2, $ns3 ) = ( 'ns1', 'ns2', 'ns3' );
my ( $cache1, $cache1a, $cache2, $cache3 ) =
map { $self->new_cache( namespace => $_ ) } ( $ns1, $ns1, $ns2, $ns3 );
cmp_deeply(
[ map { $_->namespace } ( $cache1, $cache1a, $cache2, $cache3 ) ],
[ $ns1, $ns1, $ns2, $ns3 ],
'cache->namespace()'
);
$self->set_some_keys($cache1);
cmp_deeply(
$cache1->dump_as_hash(),
$cache1a->dump_as_hash(),
'cache1 and cache1a are same cache'
);
cmp_deeply( [ $cache2->get_keys() ],
[], 'cache2 empty after setting keys in cache1' );
$cache3->set( $self->{keys}->{medium}, 'different' );
is(
$cache1->get('medium'),
$self->{values}->{medium},
'cache1{medium} = medium'
);
is( $cache3->get('medium'), 'different', 'cache1{medium} = different' );
if ( $self->supports_get_namespaces() ) {
# get_namespaces may or may not automatically include empty namespaces
cmp_deeply(
[ $cache1->get_namespaces() ],
supersetof( $ns1, $ns3 ),
"get_namespaces contains $ns1 and $ns3"
);
foreach my $c ( $cache0, $cache1, $cache1a, $cache2, $cache3 ) {
cmp_deeply(
[ $cache->get_namespaces() ],
[ $c->get_namespaces() ],
'get_namespaces the same regardless of which cache asks'
);
}
}
else {
throws_ok(
sub { $cache1->get_namespaces() },
qr/not supported/,
"get_namespaces not supported"
);
SKIP: { skip "get_namespaces not supported", 5 }
}
}
sub test_persist : Tests {
my $self = shift;
my $cache = $self->{cache};
my $hash;
{
my $cache1 = $self->new_cache();
$self->set_some_keys($cache1);
$hash = $cache1->dump_as_hash();
}
my $cache2 = $self->new_cache();
cmp_deeply( $hash, $cache2->dump_as_hash(),
'cache persisted between cache object creations' );
}
sub test_multi : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $keys, $values, $keynames ) =
( $self->{keys}, $self->{values}, $self->{keynames} );
my @ordered_keys = map { $keys->{$_} } @{$keynames};
my @ordered_values =
map { $values->{$_} } @{$keynames};
my %ordered_scalar_key_values =
map { ( $keys->{$_}, $values->{$_} ) }
grep { !ref( $keys->{$_} ) } @{$keynames};
cmp_deeply( $cache->get_multi_arrayref( ['foo'] ),
[undef], "get_multi_arrayref before set" );
$cache->set_multi( \%ordered_scalar_key_values );
$cache->set( $keys->{arrayref}, $values->{arrayref} );
$cache->set( $keys->{hashref}, $values->{hashref} );
cmp_deeply( $cache->get_multi_arrayref( \@ordered_keys ),
\@ordered_values, "get_multi_arrayref" );
cmp_deeply( $cache->get( $ordered_keys[0] ),
$ordered_values[0], "get one after set_multi" );
cmp_deeply(
$cache->get_multi_arrayref( [ reverse @ordered_keys ] ),
[ reverse @ordered_values ],
"get_multi_arrayref"
);
cmp_deeply(
$cache->get_multi_hashref( [ grep { !ref($_) } @ordered_keys ] ),
\%ordered_scalar_key_values, "get_multi_hashref" );
cmp_set(
[ $cache->get_keys ],
[ $self->process_keys( $cache, @ordered_keys ) ],
"get_keys after set_multi"
);
$cache->remove_multi( \@ordered_keys );
cmp_deeply(
$cache->get_multi_arrayref( \@ordered_keys ),
[ (undef) x scalar(@ordered_values) ],
"get_multi_arrayref after remove_multi"
);
cmp_set( [ $cache->get_keys ], [], "get_keys after remove_multi" );
}
sub test_multi_no_keys : Tests {
my $self = shift;
my $cache = $self->{cache};
cmp_deeply( $cache->get_multi_arrayref( [] ),
[], "get_multi_arrayref (no args)" );
cmp_deeply( $cache->get_multi_hashref( [] ),
{}, "get_multi_hashref (no args)" );
lives_ok { $cache->set_multi( {} ) } "set_multi (no args)";
lives_ok { $cache->remove_multi( [] ) } "remove_multi (no args)";
}
sub test_l1_cache : Tests {
my $self = shift;
my @keys = map { "key$_" } ( 0 .. 2 );
my @values = map { "value$_" } ( 0 .. 2 );
my ( $cache, $l1_cache );
return "skipping - no support for clear" unless $self->supports_clear();
my $test_l1_cache = sub {
is( $l1_cache->subcache_type, "l1_cache", "subcache_type = l1_cache" );
# Get on cache should populate l1 cache
#
$cache->set( $keys[0], $values[0] );
$l1_cache->clear();
ok( !$l1_cache->get( $keys[0] ), "l1 miss after clear" );
is( $cache->get( $keys[0] ),
$values[0], "primary hit after primary set" );
is( $l1_cache->get( $keys[0] ), $values[0],
"l1 hit after primary get" );
# Primary cache should be reading l1 cache first
#
$l1_cache->set( $keys[0], $values[1] );
is( $cache->get( $keys[0] ),
$values[1], "got new value set explicitly in l1 cache" );
$l1_cache->remove( $keys[0] );
is( $cache->get( $keys[0] ), $values[0], "got old value again" );
$cache->clear();
ok( !$cache->get( $keys[0] ), "miss after clear" );
ok( !$l1_cache->get( $keys[0] ), "miss after clear" );
# get_multi_* - one from l1 cache, one from primary cache, one miss
#
$cache->set( $keys[0], $values[0] );
$cache->set( $keys[1], $values[0] );
$l1_cache->remove( $keys[0] );
$l1_cache->set( $keys[1], $values[1] );
cmp_deeply(
$cache->get_multi_arrayref( [ $keys[0], $keys[1], $keys[2] ] ),
[ $values[0], $values[1], undef ],
"get_multi_arrayref"
);
cmp_deeply(
$cache->get_multi_hashref( [ $keys[0], $keys[1], $keys[2] ] ),
{
$keys[0] => $values[0],
$keys[1] => $values[1],
$keys[2] => undef
},
"get_multi_hashref"
);
$self->_test_logging_with_l1_cache( $cache, $l1_cache );
$self->_test_common_subcache_features( $cache, $l1_cache, 'l1_cache' );
};
# Test with current cache in primary position...
#
$cache =
$self->new_cache( l1_cache => { driver => 'Memory', datastore => {} } );
$l1_cache = $cache->l1_cache;
isa_ok( $cache, $self->testing_driver_class, 'cache' );
isa_ok( $l1_cache, 'CHI::Driver::Memory', 'l1_cache' );
$test_l1_cache->();
# and in l1 position
#
$cache = $self->testing_chi_root_class->new(
driver => 'Memory',
datastore => {},
l1_cache => { $self->new_cache_options() }
);
$l1_cache = $cache->l1_cache;
isa_ok( $cache, 'CHI::Driver::Memory', 'cache' );
isa_ok( $l1_cache, $self->testing_driver_class, 'l1_cache' );
$test_l1_cache->();
}
sub test_mirror_cache : Tests {
my $self = shift;
my ( $cache, $mirror_cache );
my ( $key, $value, $key2, $value2 ) = $self->kvpair(2);
return "skipping - no support for clear" unless $self->supports_clear();
my $test_mirror_cache = sub {
is( $mirror_cache->subcache_type, "mirror_cache" );
# Get on either cache should not populate the other, and should not be able to see
# mirror keys from regular cache
#
$cache->set( $key, $value );
$mirror_cache->remove($key);
$cache->get($key);
ok( !$mirror_cache->get($key), "key not in mirror_cache" );
$mirror_cache->set( $key2, $value2 );
ok( !$cache->get($key2), "key2 not in cache" );
$self->_test_logging_with_mirror_cache( $cache, $mirror_cache );
$self->_test_common_subcache_features( $cache, $mirror_cache,
'mirror_cache' );
};
my $file_cache_options = sub {
my $root_dir =
tempdir( "chi-test-mirror-cache-XXXX", TMPDIR => 1, CLEANUP => 1 );
return ( driver => 'File', root_dir => $root_dir, depth => 3 );
};
# Test with current cache in primary position...
#
$cache = $self->new_cache( mirror_cache => { $file_cache_options->() } );
$mirror_cache = $cache->mirror_cache;
isa_ok( $cache, $self->testing_driver_class );
isa_ok( $mirror_cache, 'CHI::Driver::File' );
$test_mirror_cache->();
# and in mirror position
#
$cache =
$self->testing_chi_root_class->new( $file_cache_options->(),
mirror_cache => { $self->new_cache_options() } );
$mirror_cache = $cache->mirror_cache;
isa_ok( $cache, 'CHI::Driver::File' );
isa_ok( $mirror_cache, $self->testing_driver_class );
$test_mirror_cache->();
}
sub test_subcache_overridable_params : Tests {
my ($self) = @_;
my $cache;
warning_like {
$cache = $self->new_cache(
l1_cache => {
driver => 'Memory',
on_get_error => 'log',
datastore => {},
expires_variance => 0.5,
serializer => 'Foo'
}
);
}
qr/cannot override these keys/, "non-overridable subcache keys";
is( $cache->l1_cache->expires_variance, $cache->expires_variance );
is( $cache->l1_cache->serializer, $cache->serializer );
is( $cache->l1_cache->on_set_error, $cache->on_set_error );
is( $cache->l1_cache->on_get_error, 'log' );
}
# Run logging tests for a cache with an l1_cache
#
sub _test_logging_with_l1_cache {
my ( $self, $cache ) = @_;
$cache->clear();
my $log = activate_test_logger();
my ( $key, $value ) = $self->kvpair();
my $driver = $cache->label;
my $miss_not_in_cache = 'MISS \(not in cache\)';
my $miss_expired = 'MISS \(expired\)';
my $start_time = time();
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_not_in_cache/);
$log->contains_ok(
qr/cache get for .* key='$key', cache='.*l1.*': $miss_not_in_cache/);
$log->empty_ok();
$cache->set( $key, $value, 81 );
$log->contains_ok(
qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='$driver'/
);
$log->contains_ok(
qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='.*l1.*'/
);
$log->empty_ok();
$cache->get($key);
$log->contains_ok(qr/cache get for .* key='$key', cache='.*l1.*': HIT/);
$log->empty_ok();
local $CHI::Driver::Test_Time = $start_time + 120;
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_expired/);
$log->contains_ok(
qr/cache get for .* key='$key', cache='.*l1.*': $miss_expired/);
$log->empty_ok();
$cache->remove($key);
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_not_in_cache/);
$log->contains_ok(
qr/cache get for .* key='$key', cache='.*l1.*': $miss_not_in_cache/);
$log->empty_ok();
}
sub _test_logging_with_mirror_cache {
my ( $self, $cache ) = @_;
$cache->clear();
my $log = activate_test_logger();
my ( $key, $value ) = $self->kvpair();
my $driver = $cache->label;
my $miss_not_in_cache = 'MISS \(not in cache\)';
my $miss_expired = 'MISS \(expired\)';
my $start_time = time();
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_not_in_cache/);
$log->empty_ok();
$cache->set( $key, $value, 81 );
$log->contains_ok(
qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='$driver'/
);
$log->contains_ok(
qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='.*mirror.*'/
);
$log->empty_ok();
$cache->get($key);
$log->contains_ok(qr/cache get for .* key='$key', cache='$driver': HIT/);
$log->empty_ok();
local $CHI::Driver::Test_Time = $start_time + 120;
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_expired/);
$log->empty_ok();
$cache->remove($key);
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_not_in_cache/);
$log->empty_ok();
}
# Run tests common to l1_cache and mirror_cache
#
sub _test_common_subcache_features {
my ( $self, $cache, $subcache, $subcache_type ) = @_;
my ( $key, $value, $key2, $value2 ) = $self->kvpair(2);
for ( $cache, $subcache ) { $_->clear() }
# Test informational methods
#
ok( !$cache->is_subcache, "is_subcache - false" );
ok( $subcache->is_subcache, "is_subcache - true" );
ok( $cache->has_subcaches, "has_subcaches - true" );
ok( !$subcache->has_subcaches, "has_subcaches - false" );
ok( !$cache->can('parent_cache'), "parent_cache - cannot" );
is( $subcache->parent_cache, $cache, "parent_cache - defined" );
ok( !$cache->can('subcache_type'), "subcache_type - cannot" );
is( $subcache->subcache_type, $subcache_type, "subcache_type - defined" );
cmp_deeply( $cache->subcaches, [$subcache], "subcaches - defined" );
ok( !$subcache->can('subcaches'), "subcaches - cannot" );
is( $cache->$subcache_type, $subcache, "$subcache_type - defined" );
ok( !$subcache->can($subcache_type), "$subcache_type - cannot" );
# Test that sets and various kinds of removals and expirations are distributed to both
# the primary cache and the subcache
#
my ( $test_remove_method, $confirm_caches_empty,
$confirm_caches_populated );
$test_remove_method = sub {
my ( $desc, $remove_code ) = @_;
$desc = "testing $desc";
$confirm_caches_empty->("$desc: before set");
$cache->set( $key, $value );
$cache->set( $key2, $value2 );
$confirm_caches_populated->("$desc: after set");
$remove_code->();
$confirm_caches_empty->("$desc: before set_multi");
$cache->set_multi( { $key => $value, $key2 => $value2 } );
$confirm_caches_populated->("$desc: after set_multi");
$remove_code->();
$confirm_caches_empty->("$desc: before return");
};
$confirm_caches_empty = sub {
my ($desc) = @_;
ok( !defined( $cache->get($key) ),
"primary cache is not populated with '$key' - $desc" );
ok( !defined( $subcache->get($key) ),
"subcache is not populated with '$key' - $desc" );
ok( !defined( $cache->get($key2) ),
"primary cache is not populated #2 with '$key2' - $desc" );
ok( !defined( $subcache->get($key2) ),
"subcache is not populated #2 with '$key2' - $desc" );
};
$confirm_caches_populated = sub {
my ($desc) = @_;
is( $cache->get($key), $value,
"primary cache is populated with '$key' - $desc" );
is( $subcache->get($key), $value,
"subcache is populated with '$key' - $desc" );
is( $cache->get($key2), $value2,
"primary cache is populated with '$key2' - $desc" );
is( $subcache->get($key2), $value2,
"subcache is populated with '$key2' - $desc" );
};
$test_remove_method->(
'remove', sub { $cache->remove($key); $cache->remove($key2) }
);
$test_remove_method->(
'expire', sub { $cache->expire($key); $cache->expire($key2) }
);
$test_remove_method->( 'clear', sub { $cache->clear() } );
}
sub _verify_cache_is_cleared {
my ( $self, $cache, $desc ) = @_;
cmp_deeply( [ $cache->get_keys ], [], "get_keys ($desc)" );
is( scalar( $cache->get_keys ), 0, "scalar(get_keys) = 0 ($desc)" );
while ( my ( $keyname, $key ) = each( %{ $self->{keys} } ) ) {
ok( !defined $cache->get($key),
"key '$keyname' no longer defined ($desc)" );
}
}
sub process_keys {
my ( $self, $cache, @keys ) = @_;
$self->process_key( $cache, 'foo' );
return map { $self->process_key( $cache, $_ ) } @keys;
}
sub process_key {
my ( $self, $cache, $key ) = @_;
return $cache->unescape_key(
$cache->escape_key( $cache->transform_key($key) ) );
}
sub test_clear : Tests {
my $self = shift;
my $cache = $self->new_cache( namespace => 'name' );
my $cache2 = $self->new_cache( namespace => 'other' );
my $cache3 = $self->new_cache( namespace => 'name' );
$self->num_tests( $self->{key_count} * 2 + 5 );
if ( $self->supports_clear() ) {
$self->set_some_keys($cache);
$self->set_some_keys($cache2);
$cache->clear();
$self->_verify_cache_is_cleared( $cache, 'cache after clear' );
$self->_verify_cache_is_cleared( $cache3, 'cache3 after clear' );
cmp_set(
[ $cache2->get_keys ],
[ $self->process_keys( $cache2, values( %{ $self->{keys} } ) ) ],
'cache2 untouched by clear'
);
}
else {
throws_ok(
sub { $cache->clear() },
qr/not supported/,
"clear not supported"
);
SKIP: { skip "clear not supported", 9 }
}
}
sub test_logging : Tests {
my $self = shift;
my $cache = $self->{cache};
my $log = activate_test_logger();
my ( $key, $value ) = $self->kvpair();
my $driver = $cache->label;
my $miss_not_in_cache = 'MISS \(not in cache\)';
my $miss_expired = 'MISS \(expired\)';
my $start_time = time();
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_not_in_cache/);
$log->empty_ok();
$cache->set( $key, $value );
$log->contains_ok(
qr/cache set for .* key='$key', size=\d+, expires='never', cache='$driver'/
);
$log->empty_ok();
$cache->set( $key, $value, 81 );
$log->contains_ok(
qr/cache set for .* key='$key', size=\d+, expires='1m2[012]s', cache='$driver'/
);
$log->empty_ok();
$cache->get($key);
$log->contains_ok(qr/cache get for .* key='$key', cache='$driver': HIT/);
$log->empty_ok();
local $CHI::Driver::Test_Time = $start_time + 120;
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_expired/);
$log->empty_ok();
$cache->remove($key);
$cache->get($key);
$log->contains_ok(
qr/cache get for .* key='$key', cache='$driver': $miss_not_in_cache/);
$log->empty_ok();
}
sub test_stats : Tests {
my $self = shift;
my $stats = $self->testing_chi_root_class->stats;
$stats->enable();
my ( $key, $value ) = $self->kvpair();
my $start_time = time();
my $cache;
$cache = $self->new_cache( namespace => 'Foo' );
$cache->get($key);
$cache->set( $key, $value, 80 );
$cache->get($key);
local $CHI::Driver::Test_Time = $start_time + 120;
$cache->get($key);
$cache->remove($key);
$cache->get($key);
$cache = $self->new_cache( namespace => 'Bar' );
$cache->set( $key, scalar( $value x 3 ) );
$cache->set( $key, $value );
my $log = activate_test_logger();
my $label = $cache->label;
$log->empty_ok();
$stats->flush();
$log->contains_ok(
qr/CHI stats: namespace='Foo'; cache='$label'; start=.*; end=.*; absent_misses=2; expired_misses=1; hits=1; set_key_size=6; set_value_size=20; sets=1/
);
$log->contains_ok(
qr/CHI stats: namespace='Bar'; cache='$label'; start=.*; end=.*; set_key_size=12; set_value_size=52; sets=2/
);
$log->empty_ok();
my @logs = (
"CHI stats: namespace='Foo'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; hits=3; sets=5",
"CHI stats: namespace='Foo'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; hits=1; sets=7",
"CHI stats: namespace='Bar'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; hits=4; sets=9",
"CHI stats: namespace='Foo'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; sets=3",
"CHI stats: namespace='Foo'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; hits=8",
"CHI stats: namespace='Foo'; cache='Memory'; start=20090102:12:53:05; end=20090102:12:58:05; sets=2",
"CHI stats: namespace='Bar'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; hits=10; sets=1",
"CHI stats: namespace='Bar'; cache='File'; start=20090102:12:53:05; end=20090102:12:58:05; hits=3; set_errors=2",
);
my $log_dir = tempdir( "chi-test-stats-XXXX", TMPDIR => 1, CLEANUP => 1 );
write_file( "$log_dir/log1", join( "\n", splice( @logs, 0, 4 ) ) . "\n" );
write_file( "$log_dir/log2", join( "\n", @logs ) );
open( my $fh2, "<", "$log_dir/log2" ) or die "cannot open $log_dir/log2";
my $results = $stats->parse_stats_logs( "$log_dir/log1", $fh2 );
close($fh2);
cmp_deeply(
$results,
[
{
root_class => 'CHI',
namespace => 'Foo',
cache => 'File',
hits => 12,
sets => 15
},
{
root_class => 'CHI',
namespace => 'Bar',
cache => 'File',
hits => 17,
sets => 10,
set_errors => 2
},
{
root_class => 'CHI',
namespace => 'Foo',
cache => 'Memory',
sets => 2
}
],
'parse_stats_logs'
);
}
sub test_cache_object : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $key, $value ) = $self->kvpair();
my $start_time = time();
$cache->set( $key, $value, { expires_at => $start_time + 10 } );
is_between( $cache->get_object($key)->created_at,
$start_time, $start_time + 2 );
is_between( $cache->get_object($key)->get_created_at,
$start_time, $start_time + 2 );
is( $cache->get_object($key)->expires_at, $start_time + 10 );
is( $cache->get_object($key)->get_expires_at, $start_time + 10 );
local $CHI::Driver::Test_Time = $start_time + 50;
$cache->set( $key, $value );
is_between(
$cache->get_object($key)->created_at,
$start_time + 50,
$start_time + 52
);
is_between(
$cache->get_object($key)->get_created_at,
$start_time + 50,
$start_time + 52
);
}
sub test_size_awareness : Tests {
my $self = shift;
my ( $key, $value ) = $self->kvpair();
ok( !$self->new_cleared_cache()->is_size_aware(),
"not size aware by default" );
ok( $self->new_cleared_cache( is_size_aware => 1 )->is_size_aware(),
"is_size_aware turns on size awareness" );
ok( $self->new_cleared_cache( max_size => 10 )->is_size_aware(),
"max_size turns on size awareness" );
my $cache = $self->new_cleared_cache( is_size_aware => 1 );
is( $cache->get_size(), 0, "size is 0 for empty" );
$cache->set( $key, $value );
is_about( $cache->get_size, 20, "size is about 20 with one value" );
$cache->set( $key, scalar( $value x 5 ) );
is_about( $cache->get_size, 45, "size is 45 after overwrite" );
$cache->set( $key, scalar( $value x 5 ) );
is_about( $cache->get_size, 45, "size is still 45 after same overwrite" );
$cache->set( $key, scalar( $value x 2 ) );
is_about( $cache->get_size, 26, "size is 26 after overwrite" );
$cache->remove($key);
is( $cache->get_size, 0, "size is 0 again after removing key" );
$cache->set( $key, $value );
is_about( $cache->get_size, 20, "size is about 20 with one value" );
$cache->clear();
is( $cache->get_size, 0, "size is 0 again after clear" );
my $time = time() + 10;
$cache->set( $key, $value, { expires_at => $time } );
is( $cache->get_expires_at($key),
$time, "set options respected by size aware cache" );
}
sub test_max_size : Tests {
my $self = shift;
is( $self->new_cache( max_size => '30k' )->max_size,
30 * 1024, 'max_size parsing' );
my $cache = $self->new_cleared_cache( max_size => 99 );
ok( $cache->is_size_aware, "is size aware when max_size specified" );
my $value_20 = 'x' x 6;
for ( my $i = 0 ; $i < 5 ; $i++ ) {
$cache->set( "key$i", $value_20 );
}
for ( my $i = 0 ; $i < 10 ; $i++ ) {
$cache->set( "key" . int( rand(10) ), $value_20 );
is_between( $cache->get_size, 60, 99,
"after iteration $i, size = " . $cache->get_size );
is_between( scalar( $cache->get_keys ),
3, 5, "after iteration $i, keys = " . scalar( $cache->get_keys ) );
}
}
sub test_max_size_with_l1_cache : Tests {
my $self = shift;
my $cache = $self->new_cleared_cache(
l1_cache => { driver => 'Memory', datastore => {}, max_size => 99 } );
my $l1_cache = $cache->l1_cache;
ok( $l1_cache->is_size_aware, "is size aware when max_size specified" );
my $value_20 = 'x' x 6;
my @keys = map { "key$_" } ( 0 .. 9 );
my @shuffle_keys = shuffle(@keys);
for ( my $i = 0 ; $i < 5 ; $i++ ) {
$cache->set( "key$i", $value_20 );
}
for ( my $i = 0 ; $i < 10 ; $i++ ) {
my $key = $shuffle_keys[$i];
$cache->set( $key, $value_20 );
is_between( $l1_cache->get_size, 60, 99,
"after iteration $i, size = " . $l1_cache->get_size );
is_between( scalar( $l1_cache->get_keys ),
3, 5,
"after iteration $i, keys = " . scalar( $l1_cache->get_keys ) );
}
cmp_deeply( [ sort $cache->get_keys ],
\@keys, "primary cache still has all keys" );
# Now test population by writeback
$l1_cache->clear();
is( $l1_cache->get_size, 0, "l1 size is 0 after clear" );
for ( my $i = 0 ; $i < 5 ; $i++ ) {
$cache->get("key$i");
}
for ( my $i = 0 ; $i < 10 ; $i++ ) {
my $key = $shuffle_keys[$i];
$cache->get($key);
is_between( $l1_cache->get_size, 60, 99,
"after iteration $i, size = " . $l1_cache->get_size );
is_between( scalar( $l1_cache->get_keys ),
3, 5,
"after iteration $i, keys = " . scalar( $l1_cache->get_keys ) );
}
}
sub test_custom_discard_policy : Tests {
my $self = shift;
my $value_20 = 'x' x 6;
my $highest_first = sub {
my $c = shift;
my @sorted_keys = sort( $c->get_keys );
return sub { pop(@sorted_keys) };
};
my $cache = $self->new_cleared_cache(
is_size_aware => 1,
discard_policy => $highest_first
);
for ( my $j = 0 ; $j < 10 ; $j += 2 ) {
$cache->clear();
for ( my $i = 0 ; $i < 10 ; $i++ ) {
my $k = ( $i + $j ) % 10;
$cache->set( "key$k", $value_20 );
}
$cache->discard_to_size(100);
cmp_set(
[ $cache->get_keys ],
[ map { "key$_" } ( 0 .. 4 ) ],
"5 lowest"
);
$cache->discard_to_size(20);
cmp_set( [ $cache->get_keys ], ["key0"], "1 lowest" );
}
}
sub test_discard_timeout : Tests {
my $self = shift;
return 'author testing only' unless ( $ENV{AUTHOR_TESTING} );
my $bad_policy = sub {
return sub { '1' };
};
my $cache = $self->new_cleared_cache(
is_size_aware => 1,
discard_policy => $bad_policy
);
ok( defined( $cache->discard_timeout ) && $cache->discard_timeout > 1,
"positive discard timeout" );
$cache->discard_timeout(1);
is( $cache->discard_timeout, 1, "can set timeout" );
my $start_time = time;
$cache->set( 2, 2 );
throws_ok { $cache->discard_to_size(0) } qr/discard timeout .* reached/;
ok(
time >= $start_time && time <= $start_time + 4,
sprintf(
"time (%d) is between %d and %d",
time, $start_time, $start_time + 4
)
);
}
sub test_size_awareness_with_subcaches : Tests {
my $self = shift;
my ( $cache, $l1_cache );
my $set_values = sub {
my $value_20 = 'x' x 6;
for ( my $i = 0 ; $i < 20 ; $i++ ) {
$cache->set( "key$i", $value_20 );
}
$l1_cache = $cache->l1_cache;
};
my $is_size_aware = sub {
my $c = shift;
my $label = $c->label;
ok( $c->is_size_aware, "$label is size aware" );
my $max_size = $c->max_size;
ok( $max_size > 0, "$label has max size" );
is_between( $c->get_size, $max_size - 40,
$max_size, "$label size = " . $c->get_size );
is_between(
scalar( $c->get_keys ),
( $max_size + 1 ) / 20 - 2,
( $max_size + 1 ) / 20,
"$label keys = " . scalar( $c->get_keys )
);
};
my $is_not_size_aware = sub {
my $c = shift;
my $label = $c->label;
ok( !$c->is_size_aware, "$label is not size aware" );
is( $c->get_keys, 20, "$label keys = 20" );
};
$cache = $self->new_cleared_cache(
l1_cache => { driver => 'Memory', datastore => {}, max_size => 99 } );
$set_values->();
$is_not_size_aware->($cache);
$is_size_aware->($l1_cache);
$cache = $self->new_cleared_cache(
l1_cache => { driver => 'Memory', datastore => {}, max_size => 99 },
max_size => 199
);
$set_values->();
$is_size_aware->($cache);
$is_size_aware->($l1_cache);
$cache = $self->new_cleared_cache(
l1_cache => { driver => 'Memory', datastore => {} },
max_size => 199
);
$set_values->();
$is_size_aware->($cache);
# Cannot call is_not_size_aware because the get_keys check will
# fail. Keys will be removed from the l1_cache when they are removed
# from the main cache, even though l1_cache does not have a max
# size. Not sure if this is the correct behavior, but for now, we're not
# going to test it. Normally, l1 caches will be more size limited than
# their parent caches.
#
ok( !$l1_cache->is_size_aware, $l1_cache->label . " is not size aware" );
}
sub is_about {
my ( $value, $expected, $msg ) = @_;
my $margin = int( $expected * 0.1 );
if ( abs( $value - $expected ) <= $margin ) {
pass($msg);
}
else {
fail("$msg - got $value, expected $expected");
}
}
sub test_busy_lock : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $key, $value ) = $self->kvpair();
my @bl = ( busy_lock => '30 sec' );
my $start_time = time();
local $CHI::Driver::Test_Time = $start_time;
$cache->set( $key, $value, 100 );
local $CHI::Driver::Test_Time = $start_time + 90;
is( $cache->get( $key, @bl ), $value, "hit before expiration" );
is(
$cache->get_expires_at($key),
$start_time + 100,
"expires_at before expiration"
);
local $CHI::Driver::Test_Time = $start_time + 110;
ok( !defined( $cache->get( $key, @bl ) ), "miss after expiration" );
is(
$cache->get_expires_at($key),
$start_time + 140,
"expires_at after busy lock"
);
is( $cache->get( $key, @bl ), $value, "hit after busy lock" );
}
sub test_obj_ref : Tests {
my $self = shift;
# Make sure obj_ref works in conjunction with subcaches too
my $cache =
$self->new_cache( l1_cache => { driver => 'Memory', datastore => {} } );
my $obj;
my ( $key, $value ) = ( 'medium', [ a => 5, b => 6 ] );
my $validate_obj = sub {
isa_ok( $obj, 'CHI::CacheObject' );
is( $obj->key, $key, "keys match" );
cmp_deeply( $obj->value, $value, "values match" );
};
$cache->get( $key, obj_ref => \$obj );
ok( !defined($obj), "obj not defined on miss" );
$cache->set( $key, $value, { obj_ref => \$obj } );
$validate_obj->();
undef $obj;
ok( !defined($obj), "obj not defined before get" );
$cache->get( $key, obj_ref => \$obj );
$validate_obj->();
}
sub test_metacache : Tests {
my $self = shift;
my $cache = $self->{cache};
ok( !defined( $cache->{metacache} ), "metacache is lazy" );
$cache->metacache->set( 'foo', 5 );
ok( defined( $cache->{metacache} ), "metacache autovivified" );
is( $cache->metacache->get('foo'), 5 );
}
sub test_scalar_return_values : Tests {
my $self = shift;
my $cache = $self->{cache};
my $check = sub {
my ($code) = @_;
my $scalar_result = $code->();
my @list = $code->();
cmp_deeply( \@list, [$scalar_result] );
};
$check->( sub { $cache->fetch('a') } );
$check->( sub { $cache->get('a') } );
$check->( sub { $cache->set( 'a', 5 ) } );
$check->( sub { $cache->fetch('a') } );
$check->( sub { $cache->get('a') } );
}
sub test_no_leak : Tests {
my ($self) = @_;
my $weakref;
{
my $cache = $self->new_cache();
$weakref = $cache;
weaken($weakref);
ok( defined($weakref) && $weakref->isa('CHI::Driver'),
"weakref is defined" );
}
ok( !defined($weakref), "weakref is no longer defined - cache was freed" );
}
Class::MOP::Class->create( 'My::CHI' => ( superclasses => ['CHI'] ) );
sub test_driver_properties : Tests {
my $self = shift;
my $cache = $self->{cache};
is( $cache->chi_root_class, 'CHI', 'chi_root_class=CHI' );
my $cache2 = My::CHI->new( $self->new_cache_options() );
is( $cache2->chi_root_class, 'My::CHI', 'chi_root_class=My::CHI' );
}
sub test_missing_params : Tests {
my $self = shift;
my $cache = $self->{cache};
# These methods require a key
foreach my $method (
qw(get get_object get_expires_at exists_and_is_expired is_valid set expire compute get_multi_arrayref get_multi_hashref set_multi remove_multi)
)
{
throws_ok(
sub { $cache->$method() },
qr/must specify key/,
"$method throws error when no key passed"
);
}
}
sub test_compute : Tests {
my $self = shift;
my $cache = $self->{cache};
# Test current arg order and pre-0.40 arg order
foreach my $iter ( 0 .. 1 ) {
my $count = 5;
my $expire_time = time + 10;
my @args1 = ( { expires_at => $expire_time }, sub { $count++ } );
my @args2 = (
{
expire_if => sub { 1 }
},
sub { $count++ }
);
if ($iter) {
@args1 = reverse(@args1);
@args2 = reverse(@args2);
}
$cache->clear;
is( $cache->get('foo'), undef, "miss" );
is( $cache->compute( 'foo', @args1 ), 5, "compute - 5" );
is( $cache->get('foo'), 5, "hit - 5" );
is( $cache->get_object('foo')->expires_at, $expire_time,
"expire time" );
is( $cache->compute( 'foo', @args2 ), 6, "compute - 6" );
is( $cache->get('foo'), 6, "hit - 6" );
}
}
sub test_compress_threshold : Tests {
my $self = shift;
my $cache = $self->{cache};
my $s0 = 'x' x 180;
my $s1 = 'x' x 200;
$cache->set( 'key0', $s0 );
$cache->set( 'key1', $s1 );
is_between( $cache->get_object('key0')->size, 180, 220 );
is_between( $cache->get_object('key1')->size, 200, 240 );
my $cache2 = $self->new_cache( compress_threshold => 190 );
$cache2->set( 'key0', $s0 );
$cache2->set( 'key1', $s1 );
is_between( $cache2->get_object('key0')->size, 180, 220 );
ok( $cache2->get_object('key1')->size < 100 );
is( $cache2->get('key0'), $s0 );
is( $cache2->get('key1'), $s1 );
}
sub test_expires_on_backend : Tests {
my $self = shift;
return "skipping - no support for expires_on_backend"
unless $self->supports_expires_on_backend();
foreach my $expires_on_backend ( 0, 1 ) {
my $cache =
$self->new_cache( expires_on_backend => $expires_on_backend );
$cache->set( 'key0', 5, '2s' );
$cache->set( 'key1', 6, { expires_at => time + 2 } );
is( $cache->get('key0'), 5, 'hit key0 before expire' );
is( $cache->get('key1'), 6, 'hit key1 before expire' );
sleep(3);
ok( !defined( $cache->get('key0') ), 'miss key0 after expire' );
ok( !defined( $cache->get('key1') ), 'miss key1 after expire' );
if ($expires_on_backend) {
ok(
!defined( $cache->get_object('key0') ),
'cannot get_object(key0) after expire'
);
ok(
!defined( $cache->get_object('key1') ),
'cannot get_object(key1) after expire'
);
}
else {
ok(
$cache->get_object('key0')->is_expired(),
'can get_object(key0) after expire'
);
ok(
$cache->get_object('key1')->is_expired(),
'can get_object(key1) after expire'
);
}
}
}
sub test_append : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $key, $value ) =
( $self->{keys}->{arrayref}, $self->{values}->{medium} );
# Appending to non-existent key has no effect
#
$cache->append( $key, $value );
ok( !$cache->get($key) );
ok( $cache->set( $key, $value ) );
$cache->append( $key, $value );
is( $cache->get($key), $value . $value );
$cache->append( $key, $value );
is( $cache->get($key), $value . $value . $value );
}
sub test_add : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $key, $value ) =
( $self->{keys}->{arrayref}, $self->{values}->{medium} );
my $t = time();
$cache->add( $key, $value, { expires_at => $t + 100 } );
is( $cache->get($key), $value, "get" );
is( $cache->get_object($key)->expires_at, $t + 100, "expires_at" );
$cache->add( $key, $value . $value, { expires_at => $t + 200 } );
is( $cache->get($key), $value, "get (after add)" );
is( $cache->get_object($key)->expires_at,
$t + 100, "expires_at (after add)" );
$cache->remove($key);
$cache->add( $key, $value . $value, { expires_at => $t + 200 } );
is( $cache->get($key), $value . $value, "get (after expire and add)" );
is( $cache->get_object($key)->expires_at,
$t + 200, "expires_at (after expire and add)" );
}
sub test_replace : Tests {
my $self = shift;
my $cache = $self->{cache};
my ( $key, $value ) =
( $self->{keys}->{arrayref}, $self->{values}->{medium} );
my $t = time();
$cache->replace( $key, $value, { expires_at => $t + 100 } );
ok( !$cache->get_object($key), "get" );
$cache->set( $key, $value . $value, { expires_at => $t + 200 } );
$cache->replace( $key, $value, { expires_at => $t + 100 } );
is( $cache->get($key), $value, "get (after replace)" );
is( $cache->get_object($key)->expires_at,
$t + 100, "expires_at (after replace)" );
}
sub test_max_key_length : Tests {
my $self = shift;
# Test max_key_length and also that key does not get transformed twice in mirror_cache
#
my $mirror_store = {};
my $cache = $self->new_cleared_cache(
max_key_length => 10,
mirror_cache => { driver => 'Memory', datastore => $mirror_store }
);
foreach my $keyname ( 'medium', 'large' ) {
my ( $key, $value ) =
( $self->{keys}->{$keyname}, $self->{values}->{$keyname} );
$cache->set( $key, $value );
is( $cache->get($key), $value, $keyname );
is( $cache->mirror_cache->get($key), $value, $keyname );
}
cmp_set( [ $cache->get_keys() ],
[ 'medium', '66b08343f81782986329795e0a422a05' ], 'get_keys' );
}
1;
|