comparison t/memcached-keepalive.t @ 6:bef88ba0b378

Keepalive: distinguish between upstream servers by default. Option 'single' may be used if single pool of connections desired for some reason.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 24 Oct 2008 02:03:39 +0400
parents ca955a7f651b
children 15530a464dba
comparison
equal deleted inserted replaced
5:e7d1b49e0611 6:bef88ba0b378
18 select STDOUT; $| = 1; 18 select STDOUT; $| = 1;
19 19
20 eval { require Cache::Memcached; }; 20 eval { require Cache::Memcached; };
21 plain(skip_all => 'Cache::Memcached not installed') if $@; 21 plain(skip_all => 'Cache::Memcached not installed') if $@;
22 22
23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(7) 23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(10)
24 ->write_file_expand('nginx.conf', <<'EOF'); 24 ->write_file_expand('nginx.conf', <<'EOF');
25 25
26 master_process off; 26 master_process off;
27 daemon off; 27 daemon off;
28 28
37 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp; 37 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp;
38 proxy_temp_path %%TESTDIR%%/proxy_temp; 38 proxy_temp_path %%TESTDIR%%/proxy_temp;
39 39
40 upstream memd { 40 upstream memd {
41 server 127.0.0.1:8081; 41 server 127.0.0.1:8081;
42 keepalive; 42 keepalive 1;
43 }
44
45 upstream memd2 {
46 server 127.0.0.1:8081;
47 server 127.0.0.1:8082;
48 keepalive 1 single;
49 }
50
51 upstream memd3 {
52 server 127.0.0.1:8081;
53 server 127.0.0.1:8082;
54 keepalive 1;
55 }
56
57 upstream memd4 {
58 server 127.0.0.1:8081;
59 server 127.0.0.1:8082;
60 keepalive 10;
43 } 61 }
44 62
45 server { 63 server {
46 listen localhost:8080; 64 listen localhost:8080;
47 server_name localhost; 65 server_name localhost;
54 location /next { 72 location /next {
55 set $memcached_key $uri; 73 set $memcached_key $uri;
56 memcached_next_upstream not_found; 74 memcached_next_upstream not_found;
57 memcached_pass memd; 75 memcached_pass memd;
58 } 76 }
77
78 location /memd2 {
79 set $memcached_key "/";
80 memcached_pass memd2;
81 }
82
83 location /memd3 {
84 set $memcached_key "/";
85 memcached_pass memd3;
86 }
87
88 location /memd4 {
89 set $memcached_key "/";
90 memcached_pass memd4;
91 }
59 } 92 }
60 } 93 }
61 94
62 EOF 95 EOF
63 96
64 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081'); 97 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081');
98 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082');
65 $t->run(); 99 $t->run();
66 100
67 ############################################################################### 101 ###############################################################################
68 102
69 my $memd = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]); 103 my $memd1 = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);
70 $memd->set('/', 'SEE-THIS'); 104 my $memd2 = Cache::Memcached->new(servers => [ '127.0.0.1:8082' ]);
71 105
72 my $total = $memd->stats()->{total}->{total_connections}; 106 $memd1->set('/', 'SEE-THIS');
107 $memd2->set('/', 'SEE-THIS');
108
109 my $total = $memd1->stats()->{total}->{total_connections};
73 110
74 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request'); 111 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request');
75 like(http_get('/notfound'), qr/404/, 'keepalive memcached not found'); 112 like(http_get('/notfound'), qr/404/, 'keepalive memcached not found');
76 like(http_get('/next'), qr/404/, 113 like(http_get('/next'), qr/404/,
77 'keepalive not found with memcached_next_upstream'); 114 'keepalive not found with memcached_next_upstream');
78 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again'); 115 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
79 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again'); 116 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
80 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again'); 117 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
81 118
82 is($memd->stats()->{total}->{total_connections}, $total + 1, 'keepalive used'); 119 is($memd1->stats()->{total}->{total_connections}, $total + 1,
120 'only one connection used');
121
122 # two backends with 'single' option - should establish only one connection
123
124 $total = $memd1->stats()->{total}->{total_connections} +
125 $memd2->stats()->{total}->{total_connections};
126
127 http_get('/memd2');
128 http_get('/memd2');
129 http_get('/memd2');
130
131 is($memd1->stats()->{total}->{total_connections} +
132 $memd2->stats()->{total}->{total_connections}, $total + 1,
133 'only one connection with two backends and single');
134
135 $total = $memd1->stats()->{total}->{total_connections} +
136 $memd2->stats()->{total}->{total_connections};
137
138 # two backends without 'single' option and maximum number of cached
139 # connections set to 1 - should establish new connection on each request
140
141 http_get('/memd3');
142 http_get('/memd3');
143 http_get('/memd3');
144
145 is($memd1->stats()->{total}->{total_connections} +
146 $memd2->stats()->{total}->{total_connections}, $total + 3,
147 '3 connections should be established');
148
149 # two backends without 'single' option and maximum number of cached
150 # connections set to 10 - should establish only two connections (1 per backend)
151
152 $total = $memd1->stats()->{total}->{total_connections} +
153 $memd2->stats()->{total}->{total_connections};
154
155 http_get('/memd4');
156 http_get('/memd4');
157 http_get('/memd4');
158
159 is($memd1->stats()->{total}->{total_connections} +
160 $memd2->stats()->{total}->{total_connections}, $total + 2,
161 'connection per backend');
83 162
84 ############################################################################### 163 ###############################################################################