comparison upstream_hash.t @ 1481:06fbf6269f38

Tests: upstream hash tests for round-robin fallback on empty key.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 28 May 2019 16:25:34 +0300
parents 766bcbb632ee
children 5ac6efbe5552
comparison
equal deleted inserted replaced
1480:d23d959713b5 1481:06fbf6269f38
20 ############################################################################### 20 ###############################################################################
21 21
22 select STDERR; $| = 1; 22 select STDERR; $| = 1;
23 select STDOUT; $| = 1; 23 select STDOUT; $| = 1;
24 24
25 my $t = Test::Nginx->new()->has(qw/http proxy rewrite upstream_hash/)->plan(13); 25 my $t = Test::Nginx->new()->has(qw/http proxy rewrite upstream_hash/)->plan(15);
26 26
27 $t->write_file_expand('nginx.conf', <<'EOF'); 27 $t->write_file_expand('nginx.conf', <<'EOF');
28 28
29 %%TEST_GLOBALS%% 29 %%TEST_GLOBALS%%
30 30
164 164
165 $t->run(); 165 $t->run();
166 166
167 ############################################################################### 167 ###############################################################################
168 168
169 my ($p1, $p2, $p3) = (port(8081), port(8082), port(8083)); 169 my @ports = my ($p1, $p2, $p3) = (port(8081), port(8082), port(8083));
170 170
171 # Only requests for absent peer are moved to other peers if hash is consistent. 171 # Only requests for absent peer are moved to other peers if hash is consistent.
172 # Check this by comparing two upstreams with different number of peers. 172 # Check this by comparing two upstreams with different number of peers.
173 173
174 ok(!cmp_peers([iter('/', 20)], [iter('/2', 20)], $p2), 'inconsistent'); 174 ok(!cmp_peers([iter('/', 20)], [iter('/2', 20)], $p2), 'inconsistent');
175 ok(cmp_peers([iter('/c', 20)], [iter('/c2', 20)], $p2), 'consistent'); 175 ok(cmp_peers([iter('/c', 20)], [iter('/c2', 20)], $p2), 'consistent');
176 ok(cmp_peers([iter('/cw', 20)], [iter('/cw2', 20)], $p2), 'consistent weight'); 176 ok(cmp_peers([iter('/cw', 20)], [iter('/cw2', 20)], $p2), 'consistent weight');
177 177
178 like(many('/?a=1', 10), qr/($p1|$p2|$p3): 10/, 'stable hash'); 178 like(many('/?a=1', 10), qr/($p1|$p2|$p3): 10/, 'stable hash');
179 like(many('/c?a=1', 10), qr/($p1|$p2|$p3): 10/, 'stable hash - consistent'); 179 like(many('/c?a=1', 10), qr/($p1|$p2|$p3): 10/, 'stable hash - consistent');
180
181 # fallback to round-robin
182
183 TODO: {
184 local $TODO = 'not yet' unless $t->has_version('1.17.1');
185
186 like(many('/?a=', 6), qr/$p1: 2, $p2: 2, $p3: 2/, 'empty key');
187 like(many('/c?a=', 6), qr/$p1: 2, $p2: 2, $p3: 2/, 'empty key - consistent');
188
189 }
180 190
181 my @res = iter('/', 10); 191 my @res = iter('/', 10);
182 192
183 is(@res, 10, 'all hashed peers'); 193 is(@res, 10, 'all hashed peers');
184 194
242 $ports{$1} = 0 unless defined $ports{$1}; 252 $ports{$1} = 0 unless defined $ports{$1};
243 $ports{$1}++; 253 $ports{$1}++;
244 } 254 }
245 } 255 }
246 256
247 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; 257 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports;
248 } 258 return join ', ', map { $_ . ": " . $ports{$_} } @keys;
249 259 }
250 ############################################################################### 260
261 ###############################################################################