Skip to content

Commit

Permalink
Improve pidfile reporting and test coverage
Browse files Browse the repository at this point in the history
 * Output reduced to a single line
 * Integration test added that also suppresses and checks output
  • Loading branch information
raggi committed May 13, 2012
1 parent b4d7b95 commit edc8b92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
9 changes: 2 additions & 7 deletions lib/rack/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,10 @@ def write_pid

def check_pid!
case pidfile_process_status
when :running
STDERR.puts "The process is still alive. aborting."
exit(1)
when :not_owned
STDERR.puts "The process is still alive. And owned by another user aborting."
when :running, :not_owned
$stderr.puts "A server is already running. Check #{options[:pid]}."
exit(1)
when :dead
STDERR.puts "The process is no longer running. Removing the pid file."
::File.delete(options[:pid])
end
end
Expand All @@ -341,7 +337,6 @@ def pidfile_process_status
return :exited unless ::File.exist?(options[:pid])

pid = ::File.read(options[:pid]).to_i
STDERR.puts "There is already a pid file wrote a by process with pid #{pid}"
Process.kill(0, pid)
:running
rescue Errno::ESRCH
Expand Down
21 changes: 21 additions & 0 deletions test/spec_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def app
lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }
end

def with_stderr
old, $stderr = $stderr, StringIO.new
yield $stderr
ensure
$stderr = old
end

it "overrides :config if :app is passed in" do
server = Rack::Server.new(:app => "FOO")
server.app.should == "FOO"
Expand Down Expand Up @@ -97,4 +104,18 @@ def app
server.send(:pidfile_process_status).should.eql :not_owned
end

should "inform the user about existing pidfiles with running processes" do
pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
server = Rack::Server.new(:pid => pidfile)
with_stderr do |err|
should.raise(SystemExit) do
server.start
end
err.rewind
output = err.read
output.should.match(/already running/)
output.should.include? pidfile
end
end

end

0 comments on commit edc8b92

Please sign in to comment.