comparison lib/Test/Nginx.pm @ 290:f781b087b7aa

Tests: fixed possible test suite hang on exit. In some cases (for example, call of global object method in a function), test suite can hang during exit, because temporary directory holding a pid file is removed prior to executing global test object desctructor. The root cause is that the directory removal occurs in the END block (in the File::Temp package) and the order of executing destructors and END blocks is not specified in Perl. The fix is to remove temporary directory in the test object destructor explicitly and get rid of 'CLEANUP' flag in the tempdir() call. The temporary directory is removed by the means of rmtree() function, called inside an eval block to exclude the case when rmtree may abort (some versions are known to do it).
author Vladimir Homutov <vl@nginx.com>
date Thu, 30 May 2013 07:38:23 +0400
parents cbd4f6eca676
children a113f4d55b12
comparison
equal deleted inserted replaced
289:cbd4f6eca676 290:f781b087b7aa
17 gzip => [ qw/ http_gzip_request http_gzip_like / ] 17 gzip => [ qw/ http_gzip_request http_gzip_like / ]
18 ); 18 );
19 19
20 ############################################################################### 20 ###############################################################################
21 21
22 use File::Path qw/ rmtree /;
22 use File::Temp qw/ tempdir /; 23 use File::Temp qw/ tempdir /;
23 use IO::Socket; 24 use IO::Socket;
24 use POSIX qw/ waitpid WNOHANG /; 25 use POSIX qw/ waitpid WNOHANG /;
25 use Socket qw/ CRLF /; 26 use Socket qw/ CRLF /;
26 use Test::More qw//; 27 use Test::More qw//;
34 my $self = {}; 35 my $self = {};
35 bless $self; 36 bless $self;
36 37
37 $self->{_testdir} = tempdir( 38 $self->{_testdir} = tempdir(
38 'nginx-test-XXXXXXXXXX', 39 'nginx-test-XXXXXXXXXX',
39 TMPDIR => 1, 40 TMPDIR => 1
40 CLEANUP => not $ENV{TEST_NGINX_LEAVE}
41 ) 41 )
42 or die "Can't create temp directory: $!\n"; 42 or die "Can't create temp directory: $!\n";
43 $self->{_testdir} =~ s!\\!/!g if $^O eq 'MSWin32'; 43 $self->{_testdir} =~ s!\\!/!g if $^O eq 'MSWin32';
44 44
45 return $self; 45 return $self;
49 my ($self) = @_; 49 my ($self) = @_;
50 $self->stop(); 50 $self->stop();
51 $self->stop_daemons(); 51 $self->stop_daemons();
52 if ($ENV{TEST_NGINX_CATLOG}) { 52 if ($ENV{TEST_NGINX_CATLOG}) {
53 system("cat $self->{_testdir}/error.log"); 53 system("cat $self->{_testdir}/error.log");
54 }
55 if (not $ENV{TEST_NGINX_LEAVE}) {
56 eval { rmtree($self->{_testdir}); };
54 } 57 }
55 } 58 }
56 59
57 sub has($;) { 60 sub has($;) {
58 my ($self, @features) = @_; 61 my ($self, @features) = @_;