From 372257e9bd8309443b057e4a45508c81f1f6054c Mon Sep 17 00:00:00 2001 From: Jose Luis Perez Diez Date: Mon, 11 Apr 2016 14:02:00 +0200 Subject: [PATCH] Rewrite Error message Address in use #485 "Can't create listen socket: Address already in use" is rewriten as "Can't create listen socket for (http://*:3000): Address already in use" it changes all "Can't create listen socket:" messages to "Can't create listen socket for ($listen_URL):" It has a test in t/command/daemon.t that test the new messge when no port is specified Signed-off-by: Jose Luis Perez Diez --- lib/Statocles/Command.pm | 10 +++++++++- t/command/daemon.t | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/Statocles/Command.pm b/lib/Statocles/Command.pm index 1e4291b3..df4ca893 100644 --- a/lib/Statocles/Command.pm +++ b/lib/Statocles/Command.pm @@ -170,7 +170,15 @@ sub main { } # Using start() instead of run() so we can stop() inside the tests - $daemon->start; + # and eval to do a partial rewrite of some error messages + eval {$daemon->start}; + if ($@ ) { + if ( $@ =~ /^Can't create listen socket:/) { + my $port = join ' or ',@{$daemon->listen()}; + $@ =~ s/:/ for ($port):/; + } + die $@ + } # Find the port we're listening on my $id = $daemon->acceptors->[0]; diff --git a/t/command/daemon.t b/t/command/daemon.t index 2d0e4f08..8e5b332c 100644 --- a/t/command/daemon.t +++ b/t/command/daemon.t @@ -117,6 +117,30 @@ subtest 'listen on a specific port' => sub { 'contains http port information'; }; +subtest 'Try to launch 2 daemons' => sub { + + local $ENV{MOJO_LOG_LEVEL} = 'info'; # But sometimes this isn't set? + my @args = ( + '--config' => "$config_fn", + 'daemon', + '-v', # watch lines are now behind -v + ); + if (my $pid = fork) { + sleep 1; + eval { Statocles::Command->main( @args ) }; + kill 9, $pid; + } else { + capture { Statocles::Command->main( @args ) }; + } + my $err = $@; + my $port = $1 if $@ =~ qr{http://\*:(\d*)}; + is $port, 3000, 'correct port'; + ok $err, "there was anoter proccess listening at $port"; + like $@, qr{^Can't create listen socket for \(http://\*:$port\):}, + 'contains http port information'; +}; + + subtest '--date' => sub { my ( $tmp, $config_fn, $config ) = build_temp_site( $SHARE_DIR );