diff --git a/Rakefile b/Rakefile index 3f64f0d1..39975921 100644 --- a/Rakefile +++ b/Rakefile @@ -104,3 +104,14 @@ if IS_JRUBY else task :test => [:compile] end + +namespace :test do + desc "Run the integration tests" + task :integration do + sh "cd test/shell; sh run.sh" + end + + desc "Run all tests" + task :all => [:test, "test:integration"] +end + diff --git a/lib/puma/cli.rb b/lib/puma/cli.rb index 012e5d25..f5bd623b 100644 --- a/lib/puma/cli.rb +++ b/lib/puma/cli.rb @@ -323,6 +323,20 @@ module Puma log " - Goodbye!" end + def redirect_io + stdout = @options[:redirect_stdout] + stderr = @options[:redirect_stderr] + append = @options[:redirect_append] + + if stdout + STDOUT.reopen stdout, (append ? "a" : "w") + end + + if stderr + STDOUT.reopen stderr, (append ? "a" : "w") + end + end + # Parse the options, load the rackup, start the server and wait # for it to finish. # @@ -349,8 +363,6 @@ module Puma write_pid write_state - @binder.parse @options[:binds], self - if clustered run_cluster else @@ -362,15 +374,17 @@ module Puma min_t = @options[:min_threads] max_t = @options[:max_threads] + log "Puma #{Puma::Const::PUMA_VERSION} starting..." + log "* Min threads: #{min_t}, max threads: #{max_t}" + log "* Environment: #{ENV['RACK_ENV']}" + + @binder.parse @options[:binds], self + server = Puma::Server.new @config.app, @events server.binder = @binder server.min_threads = min_t server.max_threads = max_t - log "Puma #{Puma::Const::PUMA_VERSION} starting..." - log "* Min threads: #{min_t}, max threads: #{max_t}" - log "* Environment: #{ENV['RACK_ENV']}" - @server = server if str = @options[:control_url] @@ -429,6 +443,8 @@ module Puma log "Use Ctrl-C to stop" end + redirect_io + begin server.run.join rescue Interrupt @@ -524,6 +540,8 @@ module Puma log "* Min threads: #{@options[:min_threads]}, max threads: #{@options[:max_threads]}" log "* Environment: #{ENV['RACK_ENV']}" + @binder.parse @options[:binds], self + @master_pid = Process.pid read, write = IO.pipe @@ -560,12 +578,12 @@ module Puma if @options[:daemon] Process.daemon(true) - STDOUT.reopen "/tmp/puma.out", "a" - STDOUT.reopen "/tmp/puma.err", "a" else log "Use Ctrl-C to stop" end + redirect_io + spawn_workers begin diff --git a/lib/puma/configuration.rb b/lib/puma/configuration.rb index a8cd897b..a8350b36 100644 --- a/lib/puma/configuration.rb +++ b/lib/puma/configuration.rb @@ -195,13 +195,9 @@ module Puma # Redirect STDOUT and STDERR to files specified. def stdout_redirect(stdout=nil, stderr=nil, append=false) - if stdout - STDOUT.reopen stdout, (append ? "a" : "w") - end - - if stderr - STDOUT.reopen stderr, (append ? "a" : "w") - end + @options[:redirect_stdout] = stdout + @options[:redirect_stderr] = stderr + @options[:redirect_append] = append end # Configure +min+ to be the minimum number of threads to use to answer diff --git a/test/shell/run.sh b/test/shell/run.sh new file mode 100644 index 00000000..12d19337 --- /dev/null +++ b/test/shell/run.sh @@ -0,0 +1,7 @@ +if ruby t1.rb > /dev/null 2>&1; then + echo "t1 OK" + exit 0 +else + echo "t1 FAIL" + exit 1 +fi diff --git a/test/shell/t1.rb b/test/shell/t1.rb new file mode 100644 index 00000000..2ecc0078 --- /dev/null +++ b/test/shell/t1.rb @@ -0,0 +1,19 @@ +system "ruby -I../../lib ../../bin/puma -p 10102 -C t1_conf.rb ../hello.ru &" +sleep 5 +system "curl http://localhost:10102/" + +system "kill `cat t1-pid`" + +sleep 1 + +log = File.read("t1-stdout") + +File.unlink "t1-stdout" +File.unlink "t1-pid" + +if log =~ %r!GET / HTTP/1\.1! + exit 0 +else + exit 1 +end + diff --git a/test/shell/t1_conf.rb b/test/shell/t1_conf.rb new file mode 100644 index 00000000..bde7167c --- /dev/null +++ b/test/shell/t1_conf.rb @@ -0,0 +1,2 @@ +stdout_redirect "t1-stdout" +pidfile "t1-pid"