From 44c74c2299db54e3e3e01852f2886a657d335c52 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Fri, 2 May 2008 20:03:33 +0000 Subject: [PATCH] added some database cache tests while testing out a theory, the theory is busted, but at least we have some more tests --- lib/WebGUI/Cache/Database.pm | 9 ++-- t/Cache/Database.t | 79 ++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 t/Cache/Database.t diff --git a/lib/WebGUI/Cache/Database.pm b/lib/WebGUI/Cache/Database.pm index 3297d9d36..81e8a89df 100644 --- a/lib/WebGUI/Cache/Database.pm +++ b/lib/WebGUI/Cache/Database.pm @@ -88,16 +88,15 @@ sub flush { =head2 get ( ) -Retrieve content from the filesystem cache. +Retrieve content from the database cache. =cut sub get { my $self = shift; - return undef if ($self->session->config->get("disableCache")); - # getting better performance using native dbi than webgui sql - my $dbh = $self->session->db->dbh; - my $sth = $dbh->prepare("select content from cache where namespace=? and cachekey=? and expires>?"); + my $session = $self->session; + return undef if ($session->config->get("disableCache")); + my $sth = $session->db->dbh->prepare("select content from cache where namespace=? and cachekey=? and expires>?"); $sth->execute($self->{_namespace},$self->{_key},time()); my $data = $sth->fetchrow_arrayref; $sth->finish; diff --git a/t/Cache/Database.t b/t/Cache/Database.t new file mode 100644 index 000000000..dc2f98189 --- /dev/null +++ b/t/Cache/Database.t @@ -0,0 +1,79 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; +use Storable qw(freeze thaw); +use Test::More; +use Time::HiRes; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Cache::Database; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + +# presupposes that there are cached items to test +my $cacheEntries = $session->db->buildArrayRefOfHashRefs("select expires,cachekey,namespace,content from cache order by rand() limit 100"); + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 2 + scalar(@{$cacheEntries}); # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# put your tests here + +my $cache = WebGUI::Cache::Database->new($session, "this", "that"); +my $testValue = "a rock that has no earthly business in that field"; + +$cache->set($testValue); +is($cache->get, $testValue, "set/get works"); +$cache->delete; +is($cache->get, undef, "delete works"); + + + +# performance tests +my $numTests = 0; +my $totalTime = 0; +foreach my $entry (@{$cacheEntries}) { + my $start = [Time::HiRes::gettimeofday]; + my $cache = WebGUI::Cache::Database->new($session, $entry->{cachekey}, $entry->{namespace}); + $cache->{_key} = $entry->{cachekey}; # evil: don't do this at home kids + my $value = $cache->get; + if ($entry->{expires} > time()) { + my $entryValue = $entry->{content}; + eval { $entryValue = thaw($entryValue); }; + $entryValue = ($entryValue && ref $entryValue) ? $$entryValue : undef; + is_deeply($value, $entryValue, "cache entry is valid"); + } + else { + is($value, undef, "cache entry has timed out"); + } + $numTests++; + $totalTime += Time::HiRes::tv_interval($start); +} +print "\nTime to run $numTests cache tests is $totalTime seconds. Average time per test is ".($totalTime/$numTests)." seconds.\n" if ($numTests > 0); +# end performance tests + + +#---------------------------------------------------------------------------- +# Cleanup +END { + +}