1#!/usr/bin/env perl
2#
3use strict;
4use warnings;
5use Test::More;
6use FindBin qw($Bin);
7use lib "$Bin/lib";
8use Carp qw(croak);
9use MemcachedTest;
10use IO::Socket qw(AF_INET SOCK_STREAM);
11use IO::Select;
12
13if (!supports_proxy()) {
14    plan skip_all => 'proxy not enabled';
15    exit 0;
16}
17
18my $ext_path;
19
20if (!supports_extstore()) {
21    plan skip_all => 'extstore not enabled';
22    exit 0;
23}
24
25$ext_path = "/tmp/extstore.$$";
26
27my $value;
28{
29    my @chars = ("C".."Z");
30    for (1 .. 20000) {
31        $value .= $chars[rand @chars];
32    }
33}
34
35my $t = Memcached::ProxyTest->new(servers => [12081]);
36
37my $p_srv = new_memcached("-m 64 -U 0 -o ext_page_size=8,ext_wbuf_size=2,ext_threads=1,ext_item_size=512,ext_item_age=2,ext_path=$ext_path:64m,ext_max_sleep=100000,proxy_config=./t/proxyintext.lua -t 1");
38my $ps = $p_srv->sock;
39$ps->autoflush(1);
40
41$t->set_c($ps);
42#$t->accept_backends();
43
44{
45    test_basic();
46    subtest 'extstore tests', \&test_ext;
47}
48
49done_testing();
50
51sub test_basic {
52    subtest 'top level in memory mcp.internal() values' => sub {
53        $t->c_send("ms top/a 2 F1 Oa1\r\nhi\r\n");
54        $t->c_recv("HD Oa1\r\n", "set small value");
55
56        $t->c_send("mg top/a v f Oa2\r\n");
57        $t->c_recv("VA 2 f1 Oa2\r\n", "header received");
58        $t->c_recv("hi\r\n", "payload received");
59        $t->clear();
60    };
61
62    subtest 'sub level in memory mcp.internal() values' => sub {
63        plan skip_all => 'sub-rctx internal calls do not work';
64        $t->c_send("ms split/b 2 F2 Ob1\r\nho\r\n");
65        $t->c_recv("HD Ob1\r\n", "set small to subrctx");
66
67        $t->c_send("mg split/b v f Ob2\r\n");
68        $t->c_recv("VA 2 f2 Ob2\r\n", "header received");
69        $t->c_recv("ho\r\n", "payload received");
70        $t->clear();
71    };
72}
73
74# don't need tons of keys for this test as we're focusing on fetch/return
75# functionality.
76sub test_ext {
77    my $count = 20;
78    for my $c (1 .. $count) {
79        $t->c_send("ms top/$c 20000 F$c\r\n$value\r\n");
80        $t->c_recv("HD\r\n");
81
82        # sub-rctx internal calls do not work.
83        #$t->c_send("ms split/$c 20000 F$c\r\n$value\r\n");
84        #$t->c_recv("HD\r\n");
85    }
86    $t->clear();
87
88    wait_ext_flush($ps);
89
90    $t->c_send("mg top/1 f v\r\n");
91    $t->c_recv("VA 20000 f1\r\n", "header received");
92    $t->c_recv("$value\r\n", "value received");
93
94    #$t->c_send("mg split/2 f v\r\n");
95    #$t->c_recv("VA 20000 F2\r\n", "header received");
96    #$t->c_recv("$value\r\n", "value received");
97    $t->clear();
98}
99
100END {
101    unlink $ext_path if $ext_path;
102}
103