summaryrefslogtreecommitdiff
path: root/t/proxyustats.t
blob: 0d5e340fbf930e39a38127d0dd9b90d08b498410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use FindBin qw($Bin);
use lib "$Bin/lib";
use Carp qw(croak);
use MemcachedTest;
use IO::Select;
use IO::Socket qw(AF_INET SOCK_STREAM);

# TODO: possibly... set env var to a generated temp filename before starting
# the server so we can pass that in?
my $configfile = "/tmp/proxyustats.lua";

if (!supports_proxy()) {
    plan skip_all => 'proxy not enabled';
    exit 0;
}

# Set up some server sockets.
sub mock_server {
    my $port = shift;
    my $srv = IO::Socket->new(
        Domain => AF_INET,
        Type => SOCK_STREAM,
        Proto => 'tcp',
        LocalHost => '127.0.0.1',
        LocalPort => $port,
        ReusePort => 1,
        Listen => 5) || die "IO::Socket: $@";
    return $srv;
}

sub accept_backend {
    my $srv = shift;
    my $be = $srv->accept();
    $be->autoflush(1);
    ok(defined $be, "mock backend created");
    like(<$be>, qr/version/, "received version command");
    print $be "VERSION 1.0.0-mock\r\n";

    return $be;
}

# Put a version command down the pipe to ensure the socket is clear.
# client version commands skip the proxy code
sub check_version {
    my $ps = shift;
    print $ps "version\r\n";
    like(<$ps>, qr/VERSION /, "version received");
}

# A single line containing prefix, start ustat and end ustat index.
sub write_config {
    my $cmd = shift;
    open(my $fh, "> $configfile") or die "Couldn't overwrite $configfile: $!";
    print $fh $cmd;
    close($fh);
}

sub wait_reload {
    my $w = shift;
    like(<$w>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=start/, "reload started");
    like(<$w>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=done/, "reload completed");
}

my ($p_srv, $ps, $watcher);
sub restart_memcached {
    if ($p_srv) {
        $p_srv->stop();
    }
    write_config('return "a 1 0"');
    $p_srv = new_memcached('-o proxy_config=./t/proxyustats.lua');
    $ps = $p_srv->sock;
    $ps->autoflush(1);

    $watcher = $p_srv->new_sock;
    print $watcher "watch proxyevents\n";
    is(<$watcher>, "OK\r\n", "watcher enabled");
}

diag "testing failure to start";
write_config("invalid");
eval {
    $p_srv = new_memcached('-o proxy_config=./t/proxyustats.lua');
};
ok($@ && $@ =~ m/Failed to connect/, "server successfully not started");

restart_memcached();

subtest 'succeeded to allocate 1024 ustats' => sub {
    my $pfx = "a";
    my $first = 1;
    my $last = 1024;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    wait_reload($watcher);

    my $stats = mem_stats($ps, 'proxy');
    for my $i ($first..$last) {
        my $ustat = "user_" . $pfx . $i;
        is($stats->{$ustat}, 0, $ustat . " found");
    }
};

subtest 'failed to allocate 1025 ustats' => sub {
    my $pfx = "a";
    my $first = 1025;
    my $last = 1025;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    unlike(<$watcher>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=start/, "reload not started");
    unlike(<$watcher>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=done/, "reload not completed");

    restart_memcached();
};

subtest 'failed to allocate ustat at index 0' => sub {
    my $pfx = "a";
    my $first = 0;
    my $last = 0;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    unlike(<$watcher>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=start/, "reload not started");
    unlike(<$watcher>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=done/, "reload not completed");

    restart_memcached();
};


subtest 'succeeded to allocate ustat at 1024 only' => sub {
    # restart memcached to clear any ustats.
    restart_memcached();

    my $pfx = "a";
    my $first = 1024;
    my $last = 1024;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    wait_reload($watcher);

    my $stats = mem_stats($ps, 'proxy');
    while (my ($ustat, $value) = each %{$stats}) {
        if (index($ustat, "user_") == 0) {
            is($ustat, "user_a1024", "user_a1024 found");
        }
    }
};

subtest 'ustats incr/decr and perseverance over reload' => sub {
    # restart memcached to clear any ustats.
    restart_memcached();

    my $pfx = "a";
    my $first = 1;
    my $last = 3;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    wait_reload($watcher);

    print $ps "mg 1\r\n";
    is(scalar <$ps>, "HD\r\n", "mg 1 hit");
    print $ps "mg 2\r\n";
    is(scalar <$ps>, "HD\r\n", "mg 2 hit");
    print $ps "mg 2\r\n";
    is(scalar <$ps>, "HD\r\n", "mg 2 hit");

    my $stats = mem_stats($ps, 'proxy');
    is($stats->{user_a1}, 1, "user_a1 is 1");
    is($stats->{user_a2}, 4, "user_a2 is 4");
    is($stats->{user_a3}, 0, "user_a3 is 0");

    $pfx = "b";
    $first = 2;
    $last = 4;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    wait_reload($watcher);

    # subtract 2 at idx 2.
    print $ps "mg -2\r\n";
    is(scalar <$ps>, "HD\r\n", "mg -2 hit");

    $stats = mem_stats($ps, 'proxy');
    is($stats->{user_a1}, 1, "user_a1 is 1");
    is($stats->{user_b2}, 2, "user_b2 is 2");
    is($stats->{user_b3}, 0, "user_b3 is 0");
    is($stats->{user_b4}, 0, "user_b4 is 0");
};

subtest 'failed to allocate ustat longer than 128 chars' => sub {
    my $pfx = '*' x 128;
    my $first = 1;
    my $last = 1;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    unlike(<$watcher>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=start/, "reload not started");
    unlike(<$watcher>, qr/ts=(\S+) gid=\d+ type=proxy_conf status=done/, "reload not completed");

    restart_memcached();
};

subtest 'negative ustat value underflow' => sub {
    # restart memcached to clear any ustats.
    restart_memcached();

    my $pfx = "a";
    my $first = 1;
    my $last = 1;
    write_config('return "' . $pfx . ' ' . $first . ' ' . $last . '"');
    $p_srv->reload();
    wait_reload($watcher);

    print $ps "mg -1\r\n";
    is(scalar <$ps>, "HD\r\n", "mg -1 hit");

    my $stats = mem_stats($ps, 'proxy');
    isnt($stats->{user_a1}, -1, "user_a1 is not -1");
};

done_testing();

END {
    unlink $configfile;
}