#!/usr/bin/perl
# Test connection memory limits.

use strict;
use warnings;
use Test::More;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;

my $server = new_memcached('-o resp_obj_mem_limit=1,read_buf_mem_limit=1 -t 32 -R 500');
my $sock = $server->sock;

# The minimum limit is 1 megabyte. This is then split between each of the
# worker threads, which ends up being a lot of memory for a quick test.
# So we use a high worker thread count to split them down more.

{
    # easiest method is an ascii multiget.
    my $key = 'foo';
    my @keys = ();
    for (1 .. 500) {
        push(@keys, $key);
    }
    my $keylist = join(' ', @keys);
    chop($keylist);
    print $sock "get ", $keylist, "\r\n";
    like(<$sock>, qr/SERVER_ERROR out of memory writing/, "OOM'ed multiget");
    my $stats = mem_stats($sock);
    isnt(0, $stats->{'response_obj_oom'}, 'non zero response object OOM counter: ' . $stats->{'response_obj_oom'});
}

{
    # stacked ascii responses, which should cause a connection close.
    my $s = $server->new_sock;
    my @keys = ();
    for (1 .. 500) {
        push(@keys, "mg foo v\r\n");
    }
    my $cmd = join('', @keys);
    print $s $cmd;
    ok(!defined <$s>, 'sock disconnected after overflow');

    my $stats = mem_stats($sock);
    cmp_ok($stats->{'response_obj_oom'}, '>', 1, 'another OOM recorded');
}

SKIP: {
    skip "read_buf test borks on travis CI. don't have patience to fix.", 1;
    # test read buffer limits.
    # spam connections with a partial command.. a set in this case is easy.
    my @conns = ();
    for (1 .. 128) {
        my $s = $server->new_sock;
        #if (!defined($s)) {
            # Don't need the spam of every individual conn made.
            #}
        ok(defined($s), 'new conn made');
        # Partial set command, should attach a read buffer but not release it.
        print $s "set foo 0 0 2\r\n";
        push(@conns, $s);
    }
    # Close everything so we have a red buffer available to get stats.
    for my $s (@conns) {
        $s->close();
    }
    my $stats = mem_stats($sock);
    cmp_ok($stats->{'read_buf_oom'}, '>', 1, 'read buffer based OOM recorded');
}

done_testing();
