public-plugins
MemcachedTest
Toolbar
Package variables
Globals (from "use vars" definitions)
@EXPORT = qw(new_memcached sleep mem_get_is mem_gets mem_gets_is mem_stats free_port)
Privates (from "my" definitions)
$builddir = getcwd
Included modules
Carp qw ( croak )
Cwd
Exporter ' import '
IO::Socket::INET
IO::Socket::UNIX
Synopsis
No synopsis!
Description
No description!
Methods
free_port | No description | Code |
mem_get_is | No description | Code |
mem_gets | No description | Code |
mem_gets_is | No description | Code |
mem_stats | No description | Code |
new_memcached | No description | Code |
sleep | No description | Code |
supports_udp | No description | Code |
Methods description
None available.
Methods code
sub free_port
{ my $type = shift || "tcp";
my $sock;
my $port;
while (!$sock) {
$port = int(rand(20000)) + 30000;
$sock = IO::Socket::INET->new(LocalAddr => '127.0.0.1',
LocalPort => $port,
Proto => $type,
ReuseAddr => 1);
}
return $port; } |
sub mem_get_is
{ my ($sock_opts, $key, $val, $msg) = @_;
my $opts = ref $sock_opts eq "HASH" ? $sock_opts : {};
my $sock = ref $sock_opts eq "HASH" ? $opts->{sock} : $sock_opts;
my $expect_flags = $opts->{flags} || 0;
my $dval = defined $val ? "'$val'" : "<undef>";
$msg ||= "$key == $dval";
print $sock "get $key\r\n";
if (! defined $val) {
my $line = scalar <$sock>;
if ($line =~ /^VALUE/) {
$line .= scalar(<$sock>) . scalar(<$sock>);
}
Test::More::is($line, "END\r\n", $msg);
} else {
my $len = length($val);
my $body = scalar(<$sock>);
my $expected = "VALUE $key $expect_flags $len\r\n$val\r\nEND\r\n";
if (!$body || $body =~ /^END/) {
Test::More::is($body, $expected, $msg);
return;
}
$body .= scalar(<$sock>) . scalar(<$sock>);
Test::More::is($body, $expected, $msg);
} } |
sub mem_gets
{ my ($sock_opts, $key) = @_;
my $opts = ref $sock_opts eq "HASH" ? $sock_opts : {};
my $sock = ref $sock_opts eq "HASH" ? $opts->{sock} : $sock_opts;
my $val;
my $expect_flags = $opts->{flags} || 0;
print $sock "gets $key\r\n";
my $response = <$sock>;
if ($response =~ /^END/) {
return "NOT_FOUND";
}
else
{
$response =~ /VALUE (.*) (\d+) (\d+) (\d+)/;
my $flags = $2;
my $len = $3;
my $identifier = $4;
read $sock, $val , $len;
$_ = <$sock>;
$_ = <$sock>;
return ($identifier,$val);
} } |
sub mem_gets_is
{ my ($sock_opts, $identifier, $key, $val, $msg) = @_;
my $opts = ref $sock_opts eq "HASH" ? $sock_opts : {};
my $sock = ref $sock_opts eq "HASH" ? $opts->{sock} : $sock_opts;
my $expect_flags = $opts->{flags} || 0;
my $dval = defined $val ? "'$val'" : "<undef>";
$msg ||= "$key == $dval";
print $sock "gets $key\r\n";
if (! defined $val) {
my $line = scalar <$sock>;
if ($line =~ /^VALUE/) {
$line .= scalar(<$sock>) . scalar(<$sock>);
}
Test::More::is($line, "END\r\n", $msg);
} else {
my $len = length($val);
my $body = scalar(<$sock>);
my $expected = "VALUE $key $expect_flags $len $identifier\r\n$val\r\nEND\r\n";
if (!$body || $body =~ /^END/) {
Test::More::is($body, $expected, $msg);
return;
}
$body .= scalar(<$sock>) . scalar(<$sock>);
Test::More::is($body, $expected, $msg);
} } |
sub mem_stats
{ my ($sock, $type) = @_;
$type = $type ? " $type" : "";
print $sock "stats$type\r\n";
my $stats = {};
while (<$sock>) {
last if /^(\.|END)/;
/^STAT (\S+) (\d+)/;
$stats->{$1} = $2;
}
return $stats; } |
sub new_memcached
{ my ($args, $passed_port) = @_;
my $port = $passed_port || free_port();
my $udpport = free_port("udp");
$args .= " -p $port";
if (supports_udp()) {
$args .= " -U $udpport";
}
if ($< == 0) {
$args .= " -u root";
}
my $childpid = fork();
my $exe = "$builddir/memcached-debug";
croak("memcached binary doesn't exist. Haven't run 'make' ?\n") unless -e $exe;
croak("memcached binary not executable\n") unless -x _;
unless ($childpid) {
exec "$exe $args";
exit; }
if ($args =~ /-s (\S+)/) {
sleep 1;
my $filename = $1;
my $conn = IO::Socket::UNIX->new(Peer => $filename) ||
croak("Failed to connect to unix domain socket: $! '$filename'");
return Memcached::Handle->new(pid => $childpid,
conn => $conn,
domainsocket => $filename,
port => $port);
}
for (1..20) {
my $conn = IO::Socket::INET->new(PeerAddr => "127.0.0.1:$port");
if ($conn) {
return Memcached::Handle->new(pid => $childpid,
conn => $conn,
udpport => $udpport,
port => $port);
}
select undef, undef, undef, 0.10;
}
croak("Failed to startup/connect to memcached server.");
}
package Memcached::Handle; } |
sub sleep
{ my $n = shift;
select undef, undef, undef, $n; } |
sub supports_udp
{ my $output = `$builddir/memcached-debug -h`; return 0 if $output =~ /^memcached 1\.1\./;
return 1; } |
General documentation
No general documentation available.