| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | ###################################################################### | 
| 2 |  |  |  |  |  |  | # $Id: CacheTester.pm,v 1.20 2002/04/07 17:04:46 dclinton Exp $ | 
| 3 |  |  |  |  |  |  | # Copyright (C) 2001-2003 DeWitt Clinton  All Rights Reserved | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # Software distributed under the License is distributed on an "AS | 
| 6 |  |  |  |  |  |  | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or | 
| 7 |  |  |  |  |  |  | # implied. See the License for the specific language governing | 
| 8 |  |  |  |  |  |  | # rights and limitations under the License. | 
| 9 |  |  |  |  |  |  | ###################################################################### | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | package Cache::CacheTester; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 4 |  |  | 4 |  | 2055 | use strict; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 129 |  | 
| 14 | 4 |  |  | 4 |  | 1448 | use Cache::BaseCacheTester; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 108 |  | 
| 15 | 4 |  |  | 4 |  | 20 | use Cache::Cache; | 
|  | 4 |  |  |  |  | 4 |  | 
|  | 4 |  |  |  |  | 211 |  | 
| 16 | 4 |  |  | 4 |  | 2037 | use Error qw( :try ); | 
|  | 4 |  |  |  |  | 20106 |  | 
|  | 4 |  |  |  |  | 27 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 4 |  |  | 4 |  | 722 | use vars qw( @ISA $EXPIRES_DELAY ); | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 5520 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | @ISA = qw ( Cache::BaseCacheTester ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | $EXPIRES_DELAY = 2; | 
| 23 |  |  |  |  |  |  | $Error::Debug = 1; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub test | 
| 26 |  |  |  |  |  |  | { | 
| 27 | 4 |  |  | 4 | 1 | 22 | my ( $self, $cache ) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | try | 
| 30 |  |  |  |  |  |  | { | 
| 31 | 4 |  |  | 4 |  | 127 | $cache->Clear( ); | 
| 32 | 4 |  |  |  |  | 44 | $self->_test_one( $cache ); | 
| 33 | 4 |  |  |  |  | 17 | $self->_test_two( $cache ); | 
| 34 | 4 |  |  |  |  | 20 | $self->_test_three( $cache ); | 
| 35 | 4 |  |  |  |  | 21 | $self->_test_four( $cache ); | 
| 36 | 4 |  |  |  |  | 24 | $self->_test_five( $cache ); | 
| 37 | 4 |  |  |  |  | 18 | $self->_test_six( $cache ); | 
| 38 | 4 |  |  |  |  | 20 | $self->_test_seven( $cache ); | 
| 39 | 4 |  |  |  |  | 21 | $self->_test_eight( $cache ); | 
| 40 | 4 |  |  |  |  | 23 | $self->_test_nine( $cache ); | 
| 41 | 4 |  |  |  |  | 19 | $self->_test_ten( $cache ); | 
| 42 | 4 |  |  |  |  | 18 | $self->_test_eleven( $cache ); | 
| 43 | 4 |  |  |  |  | 19 | $self->_test_twelve( $cache ); | 
| 44 | 4 |  |  |  |  | 19 | $self->_test_thirteen( $cache ); | 
| 45 | 4 |  |  |  |  | 23 | $self->_test_fourteen( $cache ); | 
| 46 | 4 |  |  |  |  | 25 | $self->_test_fifteen( $cache ); | 
| 47 | 4 |  |  |  |  | 32 | $self->_test_sixteen( $cache ); | 
| 48 | 4 |  |  |  |  | 25 | $self->_test_seventeen( $cache ); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  | catch Error with | 
| 51 |  |  |  |  |  |  | { | 
| 52 | 0 |  |  | 0 |  | 0 | my $error = shift; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  |  |  | 0 | print STDERR "\nError:\n"; | 
| 55 | 0 |  |  |  |  | 0 | print STDERR $error->stringify( ) . "\n"; | 
| 56 | 0 |  |  |  |  | 0 | print STDERR $error->stacktrace( ) . "\n"; | 
| 57 | 0 |  |  |  |  | 0 | print STDERR "\n"; | 
| 58 |  |  |  |  |  |  | } | 
| 59 | 4 |  |  |  |  | 58 | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Test the getting, setting, and removal of a scalar | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub _test_one | 
| 65 |  |  |  |  |  |  | { | 
| 66 | 4 |  |  | 4 |  | 7 | my ( $self, $cache ) = @_; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 4 | 50 |  |  |  | 13 | $cache or | 
| 69 |  |  |  |  |  |  | croak( "cache required" ); | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 4 |  |  |  |  | 7 | my $key = 'Test Key'; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 4 |  |  |  |  | 5 | my $value = 'Test Value'; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 4 |  |  |  |  | 26 | $cache->set( $key, $value ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 4 |  |  |  |  | 39 | my $fetched_value = $cache->get( $key ); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 4 | 50 |  |  |  | 42 | ( $fetched_value eq $value ) ? | 
| 80 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$fetched_value eq $value' ); | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 4 |  |  |  |  | 72 | $cache->remove( $key ); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 4 |  |  |  |  | 24 | my $fetched_removed_value = $cache->get( $key ); | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 4 | 50 |  |  |  | 24 | ( not defined $fetched_removed_value ) ? | 
| 87 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $fetched_removed_value' ); | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # Test the getting, setting, and removal of a list | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub _test_two | 
| 94 |  |  |  |  |  |  | { | 
| 95 | 4 |  |  | 4 |  | 8 | my ( $self, $cache ) = @_; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 4 | 50 |  |  |  | 17 | $cache or | 
| 98 |  |  |  |  |  |  | croak( "cache required" ); | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 4 |  |  |  |  | 7 | my $key = 'Test Key'; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 4 |  |  |  |  | 14 | my @value_list = ( 'One', 'Two', 'Three' ); | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 4 |  |  |  |  | 42 | $cache->set( $key, \@value_list ); | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 4 |  |  |  |  | 20 | my $fetched_value_list_ref = $cache->get( $key ); | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 4 | 50 | 33 |  |  | 51 | if ( ( $fetched_value_list_ref->[0] eq 'One' ) and | 
|  |  |  | 33 |  |  |  |  | 
| 109 |  |  |  |  |  |  | ( $fetched_value_list_ref->[1] eq 'Two' ) and | 
| 110 |  |  |  |  |  |  | ( $fetched_value_list_ref->[2] eq 'Three' ) ) | 
| 111 |  |  |  |  |  |  | { | 
| 112 | 4 |  |  |  |  | 20 | $self->ok( ); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | else | 
| 115 |  |  |  |  |  |  | { | 
| 116 | 0 |  |  |  |  | 0 | $self->not_ok( 'fetched list does not match set list' ); | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 4 |  |  |  |  | 23 | $cache->remove( $key ); | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 4 |  |  |  |  | 22 | my $fetched_removed_value = $cache->get( $key ); | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 4 | 50 |  |  |  | 28 | ( not defined $fetched_removed_value ) ? | 
| 124 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $fetched_removed_value' ); | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # Test the getting, setting, and removal of a blessed object | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | sub _test_three | 
| 131 |  |  |  |  |  |  | { | 
| 132 | 4 |  |  | 4 |  | 7 | my ( $self, $cache ) = @_; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 4 | 50 |  |  |  | 17 | $cache or | 
| 135 |  |  |  |  |  |  | croak( "cache required" ); | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 4 |  |  |  |  | 9 | my $key = 'Test Key'; | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 4 |  |  |  |  | 8 | my $value = 'Test Value'; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 4 |  |  |  |  | 16 | $cache->set( $key, $value ); | 
| 142 |  |  |  |  |  |  |  | 
| 143 | 4 |  |  |  |  | 13 | my $cache_key = 'Cache Key'; | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 4 |  |  |  |  | 17 | $cache->set( $cache_key, $cache ); | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 4 |  |  |  |  | 27 | my $fetched_cache = $cache->get( $cache_key ); | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 4 | 50 |  |  |  | 34 | ( defined $fetched_cache ) ? | 
| 150 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'defined $fetched_cache' ); | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 4 |  |  |  |  | 22 | my $fetched_value = $fetched_cache->get( $key ); | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 4 | 50 |  |  |  | 34 | ( $fetched_value eq $value ) ? | 
| 155 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$fetched_value eq $value' ); | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | # Test the expiration of an object | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | sub _test_four | 
| 162 |  |  |  |  |  |  | { | 
| 163 | 4 |  |  | 4 |  | 6 | my ( $self, $cache ) = @_; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 4 |  |  |  |  | 11 | my $expires_in = $EXPIRES_DELAY; | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 4 |  |  |  |  | 9 | my $key = 'Test Key'; | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 4 |  |  |  |  | 9 | my $value = 'Test Value'; | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 4 |  |  |  |  | 11 | my $start = time; | 
| 172 | 4 |  |  |  |  | 20 | $cache->set( $key, $value, $expires_in ); | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 4 |  |  |  |  | 22 | my $fetched_value = $cache->get( $key ); | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 4 | 50 |  |  |  | 21 | if (time - $start < $expires_in) { | 
| 177 | 4 | 50 |  |  |  | 26 | ( $fetched_value eq $value ) ? | 
| 178 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$fetched_value eq $value' ); | 
| 179 |  |  |  |  |  |  | } else { | 
| 180 | 0 |  |  |  |  | 0 | $self->skip( '$fetched_value eq $value (not finished in ' . | 
| 181 |  |  |  |  |  |  | $expires_in . ' s)' ); | 
| 182 |  |  |  |  |  |  | } | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 4 |  |  |  |  | 12000676 | sleep( $EXPIRES_DELAY + 1 ); | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 4 |  |  |  |  | 94 | my $fetched_expired_value = $cache->get( $key ); | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 4 | 50 |  |  |  | 43 | ( not defined $fetched_expired_value ) ? | 
| 189 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $fetched_expired_value' ); | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | # Test that caches make deep copies of values | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | sub _test_five | 
| 197 |  |  |  |  |  |  | { | 
| 198 | 4 |  |  | 4 |  | 10 | my ( $self, $cache ) = @_; | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 4 | 50 |  |  |  | 17 | $cache or | 
| 201 |  |  |  |  |  |  | croak( "cache required" ); | 
| 202 |  |  |  |  |  |  |  | 
| 203 | 4 |  |  |  |  | 10 | my $key = 'Test Key'; | 
| 204 |  |  |  |  |  |  |  | 
| 205 | 4 |  |  |  |  | 16 | my @value_list = ( 'One', 'Two', 'Three' ); | 
| 206 |  |  |  |  |  |  |  | 
| 207 | 4 |  |  |  |  | 26 | $cache->set( $key, \@value_list ); | 
| 208 |  |  |  |  |  |  |  | 
| 209 | 4 |  |  |  |  | 18 | @value_list = ( ); | 
| 210 |  |  |  |  |  |  |  | 
| 211 | 4 |  |  |  |  | 18 | my $fetched_value_list_ref = $cache->get( $key ); | 
| 212 |  |  |  |  |  |  |  | 
| 213 | 4 | 50 | 33 |  |  | 54 | if ( ( $fetched_value_list_ref->[0] eq 'One' ) and | 
|  |  |  | 33 |  |  |  |  | 
| 214 |  |  |  |  |  |  | ( $fetched_value_list_ref->[1] eq 'Two' ) and | 
| 215 |  |  |  |  |  |  | ( $fetched_value_list_ref->[2] eq 'Three' ) ) | 
| 216 |  |  |  |  |  |  | { | 
| 217 | 4 |  |  |  |  | 20 | $self->ok( ); | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  | else | 
| 220 |  |  |  |  |  |  | { | 
| 221 | 0 |  |  |  |  | 0 | $self->not_ok( 'fetched deep list does not match set deep list' ); | 
| 222 |  |  |  |  |  |  | } | 
| 223 |  |  |  |  |  |  | } | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | # Test clearing a cache | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | sub _test_six | 
| 230 |  |  |  |  |  |  | { | 
| 231 | 4 |  |  | 4 |  | 9 | my ( $self, $cache ) = @_; | 
| 232 |  |  |  |  |  |  |  | 
| 233 | 4 | 50 |  |  |  | 20 | $cache or | 
| 234 |  |  |  |  |  |  | croak( "cache required" ); | 
| 235 |  |  |  |  |  |  |  | 
| 236 | 4 |  |  |  |  | 8 | my $key = 'Test Key'; | 
| 237 |  |  |  |  |  |  |  | 
| 238 | 4 |  |  |  |  | 9 | my $value = 'Test Value'; | 
| 239 |  |  |  |  |  |  |  | 
| 240 | 4 |  |  |  |  | 14 | $cache->set( $key, $value ); | 
| 241 |  |  |  |  |  |  |  | 
| 242 | 4 |  |  |  |  | 51 | $cache->clear( ); | 
| 243 |  |  |  |  |  |  |  | 
| 244 | 4 |  |  |  |  | 27 | my $fetched_cleared_value = $cache->get( $key ); | 
| 245 |  |  |  |  |  |  |  | 
| 246 | 4 | 50 |  |  |  | 46 | ( not defined $fetched_cleared_value ) ? | 
| 247 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $fetched_cleared_value' ); | 
| 248 |  |  |  |  |  |  | } | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | # Test sizing of the cache | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | sub _test_seven | 
| 254 |  |  |  |  |  |  | { | 
| 255 | 4 |  |  | 4 |  | 10 | my ( $self, $cache ) = @_; | 
| 256 |  |  |  |  |  |  |  | 
| 257 | 4 |  |  |  |  | 46 | my $empty_size = $cache->size( ); | 
| 258 |  |  |  |  |  |  |  | 
| 259 | 4 | 50 |  |  |  | 27 | ( $empty_size == 0 ) ? | 
| 260 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$empty_size == 0' ); | 
| 261 |  |  |  |  |  |  |  | 
| 262 | 4 |  |  |  |  | 10 | my $first_key = 'First Test Key'; | 
| 263 |  |  |  |  |  |  |  | 
| 264 | 4 |  |  |  |  | 10 | my $value = 'Test Value'; | 
| 265 |  |  |  |  |  |  |  | 
| 266 | 4 |  |  |  |  | 18 | $cache->set( $first_key, $value ); | 
| 267 |  |  |  |  |  |  |  | 
| 268 | 4 |  |  |  |  | 25 | my $first_size = $cache->size( ); | 
| 269 |  |  |  |  |  |  |  | 
| 270 | 4 | 50 |  |  |  | 32 | ( $first_size > $empty_size ) ? | 
| 271 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$first_size > $empty_size' ); | 
| 272 |  |  |  |  |  |  |  | 
| 273 | 4 |  |  |  |  | 10 | my $second_key = 'Second Test Key'; | 
| 274 |  |  |  |  |  |  |  | 
| 275 | 4 |  |  |  |  | 19 | $cache->set( $second_key, $value ); | 
| 276 |  |  |  |  |  |  |  | 
| 277 | 4 |  |  |  |  | 27 | my $second_size = $cache->size( ); | 
| 278 |  |  |  |  |  |  |  | 
| 279 | 4 | 50 |  |  |  | 27 | ( $second_size > $first_size ) ? | 
| 280 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$second_size > $first_size' ); | 
| 281 |  |  |  |  |  |  | } | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  | # Test purging the cache | 
| 285 |  |  |  |  |  |  |  | 
| 286 |  |  |  |  |  |  | sub _test_eight | 
| 287 |  |  |  |  |  |  | { | 
| 288 | 4 |  |  | 4 |  | 13 | my ( $self, $cache ) = @_; | 
| 289 |  |  |  |  |  |  |  | 
| 290 | 4 |  |  |  |  | 17 | $cache->clear( ); | 
| 291 |  |  |  |  |  |  |  | 
| 292 | 4 |  |  |  |  | 26 | my $empty_size = $cache->size( ); | 
| 293 |  |  |  |  |  |  |  | 
| 294 | 4 | 50 |  |  |  | 29 | ( $empty_size == 0 ) ? | 
| 295 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$empty_size == 0' ); | 
| 296 |  |  |  |  |  |  |  | 
| 297 | 4 |  |  |  |  | 11 | my $expires_in = $EXPIRES_DELAY; | 
| 298 |  |  |  |  |  |  |  | 
| 299 | 4 |  |  |  |  | 13 | my $key = 'Test Key'; | 
| 300 |  |  |  |  |  |  |  | 
| 301 | 4 |  |  |  |  | 9 | my $value = 'Test Value'; | 
| 302 |  |  |  |  |  |  |  | 
| 303 | 4 |  |  |  |  | 21 | $cache->set( $key, $value, $expires_in ); | 
| 304 |  |  |  |  |  |  |  | 
| 305 | 4 |  |  |  |  | 24 | my $pre_purge_size = $cache->size( ); | 
| 306 |  |  |  |  |  |  |  | 
| 307 | 4 | 50 |  |  |  | 33 | ( $pre_purge_size > $empty_size ) ? | 
| 308 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$pre_purge_size > $empty_size' ); | 
| 309 |  |  |  |  |  |  |  | 
| 310 | 4 |  |  |  |  | 12000520 | sleep( $EXPIRES_DELAY + 1 ); | 
| 311 |  |  |  |  |  |  |  | 
| 312 | 4 |  |  |  |  | 139 | $cache->purge( ); | 
| 313 |  |  |  |  |  |  |  | 
| 314 | 4 |  |  |  |  | 23 | my $post_purge_size = $cache->size( ); | 
| 315 |  |  |  |  |  |  |  | 
| 316 | 4 | 50 |  |  |  | 39 | ( $post_purge_size == $empty_size ) ? | 
| 317 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$post_purge_size == $empty_size' ); | 
| 318 |  |  |  |  |  |  | } | 
| 319 |  |  |  |  |  |  |  | 
| 320 |  |  |  |  |  |  |  | 
| 321 |  |  |  |  |  |  | # Test the getting, setting, and removal of a scalar across cache instances | 
| 322 |  |  |  |  |  |  |  | 
| 323 |  |  |  |  |  |  | sub _test_nine | 
| 324 |  |  |  |  |  |  | { | 
| 325 | 4 |  |  | 4 |  | 8 | my ( $self, $cache1 ) = @_; | 
| 326 |  |  |  |  |  |  |  | 
| 327 | 4 | 50 |  |  |  | 17 | $cache1 or | 
| 328 |  |  |  |  |  |  | croak( "cache required" ); | 
| 329 |  |  |  |  |  |  |  | 
| 330 | 4 | 50 |  |  |  | 57 | my $cache2 = $cache1->new( ) or | 
| 331 |  |  |  |  |  |  | croak( "Couldn't construct new cache" ); | 
| 332 |  |  |  |  |  |  |  | 
| 333 | 4 |  |  |  |  | 8 | my $key = 'Test Key'; | 
| 334 |  |  |  |  |  |  |  | 
| 335 | 4 |  |  |  |  | 8 | my $value = 'Test Value'; | 
| 336 |  |  |  |  |  |  |  | 
| 337 | 4 |  |  |  |  | 83 | $cache1->set( $key, $value ); | 
| 338 |  |  |  |  |  |  |  | 
| 339 | 4 |  |  |  |  | 20 | my $fetched_value = $cache2->get( $key ); | 
| 340 |  |  |  |  |  |  |  | 
| 341 | 4 | 50 |  |  |  | 33 | ( $fetched_value eq $value ) ? | 
| 342 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$fetched_value eq $value' ); | 
| 343 |  |  |  |  |  |  | } | 
| 344 |  |  |  |  |  |  |  | 
| 345 |  |  |  |  |  |  |  | 
| 346 |  |  |  |  |  |  | # Test Clear() and Size() as instance methods | 
| 347 |  |  |  |  |  |  |  | 
| 348 |  |  |  |  |  |  | sub _test_ten | 
| 349 |  |  |  |  |  |  | { | 
| 350 | 4 |  |  | 4 |  | 10 | my ( $self, $cache ) = @_; | 
| 351 |  |  |  |  |  |  |  | 
| 352 | 4 | 50 |  |  |  | 19 | $cache or | 
| 353 |  |  |  |  |  |  | croak( "cache required" ); | 
| 354 |  |  |  |  |  |  |  | 
| 355 | 4 |  |  |  |  | 6 | my $key = 'Test Key'; | 
| 356 |  |  |  |  |  |  |  | 
| 357 | 4 |  |  |  |  | 7 | my $value = 'Test Value'; | 
| 358 |  |  |  |  |  |  |  | 
| 359 | 4 |  |  |  |  | 13 | $cache->set( $key, $value ); | 
| 360 |  |  |  |  |  |  |  | 
| 361 | 4 |  |  |  |  | 24 | my $full_size = $cache->Size( ); | 
| 362 |  |  |  |  |  |  |  | 
| 363 | 4 | 50 |  |  |  | 24 | ( $full_size > 0 ) ? | 
| 364 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$full_size > 0' ); | 
| 365 |  |  |  |  |  |  |  | 
| 366 | 4 |  |  |  |  | 20 | $cache->Clear( ); | 
| 367 |  |  |  |  |  |  |  | 
| 368 | 4 |  |  |  |  | 21 | my $empty_size = $cache->Size( ); | 
| 369 |  |  |  |  |  |  |  | 
| 370 | 4 | 50 |  |  |  | 26 | ( $empty_size == 0 ) ? | 
| 371 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$empty_size == 0' ); | 
| 372 |  |  |  |  |  |  | } | 
| 373 |  |  |  |  |  |  |  | 
| 374 |  |  |  |  |  |  |  | 
| 375 |  |  |  |  |  |  | # Test Purge(), Clear(), and Size() as instance methods | 
| 376 |  |  |  |  |  |  |  | 
| 377 |  |  |  |  |  |  | sub _test_eleven | 
| 378 |  |  |  |  |  |  | { | 
| 379 | 4 |  |  | 4 |  | 8 | my ( $self, $cache ) = @_; | 
| 380 |  |  |  |  |  |  |  | 
| 381 | 4 |  |  |  |  | 14 | $cache->Clear( ); | 
| 382 |  |  |  |  |  |  |  | 
| 383 | 4 |  |  |  |  | 19 | my $empty_size = $cache->Size( ); | 
| 384 |  |  |  |  |  |  |  | 
| 385 | 4 | 50 |  |  |  | 23 | ( $empty_size == 0 ) ? | 
| 386 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$empty_size == 0' ); | 
| 387 |  |  |  |  |  |  |  | 
| 388 | 4 |  |  |  |  | 13 | my $expires_in = $EXPIRES_DELAY; | 
| 389 |  |  |  |  |  |  |  | 
| 390 | 4 |  |  |  |  | 11 | my $key = 'Test Key'; | 
| 391 |  |  |  |  |  |  |  | 
| 392 | 4 |  |  |  |  | 8 | my $value = 'Test Value'; | 
| 393 |  |  |  |  |  |  |  | 
| 394 | 4 |  |  |  |  | 19 | $cache->set( $key, $value, $expires_in ); | 
| 395 |  |  |  |  |  |  |  | 
| 396 | 4 |  |  |  |  | 21 | my $pre_purge_size = $cache->Size( ); | 
| 397 |  |  |  |  |  |  |  | 
| 398 | 4 | 50 |  |  |  | 29 | ( $pre_purge_size > $empty_size ) ? | 
| 399 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$pre_purge_size > $empty_size' ); | 
| 400 |  |  |  |  |  |  |  | 
| 401 | 4 |  |  |  |  | 12000765 | sleep( $EXPIRES_DELAY + 1 ); | 
| 402 |  |  |  |  |  |  |  | 
| 403 | 4 |  |  |  |  | 96 | $cache->Purge( ); | 
| 404 |  |  |  |  |  |  |  | 
| 405 | 4 |  |  |  |  | 18 | my $purged_object = $cache->get_object( $key ); | 
| 406 |  |  |  |  |  |  |  | 
| 407 | 4 | 50 |  |  |  | 38 | ( not defined $purged_object ) ? | 
| 408 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $purged_object' ); | 
| 409 |  |  |  |  |  |  | } | 
| 410 |  |  |  |  |  |  |  | 
| 411 |  |  |  |  |  |  |  | 
| 412 |  |  |  |  |  |  | # Test Purge(), Clear(), and Size() as static methods | 
| 413 |  |  |  |  |  |  |  | 
| 414 |  |  |  |  |  |  | sub _test_twelve | 
| 415 |  |  |  |  |  |  | { | 
| 416 | 4 |  |  | 4 |  | 9 | my ( $self, $cache ) = @_; | 
| 417 |  |  |  |  |  |  |  | 
| 418 | 4 | 50 |  |  |  | 18 | my $class = ref $cache or | 
| 419 |  |  |  |  |  |  | croak( "Couldn't get ref \$cache" ); | 
| 420 |  |  |  |  |  |  |  | 
| 421 | 4 |  |  | 4 |  | 27 | no strict 'refs'; | 
|  | 4 |  |  |  |  | 4 |  | 
|  | 4 |  |  |  |  | 594 |  | 
| 422 |  |  |  |  |  |  |  | 
| 423 | 4 |  |  |  |  | 6 | &{"${class}::Clear"}( ); | 
|  | 4 |  |  |  |  | 31 |  | 
| 424 |  |  |  |  |  |  |  | 
| 425 | 4 |  |  |  |  | 13 | my $empty_size = &{"${class}::Size"}( ); | 
|  | 4 |  |  |  |  | 26 |  | 
| 426 |  |  |  |  |  |  |  | 
| 427 | 4 | 50 |  |  |  | 28 | ( $empty_size == 0 ) ? | 
| 428 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$empty_size == 0' ); | 
| 429 |  |  |  |  |  |  |  | 
| 430 | 4 |  |  |  |  | 9 | my $expires_in = $EXPIRES_DELAY; | 
| 431 |  |  |  |  |  |  |  | 
| 432 | 4 |  |  |  |  | 9 | my $key = 'Test Key'; | 
| 433 |  |  |  |  |  |  |  | 
| 434 | 4 |  |  |  |  | 5 | my $value = 'Test Value'; | 
| 435 |  |  |  |  |  |  |  | 
| 436 | 4 |  |  |  |  | 18 | $cache->set( $key, $value, $expires_in ); | 
| 437 |  |  |  |  |  |  |  | 
| 438 | 4 |  |  |  |  | 12 | my $pre_purge_size = &{"${class}::Size"}( ); | 
|  | 4 |  |  |  |  | 25 |  | 
| 439 |  |  |  |  |  |  |  | 
| 440 | 4 | 50 |  |  |  | 26 | ( $pre_purge_size > $empty_size ) ? | 
| 441 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$pre_purge_size > $empty_size' ); | 
| 442 |  |  |  |  |  |  |  | 
| 443 | 4 |  |  |  |  | 12000530 | sleep( $EXPIRES_DELAY + 1 ); | 
| 444 |  |  |  |  |  |  |  | 
| 445 | 4 |  |  |  |  | 40 | &{"${class}::Purge"}( ); | 
|  | 4 |  |  |  |  | 96 |  | 
| 446 |  |  |  |  |  |  |  | 
| 447 | 4 |  |  |  |  | 29 | my $purged_object = $cache->get_object( $key ); | 
| 448 |  |  |  |  |  |  |  | 
| 449 | 4 | 50 |  |  |  | 40 | ( not defined $purged_object ) ? | 
| 450 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $purged_object' ); | 
| 451 |  |  |  |  |  |  |  | 
| 452 | 4 |  |  | 4 |  | 19 | use strict; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 2527 |  | 
| 453 |  |  |  |  |  |  | } | 
| 454 |  |  |  |  |  |  |  | 
| 455 |  |  |  |  |  |  |  | 
| 456 |  |  |  |  |  |  |  | 
| 457 |  |  |  |  |  |  | # Test the expiration of an object with extended syntax | 
| 458 |  |  |  |  |  |  |  | 
| 459 |  |  |  |  |  |  | sub _test_thirteen | 
| 460 |  |  |  |  |  |  | { | 
| 461 | 4 |  |  | 4 |  | 8 | my ( $self, $cache ) = @_; | 
| 462 |  |  |  |  |  |  |  | 
| 463 | 4 |  |  |  |  | 8 | my $expires_in = $EXPIRES_DELAY; | 
| 464 |  |  |  |  |  |  |  | 
| 465 | 4 |  |  |  |  | 7 | my $key = 'Test Key'; | 
| 466 |  |  |  |  |  |  |  | 
| 467 | 4 |  |  |  |  | 7 | my $value = 'Test Value'; | 
| 468 |  |  |  |  |  |  |  | 
| 469 | 4 |  |  |  |  | 7 | my $start = time; | 
| 470 | 4 |  |  |  |  | 17 | $cache->set( $key, $value, $expires_in ); | 
| 471 |  |  |  |  |  |  |  | 
| 472 | 4 |  |  |  |  | 21 | my $fetched_value = $cache->get( $key ); | 
| 473 |  |  |  |  |  |  |  | 
| 474 | 4 | 50 |  |  |  | 21 | if (time - $start < $expires_in) { | 
| 475 | 4 | 50 |  |  |  | 23 | ( $fetched_value eq $value ) ? | 
| 476 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$fetched_value eq $value' ); | 
| 477 |  |  |  |  |  |  | } else { | 
| 478 | 0 |  |  |  |  | 0 | $self->skip( '$fetched_value eq $value (not finished in ' . | 
| 479 |  |  |  |  |  |  | $expires_in . ' s)' ); | 
| 480 |  |  |  |  |  |  | } | 
| 481 |  |  |  |  |  |  |  | 
| 482 | 4 |  |  |  |  | 12000534 | sleep( $EXPIRES_DELAY + 1 ); | 
| 483 |  |  |  |  |  |  |  | 
| 484 | 4 |  |  |  |  | 91 | my $fetched_expired_value = $cache->get( $key ); | 
| 485 |  |  |  |  |  |  |  | 
| 486 | 4 | 50 |  |  |  | 79 | ( not defined $fetched_expired_value ) ? | 
| 487 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $fetched_expired_value' ); | 
| 488 |  |  |  |  |  |  | } | 
| 489 |  |  |  |  |  |  |  | 
| 490 |  |  |  |  |  |  |  | 
| 491 |  |  |  |  |  |  | # test the get_keys method | 
| 492 |  |  |  |  |  |  |  | 
| 493 |  |  |  |  |  |  | sub _test_fourteen | 
| 494 |  |  |  |  |  |  | { | 
| 495 | 4 |  |  | 4 |  | 10 | my ( $self, $cache ) = @_; | 
| 496 |  |  |  |  |  |  |  | 
| 497 | 4 |  |  |  |  | 31 | $cache->Clear( ); | 
| 498 |  |  |  |  |  |  |  | 
| 499 | 4 |  |  |  |  | 28 | my $empty_size = $cache->Size( ); | 
| 500 |  |  |  |  |  |  |  | 
| 501 | 4 | 50 |  |  |  | 32 | ( $empty_size == 0 ) ? | 
| 502 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$empty_size == 0' ); | 
| 503 |  |  |  |  |  |  |  | 
| 504 | 4 |  |  |  |  | 38 | my @keys = sort ( 'John', 'Paul', 'Ringo', 'George' ); | 
| 505 |  |  |  |  |  |  |  | 
| 506 | 4 |  |  |  |  | 9 | my $value = 'Test Value'; | 
| 507 |  |  |  |  |  |  |  | 
| 508 | 4 |  |  |  |  | 12 | foreach my $key ( @keys ) | 
| 509 |  |  |  |  |  |  | { | 
| 510 | 16 |  |  |  |  | 59 | $cache->set( $key, $value ); | 
| 511 |  |  |  |  |  |  | } | 
| 512 |  |  |  |  |  |  |  | 
| 513 | 4 |  |  |  |  | 23 | my @cached_keys = sort $cache->get_keys( ); | 
| 514 |  |  |  |  |  |  |  | 
| 515 | 4 |  |  |  |  | 24 | my $arrays_equal = Arrays_Are_Equal( \@keys, \@cached_keys ); | 
| 516 |  |  |  |  |  |  |  | 
| 517 | 4 | 50 |  |  |  | 33 | ( $arrays_equal == 1 ) ? | 
| 518 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$arrays_equal == 1' ); | 
| 519 |  |  |  |  |  |  | } | 
| 520 |  |  |  |  |  |  |  | 
| 521 |  |  |  |  |  |  |  | 
| 522 |  |  |  |  |  |  | # test the auto_purge on set functionality | 
| 523 |  |  |  |  |  |  |  | 
| 524 |  |  |  |  |  |  | sub _test_fifteen | 
| 525 |  |  |  |  |  |  | { | 
| 526 | 4 |  |  | 4 |  | 11 | my ( $self, $cache ) = @_; | 
| 527 |  |  |  |  |  |  |  | 
| 528 | 4 |  |  |  |  | 24 | $cache->Clear( ); | 
| 529 |  |  |  |  |  |  |  | 
| 530 | 4 |  |  |  |  | 17 | my $expires_in = $EXPIRES_DELAY; | 
| 531 |  |  |  |  |  |  |  | 
| 532 | 4 |  |  |  |  | 56 | $cache->set_auto_purge_interval( $expires_in ); | 
| 533 |  |  |  |  |  |  |  | 
| 534 | 4 |  |  |  |  | 17 | $cache->set_auto_purge_on_set( 1 ); | 
| 535 |  |  |  |  |  |  |  | 
| 536 | 4 |  |  |  |  | 6 | my $key = 'Test Key'; | 
| 537 |  |  |  |  |  |  |  | 
| 538 | 4 |  |  |  |  | 11 | my $value = 'Test Value'; | 
| 539 |  |  |  |  |  |  |  | 
| 540 | 4 |  |  |  |  | 9 | my $start = time; | 
| 541 | 4 |  |  |  |  | 18 | $cache->set( $key, $value, $expires_in ); | 
| 542 |  |  |  |  |  |  |  | 
| 543 | 4 |  |  |  |  | 36 | my $fetched_value = $cache->get( $key ); | 
| 544 |  |  |  |  |  |  |  | 
| 545 | 4 | 50 |  |  |  | 25 | if (time - $start < $expires_in) { | 
| 546 | 4 | 50 |  |  |  | 29 | ( $fetched_value eq $value ) ? | 
| 547 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( '$fetched_value eq $value' ); | 
| 548 |  |  |  |  |  |  | } else { | 
| 549 | 0 |  |  |  |  | 0 | $self->skip( '$fetched_value eq $value (not finished in ' . | 
| 550 |  |  |  |  |  |  | $expires_in . ' s)' ); | 
| 551 |  |  |  |  |  |  | } | 
| 552 |  |  |  |  |  |  |  | 
| 553 | 4 |  |  |  |  | 12000674 | sleep( $EXPIRES_DELAY + 1 ); | 
| 554 |  |  |  |  |  |  |  | 
| 555 | 4 |  |  |  |  | 90 | $cache->set( "Trigger auto_purge", "Empty" ); | 
| 556 |  |  |  |  |  |  |  | 
| 557 | 4 |  |  |  |  | 31 | my $fetched_expired_object = $cache->get_object( $key ); | 
| 558 |  |  |  |  |  |  |  | 
| 559 | 4 | 50 |  |  |  | 46 | ( not defined $fetched_expired_object ) ? | 
| 560 |  |  |  |  |  |  | $self->ok( ) : $self->not_ok( 'not defined $fetched_expired_object' ); | 
| 561 |  |  |  |  |  |  |  | 
| 562 | 4 |  |  |  |  | 30 | $cache->Clear( ); | 
| 563 |  |  |  |  |  |  | } | 
| 564 |  |  |  |  |  |  |  | 
| 565 |  |  |  |  |  |  |  | 
| 566 |  |  |  |  |  |  |  | 
| 567 |  |  |  |  |  |  | # test the auto_purge_interval functionality | 
| 568 |  |  |  |  |  |  |  | 
| 569 |  |  |  |  |  |  | sub _test_sixteen | 
| 570 |  |  |  |  |  |  | { | 
| 571 | 4 |  |  | 4 |  | 9 | my ( $self, $cache ) = @_; | 
| 572 |  |  |  |  |  |  |  | 
| 573 | 4 |  |  |  |  | 11 | my $expires_in = $EXPIRES_DELAY; | 
| 574 |  |  |  |  |  |  |  | 
| 575 | 4 |  |  |  |  | 13 | my $ok = eval { | 
| 576 | 4 |  |  |  |  | 30 | $cache = $cache->new( { 'auto_purge_interval' => $expires_in } ); | 
| 577 | 4 |  |  |  |  | 14 | 1; | 
| 578 |  |  |  |  |  |  | }; | 
| 579 |  |  |  |  |  |  |  | 
| 580 | 4 | 50 |  |  |  | 31 | $ok ? $self->ok( ) | 
| 581 |  |  |  |  |  |  | : $self->not_ok( "couldn't create autopurge cache" ); | 
| 582 |  |  |  |  |  |  | } | 
| 583 |  |  |  |  |  |  |  | 
| 584 |  |  |  |  |  |  |  | 
| 585 |  |  |  |  |  |  | # test the get_namespaces method | 
| 586 |  |  |  |  |  |  |  | 
| 587 |  |  |  |  |  |  | sub _test_seventeen | 
| 588 |  |  |  |  |  |  | { | 
| 589 | 4 |  |  | 4 |  | 11 | my ( $self, $cache ) = @_; | 
| 590 |  |  |  |  |  |  |  | 
| 591 | 4 |  |  |  |  | 20 | $cache->set( 'a', '1' ); | 
| 592 | 4 |  |  |  |  | 27 | $cache->set_namespace( 'namespace' ); | 
| 593 | 4 |  |  |  |  | 15 | $cache->set( 'b', '2' ); | 
| 594 |  |  |  |  |  |  |  | 
| 595 | 4 | 50 |  |  |  | 111 | if ( Arrays_Are_Equal( [ sort( $cache->get_namespaces( ) ) ], | 
| 596 |  |  |  |  |  |  | [ sort( 'Default', 'namespace' ) ] ) ) | 
| 597 |  |  |  |  |  |  | { | 
| 598 | 4 |  |  |  |  | 23 | $self->ok( ); | 
| 599 |  |  |  |  |  |  | } | 
| 600 |  |  |  |  |  |  | else | 
| 601 |  |  |  |  |  |  | { | 
| 602 | 0 |  |  |  |  | 0 | $self->not_ok( "get_namespaces returned the wrong namespaces" ); | 
| 603 |  |  |  |  |  |  | } | 
| 604 |  |  |  |  |  |  |  | 
| 605 | 4 |  |  |  |  | 34 | $cache->Clear( ); | 
| 606 |  |  |  |  |  |  | } | 
| 607 |  |  |  |  |  |  |  | 
| 608 |  |  |  |  |  |  |  | 
| 609 |  |  |  |  |  |  |  | 
| 610 |  |  |  |  |  |  | sub Arrays_Are_Equal | 
| 611 |  |  |  |  |  |  | { | 
| 612 | 8 |  |  | 8 | 0 | 19 | my ( $first_array_ref, $second_array_ref ) = @_; | 
| 613 |  |  |  |  |  |  |  | 
| 614 | 8 |  |  |  |  | 47 | local $^W = 0;  # silence spurious -w undef complaints | 
| 615 |  |  |  |  |  |  |  | 
| 616 | 8 | 50 |  |  |  | 36 | return 0 unless @$first_array_ref == @$second_array_ref; | 
| 617 |  |  |  |  |  |  |  | 
| 618 | 8 |  |  |  |  | 36 | for (my $i = 0; $i < @$first_array_ref; $i++) | 
| 619 |  |  |  |  |  |  | { | 
| 620 | 24 | 50 |  |  |  | 85 | return 0 if $first_array_ref->[$i] ne $second_array_ref->[$i]; | 
| 621 |  |  |  |  |  |  | } | 
| 622 |  |  |  |  |  |  |  | 
| 623 | 8 |  |  |  |  | 33 | return 1; | 
| 624 |  |  |  |  |  |  | } | 
| 625 |  |  |  |  |  |  |  | 
| 626 |  |  |  |  |  |  |  | 
| 627 |  |  |  |  |  |  | 1; | 
| 628 |  |  |  |  |  |  |  | 
| 629 |  |  |  |  |  |  |  | 
| 630 |  |  |  |  |  |  | __END__ |