AnyEvent-Redis-RipeRedis
view release on metacpan or search on metacpan
t/tlib/Test/RedisRunner.pm view on Meta::CPAN
my $self = shift;
my $tmpdir = $self->{tmpdir};
open( my $conffh, '>', "$tmpdir/redis.conf" ) or croak "cannot write conf: $!";
print $conffh $self->_conf_string;
close $conffh;
exec 'redis-server', "$tmpdir/redis.conf" or do {
if ( $! == Errno::ENOENT ) {
print STDERR "exec failed: no such file or directory\n";
}
else {
print STDERR "exec failed: unexpected error: $!\n";
}
exit( $? );
};
return;
}
sub stop {
my $self = shift;
my $sig = shift;
local $?; # waitpid may change this value :/
return unless defined $self->{pid};
$sig ||= SIGTERM;
kill( $sig, $self->{pid} );
while ( waitpid( $self->{pid}, WNOHANG ) >= 0 ) {
}
undef $self->{pid};
return;
}
sub wait_exit {
my $self = shift;
local $?;
my $kid;
my $pid = $self->{pid};
do {
$kid = waitpid( $pid, WNOHANG );
sleep 0.1;
} while $kid >= 0;
undef $self->{pid};
return;
}
sub connect_info {
my $self = shift;
my $conf = $self->{conf};
my $host = $conf->{bind} || '0.0.0.0';
my $port = $conf->{port};
if ( !$port || $port == 0 ) {
$host = 'unix/';
$port = $conf->{unixsocket};
}
return (
host => $host,
port => $port,
);
}
sub _conf_string {
my $self = shift;
my $conf = q[];
my %conf = %{ $self->{conf} };
while ( my ( $k, $v ) = each %conf ) {
next unless defined $v;
$conf .= "$k $v\n";
}
return $conf;
}
sub DESTROY {
my $self = shift;
if ( defined $self->{pid} && $$ == $self->{_owner_pid} ) {
$self->stop();
}
return;
}
__END__
=head1 NAME
Test::RedisRunner - redis-server runner for tests.
=head1 SYNOPSIS
use Redis;
use Test::RedisRunner;
use Test::More;
my $redis_server;
eval {
$redis_server = Test::RedisRunner->new;
} or plan skip_all => 'redis-server is required to this test';
my $redis = Redis->new( $redis_server->connect_info );
is $redis->ping, 'PONG', 'ping pong ok';
done_testing;
=head1 DESCRIPTION
( run in 0.785 second using v1.01-cache-2.11-cpan-2398b32b56e )