Permalink
Browse files

updated Test::Nginx to enable valgrind support.

  • Loading branch information...
1 parent b9f2d75 commit 06d70767b08579e6ddd92356ded2bebd2ab8846a @agentzh agentzh committed Jun 2, 2010
Showing with 125 additions and 22 deletions.
  1. +3 −1 test/lib/Test/Nginx/LWP.pm
  2. +31 −9 test/lib/Test/Nginx/Socket.pm
  3. +91 −12 test/lib/Test/Nginx/Util.pm
@@ -32,6 +32,8 @@ use Test::Nginx::Util qw(
master_process_enabled
config_preamble
repeat_each
+ no_shuffle
+ no_root_location
);
our $UserAgent = LWP::UserAgent->new;
@@ -43,7 +45,7 @@ $UserAgent->agent(__PACKAGE__);
our @EXPORT = qw( plan run_tests run_test
repeat_each config_preamble worker_connections
master_process_enabled
- no_long_string);
+ no_long_string no_shuffle no_root_location);
sub no_long_string () {
$NoLongString = 1;
@@ -7,12 +7,14 @@ use Test::Base -Base;
our $VERSION = '0.08';
+use Encode;
use Data::Dumper;
use Time::HiRes qw(sleep time);
use Test::LongString;
use List::MoreUtils qw( any );
use IO::Select ();
+our $ServerAddr = 'localhost';
our $Timeout = 2;
use Test::Nginx::Util qw(
@@ -38,6 +40,8 @@ use Test::Nginx::Util qw(
workers
master_on
log_level
+ no_shuffle
+ no_root_location
);
#use Smart::Comments::JSON '###';
@@ -53,9 +57,11 @@ our @EXPORT = qw( plan run_tests run_test
repeat_each config_preamble worker_connections
master_process_enabled
no_long_string workers master_on
- log_level);
+ log_level no_shuffle no_root_location
+ server_addr
+);
-sub send_request ($$$);
+sub send_request ($$$$);
sub run_test_helper ($);
@@ -67,6 +73,15 @@ sub no_long_string () {
$NoLongString = 1;
}
+sub server_addr (@) {
+ if (@_) {
+ #warn "setting server addr to $_[0]\n";
+ $ServerAddr = shift;
+ } else {
+ return $ServerAddr;
+ }
+}
+
$RunTestHelper = \&run_test_helper;
sub parse_request ($$) {
@@ -199,7 +214,7 @@ $parsed_req->{content}";
}
my $raw_resp = send_request($req, $block->raw_request_middle_delay,
- $timeout);
+ $timeout, $block->name);
#warn "raw resonse: [$raw_resp]\n";
@@ -243,7 +258,7 @@ $parsed_req->{content}";
fail "$name - invalid chunked body received: $&";
return;
} else {
- fail "$name - no last chunk found";
+ fail "$name - no last chunk found - $raw";
return;
}
}
@@ -297,6 +312,10 @@ $parsed_req->{content}";
$expected = $block->response_body;
}
+ if ($block->charset) {
+ Encode::from_to($expected, 'UTF-8', $block->charset);
+ }
+
$expected =~ s/\$ServerPort\b/$ServerPort/g;
$expected =~ s/\$ServerPortForClient\b/$ServerPortForClient/g;
#warn show_all_chars($content);
@@ -322,16 +341,17 @@ $parsed_req->{content}";
}
}
-sub send_request ($$$) {
- my ($req, $middle_delay, $timeout) = @_;
+sub send_request ($$$$) {
+ my ($req, $middle_delay, $timeout, $name) = @_;
my @req_bits = ref $req ? @$req : ($req);
my $sock = IO::Socket::INET->new(
- PeerAddr => 'localhost',
+ PeerAddr => $ServerAddr,
PeerPort => $ServerPortForClient,
Proto => 'tcp'
- ) or die "Can't connect to localhost:$ServerPortForClient: $!\n";
+ ) or
+ die "Can't connect to $ServerAddr:$ServerPortForClient: $!\n";
my $flags = fcntl $sock, F_GETFL, 0
or die "Failed to get flags: $!\n";
@@ -347,6 +367,7 @@ sub send_request ($$$) {
write_buf => shift @req_bits,
middle_delay => $middle_delay,
sock => $sock,
+ name => $name,
};
my $readable_hdls = IO::Select->new($sock);
@@ -469,7 +490,8 @@ sub send_request ($$$) {
}
sub timeout_event_handler ($) {
- warn "socket client: timed out";
+ my $ctx = shift;
+ warn "ERROR: socket client: timed out - $ctx->{name}\n";
}
sub error_event_handler ($) {
View
@@ -13,16 +13,25 @@ use HTTP::Response;
use Module::Install::Can;
use Cwd qw( cwd );
use List::Util qw( shuffle );
+use Time::HiRes qw( sleep );
our $NoNginxManager = 0;
our $Profiling = 0;
our $RepeatEach = 1;
our $MAX_PROCESSES = 10;
+our $NoShuffle = 0;
+
+our $UseValgrind = $ENV{TEST_NGINX_USE_VALGRIND};
+
+sub no_shuffle () {
+ $NoShuffle = 1;
+}
+
our $ForkManager;
-if ($Profiling) {
+if ($Profiling || $UseValgrind) {
eval "use Parallel::ForkManager";
if ($@) {
die "Failed to load Parallel::ForkManager: $@\n";
@@ -36,7 +45,8 @@ our $LogLevel = 'debug';
our $MasterProcessEnabled = 'off';
our $DaemonEnabled = 'on';
our $ServerPort = 1984;
-our $ServerPortForClient = 1984;
+our $ServerPortForClient = $ENV{TEST_NGINX_CLIENT_PORT} || 1984;
+our $NoRootLocation = 0;
#our $ServerPortForClient = 1984;
@@ -56,6 +66,10 @@ sub worker_connections (@) {
}
}
+sub no_root_location () {
+ $NoRootLocation = 1;
+}
+
sub workers (@) {
if (@_) {
#warn "setting workers to $_[0]";
@@ -110,10 +124,12 @@ our @EXPORT_OK = qw(
repeat_each
master_process_enabled
log_level
+ no_shuffle
+ no_root_location
);
-if ($Profiling) {
+if ($Profiling || $UseValgrind) {
$DaemonEnabled = 'off';
$MasterProcessEnabled = 'off';
}
@@ -148,13 +164,13 @@ sub run_tests () {
#warn "[INFO] Using nginx version $NginxVersion ($NginxRawVersion)\n";
}
- for my $block (shuffle Test::Base::blocks()) {
+ for my $block ($NoShuffle ? Test::Base::blocks() : shuffle Test::Base::blocks()) {
#for (1..3) {
run_test($block);
#}
}
- if ($Profiling) {
+ if ($Profiling || $UseValgrind) {
$ForkManager->wait_all_children;
}
}
@@ -213,11 +229,11 @@ http {
default_type text/plain;
keepalive_timeout 68;
- $http_config
+$http_config
server {
listen $ServerPort;
- server_name localhost;
+ server_name 'localhost';
client_max_body_size 30M;
#client_body_buffer_size 4k;
@@ -230,10 +246,18 @@ $ConfigPreamble
$config
# End test case config.
+_EOC_
+
+ if (! $NoRootLocation) {
+ print $out <<_EOC_;
location / {
root $HtmlDir;
index index.html index.htm;
}
+_EOC_
+ }
+
+ print $out <<_EOC_;
}
}
@@ -420,16 +444,38 @@ start_nginx:
$cmd = "nginx -c $ConfFile > /dev/null";
}
- if ($Profiling) {
+ if ($UseValgrind) {
+ if (-f 'valgrind.suppress') {
+ $cmd = "valgrind -q --leak-check=full --gen-suppressions=all --suppressions=valgrind.suppress $cmd";
+ } else {
+ $cmd = "valgrind -q --leak-check=full --gen-suppressions=all $cmd";
+ }
+
+ warn "$name\n";
+ #warn "$cmd\n";
+ }
+
+ if ($Profiling || $UseValgrind) {
my $pid = $ForkManager->start;
if (!$pid) {
# child process
+ exec $cmd;
+
+=begin cmt
+
if (system($cmd) != 0) {
Test::More::BAIL_OUT("$name - Cannot start nginx using command \"$cmd\".");
}
$ForkManager->finish; # terminate the child process
+
+=end cmt
+
+=cut
+
}
+ #warn "sleeping";
+ sleep 1;
} else {
if (system($cmd) != 0) {
Test::More::BAIL_OUT("$name - Cannot start nginx using command \"$cmd\".");
@@ -440,6 +486,13 @@ start_nginx:
}
}
+ if ($block->init) {
+ eval $block->init;
+ if ($@) {
+ Test::More::BAIL_OUT("$name - init failed: $@");
+ }
+ }
+
my $i = 0;
while ($i++ < $RepeatEach) {
if ($should_skip) {
@@ -459,13 +512,40 @@ start_nginx:
}
}
- if (defined $block->quit && $Profiling) {
- warn "Found quit...";
+ if ($Profiling || $UseValgrind) {
+ #warn "Found quit...";
if (-f $PidFile) {
+ #warn "found pid file...";
my $pid = get_pid_from_pidfile($name);
if (system("ps $pid > /dev/null") == 0) {
write_config_file($config, $block->http_config);
if (kill(SIGQUIT, $pid) == 0) { # send quit signal
+ warn("$name - Failed to send quit signal to the nginx process with PID $pid");
+ }
+ sleep 0.1;
+ if (-f $PidFile) {
+ #warn "killing with force (valgrind or profile)...\n";
+ kill(SIGKILL, $pid);
+ sleep 0.02;
+ } else {
+ #warn "nginx killed";
+ }
+ } else {
+ unlink $PidFile or
+ die "Failed to remove pid file $PidFile\n";
+ }
+ } else {
+ #warn "pid file not found";
+ }
+ }
+}
+
+END {
+ if ($UseValgrind) {
+ if (-f $PidFile) {
+ my $pid = get_pid_from_pidfile('');
+ if (system("ps $pid > /dev/null") == 0) {
+ if (kill(SIGQUIT, $pid) == 0) { # send quit signal
#warn("$name - Failed to send quit signal to the nginx process with PID $pid");
}
sleep 0.02;
@@ -475,8 +555,7 @@ start_nginx:
sleep 0.02;
}
} else {
- unlink $PidFile or
- die "Failed to remove pid file $PidFile\n";
+ unlink $PidFile;
}
}
}

0 comments on commit 06d7076

Please sign in to comment.