Mercurial > hg > nginx-tests
annotate http_resolver_cleanup.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 | 78cb8e66b6bc |
children | e4974af3fb12 |
rev | line source |
---|---|
1098
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
2 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
3 # (C) Sergey Kandaurov |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
4 # (C) Nginx, Inc. |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
5 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
6 # Tests for http resolver, worker process termination. |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
7 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
8 ############################################################################### |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
9 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
10 use warnings; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
11 use strict; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
12 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
13 use Test::More; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
14 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
16 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
17 use lib 'lib'; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
18 use Test::Nginx; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
19 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
20 ############################################################################### |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
21 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
22 select STDERR; $| = 1; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
23 select STDOUT; $| = 1; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
24 |
1101
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
25 my $t = Test::Nginx->new()->has(qw/http proxy/); |
1098
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
26 |
1101
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
27 plan(skip_all => 'win32') if $^O eq 'MSWin32'; |
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
28 plan(skip_all => 'use-after-free on shutdown') unless $ENV{TEST_NGINX_UNSAFE} |
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
29 or $t->has_version('1.11.8'); |
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
30 |
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
31 $t->write_file_expand('nginx.conf', <<'EOF')->plan(1); |
1098
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
32 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
33 %%TEST_GLOBALS%% |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
34 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
35 daemon off; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
36 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
37 events { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
38 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
39 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
40 http { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
41 %%TEST_GLOBALS_HTTP%% |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
42 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
43 server { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
44 listen 127.0.0.1:8080; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
45 server_name localhost; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
46 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
47 location / { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
48 resolver 127.0.0.1:%%PORT_8081_UDP%%; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
49 proxy_pass http://example.net/$args; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
50 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
51 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
52 location /pid { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
53 add_header X-Pid $pid always; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
54 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
55 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
56 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
57 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
58 EOF |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
59 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
60 $t->run_daemon(\&dns_daemon, $t); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
61 $t->run()->waitforfile($t->testdir . '/' . port(8081)); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
62 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
63 ############################################################################### |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
64 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
65 # truncated UDP response, no response over TCP |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
66 |
1101
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
67 my $s = http_get('/', start => 1); |
1098
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
68 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
69 pass('request'); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
70 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
71 sleep 1; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
72 |
1101
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
73 # retrasmission timer wasn't removed during resolver cleanup, |
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
74 # while the event memory was freed, resulting in use-after-free |
78cb8e66b6bc
Tests: skipped the whole http_resolver_cleanup.t where appropriate.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1098
diff
changeset
|
75 # when later removing timer in TCP connection |
1098
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
76 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
77 http_get('/pid') =~ qr/X-Pid: (\d+)/; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
78 kill 'TERM', $1; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
79 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
80 ############################################################################### |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
81 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
82 sub dns_daemon { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
83 my ($t) = @_; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
84 my ($data); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
85 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
86 my $socket = IO::Socket::INET->new( |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
87 LocalAddr => '127.0.0.1', |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
88 LocalPort => port(8081), |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
89 Proto => 'udp', |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
90 ) |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
91 or die "Can't create UDP socket: $!\n"; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
92 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
93 # signal we are ready |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
94 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
95 open my $fh, '>', $t->testdir() . '/' . port(8081); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
96 close $fh; |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
97 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
98 while (1) { |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
99 $socket->recv($data, 65536); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
100 # truncation bit set |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
101 $data |= pack("n2", 0, 0x8380); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
102 $socket->send($data); |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
103 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
104 } |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
105 |
bb1974010d0a
Tests: resolver cleanup tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff
changeset
|
106 ############################################################################### |