Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ requires 'Time::HiRes';
on configure => sub {
requires 'Module::Build::Tiny', '0.035';
requires 'perl', '5.008_001';
requires 'Redis';
};

on test => sub {
requires 'Redis';
requires 'Test::More', '0.98';
requires 'Test::TCP';
};
25 changes: 25 additions & 0 deletions lib/Test/RedisServer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use File::Temp;
use POSIX qw(SIGTERM WNOHANG);
use Time::HiRes qw(sleep);
use Errno ();
use Redis;

has auto_start => (
is => 'rw',
Expand All @@ -36,6 +37,11 @@ has tmpdir => (
lazy_build => 1,
);

has _redis => (
is => 'rw',
isa => 'Redis',
);

no Mouse;

sub BUILD {
Expand Down Expand Up @@ -133,6 +139,13 @@ sub start {
};
}

# This is sometimes needed to send commands to RedisServer during the stop process.
# Generally, we would like to generate it lazily and not have it as a property
# of the object. However, if you try to create the object at the stop,
# the object generation may fail, such as missing the socket file. Therefore,
# we will make the object and store it as property here.
$self->_redis( Redis->new($self->connect_info) );

$self->pid($pid);
}

Expand Down Expand Up @@ -163,6 +176,18 @@ sub stop {
local $?; # waitpid may change this value :/
return unless defined $self->pid;

# If the tmpdir has disappeared, clear the save config to prevent saving
# in the server terminating process. The newer Redis will save on stop
# for robustness, but will keep blocking if the directory is missing.
#
# It is unlikely that tmpdir will disappear first, but if both the RedisServer
# object and the tmpdir are defined globally, it may happen because the order
# in which they are DESTLOYed is uncertain.
if (! -f $self->tmpdir) {
$self->_redis->config_set('appendonly', 'no');
$self->_redis->config_set('save', '');
}

$sig ||= SIGTERM;

kill $sig, $self->pid;
Expand Down
20 changes: 20 additions & 0 deletions t/no_save.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use strict;
use warnings;
use Test::More;
use File::Path 'remove_tree';

use Test::RedisServer;

eval { Test::RedisServer->new } or plan skip_all => 'redis-server is required in PATH to run this test';

my $tmpdir = File::Temp->newdir;
my $server = Test::RedisServer->new(tmpdir => $tmpdir);
ok $server->pid, 'pid ok';

remove_tree($tmpdir);
ok ! -f $tmpdir;

$server->stop;
pass 'redis exit ok';

done_testing;