comparison upstream_hash.t @ 952:e9064d691790

Tests: converted tests to run in parallel.
author Andrey Zelenkov <zelenkov@nginx.com>
date Tue, 21 Jun 2016 16:39:13 +0300
parents 6bb1f2ccd386
children 882267679006
comparison
equal deleted inserted replaced
951:9361c7eddfc1 952:e9064d691790
36 http { 36 http {
37 %%TEST_GLOBALS_HTTP%% 37 %%TEST_GLOBALS_HTTP%%
38 38
39 upstream u { 39 upstream u {
40 hash $arg_a; 40 hash $arg_a;
41 server 127.0.0.1:8081; 41 server 127.0.0.1:%%PORT_1%%;
42 server 127.0.0.1:8082; 42 server 127.0.0.1:%%PORT_2%%;
43 server 127.0.0.1:8083; 43 server 127.0.0.1:%%PORT_3%%;
44 } 44 }
45 45
46 upstream u2 { 46 upstream u2 {
47 hash $arg_a; 47 hash $arg_a;
48 server 127.0.0.1:8081; 48 server 127.0.0.1:%%PORT_1%%;
49 server 127.0.0.1:8083; 49 server 127.0.0.1:%%PORT_3%%;
50 } 50 }
51 51
52 upstream cw { 52 upstream cw {
53 hash $arg_a consistent; 53 hash $arg_a consistent;
54 server 127.0.0.1:8081; 54 server 127.0.0.1:%%PORT_1%%;
55 server 127.0.0.1:8082; 55 server 127.0.0.1:%%PORT_2%%;
56 server 127.0.0.1:8083 weight=10; 56 server 127.0.0.1:%%PORT_3%% weight=10;
57 } 57 }
58 58
59 upstream cw2 { 59 upstream cw2 {
60 hash $arg_a consistent; 60 hash $arg_a consistent;
61 server 127.0.0.1:8081; 61 server 127.0.0.1:%%PORT_1%%;
62 server 127.0.0.1:8083 weight=10; 62 server 127.0.0.1:%%PORT_3%% weight=10;
63 } 63 }
64 64
65 upstream c { 65 upstream c {
66 hash $arg_a consistent; 66 hash $arg_a consistent;
67 server 127.0.0.1:8081; 67 server 127.0.0.1:%%PORT_1%%;
68 server 127.0.0.1:8082; 68 server 127.0.0.1:%%PORT_2%%;
69 server 127.0.0.1:8083; 69 server 127.0.0.1:%%PORT_3%%;
70 } 70 }
71 71
72 upstream c2 { 72 upstream c2 {
73 hash $arg_a consistent; 73 hash $arg_a consistent;
74 server 127.0.0.1:8081; 74 server 127.0.0.1:%%PORT_1%%;
75 server 127.0.0.1:8083; 75 server 127.0.0.1:%%PORT_3%%;
76 } 76 }
77 77
78 upstream bad { 78 upstream bad {
79 hash $arg_a; 79 hash $arg_a;
80 server 127.0.0.1:8081; 80 server 127.0.0.1:%%PORT_1%%;
81 server 127.0.0.1:8084; 81 server 127.0.0.1:%%PORT_4%%;
82 } 82 }
83 83
84 upstream cbad { 84 upstream cbad {
85 hash $arg_a consistent; 85 hash $arg_a consistent;
86 server 127.0.0.1:8081; 86 server 127.0.0.1:%%PORT_1%%;
87 server 127.0.0.1:8084; 87 server 127.0.0.1:%%PORT_4%%;
88 } 88 }
89 89
90 server { 90 server {
91 listen 127.0.0.1:8080; 91 listen 127.0.0.1:%%PORT_0%%;
92 server_name localhost; 92 server_name localhost;
93 93
94 location / { 94 location / {
95 proxy_pass http://u; 95 proxy_pass http://u;
96 } 96 }
120 proxy_next_upstream http_502; 120 proxy_next_upstream http_502;
121 } 121 }
122 } 122 }
123 123
124 server { 124 server {
125 listen 127.0.0.1:8081; 125 listen 127.0.0.1:%%PORT_1%%;
126 listen 127.0.0.1:8082; 126 listen 127.0.0.1:%%PORT_2%%;
127 listen 127.0.0.1:8083; 127 listen 127.0.0.1:%%PORT_3%%;
128 server_name localhost; 128 server_name localhost;
129 129
130 add_header X-Port $server_port; 130 add_header X-Port $server_port;
131 131
132 location / { 132 location / {
133 return 204; 133 return 204;
134 } 134 }
135 135
136 location /502 { 136 location /502 {
137 if ($server_port = 8083) { 137 if ($server_port = %%PORT_3%%) {
138 return 502; 138 return 502;
139 } 139 }
140 return 204; 140 return 204;
141 } 141 }
142 } 142 }
143 143
144 server { 144 server {
145 listen 127.0.0.1:8084; 145 listen 127.0.0.1:%%PORT_4%%;
146 server_name localhost; 146 server_name localhost;
147 return 444; 147 return 444;
148 } 148 }
149 } 149 }
150 150
152 152
153 $t->run(); 153 $t->run();
154 154
155 ############################################################################### 155 ###############################################################################
156 156
157 my ($p1, $p2, $p3) = (port(1), port(2), port(3));
158
157 # Only requests for absent peer are moved to other peers if hash is consistent. 159 # Only requests for absent peer are moved to other peers if hash is consistent.
158 # Check this by comparing two upstreams with different number of peers. 160 # Check this by comparing two upstreams with different number of peers.
159 161
160 ok(!cmp_peers([iter('/', 20)], [iter('/2', 20)], 8082), 'inconsistent'); 162 ok(!cmp_peers([iter('/', 20)], [iter('/2', 20)], $p2), 'inconsistent');
161 ok(cmp_peers([iter('/c', 20)], [iter('/c2', 20)], 8082), 'consistent'); 163 ok(cmp_peers([iter('/c', 20)], [iter('/c2', 20)], $p2), 'consistent');
162 ok(cmp_peers([iter('/cw', 20)], [iter('/cw2', 20)], 8082), 'consistent weight'); 164 ok(cmp_peers([iter('/cw', 20)], [iter('/cw2', 20)], $p2), 'consistent weight');
163 165
164 like(many('/?a=1', 10), qr/808\d: 10/, 'stable hash'); 166 like(many('/?a=1', 10), qr/($p1|$p2|$p3): 10/, 'stable hash');
165 like(many('/c?a=1', 10), qr/808\d: 10/, 'stable hash - consistent'); 167 like(many('/c?a=1', 10), qr/($p1|$p2|$p3): 10/, 'stable hash - consistent');
166 168
167 my @res = iter('/', 10); 169 my @res = iter('/', 10);
168 170
169 is(@res, 10, 'all hashed peers'); 171 is(@res, 10, 'all hashed peers');
170 172
171 @res = grep { $_ != 8083 } @res; 173 @res = grep { $_ != $p3 } @res;
172 my @res2 = iter('/502', 10); 174 my @res2 = iter('/502', 10);
173 175
174 is_deeply(\@res, \@res2, 'no proxy_next_upstream'); 176 is_deeply(\@res, \@res2, 'no proxy_next_upstream');
175 isnt(@res2, 10, 'no proxy_next_upstream peers'); 177 isnt(@res2, 10, 'no proxy_next_upstream peers');
176 178
177 is(iter('/pnu/502', 10), 10, 'proxy_next_upstream peers'); 179 is(iter('/pnu/502', 10), 10, 'proxy_next_upstream peers');
178 180
179 @res = grep { $_ == 8081 } iter('/bad', 20); 181 @res = grep { $_ == $p1 } iter('/bad', 20);
180 is(@res, 20, 'all hashed peers - bad'); 182 is(@res, 20, 'all hashed peers - bad');
181 183
182 @res = grep { $_ == 8081 } iter('/cbad', 20); 184 @res = grep { $_ == $p1 } iter('/cbad', 20);
183 is(@res, 20, 'all hashed peers - bad consistent'); 185 is(@res, 20, 'all hashed peers - bad consistent');
184 186
185 ############################################################################### 187 ###############################################################################
186 188
187 # Returns true if two arrays follow consistency, i.e., they may only differ 189 # Returns true if two arrays follow consistency, i.e., they may only differ