Mercurial > hg > nginx-tests
comparison stream_proxy_next_upstream.t @ 816:77359b849cd5
Tests: stream package.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Mon, 20 Jul 2015 15:06:09 +0300 |
parents | ca54b445d982 |
children | e9064d691790 |
comparison
equal
deleted
inserted
replaced
815:9f5f604a840e | 816:77359b849cd5 |
---|---|
10 use warnings; | 10 use warnings; |
11 use strict; | 11 use strict; |
12 | 12 |
13 use Test::More; | 13 use Test::More; |
14 | 14 |
15 use IO::Select; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | 15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
18 | 16 |
19 use lib 'lib'; | 17 use lib 'lib'; |
20 use Test::Nginx; | 18 use Test::Nginx; |
19 use Test::Nginx::Stream qw/ stream /; | |
21 | 20 |
22 ############################################################################### | 21 ############################################################################### |
23 | 22 |
24 select STDERR; $| = 1; | 23 select STDERR; $| = 1; |
25 select STDOUT; $| = 1; | 24 select STDOUT; $| = 1; |
74 $t->run_daemon(\&stream_daemon); | 73 $t->run_daemon(\&stream_daemon); |
75 $t->run()->waitforsocket('127.0.0.1:8089'); | 74 $t->run()->waitforsocket('127.0.0.1:8089'); |
76 | 75 |
77 ############################################################################### | 76 ############################################################################### |
78 | 77 |
79 is(stream_get('.', '127.0.0.1:8081'), '', 'next upstream off'); | 78 is(stream('127.0.0.1:8081')->io('.'), '', 'next off'); |
80 is(stream_get('.', '127.0.0.1:8082'), 'SEE-THIS', 'next upstream on'); | 79 is(stream('127.0.0.1:8082')->io('.'), 'SEE-THIS', 'next on'); |
81 | 80 |
82 # make sure backup is not tried | 81 # make sure backup is not tried |
83 | 82 |
84 is(stream_get('.', '127.0.0.1:8083'), '', 'next upstream tries'); | 83 is(stream('127.0.0.1:8083')->io('.'), '', 'next tries'); |
85 | |
86 ############################################################################### | |
87 | |
88 sub stream_get { | |
89 my ($data, $peer) = @_; | |
90 | |
91 my $s = stream_connect($peer); | |
92 stream_write($s, $data); | |
93 | |
94 $data = ''; | |
95 while (my $buf = stream_read($s)) { | |
96 $data .= $buf; | |
97 } | |
98 return $data; | |
99 } | |
100 | |
101 sub stream_connect { | |
102 my $peer = shift; | |
103 my $s = IO::Socket::INET->new( | |
104 Proto => 'tcp', | |
105 PeerAddr => $peer | |
106 ) | |
107 or die "Can't connect to nginx: $!\n"; | |
108 | |
109 return $s; | |
110 } | |
111 | |
112 sub stream_write { | |
113 my ($s, $message) = @_; | |
114 | |
115 local $SIG{PIPE} = 'IGNORE'; | |
116 | |
117 $s->blocking(0); | |
118 while (IO::Select->new($s)->can_write(1.5)) { | |
119 my $n = $s->syswrite($message); | |
120 last unless $n; | |
121 $message = substr($message, $n); | |
122 last unless length $message; | |
123 } | |
124 | |
125 if (length $message) { | |
126 $s->close(); | |
127 } | |
128 } | |
129 | |
130 sub stream_read { | |
131 my ($s) = @_; | |
132 my ($buf); | |
133 | |
134 $s->blocking(0); | |
135 if (IO::Select->new($s)->can_read(5)) { | |
136 $s->sysread($buf, 1024); | |
137 }; | |
138 | |
139 log_in($buf); | |
140 return $buf; | |
141 } | |
142 | 84 |
143 ############################################################################### | 85 ############################################################################### |
144 | 86 |
145 sub stream_daemon { | 87 sub stream_daemon { |
146 my $server = IO::Socket::INET->new( | 88 my $server = IO::Socket::INET->new( |