comparison lib/Test/Nginx.pm @ 969:1edb092149e2

Tests: simplified parallel tests. Now configurations are automatically scanned for "127.0.0.1:8xxx", and ports are auto-replaced by free ones. The exact port specified is used if it's free.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 08 Jul 2016 02:21:14 +0300
parents c26b0eaed67a
children 313e3f1905d4
comparison
equal deleted inserted replaced
968:ce687b25ea49 969:1edb092149e2
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 our %ports = ();
35 35
36 sub new { 36 sub new {
37 my $self = {}; 37 my $self = {};
38 bless $self; 38 bless $self;
39 39
311 311
312 sub port { 312 sub port {
313 my ($num, %opts) = @_; 313 my ($num, %opts) = @_;
314 my ($s_tcp, $s_udp, $port); 314 my ($s_tcp, $s_udp, $port);
315 315
316 goto done if defined $ports[$num]; 316 $num += 8080 if $num < 1000;
317 goto done if defined $ports{$num};
318
319 $port = $num;
317 320
318 for (1 .. 10) { 321 for (1 .. 10) {
319 $port = 8000 + int(rand(1000)); 322 $port = 8000 + int(rand(1000)) unless $_ == 1;
320 323
321 $s_udp = IO::Socket::INET->new( 324 $s_udp = IO::Socket::INET->new(
322 Proto => 'udp', 325 Proto => 'udp',
323 LocalAddr => '127.0.0.1:' . $port, 326 LocalAddr => '127.0.0.1:' . $port,
324 ) or next; 327 ) or next;
330 ) and last; 333 ) and last;
331 } 334 }
332 335
333 die "Port limit exceeded" unless defined $s_tcp and defined $s_udp; 336 die "Port limit exceeded" unless defined $s_tcp and defined $s_udp;
334 337
335 $ports[$num] = {port => $port, socket => $opts{udp} ? $s_tcp : $s_udp}; 338 $ports{$num} = {
339 port => $port,
340 socket => $opts{udp} ? $s_tcp : $s_udp
341 };
336 342
337 done: 343 done:
338 return $ports[$num]{socket} if $opts{socket}; 344 return $ports{$num}{socket} if $opts{socket};
339 return $ports[$num]{port}; 345 return $ports{$num}{port};
340 } 346 }
341 347
342 sub dump_config() { 348 sub dump_config() {
343 my ($self) = @_; 349 my ($self) = @_;
344 350
460 my ($self, $name, $content) = @_; 466 my ($self, $name, $content) = @_;
461 467
462 $content =~ s/%%TEST_GLOBALS%%/$self->test_globals()/gmse; 468 $content =~ s/%%TEST_GLOBALS%%/$self->test_globals()/gmse;
463 $content =~ s/%%TEST_GLOBALS_HTTP%%/$self->test_globals_http()/gmse; 469 $content =~ s/%%TEST_GLOBALS_HTTP%%/$self->test_globals_http()/gmse;
464 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms; 470 $content =~ s/%%TESTDIR%%/$self->{_testdir}/gms;
471
472 $content =~ s/127\.0\.0\.1:(8\d\d\d)/'127.0.0.1:' . port($1)/gmse;
473
465 $content =~ s/%%PORT_(\d+)%%/port($1)/gmse; 474 $content =~ s/%%PORT_(\d+)%%/port($1)/gmse;
466 $content =~ s/%%PORT_(\d+)_UDP%%/port($1, udp => 1)/gmse; 475 $content =~ s/%%PORT_(\d+)_UDP%%/port($1, udp => 1)/gmse;
467 476
468 return $self->write_file($name, $content); 477 return $self->write_file($name, $content);
469 } 478 }
643 local $SIG{PIPE} = sub { die "sigpipe\n" }; 652 local $SIG{PIPE} = sub { die "sigpipe\n" };
644 alarm(8); 653 alarm(8);
645 654
646 $s = $extra{socket} || IO::Socket::INET->new( 655 $s = $extra{socket} || IO::Socket::INET->new(
647 Proto => 'tcp', 656 Proto => 'tcp',
648 PeerAddr => '127.0.0.1:' . ($ports[0]{port} || 8080) 657 PeerAddr => '127.0.0.1:' . port(8080)
649 ) 658 )
650 or die "Can't connect to nginx: $!\n"; 659 or die "Can't connect to nginx: $!\n";
651 660
652 log_out($request); 661 log_out($request);
653 $s->print($request); 662 $s->print($request);