diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index 0bde50af..76fc8368 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -7,7 +7,6 @@ module Puma @phase = 0 @workers = [] - @worker_index = 0 @next_check = nil @phased_state = :idle @@ -99,9 +98,10 @@ module Puma end def next_worker_index - i = @worker_index - @worker_index += 1 - i + all_positions = 0...@options[:workers] + occupied_positions = @workers.map { |w| w.index } + available_positions = all_positions.to_a - occupied_positions + available_positions.first end def all_workers_booted? diff --git a/test/shell/run.sh b/test/shell/run.sh index 120b1f8d..ac63416d 100644 --- a/test/shell/run.sh +++ b/test/shell/run.sh @@ -14,4 +14,11 @@ else ERROR=2 fi +if ruby -rubygems t3.rb > /dev/null 2>&1; then + echo "t3 OK" +else + echo "t3 FAIL" + ERROR=3 +fi + exit $ERROR diff --git a/test/shell/t3.rb b/test/shell/t3.rb new file mode 100644 index 00000000..cb52428a --- /dev/null +++ b/test/shell/t3.rb @@ -0,0 +1,25 @@ +system "ruby -rubygems -I../../lib ../../bin/puma -p 10102 -C t3_conf.rb ../hello.ru &" +sleep 5 + +worker_pid_was_present = File.file? "t3-worker-2-pid" + +system "kill `cat t3-worker-2-pid`" # kill off a worker + +sleep 2 + +worker_index_within_number_of_workers = !File.file?("t3-worker-3-pid") + +system "kill `cat t3-pid`" + +File.unlink "t3-pid" if File.file? "t3-pid" +File.unlink "t3-worker-0-pid" if File.file? "t3-worker-0-pid" +File.unlink "t3-worker-1-pid" if File.file? "t3-worker-1-pid" +File.unlink "t3-worker-2-pid" if File.file? "t3-worker-2-pid" +File.unlink "t3-worker-3-pid" if File.file? "t3-worker-3-pid" + +if worker_pid_was_present and worker_index_within_number_of_workers + exit 0 +else + exit 1 +end + diff --git a/test/shell/t3_conf.rb b/test/shell/t3_conf.rb new file mode 100644 index 00000000..1be95ce2 --- /dev/null +++ b/test/shell/t3_conf.rb @@ -0,0 +1,5 @@ +pidfile "t3-pid" +workers 3 +on_worker_boot do |index| + File.open("t3-worker-#{index}-pid", "w") { |f| f.puts Process.pid } +end