HealthCheck-Diagnostic-Redis

 view release on metacpan or  search on metacpan

t/HealthCheck-Diagnostic-Redis.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::MockModule;
use Test::Differences;

BEGIN { use_ok('HealthCheck::Diagnostic::Redis') };

diag(qq(HealthCheck::Diagnostic::Redis Perl $], $^X));

my %fake_redis_store = (
    recent_key    => 1234,
    read_this_key => 'once upon a time',
);

# Mock the Redis module so that we can pretend to have good and bad hosts.
my $mock = Test::MockModule->new( 'Redis::Fast' );
$mock->mock( new => sub {
    my ( $class, %params ) = @_;

    my $host = $params{server};
    $host =~ s/:6379$//;

    # In-accessible hosts should not be reached.
    die "Redis::Fast: Bad host name: $host"
        if $host eq 'inaccessible-host';

    return bless( \%params, 'Redis::Fast' );
} );
$mock->mock( get => sub {
    my ($self, $key) = @_;
    return $fake_redis_store{$key};
} );
$mock->mock( set => sub {
    my ($self, $key, $val) = @_;
    $fake_redis_store{ $key } = $val;
} );
$mock->mock( del => sub {
    my ($self, $key) = @_;
    delete $fake_redis_store{ $key };
} );
$mock->mock( ping => sub {
    my $self = shift;
    return $self->{server} =~ /badping/ ? 0 : 1;
} );
$mock->mock( randomkey => sub {
    my $self = shift;
    return 'recent_key';
} );
# We have to mock out destroy, or things break terribly because of Redis::Fast
# AUTOLOAD...
$mock->mock( DESTROY => sub {} );

# Check that we can use HealthCheck as a class.
{
    my $result = HealthCheck::Diagnostic::Redis->check(
        host => 'good-host',
    );
    is $result->{status}, 'OK',
        'Can use HealthCheck as a class.';
    is $result->{info}, 'Successful connection for good-host Redis',
        'Info message is correct for instance check.';
}

# Check that we can use HealthCheck with initialized values too.



( run in 0.764 second using v1.01-cache-2.11-cpan-6aa56a78535 )