Skip to content
Permalink
Browse files

improve shutdown tests

new tests were flaky for slow systems; for some reason writing to the
closet socket would exit prove instead of throw any kind of error.
Instead we check that the child pid actually exits and loop a bit to
avoid the race.
  • Loading branch information
dormando committed Nov 20, 2020
1 parent 4821f7e commit c472369fed5981ba8c004d426cee62d5165c47ca
Showing with 23 additions and 4 deletions.
  1. +7 −0 t/lib/MemcachedTest.pm
  2. +16 −4 t/shutdown.t
@@ -387,6 +387,7 @@ END {

############################################################################
package Memcached::Handle;
use POSIX ":sys_wait_h";
sub new {
my ($class, %params) = @_;
return bless \%params, $class;
@@ -407,6 +408,12 @@ sub graceful_stop {
kill 'SIGUSR1', $self->{pid};
}

# -1 if the pid is actually dead.
sub is_running {
my $self = shift;
return waitpid($self->{pid}, WNOHANG) >= 0 ? 1 : 0;
}

sub host { $_[0]{host} }
sub port { $_[0]{port} }
sub udpport { $_[0]{udpport} }
@@ -31,8 +31,7 @@ use MemcachedTest;
print $sock "version\r\n";
like(scalar <$sock>, qr/VERSION/, "server is initially alive");
print $sock "shutdown\r\n";
print $sock "version\r\n";
is(scalar <$sock>, undef, "server has been normally shut down");
still_going($server);
}

# Graceful shutdown
@@ -42,8 +41,21 @@ use MemcachedTest;
print $sock "version\r\n";
like(scalar <$sock>, qr/VERSION/, "server is initially alive");
print $sock "shutdown graceful\r\n";
print $sock "version\r\n";
is(scalar <$sock>, undef, "server has been gracefully shut down");
still_going($server);
}

sub still_going {
my $server = shift;
for (1..10) {
if ($server->is_running) {
sleep 1;
} else {
ok(!$server->is_running, "server stopped");
return;
}
}

ok(0, "server failed to stop");
}

done_testing();

0 comments on commit c472369

Please sign in to comment.