comparison t/memcached_keepalive.t @ 47:5cab730994f3 draft

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