From b988e0779814a73876a4a06df0a90a3f85fb08c8 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 6 Jun 2023 11:02:29 +0000 Subject: [PATCH 3/4] tests: handle $/ assignment deprecation ...by testing less. `env["rack.input"].gets' users are out-of-luck if they want anything other than "\n" or `nil', I suppose... `$/' is non-thread-local and thus non-thread-safe, which doesn't affect unicorn itself, but Ruby deprecates it for single-threaded code, too, unfortunately. Rack::Lint doesn't allow separator arguments for #gets, either, so we can't support that, either... --- test/unit/test_stream_input.rb | 25 ++++++++++++++++--------- test/unit/test_tee_input.rb | 19 +++++++++---------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/test/unit/test_stream_input.rb b/test/unit/test_stream_input.rb index 1a07ec3a..2a14135b 100644 --- a/test/unit/test_stream_input.rb +++ b/test/unit/test_stream_input.rb @@ -6,7 +6,8 @@ class TestStreamInput < Test::Unit::TestCase def setup - @rs = $/ + @rs = "\n" + $/ == "\n" or abort %q{test broken if \$/ != "\\n"} @env = {} @rd, @wr = Kgio::UNIXSocket.pair @rd.sync = @wr.sync = true @@ -15,7 +16,6 @@ def setup def teardown return if $$ != @start_pid - $/ = @rs @rd.close rescue nil @wr.close rescue nil Process.waitall @@ -54,11 +54,18 @@ def test_gets_multiline end def test_gets_empty_rs - $/ = nil r = init_request("a\nb\n\n") si = Unicorn::StreamInput.new(@rd, r) - assert_equal "a\nb\n\n", si.gets - assert_nil si.gets + pid = fork do # to avoid $/ warning (hopefully) + $/ = nil + @rd.close + @wr.write(si.gets) + @wr.close + end + @wr.close + assert_equal "a\nb\n\n", @rd.read + pid, status = Process.waitpid2(pid) + assert_predicate status, :success? end def test_read_with_equal_len @@ -90,21 +97,21 @@ def test_big_body_multi end def test_gets_long - r = init_request("hello", 5 + (4096 * 4 * 3) + "#$/foo#$/".size) + r = init_request("hello", 5 + (4096 * 4 * 3) + "#{@rs}foo#{@rs}".size) si = Unicorn::StreamInput.new(@rd, r) status = line = nil pid = fork { @rd.close 3.times { @wr.write("ffff" * 4096) } - @wr.write "#$/foo#$/" + @wr.write "#{@rs}foo#{@rs}" @wr.close } @wr.close line = si.gets assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size) - assert_equal("hello" << ("ffff" * 4096 * 3) << "#$/", line) + assert_equal("hello" << ("ffff" * 4096 * 3) << "#{@rs}", line) line = si.gets - assert_equal "foo#$/", line + assert_equal "foo#{@rs}", line assert_nil si.gets pid, status = Process.waitpid2(pid) assert status.success? diff --git a/test/unit/test_tee_input.rb b/test/unit/test_tee_input.rb index 4647e661..6f5bc8a7 100644 --- a/test/unit/test_tee_input.rb +++ b/test/unit/test_tee_input.rb @@ -9,17 +9,16 @@ class TeeInput < Unicorn::TeeInput end class TestTeeInput < Test::Unit::TestCase - def setup - @rs = $/ @rd, @wr = Kgio::UNIXSocket.pair @rd.sync = @wr.sync = true @start_pid = $$ + @rs = "\n" + $/ == "\n" or abort %q{test broken if \$/ != "\\n"} end def teardown return if $$ != @start_pid - $/ = @rs @rd.close rescue nil @wr.close rescue nil begin @@ -37,38 +36,38 @@ def check_tempfiles end def test_gets_long - r = init_request("hello", 5 + (4096 * 4 * 3) + "#$/foo#$/".size) + r = init_request("hello", 5 + (4096 * 4 * 3) + "#{@rs}foo#{@rs}".size) ti = TeeInput.new(@rd, r) status = line = nil pid = fork { @rd.close 3.times { @wr.write("ffff" * 4096) } - @wr.write "#$/foo#$/" + @wr.write "#{@rs}foo#{@rs}" @wr.close } @wr.close line = ti.gets assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size) - assert_equal("hello" << ("ffff" * 4096 * 3) << "#$/", line) + assert_equal("hello" << ("ffff" * 4096 * 3) << "#{@rs}", line) line = ti.gets - assert_equal "foo#$/", line + assert_equal "foo#{@rs}", line assert_nil ti.gets pid, status = Process.waitpid2(pid) assert status.success? end def test_gets_short - r = init_request("hello", 5 + "#$/foo".size) + r = init_request("hello", 5 + "#{@rs}foo".size) ti = TeeInput.new(@rd, r) status = line = nil pid = fork { @rd.close - @wr.write "#$/foo" + @wr.write "#{@rs}foo" @wr.close } @wr.close line = ti.gets - assert_equal("hello#$/", line) + assert_equal("hello#{@rs}", line) line = ti.gets assert_equal "foo", line assert_nil ti.gets