comparison memcached_keepalive.t @ 250:0c9f15938545

Tests: upstream keepalive tests imported.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 24 Jan 2013 02:00:03 +0400
parents
children 6cbcfb4ef7ee
comparison
equal deleted inserted replaced
249:6a0d934950bc 250:0c9f15938545
1 #!/usr/bin/perl
2
3 # (C) Maxim Dounin
4
5 # Test for memcached with keepalive.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13
14 BEGIN { use FindBin; chdir($FindBin::Bin); }
15
16 use lib 'lib';
17 use Test::Nginx;
18
19 ###############################################################################
20
21 select STDERR; $| = 1;
22 select STDOUT; $| = 1;
23
24 eval { require Cache::Memcached; };
25 plan(skip_all => 'Cache::Memcached not installed') if $@;
26
27 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(16)
28 ->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 http {
38 %%TEST_GLOBALS_HTTP%%
39
40 upstream memd {
41 server 127.0.0.1:8081;
42 keepalive 1;
43 }
44
45 upstream memd3 {
46 server 127.0.0.1:8081;
47 server 127.0.0.1:8082;
48 keepalive 1;
49 }
50
51 upstream memd4 {
52 server 127.0.0.1:8081;
53 server 127.0.0.1:8082;
54 keepalive 10;
55 }
56
57 server {
58 listen 127.0.0.1:8080;
59 server_name localhost;
60
61 location / {
62 set $memcached_key $uri;
63 memcached_pass memd;
64 }
65
66 location /next {
67 set $memcached_key $uri;
68 memcached_next_upstream not_found;
69 memcached_pass memd;
70 }
71
72 location /memd3 {
73 set $memcached_key "/";
74 memcached_pass memd3;
75 }
76
77 location /memd4 {
78 set $memcached_key "/";
79 memcached_pass memd4;
80 }
81 }
82 }
83
84 EOF
85
86 my $memhelp = `memcached -h`;
87 my @memopts1 = ();
88 my @memopts2 = ();
89
90 if ($memhelp =~ /repcached/) {
91 # repcached patches adds additional listen socket memcached
92 # that should be different too
93
94 push @memopts1, '-X', '8091';
95 push @memopts2, '-X', '8092';
96 }
97 if ($memhelp =~ /-U/) {
98 # UDP ports no longer off by default in memcached 1.2.7+
99
100 push @memopts1, '-U', '0';
101 push @memopts2, '-U', '0';
102 }
103
104 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', @memopts1);
105 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082', @memopts2);
106
107 $t->run();
108
109 $t->waitforsocket('127.0.0.1:8081')
110 or die "Unable to start memcached";
111 $t->waitforsocket('127.0.0.1:8082')
112 or die "Unable to start second memcached";
113
114 ###############################################################################
115
116 my $memd1 = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);
117 my $memd2 = Cache::Memcached->new(servers => [ '127.0.0.1:8082' ]);
118
119 $memd1->set('/', 'SEE-THIS');
120 $memd2->set('/', 'SEE-THIS');
121 $memd1->set('/big', 'X' x 1000000);
122
123 my $total = $memd1->stats()->{total}->{total_connections};
124
125 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request');
126 like(http_get('/notfound'), qr/404/, 'keepalive memcached not found');
127 like(http_get('/next'), qr/404/,
128 'keepalive not found with memcached_next_upstream');
129 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
130 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
131 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
132
133 is($memd1->stats()->{total}->{total_connections}, $total + 1,
134 'only one connection used');
135
136 # Since nginx doesn't read all data from connection in some situations (head
137 # requests, post_action, errors writing to client) we have to close such
138 # connections. Check if we really do close them.
139
140 $total = $memd1->stats()->{total}->{total_connections};
141
142 unlike(http_head('/'), qr/SEE-THIS/, 'head request');
143 like(http_get('/'), qr/SEE-THIS/, 'get after head');
144
145 is($memd1->stats()->{total}->{total_connections}, $total + 1,
146 'head request closes connection');
147
148 $total = $memd1->stats()->{total}->{total_connections};
149
150 unlike(http_head('/big'), qr/XXX/, 'big head');
151 like(http_get('/'), qr/SEE-THIS/, 'get after big head');
152
153 is($memd1->stats()->{total}->{total_connections}, $total + 1,
154 'big head request closes connection');
155
156 # two backends with maximum number of cached connections set to 1,
157 # should establish new connection on each request
158
159 $total = $memd1->stats()->{total}->{total_connections} +
160 $memd2->stats()->{total}->{total_connections};
161
162 http_get('/memd3');
163 http_get('/memd3');
164 http_get('/memd3');
165
166 is($memd1->stats()->{total}->{total_connections} +
167 $memd2->stats()->{total}->{total_connections}, $total + 3,
168 '3 connections should be established');
169
170 # two backends with maximum number of cached connections set to 10,
171 # should establish only two connections (1 per backend)
172
173 $total = $memd1->stats()->{total}->{total_connections} +
174 $memd2->stats()->{total}->{total_connections};
175
176 http_get('/memd4');
177 http_get('/memd4');
178 http_get('/memd4');
179
180 is($memd1->stats()->{total}->{total_connections} +
181 $memd2->stats()->{total}->{total_connections}, $total + 2,
182 'connection per backend');
183
184 $t->stop();
185
186 like(`grep -F '[alert]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no alerts');
187
188 ###############################################################################