Mercurial > hg > nginx-tests
comparison upstream.t @ 287:ba5b92378653
Tests: add some basic upstream tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 23 May 2013 19:32:55 +0400 |
parents | |
children | 719285b89d7e |
comparison
equal
deleted
inserted
replaced
286:f62137d1b5b1 | 287:ba5b92378653 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for upstream module and balancers. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
24 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(3); | |
25 | |
26 $t->write_file_expand('nginx.conf', <<'EOF'); | |
27 | |
28 %%TEST_GLOBALS%% | |
29 | |
30 daemon off; | |
31 | |
32 events { | |
33 } | |
34 | |
35 http { | |
36 %%TEST_GLOBALS_HTTP%% | |
37 | |
38 upstream u { | |
39 server 127.0.0.1:8081 max_fails=3 fail_timeout=10s; | |
40 server 127.0.0.1:8082 max_fails=3 fail_timeout=10s; | |
41 } | |
42 | |
43 upstream u2 { | |
44 server 127.0.0.1:8081 max_fails=3 fail_timeout=10s; | |
45 server 127.0.0.1:8082 max_fails=3 fail_timeout=10s; | |
46 } | |
47 | |
48 server { | |
49 listen 127.0.0.1:8080; | |
50 server_name localhost; | |
51 | |
52 location / { | |
53 proxy_pass http://u; | |
54 } | |
55 location /close2 { | |
56 proxy_pass http://u2; | |
57 } | |
58 } | |
59 } | |
60 | |
61 EOF | |
62 | |
63 $t->run_daemon(\&http_daemon, 8081); | |
64 $t->run_daemon(\&http_daemon, 8082); | |
65 $t->run(); | |
66 | |
67 ############################################################################### | |
68 | |
69 is(many('/', 30), '8081: 15, 8082: 15', 'balanced'); | |
70 | |
71 # from 9 first requests to 8081, only 6 will be successfull, | |
72 # 3rd, 6th, and 9th requests will fail; after this the backend | |
73 # will be considered down and won't be used till fail_timeout passes | |
74 | |
75 is(many('/close', 30), '8081: 6, 8082: 24', 'failures'); | |
76 | |
77 SKIP: { | |
78 local $TODO = 'broken in 1.3.0'; | |
79 | |
80 skip 'long test', 1 unless $ENV{TEST_NGINX_UNSAFE}; | |
81 | |
82 # bug: failures counter is reset if first request in a second succeeds | |
83 # | |
84 # delay added to make sure first 9 requests will take more than 1s; | |
85 # note that the test is racy and may unexpectedly succeed | |
86 | |
87 is(many('/close2', 30, delay => 0.2), '8081: 6, 8082: 24', 'failures delay'); | |
88 | |
89 } | |
90 | |
91 ############################################################################### | |
92 | |
93 sub many { | |
94 my ($uri, $count, %opts) = @_; | |
95 my %ports; | |
96 | |
97 for (1 .. 30) { | |
98 if (http_get($uri) =~ /X-Port: (\d+)/) { | |
99 $ports{$1} = 0 unless defined $ports{$1}; | |
100 $ports{$1}++; | |
101 } | |
102 | |
103 select undef, undef, undef, $opts{delay} if $opts{delay}; | |
104 } | |
105 | |
106 return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports; | |
107 } | |
108 | |
109 ############################################################################### | |
110 | |
111 sub http_daemon { | |
112 my ($port) = @_; | |
113 my $count = 1; | |
114 | |
115 my $server = IO::Socket::INET->new( | |
116 Proto => 'tcp', | |
117 LocalHost => '127.0.0.1', | |
118 LocalPort => $port, | |
119 Listen => 5, | |
120 Reuse => 1 | |
121 ) | |
122 or die "Can't create listening socket: $!\n"; | |
123 | |
124 while (my $client = $server->accept()) { | |
125 $client->autoflush(1); | |
126 | |
127 my $headers = ''; | |
128 my $uri = ''; | |
129 | |
130 while (<$client>) { | |
131 $headers .= $_; | |
132 last if (/^\x0d?\x0a?$/); | |
133 } | |
134 | |
135 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; | |
136 | |
137 if ($uri =~ 'close' && $port == 8081 && $count++ % 3 == 0) { | |
138 next; | |
139 } | |
140 | |
141 print $client <<EOF; | |
142 HTTP/1.1 200 OK | |
143 Connection: close | |
144 X-Port: $port | |
145 | |
146 OK | |
147 EOF | |
148 | |
149 close $client; | |
150 } | |
151 } | |
152 | |
153 ############################################################################### |