Skip to content

Instantly share code, notes, and snippets.

@leedo
Created September 23, 2012 03:54
Show Gist options
  • Save leedo/3768802 to your computer and use it in GitHub Desktop.
Save leedo/3768802 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.10;
use Test::TCP;
use AnyEvent::Handle;
say "hit Ctrl-C to cancel";
my $cv = AE::cv;
my $s = AE::signal INT => sub { unlink "/tmp/$_" for qw/redis.conf dump.rdb/; $cv->send };
test_tcp
server => sub {
my $port = shift;
my $conf = join "", <DATA>;
$conf =~ s/__PORT__/$port/;
open my $fh, '>', '/tmp/redis.conf';
print $fh $conf;
exec "redis-server /tmp/redis.conf";
},
client => sub {
my ($port, $pid) = @_;
my $h = AnyEvent::Handle->new(
connect => ["localhost", $port],
on_error => sub { warn "handle got error" },
on_eof => sub { warn "handle got eof" },
on_connect => sub {
my $h = shift;
$h->push_write("*1\r\n\$4\r\nINFO\r\n");
$h->push_read(regex => qr/^\$(\d+)\r\n/, sub {
$h->push_read(chunk => $1, sub {
my ($v) = $_[1] =~ /redis_version:(.+)/;
say "killing server $v";
kill TERM => $pid;
waitpid $pid, 0;
# the connection sticks around forever as CLOSE_WAIT in lsof -Pi:$port
# Should on_eof or on_error get triggered immediately?
# The only way I can trigger on_error is by attempting a push_(read|write)
my $t; $t = AE::timer 0, 1, sub { say qx{lsof -Pi:$port}; $t };
});
});
},
);
$cv->recv;
};
__DATA__
daemonize no
port __PORT__
timeout 300
save 900 1
save 300 10
save 60 10000
dbfilename /tmp/dump.rdb
loglevel debug
logfile stdout
databases 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment