comparison stream_error_log.t @ 816:77359b849cd5

Tests: stream package.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 20 Jul 2015 15:06:09 +0300
parents 22bade4c7e12
children 6bb1f2ccd386
comparison
equal deleted inserted replaced
815:9f5f604a840e 816:77359b849cd5
16 16
17 BEGIN { use FindBin; chdir($FindBin::Bin); } 17 BEGIN { use FindBin; chdir($FindBin::Bin); }
18 18
19 use lib 'lib'; 19 use lib 'lib';
20 use Test::Nginx; 20 use Test::Nginx;
21 use Test::Nginx::Stream qw/ stream /;
21 22
22 ############################################################################### 23 ###############################################################################
23 24
24 select STDERR; $| = 1; 25 select STDERR; $| = 1;
25 select STDOUT; $| = 1; 26 select STDOUT; $| = 1;
85 86
86 open STDERR, ">&", \*OLDERR; 87 open STDERR, ">&", \*OLDERR;
87 88
88 ############################################################################### 89 ###############################################################################
89 90
90 stream_get('data'); 91 stream()->io('data');
91 92
92 # error_log levels 93 # error_log levels
93 94
94 SKIP: { 95 SKIP: {
95 skip "no --with-debug", 1 unless $t->has_module('--with-debug'); 96 skip "no --with-debug", 1 unless $t->has_module('--with-debug');
159 my %levels_hash; 160 my %levels_hash;
160 161
161 map { $levels_hash{$_}++; } ($t->read_file($file) =~ /(\[\w+\])/g); 162 map { $levels_hash{$_}++; } ($t->read_file($file) =~ /(\[\w+\])/g);
162 163
163 return \%levels_hash; 164 return \%levels_hash;
164 }
165
166 sub stream_get {
167 my ($data, $peer) = @_;
168
169 $peer = '127.0.0.1:8080' unless defined $peer;
170 my $s = stream_connect($peer);
171 stream_write($s, $data);
172
173 $data = '';
174 while (my $buf = stream_read($s)) {
175 $data .= $buf;
176 }
177 return $data;
178 }
179
180 sub stream_connect {
181 my $peer = shift;
182 my $s = IO::Socket::INET->new(
183 Proto => 'tcp',
184 PeerAddr => $peer || '127.0.0.1:8080'
185 )
186 or die "Can't connect to nginx: $!\n";
187
188 return $s;
189 }
190
191 sub stream_write {
192 my ($s, $message) = @_;
193
194 local $SIG{PIPE} = 'IGNORE';
195
196 $s->blocking(0);
197 while (IO::Select->new($s)->can_write(1.5)) {
198 my $n = $s->syswrite($message);
199 last unless $n;
200 $message = substr($message, $n);
201 last unless length $message;
202 }
203
204 if (length $message) {
205 $s->close();
206 }
207 }
208
209 sub stream_read {
210 my ($s) = @_;
211 my ($buf);
212
213 $s->blocking(0);
214 if (IO::Select->new($s)->can_read(5)) {
215 $s->sysread($buf, 1024);
216 };
217
218 log_in($buf);
219 return $buf;
220 } 165 }
221 166
222 sub get_syslog { 167 sub get_syslog {
223 my ($data, $peer, $port) = @_; 168 my ($data, $peer, $port) = @_;
224 my ($s); 169 my ($s);
240 if ($@) { 185 if ($@) {
241 log_in("died: $@"); 186 log_in("died: $@");
242 return undef; 187 return undef;
243 } 188 }
244 189
245 stream_get($data, $peer); 190 stream($peer)->io($data);
246 $data = ''; 191 $data = '';
247 192
248 IO::Select->new($s)->can_read(1.5); 193 IO::Select->new($s)->can_read(1.5);
249 while (IO::Select->new($s)->can_read(0.1)) { 194 while (IO::Select->new($s)->can_read(0.1)) {
250 my $buffer; 195 my $buffer;