comparison lib/Test/Nginx.pm @ 951:9361c7eddfc1

Tests: parallel tests support.
author Andrey Zelenkov <zelenkov@nginx.com>
date Tue, 21 Jun 2016 16:37:58 +0300
parents b9e42c554ba7
children 3bc5b72e4377
comparison
equal deleted inserted replaced
950:991ef7781b38 951:9361c7eddfc1
9 use warnings; 9 use warnings;
10 use strict; 10 use strict;
11 11
12 use base qw/ Exporter /; 12 use base qw/ Exporter /;
13 13
14 our @EXPORT = qw/ log_in log_out http http_get http_head /; 14 our @EXPORT = qw/ log_in log_out http http_get http_head port /;
15 our @EXPORT_OK = qw/ http_gzip_request http_gzip_like http_start http_end /; 15 our @EXPORT_OK = qw/ http_gzip_request http_gzip_like http_start http_end /;
16 our %EXPORT_TAGS = ( 16 our %EXPORT_TAGS = (
17 gzip => [ qw/ http_gzip_request http_gzip_like / ] 17 gzip => [ qw/ http_gzip_request http_gzip_like / ]
18 ); 18 );
19 19
29 29
30 ############################################################################### 30 ###############################################################################
31 31
32 our $NGINX = defined $ENV{TEST_NGINX_BINARY} ? $ENV{TEST_NGINX_BINARY} 32 our $NGINX = defined $ENV{TEST_NGINX_BINARY} ? $ENV{TEST_NGINX_BINARY}
33 : '../nginx/objs/nginx'; 33 : '../nginx/objs/nginx';
34 our @ports = ();
34 35
35 sub new { 36 sub new {
36 my $self = {}; 37 my $self = {};
37 bless $self; 38 bless $self;
38 39
302 303
303 $self->{_started} = 1; 304 $self->{_started} = 1;
304 return $self; 305 return $self;
305 } 306 }
306 307
308 sub port {
309 my ($num, %opts) = @_;
310 my ($s_tcp, $s_udp, $port);
311
312 goto done if defined $ports[$num];
313
314 for (1 .. 10) {
315 $port = 8000 + int(rand(1000));
316
317 $s_udp = IO::Socket::INET->new(
318 Proto => 'udp',
319 LocalAddr => '127.0.0.1:' . $port,
320 ) or next;
321
322 $s_tcp = IO::Socket::INET->new(
323 Proto => 'tcp',
324 LocalAddr => '127.0.0.1:' . $port,
325 Reuse => 1,
326 ) and last;
327 }
328
329 die "Port limit exceeded" unless defined $s_tcp and defined $s_udp;
330
331 $ports[$num] = {port => $port, socket => $opts{udp} ? $s_tcp : $s_udp};
332
333 done:
334 return $ports[$num]{socket} if $opts{socket};
335 return $ports[$num]{port};
336 }
337
307 sub dump_config() { 338 sub dump_config() {
308 my ($self) = @_; 339 my ($self) = @_;
309 340
310 my $testdir = $self->{_testdir}; 341 my $testdir = $self->{_testdir};
311 342
425 my ($self, $name, $content) = @_; 456 my ($self, $name, $content) = @_;
426 457
427 $content =~ s/%%TEST_GLOBALS%%/$self->test_globals()/gmse; 458 $content =~ s/%%TEST_GLOBALS%%/$self->test_globals()/gmse;
428 $content =~ s/%%TEST_GLOBALS_HTTP%%/$self->test_globals_http()/gmse; 459 $content =~ s/%%TEST_GLOBALS_HTTP%%/$self->test_globals_http()/gmse;
429 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms; 460 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms;
461 $content =~ s/%%PORT_(\d+)%%/port($1)/gmse;
462 $content =~ s/%%PORT_(\d+)_UDP%%/port($1, udp => 1)/gmse;
430 463
431 return $self->write_file($name, $content); 464 return $self->write_file($name, $content);
432 } 465 }
433 466
434 sub run_daemon($;@) { 467 sub run_daemon($;@) {
606 local $SIG{PIPE} = sub { die "sigpipe\n" }; 639 local $SIG{PIPE} = sub { die "sigpipe\n" };
607 alarm(8); 640 alarm(8);
608 641
609 $s = $extra{socket} || IO::Socket::INET->new( 642 $s = $extra{socket} || IO::Socket::INET->new(
610 Proto => 'tcp', 643 Proto => 'tcp',
611 PeerAddr => '127.0.0.1:8080' 644 PeerAddr => '127.0.0.1:' . ($ports[0]{port} || 8080)
612 ) 645 )
613 or die "Can't connect to nginx: $!\n"; 646 or die "Can't connect to nginx: $!\n";
614 647
615 log_out($request); 648 log_out($request);
616 $s->print($request); 649 $s->print($request);