From 4ec354d2a0f356d7c6dfbbda6c29ef5b78f8130c Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 18 Sep 2019 12:53:20 +1000 Subject: [PATCH] [Draft] Integration test for testing memcached client https://bugs.koha-community.org/show_bug.cgi?id=13193 --- t/01-memcached.t | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 t/01-memcached.t diff --git a/t/01-memcached.t b/t/01-memcached.t new file mode 100644 index 0000000000..426763bbd2 --- /dev/null +++ b/t/01-memcached.t @@ -0,0 +1,104 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; +use Cache::Memcached::Fast; +use IO::Select; + +my $cache = Cache::Memcached::Fast->new({ + servers => ["localhost:11211"], +}); +my $map = { + akey => '100', + bkey => 'hundred', + ckey => 'cent', +}; +#Add keys for test +foreach my $key ( keys %$map ){ + $cache->add($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({ + key => "akey", + iterations => int(rand(50)), + }); + my $b_problems = check_cache({ + key => "bkey", + iterations => int(rand(50)), + }); + my $c_problems = check_cache({ + key => "ckey", + 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); +} +#Delete keys that were added +foreach my $key ( keys %$map ){ + $cache->delete($key); +} +warn "Finished $problems\n"; +is($problems,0,"There should be no problems reported."); + +sub check_cache { + my ($args) = @_; + my $problems = 0; + my $key = $args->{key}; + my $iterations = $args->{iterations}; + if ($key && defined $iterations){ + $iterations++; + #warn "$$) Fetching $key $iterations times\n"; + for ( 1 .. $iterations ){ + my $cached_value = $cache->get($key); + 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