App-Config-Chronicle
view release on metacpan or search on metacpan
t/08_new_api.t view on Meta::CPAN
subtest "Chronicle shouldn't be engaged with perl caching enabled" => sub {
my $app_config = _new_app_config(local_caching => 1);
ok $app_config->set({EMAIL_KEY() => FIRST_EMAIL}), 'Set email to chron';
my $reader_module = Test::MockModule->new('Data::Chronicle::Reader');
$reader_module->mock('get', sub { ok(0, 'get should not be called here') });
$reader_module->mock('mget', sub { ok(0, 'mget should not be called here') });
is $app_config->get(EMAIL_KEY), FIRST_EMAIL, 'Email is retrieved without chron access';
$reader_module->unmock('get');
$reader_module->unmock('mget');
};
subtest 'Chronicle should be engaged with perl caching disabled' => sub {
my $chronicle_gets;
my $app_config = _new_app_config(local_caching => 0);
my $reader_module = Test::MockModule->new('Data::Chronicle::Reader');
$reader_module->mock('get', sub { $chronicle_gets++; return {data => FIRST_EMAIL} });
$reader_module->mock('mget', sub { $chronicle_gets++; return {data => FIRST_EMAIL} });
ok $app_config->set({EMAIL_KEY() => FIRST_EMAIL}), 'Set email with write to chron';
is $app_config->get(EMAIL_KEY), FIRST_EMAIL, 'Email is retrieved with chron access';
ok $chronicle_gets, 'get engages chronicle';
$reader_module->unmock('get');
$reader_module->unmock('mget');
};
};
subtest 'Global revision updates' => sub {
my $app_config = _new_app_config();
my $old_rev = $app_config->global_revision();
set_fixed_time(++$tick);
ok $app_config->set({EMAIL_KEY() => FIRST_EMAIL}), 'Set 1 value succeeds';
my $new_rev = $app_config->global_revision();
ok $new_rev > $old_rev, 'Revision was increased';
};
subtest 'Cache syncing' => sub {
my $cached_config1 = _new_app_config(
local_caching => 1,
refresh_interval => 0
);
my $cached_config2 = _new_app_config(
local_caching => 1,
refresh_interval => 0
);
my $direct_config = _new_app_config(local_caching => 0);
ok $direct_config->set({EMAIL_KEY() => FIRST_EMAIL}), 'Set email succeeds';
is $direct_config->get(EMAIL_KEY), FIRST_EMAIL, 'Email is retrieved successfully';
is $cached_config1->get(EMAIL_KEY), DEFAULT_EMAIL, 'Cache1 contains default before first update call';
is $cached_config2->get(EMAIL_KEY), DEFAULT_EMAIL, 'Cache2 contains default before first update call';
ok $cached_config1->update_cache(), 'Cache 1 is updated';
ok $cached_config2->update_cache(), 'Cache 2 is updated';
is $cached_config1->get(EMAIL_KEY), FIRST_EMAIL, 'Cache1 is updated with email';
is $cached_config2->get(EMAIL_KEY), FIRST_EMAIL, 'Cache2 is updated with email';
set_fixed_time(++$tick); #Ensure new value is recorded at a different time
ok $cached_config1->set({EMAIL_KEY() => SECOND_EMAIL}), 'Set email via cache 1 succeeds';
is $direct_config->get(EMAIL_KEY), SECOND_EMAIL, 'Email is retrieved directly';
is $cached_config1->get(EMAIL_KEY), SECOND_EMAIL, 'Cache1 has updated email';
is $cached_config2->get(EMAIL_KEY), FIRST_EMAIL, 'Cache2 still has old email';
ok $cached_config2->update_cache(), 'Cache 2 is updated';
is $cached_config2->get(EMAIL_KEY), SECOND_EMAIL, 'Cache2 has updated email';
};
subtest 'Cache refresh_interval' => sub {
my $cached_config = _new_app_config(
local_caching => 1,
refresh_interval => 2
);
my $direct_config = _new_app_config(local_caching => 0);
set_fixed_time(++$tick); #Ensure new value is recorded at a different time
ok $direct_config->set({EMAIL_KEY() => FIRST_EMAIL}), 'Set email succeeds';
is $direct_config->get(EMAIL_KEY), FIRST_EMAIL, 'Email is retrieved successfully';
ok $cached_config->update_cache(), 'Cache is updated';
is $cached_config->get(EMAIL_KEY), FIRST_EMAIL, 'Email is retrieved successfully';
set_fixed_time(++$tick); #Ensure new value is recorded at a different time
ok $direct_config->set({EMAIL_KEY() => SECOND_EMAIL}), 'Set email succeeds';
is $direct_config->get(EMAIL_KEY), SECOND_EMAIL, 'Email is retrieved successfully';
ok !$cached_config->update_cache(), 'update not done due to refresh_interval';
is $cached_config->get(EMAIL_KEY), FIRST_EMAIL, "Cache still has old value since interval hasn't passed";
set_fixed_time($tick + $cached_config->refresh_interval);
ok $cached_config->update_cache(), 'Cache is updated';
is $cached_config->get(EMAIL_KEY), SECOND_EMAIL, 'Email is retrieved successfully from updated cache';
};
sub _new_app_config {
my $app_config;
my %options = @_;
subtest 'Setup' => sub {
my ($chronicle_r, $chronicle_w) = Data::Chronicle::Mock::get_mocked_chronicle();
lives_ok {
$app_config = App::Config::Chronicle->new(
definition_yml => "$Bin/test.yml",
chronicle_reader => $chronicle_r,
chronicle_writer => $chronicle_w,
%options
);
}
'We are living';
};
return $app_config;
}
done_testing;
( run in 2.250 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )