Mercurial > hg > nginx-tests
annotate stream_udp_upstream_hash.t @ 1236:93f749c1d5c5
Tests: fixed parallel tests execution with UDP.
Previously, when checking ports availability, a UDP socket was always created
first, then a TCP socket was created. On success, one of UDP and TCP sockets
was closed (depending on the "udp" option) and the second one was used to busy
this port in other scripts. This lead to the following problem: in an attempt
to reopen a UDP socket used in a given testing script it could be stolen by
another script as part of checking ports availability.
To solve this problem, UDP and TCP ports were split into two non-overlapping
ranges: TCP ports are only used in the range 8000-8499, and UDP ports - in
the range 8500-8999. In addition, the order of creating sockets in UDP tests
has been reversed: now a TCP socket used as a lock precedes a UDP socket.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Thu, 26 Oct 2017 18:00:21 +0300 |
parents | 196d33c2bb45 |
children | e4974af3fb12 |
rev | line source |
---|---|
868 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Stream tests for upstream hash balancer module with datagrams. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
16 | |
17 use lib 'lib'; | |
18 use Test::Nginx; | |
19 use Test::Nginx::Stream qw/ dgram /; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream stream_upstream_hash udp/)->plan(2); |
868 | 27 |
28 $t->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 stream { | |
38 proxy_responses 1; | |
39 proxy_timeout 1s; | |
40 | |
41 upstream hash { | |
42 hash $remote_addr; | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
43 server 127.0.0.1:%%PORT_8082_UDP%%; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
44 server 127.0.0.1:%%PORT_8083_UDP%%; |
868 | 45 } |
46 | |
47 upstream cons { | |
48 hash $remote_addr consistent; | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
49 server 127.0.0.1:%%PORT_8082_UDP%%; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
50 server 127.0.0.1:%%PORT_8083_UDP%%; |
868 | 51 } |
52 | |
53 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
54 listen 127.0.0.1:%%PORT_8080_UDP%% udp; |
868 | 55 proxy_pass hash; |
56 } | |
57 | |
58 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
59 listen 127.0.0.1:%%PORT_8081_UDP%% udp; |
868 | 60 proxy_pass cons; |
61 } | |
62 } | |
63 | |
64 EOF | |
65 | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
66 $t->run_daemon(\&udp_daemon, port(8082), $t); |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
67 $t->run_daemon(\&udp_daemon, port(8083), $t); |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
68 $t->run(); |
868 | 69 |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
70 $t->waitforfile($t->testdir . '/' . port(8082)); |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
71 $t->waitforfile($t->testdir . '/' . port(8083)); |
868 | 72 |
73 ############################################################################### | |
74 | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
75 my @ports = my ($port2, $port3) = (port(8082), port(8083)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
76 |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
77 is(many(10, port(8080)), "$port3: 10", 'hash'); |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
78 like(many(10, port(8081)), qr/($port2|$port3): 10/, 'hash consistent'); |
868 | 79 |
80 ############################################################################### | |
81 | |
82 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
83 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
84 my (%ports); |
868 | 85 |
86 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
87 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 88 $ports{$1} = 0 unless defined $ports{$1}; |
89 $ports{$1}++; | |
90 } | |
91 } | |
92 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
93 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
94 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 95 } |
96 | |
97 ############################################################################### | |
98 | |
99 sub udp_daemon { | |
100 my ($port, $t) = @_; | |
101 | |
102 my $server = IO::Socket::INET->new( | |
103 Proto => 'udp', | |
104 LocalAddr => '127.0.0.1:' . $port, | |
105 Reuse => 1, | |
106 ) | |
107 or die "Can't create listening socket: $!\n"; | |
108 | |
109 # signal we are ready | |
110 | |
111 open my $fh, '>', $t->testdir() . '/' . $port; | |
112 close $fh; | |
113 | |
114 while (1) { | |
115 $server->recv(my $buffer, 65536); | |
116 $buffer = $server->sockport(); | |
117 $server->send($buffer); | |
118 } | |
119 } | |
120 | |
121 ############################################################################### |