forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proxy: match, token = req:match_res(res)
If req has 'k' and/or 'O' flags, match against the result object.
- Loading branch information
Showing
6 changed files
with
366 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function mcp_config_pools() | ||
mcp.backend_connect_timeout(60) | ||
mcp.backend_read_timeout(60) | ||
mcp.backend_retry_timeout(60) | ||
local b1 = mcp.backend('b1', '127.0.0.1', 12171) | ||
return mcp.pool({b1}) | ||
end | ||
|
||
function mcp_config_routes(p) | ||
local fg = mcp.funcgen_new() | ||
local h = fg:new_handle(p) | ||
fg:ready({ | ||
n = "match", f = function(rctx) | ||
return function(r) | ||
local res = rctx:enqueue_and_wait(r, h) | ||
local match, token = r:match_res(res) | ||
if match then | ||
mcp.log("match succeeded") | ||
else | ||
if token then | ||
mcp.log("match failed: " .. token) | ||
else | ||
mcp.log("match failed: no token") | ||
end | ||
end | ||
return res | ||
end | ||
end | ||
}) | ||
mcp.attach(mcp.CMD_MG, fg) | ||
mcp.attach(mcp.CMD_MS, fg) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
#!/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::Socket qw(AF_INET SOCK_STREAM); | ||
use IO::Select; | ||
use Data::Dumper qw/Dumper/; | ||
|
||
if (!supports_proxy()) { | ||
plan skip_all => 'proxy not enabled'; | ||
exit 0; | ||
} | ||
|
||
my $t = Memcached::ProxyTest->new(servers => [12171]); | ||
|
||
my $p_srv = new_memcached('-o proxy_config=./t/proxymatch.lua -t 1'); | ||
my $ps = $p_srv->sock; | ||
$ps->autoflush(1); | ||
|
||
$t->set_c($ps); | ||
$t->accept_backends(); | ||
|
||
my $w = $p_srv->new_sock; | ||
print $w "watch proxyuser proxyevents\n"; | ||
is(<$w>, "OK\r\n", "watcher enabled"); | ||
|
||
subtest 'ms no tokens' => sub { | ||
my $cmd = "ms foo 2\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
# successful tests. | ||
subtest 'ms with k' => sub { | ||
my $cmd = "ms foo 2 k\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kfoo\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with O' => sub { | ||
my $cmd = "ms foo 2 k O1234\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kfoo O1234\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with k and O' => sub { | ||
my $cmd = "ms foo 2 k O4321\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kfoo O4321\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with O and k' => sub { | ||
my $cmd = "ms foo 2 O9876 k\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD O9876 kfoo\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
# ensure the parser works with unrelated tokens at the beginning/end of the string | ||
subtest 'ms with O and k and c' => sub { | ||
my $cmd = "ms foo 2 T99 O9876 k c\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD O9876 kfoo c2\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
# test some failures. | ||
subtest 'ms with empty k' => sub { | ||
my $cmd = "ms foo 2 k\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD k\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with empty O' => sub { | ||
my $cmd = "ms foo 2 O\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD O\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with empty O c' => sub { | ||
my $cmd = "ms foo 2 O c\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD O c2\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with wrong k' => sub { | ||
my $cmd = "ms foo 2 k\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kbar\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with wrong len k' => sub { | ||
my $cmd = "ms foo 2 k\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kfoob\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with wrong O' => sub { | ||
my $cmd = "ms foo 2 O1234\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD O4321\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with wrong len O' => sub { | ||
my $cmd = "ms foo 2 O1234\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD O43210\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with right k wrong O' => sub { | ||
my $cmd = "ms foo 2 k O1234\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kfoo O5678\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'ms with right O wrong k' => sub { | ||
my $cmd = "ms foo 2 k O1234\r\nok\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD kbar O1234\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
# don't need to test mg as hard since the match code is the same. just ensure | ||
# the rline handling is correct for both code paths. | ||
|
||
# mg successful tests | ||
subtest 'mg with k' => sub { | ||
my $cmd = "mg foo t k\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD t99 kfoo\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'mg with O' => sub { | ||
my $cmd = "mg foo t O1234\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD t99 O1234\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'mg with O and k' => sub { | ||
my $cmd = "mg foo t O1234 k c\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD t98 O1234 kfoo c2\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match succeeded/); | ||
$t->clear(); | ||
}; | ||
|
||
# mg failures | ||
subtest 'mg with wrong k' => sub { | ||
my $cmd = "mg foo t k\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD t97 kbar\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'mg with wrong O' => sub { | ||
my $cmd = "mg foo t O1234 c\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD t97 O4321 c2\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
subtest 'mg no matchable tokens' => sub { | ||
my $cmd = "mg foo t c\r\n"; | ||
$t->c_send($cmd); | ||
$t->be_recv_c(0); | ||
$t->be_send(0, "HD t97 c2\r\n"); | ||
$t->c_recv_be(); | ||
like(<$w>, qr/match failed: /); | ||
$t->clear(); | ||
}; | ||
|
||
done_testing(); |