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

Code before:

  def test_warning_message_not_outputted_when_single_worker_silenced
    cli_server "-w 1 test/rackup/hello.ru", config: "silence_single_worker_warning"

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

  def test_signal_ttin
    cli_server "-w 2 test/rackup/hello.ru"
    get_worker_pids # to consume server logs

    Process.kill :TTIN, @pid

    assert wait_for_server_to_match(/Worker 2 \(PID: \d+\) booted in/)
  end

  def test_signal_ttou
    cli_server "-w 2 test/rackup/hello.ru"
    get_worker_pids # to consume server logs

    Process.kill :TTOU, @pid

    assert wait_for_server_to_match(/Worker 1 \(PID: \d+\) terminating/)
  end

  def test_culling_strategy_youngest
    cli_server "-w 2 test/rackup/hello.ru", config: "worker_culling_strategy :youngest"
    get_worker_pids # to consume server logs

    Process.kill :TTIN, @pid

    assert wait_for_server_to_match(/Worker 2 \(PID: \d+\) booted in/)

    Process.kill :TTOU, @pid

    assert wait_for_server_to_match(/Worker 2 \(PID: \d+\) terminating/)
  end

  def test_culling_strategy_oldest
    cli_server "-w 2 test/rackup/hello.ru", config: "worker_culling_strategy :oldest"
    get_worker_pids # to consume server logs

    Process.kill :TTIN, @pid

    assert wait_for_server_to_match(/Worker 2 \(PID: \d+\) booted in/)

    Process.kill :TTOU, @pid

    assert wait_for_server_to_match(/Worker 0 \(PID: \d+\) terminating/)
  end

Review comment (bug):
These pass on master as well, I added it to ensure I don't regress on this behaviour. I've also done some puts debugging to verify this manually.

Code after:
    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' }

    assert wait_for_server_to_include('Worker 0 (PID')
    assert_match(/WARNING: Detected `RUBY_MN_THREADS/, @server_log)
  end

  def test_warning_message_not_outputted_when_single_worker_silenced
    cli_server "-w 1 test/rackup/hello.ru", config: "silence_single_worker_warning"

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

  def test_signal_ttin
    cli_server "-w 2 test/rackup/hello.ru"
    get_worker_pids # to consume server logs