Koha/t/db_dependent/Context.t
Jared Camins-Esakov 4b65389d5e Bug 9005: Allow user to disable syspref cache
Because C4::Context uses an in-memory hash for caching sysprefs,
changing a syspref under a multi-threaded persistent environment
requires a server restart. This patch makes it possible disable
the syspref cache.

To test:
1) If you are using a multi-threaded persistent server (Starman, etc.),
   change a syspref and note that the effects of the syspref change may
   or may not be visible on any given request before applying this patch.
   You will need to choose a syspref with obvious effects that can be
   seen by simply refreshing the page. I recommend enabling or
   disabling additional languages in the OPAC, since you can refresh the
   page a dozen times and reasonably expect to see the new behavior you
   set only 1/n of the time.
2) Apply patch.
3) Add "C4::Context->disable_syspref_cache();" to your koha.psgi file
4) Repeat step 1, noting that you never see the stale behavior.
5) Run test at t/db_dependent/Context.t.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
All tests passed.

Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
2012-11-08 12:41:10 -05:00

107 lines
3.7 KiB
Perl
Executable file

#!/usr/bin/perl
#
use strict;
use warnings;
use Test::More;
use Test::MockModule;
use vars qw($debug $koha $dbh $config $ret);
BEGIN {
$debug = $ENV{DEBUG} || 0;
diag("Note: The overall number of tests may vary by configuration.");
diag("First we need to check your environmental variables");
for (qw(KOHA_CONF PERL5LIB)) {
ok($ret = $ENV{$_}, "ENV{$_} = $ret");
}
use_ok('C4::Context');
use_ok('C4::Utils', qw/ :all /);
}
ok($koha = C4::Context->new, 'C4::Context->new');
ok($dbh = C4::Context->dbh(), 'Getting dbh from C4::Context');
ok($ret = C4::Context->KOHAVERSION, ' (function) KOHAVERSION = ' . ($ret||''));
ok($ret = $koha->KOHAVERSION, ' $koha->KOHAVERSION = ' . ($ret||''));
my @keys = keys %$koha;
diag("Number of keys in \%\$koha: " . scalar @keys);
our $width = 0;
if (ok(@keys)) {
$width = maxwidth(@keys);
$debug and diag "widest key is $width";
}
foreach (sort @keys) {
ok(exists $koha->{$_},
'$koha->{' . sprintf('%' . $width . 's', $_) . '} exists '
. ((defined $koha->{$_}) ? "and is defined." : "but is not defined.")
);
}
diag "Examining defined key values.";
foreach (grep {defined $koha->{$_}} sort @keys) {
print "\n";
hashdump('$koha->{' . sprintf('%' . $width . 's', $_) . '}', $koha->{$_});
}
ok($config = $koha->{config}, 'Getting $koha->{config} ');
diag "Testing syspref caching.";
my $dbh = C4::Context->dbh;
$dbh->disconnect;
my $module = new Test::MockModule('C4::Context');
$module->mock(
'_new_dbh',
sub {
my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
|| die "Cannot create handle: $DBI::errstr\n";
return $dbh;
}
);
my $history;
$dbh = C4::Context->dbh;
$dbh->{mock_add_resultset} = [ ['value'], ['thing1'] ];
$dbh->{mock_add_resultset} = [ ['value'], ['thing2'] ];
$dbh->{mock_add_resultset} = [ ['value'], ['thing3'] ];
$dbh->{mock_add_resultset} = [ ['value'], ['thing4'] ];
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing1', "Retrieved syspref (value='thing1') successfully with default behavior");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
C4::Context->disable_syspref_cache();
is(C4::Context->preference("SillyPreference"), 'thing2', "Retrieved syspref (value='thing2') successfully with disabled cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully with disabled cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
C4::Context->enable_syspref_cache();
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing3', "Retrieved syspref (value='thing3') successfully from cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
C4::Context->clear_syspref_cache();
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully after clearing cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 1, 'Retrieved syspref from database');
$dbh->{mock_clear_history} = 1;
is(C4::Context->preference("SillyPreference"), 'thing4', "Retrieved syspref (value='thing4') successfully from cache");
$history = $dbh->{mock_all_history};
is(scalar(@{$history}), 0, 'Did not retrieve syspref from database');
done_testing();
1;