Skip to content

Commit

Permalink
fixes issue #34: socket file shouldn't be removed when it daemonises
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Jones committed Jul 12, 2015
1 parent a78eb70 commit 9b9c503
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/Server/Starter.pm
Expand Up @@ -150,12 +150,6 @@ sub start_server {
}
push @sock, $sock;
}
my $path_remove_guard = Server::Starter::Guard->new(
sub {
-S $_ and unlink $_
for @$paths;
},
);
for my $path (@$paths) {
if (-S $path) {
warn "removing existing socket file:$path";
Expand Down Expand Up @@ -241,6 +235,13 @@ sub start_server {
}
}

my $path_remove_guard = Server::Starter::Guard->new(
sub {
-S $_ and unlink $_
for @$paths;
},
);

# open pid file
my $pid_file_guard = sub {
return unless $opts->{pid_file};
Expand Down
68 changes: 68 additions & 0 deletions t/13-unix-daemonize.t
@@ -0,0 +1,68 @@
use strict;
use warnings;
use utf8;
use Test::More;
use Server::Starter qw(start_server stop_server);
use Server::Starter::Guard;
use File::Temp qw(tempdir);

plan tests => 1;

my $dir = tempdir( CLEANUP => 1 );
my $pidfile = "$dir/pid";
my $sockfile = "$dir/server.sock";

fork_ok(
child => sub {
start_server(
pid_file => $pidfile,
daemonize => 1,
path => $sockfile,
exec => [ $^X, qw(t/03-starter-unix-echod.pl) ],
);
},

parent => sub {
my $guard = Server::Starter::Guard->new(sub {
stop_server( pid_file => $pidfile );
});

wait_for(sub { -e $pidfile })
or BAIL_OUT("Pidfile '$pidfile' was not created in a timely fashion");

wait_for(sub { -e $sockfile })
or BAIL_OUT("Socket '$sockfile' was not created in a timely fashion");

ok(-e $sockfile, 'there is a socket');
},
) or die "fork failed: $!";

sub fork_ok {
my (%args) = @_;

my $pid = fork;
return unless defined $pid;
if ($pid == 0) {
$args{child}->();
}
else {
$args{parent}->($pid);
}

return 1;
}

sub wait_for {
my ($code, %opts) = @_;

my $times = $opts{times} || 10;
my $every = $opts{every} || 1;

while ( $times > 0 ) {
return 1 if $code->();
$times--;
sleep $every;
}

return 0;
}

0 comments on commit 9b9c503

Please sign in to comment.