The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

package t::Redis;
use strict;
use FindBin;
use base qw(Exporter);
our @EXPORT = qw(test_redis);
sub test_redis(&;$) {
my $cb = shift;
my $args = shift;
chomp(my $redis_server = `which redis-server`);
unless ($redis_server && -e $redis_server && -x _) {
plan skip_all => 'redis-server not found in your PATH';
}
test_tcp
server => sub {
my $port = shift;
rewrite_redis_conf($port);
exec "redis-server", "t/redis.conf";
},
client => sub {
my $port = shift;
my $r = AnyEvent::Redis->new(
host => "127.0.0.1",
port => $port,
ref $args ? %$args : ()
);
$cb->($r, $port);
};
}
sub rewrite_redis_conf {
my $port = shift;
my $dir = $FindBin::Bin;
open my $in, "<", "t/redis.conf.base" or die $!;
open my $out, ">", "t/redis.conf" or die $!;
while (<$in>) {
s/__PORT__/$port/;
s/__DIR__/$dir/;
print $out $_;
}
}
END { unlink $_ for "t/redis.conf", "t/dump.rdb" }
1;