From 83f33c5ff60e3edf80b300495e280141c174fa09 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 18 Sep 2019 15:01:34 +1000 Subject: [PATCH] [Draft] Integration test Koha::Cache and memcached This test is not 100% reliable. When run with Cache::Memcached::Fast, it will usually generate errors, but not always. When run with Cache::Memcached::Fast::Safe, it never generates errors. https://bugs.koha-community.org/show_bug.cgi?id=13193 --- t/02-memcached.t | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 t/02-memcached.t diff --git a/t/02-memcached.t b/t/02-memcached.t new file mode 100644 index 0000000000..77491135d3 --- /dev/null +++ b/t/02-memcached.t @@ -0,0 +1,102 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Koha::Caches; +use Test::More tests => 1; +use Cache::Memcached::Fast; +use IO::Select; + +my $cache = Koha::Caches->get_instance('syspref'); + +my $map = { + 'syspref_timeout' => '1d', + 'syspref_independentbranches' => '0', + 'syspref_version' => '18.1101000' +}; +#Set keys for test +foreach my $key ( keys %$map ){ + $cache->set_in_cache($key,$map->{$key}); +} + +my @children = (); +my $select = IO::Select->new(); +for ( 1..10 ){ + my ($read_child,$write_parent); + pipe($read_child,$write_parent); + my $pid = fork; + if ($pid == 0){ + close($read_child); + my $a_problems = check_cache({ + keys => ["syspref_version","syspref_timeout","syspref_independentbranches"], + iterations => int(rand(50)), + }); + my $b_problems = check_cache({ + keys => ["syspref_version","syspref_timeout","syspref_independentbranches"], + iterations => int(rand(50)), + }); + my $c_problems = check_cache({ + keys => ["syspref_version","syspref_timeout","syspref_independentbranches"], + iterations => int(rand(50)), + }); + my $total_problems = $a_problems + $b_problems + $c_problems; + + print $write_parent "$total_problems\n"; + close($write_parent); + exit; + } + else { + close($write_parent); + $select->add($read_child); + push(@children,$pid); + } +} + +my $problems = 0; +while ( my @ready = $select->can_read(.25) ){ + foreach my $fh (@ready){ + my $msg = <$fh>; + if ($msg){ + chomp($msg); + $problems += $msg; + $select->remove($fh); + $fh->close; + } + } +} + +foreach my $child (@children){ + waitpid($child,0); +} +warn "Finished $problems\n"; +is($problems,0,"There should be no problems reported."); + +sub check_cache { + my ($args) = @_; + my $problems = 0; + my $keys = $args->{keys}; + my $iterations = $args->{iterations}; + if ($keys && defined $iterations){ + $iterations++; + for ( 1 .. $iterations ){ + my $index = int(rand(3)); + my $key = $keys->[$index]; + my $cached_value = $cache->get_from_cache($key); + Koha::Caches->flush_L1_caches(); + my $problem = ""; + if ( ! defined $cached_value ){ + $cached_value = 'NULL'; + $problem = 1; + } + elsif ($cached_value ne $map->{$key}){ + $problem = 1; + } + if ($problem){ + $problems++; + warn "Process:$$\t Key: $key\t Correct value: $map->{$key}\t Cached value: $cached_value\n"; + } + } + } + return $problems; +} -- 2.16.4