9
@EXPORT = qw(new_memcached sleep mem_get_is mem_stats free_port);
13
select undef, undef, undef, $n;
17
my ($sock, $type) = @_;
18
$type = $type ? " $type" : "";
19
print $sock "stats$type\r\n";
31
# works on single-line values only. no newlines in value.
32
my ($sock_opts, $key, $val, $msg) = @_;
33
my $opts = ref $sock_opts eq "HASH" ? $sock_opts : {};
34
my $sock = ref $sock_opts eq "HASH" ? $opts->{sock} : $sock_opts;
36
my $expect_flags = $opts->{flags} || 0;
37
my $dval = defined $val ? "'$val'" : "<undef>";
38
$msg ||= "$key == $dval";
40
print $sock "get $key\r\n";
42
my $line = scalar <$sock>;
43
if ($line =~ /^VALUE/) {
44
$line .= scalar(<$sock>) . scalar(<$sock>);
46
Test::More::is($line, "END\r\n", $msg);
48
my $len = length($val);
49
my $body = scalar(<$sock>);
50
my $expected = "VALUE $key $expect_flags $len\r\n$val\r\nEND\r\n";
51
if (!$body || $body =~ /^END/) {
52
Test::More::is($body, $expected, $msg);
55
$body .= scalar(<$sock>) . scalar(<$sock>);
56
Test::More::is($body, $expected, $msg);
61
my $type = shift || "tcp";
65
$port = int(rand(20000)) + 30000;
66
$sock = IO::Socket::INET->new(LocalAddr => '127.0.0.1',
75
my $output = `$Bin/../memcached-debug -h`;
76
return 0 if $output =~ /^memcached 1\.1\./;
81
my $args = shift || "";
82
my $port = free_port();
83
my $udpport = free_port("udp");
86
$args .= " -U $udpport";
91
my $childpid = fork();
93
my $exe = "$Bin/../memcached-debug";
94
croak("memcached binary doesn't exist. Haven't run 'make' ?\n") unless -e $exe;
95
croak("memcached binary not executable\n") unless -x _;
99
exit; # never gets here.
103
my $conn = IO::Socket::INET->new(PeerAddr => "127.0.0.1:$port");
105
return Memcached::Handle->new(pid => $childpid,
110
select undef, undef, undef, 0.10;
112
croak("Failed to startup/connect to memcached server.");
116
############################################################################
117
package Memcached::Handle;
119
my ($class, %params) = @_;
120
return bless \%params, $class;
125
kill 9, $self->{pid};
128
sub port { $_[0]{port} }
129
sub udpport { $_[0]{udpport} }
133
return $self->{conn} if $self->{conn} && getpeername($self->{conn});
134
return $self->new_sock;
139
return IO::Socket::INET->new(PeerAddr => "127.0.0.1:$self->{port}");
144
return IO::Socket::INET->new(PeerAddr => '127.0.0.1',
145
PeerPort => $self->{udpport},
147
LocalAddr => '127.0.0.1',
148
LocalPort => MemcachedTest::free_port('udp'),