Repository: puma/puma
File: test/test_integration_cluster.rb (Ruby)

Code before:
        exitstatus = Process.detach(pid).value.exitstatus
        [200, {}, [exitstatus.to_s]]
      end
    RUBY
    assert_equal '0', read_body(connect)
  end

  def test_fork_worker_phased_restart_with_high_worker_count
    cli_server "test/rackup/hello.ru", config: <<~RUBY
      fork_worker 0
      # to simulate worker 0 timeout, total boot time for all workers
      # needs to exceed single worker timeout
      workers 11
      # lower worker timeout from default (60) to avoid test timeout
      worker_timeout 6
    RUBY

    while (line = @server.gets).include?("phase: 0")
      # wait till all the workers are up
      break if line.include?("Worker 10")
    end

    Process.kill :USR1, @pid

    while (line = @server.gets)
      refute line.include?("Terminating timed out worker")
      break if line.include?("Worker 10")
    end
  end

  def test_prune_bundler_with_multiple_workers
    cli_server "-C test/config/prune_bundler_with_multiple_workers.rb"
    reply = read_body(connect)

    assert reply, "embedded app"
  end

  def test_load_path_includes_extra_deps
    cli_server "-w #{workers} -C test/config/prune_bundler_with_deps.rb test/rackup/hello.ru"

    load_path = []
    while (line = @server.gets) =~ /^LOAD_PATH/
      load_path << line.gsub(/^LOAD_PATH: /, '')
    end
    assert_match(%r{gems/minitest-[\d.]+/lib$}, load_path.last)
  end

  def test_load_path_does_not_include_nio4r
    cli_server "-w #{workers} -C test/config/prune_bundler_with_deps.rb test/rackup/hello.ru"

    load_path = []

Review comment (bug):
Before (with line logs):
```
bundle exec m test/test_integration_cluster.rb:290
Run options: -n "/^(test_fork_worker_phased_restart_with_high_worker_count)$/" --seed 13311

# Running:

"[43166] - Worker 0 (PID: 43169) booted in 0.0s, phase: 0\n"
"[43166] - Worker 1 (PID: 43170) booted in 0.0s, phase: 0\n"
"[43166] - Worker 2 (PID: 43171) booted in 0.0s, phase: 0\n"
"[43166] - Worker 3 (PID: 43172) booted in 0.01s, phase: 0\n"
"[43166] - Worker 4 (PID: 43173) booted in 0.01s, phase: 0\n"
"[43166] - Worker 5 (PID: 43174) booted in 0.01s, phase: 0\n"
"[43166] - Worker 6 (PID: 43175) booted in 0.01s, phase: 0\n"
"[43166] - Worker 7 (PID: 43176) booted in 0.01s, phase: 0\n"
"[43166] - Worker 8 (PID: 43177) booted in 0.01s, phase: 0\n"
"[43166] - Worker 9 (PID: 43178) booted in 0.01s, phase: 0\n"
"[43166] - Worker 10 (PID: 43179) booted in 0.01s, phase: 0\n"
"[43166] - Worker 11 (PID: 43180) booted in 0.01s, phase: 0\n"
"[43166] - Worker 12 (PID: 43181) booted in 0.01s, phase: 0\n"
"[43166] - Worker 13 (PID: 43182) booted in 0.01s, phase: 0\n"
"[43166] - Worker 15 (PID: 43184) booted in 0.01s, phase: 0\n"
"[43166] - Starting phased worker restart, phase: 1\n"
"[43166] + Changing to /Users/joshuayoung/Projects/puma\n"
"[43166] - Worker 14 (PID: 43183) booted in 0.01s, phase: 0\n"
"[43166] - Stopping 43169 for phased upgrade...\n"
"[43166] - TERM sent to 43169...\n"
"[43166] - Stopping 43169 for phased upgrade...\n"
"[43166] - Worker 0 (PID: 43187) booted in 0.01s, phase: 1\n"
"[43166] - Stopping 43170 for phased upgrade...\n"
"[43166] - TERM sent to 43170...\n"
"[43166] - Worker 1 (PID: 43189) booted in 0.01s, phase: 1\n"
"[43166] - Stopping 43171 for phased upgrade...\n"
"[43166] - TERM sent to 43171...\n"
"[43166] ! Terminating timed out worker (Worker 0 failed to check in within 2 seconds): 43187\n"
F

Fabulous run in 3.539996s, 0.2825 runs/s, 3.9548 assertions/s.
Errors & Failures:

  1) Failure:
TestIntegrationCluster#test_fork_worker_phased_restart_with_high_worker_count [/Users/joshuayoung/Projects/puma/test/test_integration_cluster.rb:308]:
Expected true to not be truthy.

1 runs, 14 assertions, 1 failures, 0 errors, 0 skips
```

Code after:
      app do |_|
        pid = spawn('ls', [:out, :err]=>'/dev/null')
        sleep 0.01
        exitstatus = Process.detach(pid).value.exitstatus
        [200, {}, [exitstatus.to_s]]
      end
    RUBY
    assert_equal '0', read_body(connect)
  end

  def test_fork_worker_phased_restart_with_high_worker_count
    worker_count = 10

    cli_server "test/rackup/hello.ru", config: <<~RUBY
      fork_worker 0
      worker_check_interval 1
      # lower worker timeout from default (60) to avoid test timeout
      worker_timeout 2
      # to simulate worker 0 timeout, total boot time for all workers
      # needs to exceed single worker timeout
      workers #{worker_count}
    RUBY

    # workers is the default
    get_worker_pids 0, worker_count

    Process.kill :USR1, @pid

    get_worker_pids 1, worker_count

    # below is so all of @server_log isn't output for failure
    refute @server_log[/.*Terminating timed out worker.*/]
  end

  def test_prune_bundler_with_multiple_workers
    cli_server "-C test/config/prune_bundler_with_multiple_workers.rb"
    reply = read_body(connect)

    assert reply, "embedded app"
  end

  def test_load_path_includes_extra_deps
    cli_server "-w #{workers} -C test/config/prune_bundler_with_deps.rb test/rackup/hello.ru",
      no_wait: true

    load_path = []
    load_path << wait_for_server_to_match(/\ALOAD_PATH: (.+)/, 1)
    while (line = @server.gets).start_with? 'LOAD_PATH: '
      load_path << line.sub(/\ALOAD_PATH: /, '')
    end
    assert_match(%r{gems/minitest-[\d.]+/lib$}, load_path.last)