comparison stream_proxy_next_upstream.t @ 553:e47cd1b95997

Tests: stream tests for proxy_next_upstream.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 21 Apr 2015 19:05:30 +0300
parents
children 4cc65166509e
comparison
equal deleted inserted replaced
552:93af034fda47 553:e47cd1b95997
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for stream proxy module, proxy_next_upstream directive and friends.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 use IO::Select;
16
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
18
19 use lib 'lib';
20 use Test::Nginx;
21
22 ###############################################################################
23
24 select STDERR; $| = 1;
25 select STDOUT; $| = 1;
26
27 my $t = Test::Nginx->new()->has(qw/stream/);
28
29 $t->write_file_expand('nginx.conf', <<'EOF');
30
31 %%TEST_GLOBALS%%
32
33 daemon off;
34
35 events {
36 }
37
38 stream {
39 upstream u {
40 server 127.0.0.1:8087 max_fails=0;
41 server 127.0.0.1:8088 max_fails=0;
42 server 127.0.0.1:8089 backup;
43 }
44
45 proxy_connect_timeout 2s;
46
47 server {
48 listen 127.0.0.1:8081;
49 proxy_pass u;
50 proxy_next_upstream off;
51 }
52
53 server {
54 listen 127.0.0.1:8082;
55 proxy_pass u;
56 proxy_next_upstream on;
57 }
58
59 server {
60 listen 127.0.0.1:8083;
61 proxy_pass u;
62 proxy_next_upstream on;
63 proxy_next_upstream_tries 2;
64 }
65 }
66
67 EOF
68
69 $t->run_daemon(\&stream_daemon);
70 $t->try_run('no proxy_next_upstream')->plan(3);
71
72 $t->waitforsocket('127.0.0.1:8089');
73
74 ###############################################################################
75
76 is(stream_get('.', '127.0.0.1:8081'), '', 'next upstream off');
77 is(stream_get('.', '127.0.0.1:8082'), 'SEE-THIS', 'next upstream on');
78
79 # make sure backup is not tried
80
81 is(stream_get('.', '127.0.0.1:8083'), '', 'next upstream tries');
82
83 ###############################################################################
84
85 sub stream_get {
86 my ($data, $peer) = @_;
87
88 my $s = stream_connect($peer);
89 stream_write($s, $data);
90 my $r = stream_read($s);
91
92 $s->close;
93 return $r;
94 }
95
96 sub stream_connect {
97 my $peer = shift;
98 my $s = IO::Socket::INET->new(
99 Proto => 'tcp',
100 PeerAddr => $peer
101 )
102 or die "Can't connect to nginx: $!\n";
103
104 return $s;
105 }
106
107 sub stream_write {
108 my ($s, $message) = @_;
109
110 local $SIG{PIPE} = 'IGNORE';
111
112 $s->blocking(0);
113 while (IO::Select->new($s)->can_write(1.5)) {
114 my $n = $s->syswrite($message);
115 last unless $n;
116 $message = substr($message, $n);
117 last unless length $message;
118 }
119
120 if (length $message) {
121 $s->close();
122 }
123 }
124
125 sub stream_read {
126 my ($s) = @_;
127 my ($buf);
128
129 $s->blocking(0);
130 if (IO::Select->new($s)->can_read(3)) {
131 $s->sysread($buf, 1024);
132 };
133
134 log_in($buf);
135 return $buf;
136 }
137
138 ###############################################################################
139
140 sub stream_daemon {
141 my $server = IO::Socket::INET->new(
142 Proto => 'tcp',
143 LocalHost => '127.0.0.1:8089',
144 Listen => 5,
145 Reuse => 1
146 )
147 or die "Can't create listening socket: $!\n";
148
149 local $SIG{PIPE} = 'IGNORE';
150
151 while (my $client = $server->accept()) {
152 $client->autoflush(1);
153
154 log2c("(new connection $client)");
155
156 $client->sysread(my $buffer, 65536) or next;
157
158 log2i("$client $buffer");
159
160 $buffer = 'SEE-THIS';
161
162 log2o("$client $buffer");
163
164 $client->syswrite($buffer);
165
166 } continue {
167 close $client;
168 }
169 }
170
171 sub log2i { Test::Nginx::log_core('|| <<', @_); }
172 sub log2o { Test::Nginx::log_core('|| >>', @_); }
173 sub log2c { Test::Nginx::log_core('||', @_); }
174
175 ###############################################################################