Blame t/flush-all.t

Packit 4e8bc4
#!/usr/bin/perl
Packit 4e8bc4
Packit 4e8bc4
use strict;
Packit 4e8bc4
use Test::More tests => 26;
Packit 4e8bc4
use FindBin qw($Bin);
Packit 4e8bc4
use lib "$Bin/lib";
Packit 4e8bc4
use MemcachedTest;
Packit 4e8bc4
Packit 4e8bc4
my $server = new_memcached();
Packit 4e8bc4
my $sock = $server->sock;
Packit 4e8bc4
my $expire;
Packit 4e8bc4
Packit 4e8bc4
print $sock "set foo 0 0 6\r\nfooval\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo");
Packit 4e8bc4
Packit 4e8bc4
mem_get_is($sock, "foo", "fooval");
Packit 4e8bc4
print $sock "flush_all\r\n";
Packit 4e8bc4
is(scalar <$sock>, "OK\r\n", "did flush_all");
Packit 4e8bc4
mem_get_is($sock, "foo", undef);
Packit 4e8bc4
Packit 4e8bc4
# Test flush_all with zero delay.
Packit 4e8bc4
print $sock "set foo 0 0 6\r\nfooval\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo");
Packit 4e8bc4
Packit 4e8bc4
mem_get_is($sock, "foo", "fooval");
Packit 4e8bc4
print $sock "flush_all 0\r\n";
Packit 4e8bc4
is(scalar <$sock>, "OK\r\n", "did flush_all");
Packit 4e8bc4
mem_get_is($sock, "foo", undef);
Packit 4e8bc4
Packit 4e8bc4
# check that flush_all doesn't blow away items that immediately get set
Packit 4e8bc4
print $sock "set foo 0 0 3\r\nnew\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo = 'new'");
Packit 4e8bc4
mem_get_is($sock, "foo", 'new');
Packit 4e8bc4
Packit 4e8bc4
# and the other form, specifying a flush_all time...
Packit 4e8bc4
my $expire = time() + 2;
Packit 4e8bc4
print $sock "flush_all $expire\r\n";
Packit 4e8bc4
is(scalar <$sock>, "OK\r\n", "did flush_all in future");
Packit 4e8bc4
Packit 4e8bc4
print $sock "set foo 0 0 4\r\n1234\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo = '1234'");
Packit 4e8bc4
mem_get_is($sock, "foo", '1234');
Packit 4e8bc4
sleep(5);
Packit 4e8bc4
mem_get_is($sock, "foo", undef);
Packit 4e8bc4
Packit 4e8bc4
print $sock "set foo 0 0 5\r\n12345\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo = '12345'");
Packit 4e8bc4
mem_get_is($sock, "foo", '12345');
Packit 4e8bc4
print $sock "flush_all 86400\r\n";
Packit 4e8bc4
is(scalar <$sock>, "OK\r\n", "did flush_all for far future");
Packit 4e8bc4
# Check foo still exists.
Packit 4e8bc4
mem_get_is($sock, "foo", '12345');
Packit 4e8bc4
print $sock "set foo2 0 0 5\r\n54321\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo2 = '54321'");
Packit 4e8bc4
mem_get_is($sock, "foo", '12345');
Packit 4e8bc4
mem_get_is($sock, "foo2", '54321');
Packit 4e8bc4
Packit 4e8bc4
# Test -F option which disables flush_all
Packit 4e8bc4
$server = new_memcached('-F');
Packit 4e8bc4
$sock = $server->sock;
Packit 4e8bc4
Packit 4e8bc4
print $sock "set foo 0 0 7\r\nfooval2\r\n";
Packit 4e8bc4
is(scalar <$sock>, "STORED\r\n", "stored foo");
Packit 4e8bc4
Packit 4e8bc4
mem_get_is($sock, "foo", "fooval2");
Packit 4e8bc4
print $sock "flush_all\r\n";
Packit 4e8bc4
is(scalar <$sock>, "CLIENT_ERROR flush_all not allowed\r\n", "flush_all was not allowed");
Packit 4e8bc4
mem_get_is($sock, "foo", "fooval2");
Packit 4e8bc4
Packit 4e8bc4
# Test that disabling CAS makes flush_all less accurate.
Packit 4e8bc4
# Due to lock ordering issues we can no longer evict items newer than
Packit 4e8bc4
# oldest_live, so we rely on the CAS counter for an exact cliff. So disabling
Packit 4e8bc4
# CAS now means all items set in the same second will fail to set.
Packit 4e8bc4
$server = new_memcached('-C');
Packit 4e8bc4
$sock = $server->sock;
Packit 4e8bc4
Packit 4e8bc4
my $failed_nocas = 0;
Packit 4e8bc4
# Running this 100,000 times failed the test a handful of times. 50 tries
Packit 4e8bc4
# should be enough.
Packit 4e8bc4
for (1..50) {
Packit 4e8bc4
    print $sock "flush_all 0\r\n";
Packit 4e8bc4
    my $foo = scalar <$sock>;
Packit 4e8bc4
    print $sock "set foo 0 0 3\r\nnew\r\n";
Packit 4e8bc4
    $foo = scalar <$sock>;
Packit 4e8bc4
    print $sock "get foo\r\n";
Packit 4e8bc4
    my $line = scalar <$sock>;
Packit 4e8bc4
    if ($line =~ /^VALUE/) {
Packit 4e8bc4
        $line = scalar <$sock>;
Packit 4e8bc4
        $line = scalar <$sock>;
Packit 4e8bc4
        print STDERR "Succeeded!!!\n";
Packit 4e8bc4
        next;
Packit 4e8bc4
    } elsif ($line =~ /^END/) {
Packit 4e8bc4
        $failed_nocas++;
Packit 4e8bc4
        last;
Packit 4e8bc4
    }
Packit 4e8bc4
}
Packit 4e8bc4
is($failed_nocas, 1, "failed to set value after flush with no CAS at least once");