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

Code before:
  end

  def test_nio4r_gem_not_required_in_master_process_when_using_control_server
    @control_tcp_port = UniquePort.call
    control_opts = "--control-url tcp://#{HOST}:#{@control_tcp_port} --control-token #{TOKEN}"
    cli_server "-w #{workers} #{control_opts} -C test/config/prune_bundler_print_nio_defined.rb test/rackup/hello.ru"

    assert wait_for_server_to_include('Starting control server')

    assert wait_for_server_to_include('defined?(::NIO): nil')
  end

  def test_application_is_loaded_exactly_once_if_using_preload_app
    cli_server "-w #{workers} --preload test/rackup/write_to_stdout_on_boot.ru"

    get_worker_pids
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 1, loading_app_count
  end

  def test_application_is_loaded_exactly_once_if_using_fork_worker
    cli_server "-w #{workers} --fork-worker test/rackup/write_to_stdout_on_boot.ru"

    get_worker_pids
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 1, loading_app_count # loaded in worker 0

    @server_log.clear
    Process.kill :SIGURG, @pid

    get_worker_pids 1, workers - 1
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 0, loading_app_count
  end

  def test_application_is_loaded_exactly_once_if_using_preload_app_with_fork_worker
    cli_server "-w #{workers} --preload --fork-worker test/rackup/write_to_stdout_on_boot.ru"

    get_worker_pids
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 1, loading_app_count # loaded in master

    @server_log.clear
    Process.kill :SIGURG, @pid

    get_worker_pids 1, workers - 1
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 0, loading_app_count
  end

  def test_warning_message_outputted_when_single_worker

Review comment (bug):
Here's an intermittent test failure when running this consecutively locally without the patch:
```
puma [fix-check-workers-phase-race-condition] bundle exec m test/test_integration_cluster.rb:495
Run options: -n "/^(test_application_is_loaded_exactly_once_if_using_fork_worker)$/" --seed 33914

# Running:

.

Fabulous run in 5.410711s, 0.1848 runs/s, 0.5545 assertions/s.

1 runs, 3 assertions, 0 failures, 0 errors, 0 skips

puma [fix-check-workers-phase-race-condition] bundle exec m test/test_integration_cluster.rb:495
Run options: -n "/^(test_application_is_loaded_exactly_once_if_using_fork_worker)$/" --seed 50616

# Running:

.

Fabulous run in 5.389803s, 0.1855 runs/s, 0.5566 assertions/s.

1 runs, 3 assertions, 0 failures, 0 errors, 0 skips

puma [fix-check-workers-phase-race-condition] bundle exec m test/test_integration_cluster.rb:495
Run options: -n "/^(test_application_is_loaded_exactly_once_if_using_fork_worker)$/" --seed 58475

# Running:

F

Fabulous run in 5.397754s, 0.1853 runs/s, 0.5558 assertions/s.
Errors & Failures:

  1) Failure:
TestIntegrationCluster#test_application_is_loaded_exactly_once_if_using_fork_worker [test/test_integration_cluster.rb:507]:
Expected: 0
  Actual: 1

1 runs, 3 assertions, 1 failures, 0 errors, 0 skips
```

Code after:

    assert wait_for_server_to_include('defined?(::NIO): nil')
  end

  def test_application_is_loaded_exactly_once_if_using_preload_app
    cli_server "-w #{workers} --preload test/rackup/write_to_stdout_on_boot.ru"

    get_worker_pids
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 1, loading_app_count
  end

  def test_application_is_loaded_exactly_once_if_using_fork_worker
    cli_server "-w #{workers} --fork-worker test/rackup/write_to_stdout_on_boot.ru"

    get_worker_pids
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 1, loading_app_count # loaded in worker 0

    @server_log.clear
    Process.kill :SIGURG, @pid

    get_worker_pids 1, workers - 1
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 0, loading_app_count
  end

  def test_application_is_loaded_exactly_once_if_using_preload_app_with_fork_worker
    cli_server "-w #{workers} --preload --fork-worker test/rackup/write_to_stdout_on_boot.ru"

    get_worker_pids
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 1, loading_app_count # loaded in master

    @server_log.clear
    Process.kill :SIGURG, @pid

    get_worker_pids 1, workers - 1
    loading_app_count = @server_log.scan("Loading app").length
    assert_equal 0, loading_app_count
  end

  def test_warning_message_outputted_when_single_worker
    cli_server "-w 1 test/rackup/hello.ru"

    assert wait_for_server_to_include('Worker 0 (PID')
    assert_match(/WARNING: Detected running cluster mode with 1 worker/, @server_log)
  end

  def test_warning_message_outputted_when_ruby_mn_threads_is_set
    cli_server "-w 1 test/rackup/hello.ru", env: { 'RUBY_MN_THREADS' => '1' }