mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
284a9ba8ec
The concurrent-ruby gem is a toolset containing many concurrency utilities. Many of these utilities include runtime-specific optimizations when possible. Rather than clutter the Rails codebase with concurrency utilities separate from the core task, such tools can be superseded by similar tools in the more specialized gem. This commit replaces `ActiveSupport::Concurrency::Latch` with `Concurrent::CountDownLatch`, which is functionally equivalent.
94 lines
2.5 KiB
Ruby
94 lines
2.5 KiB
Ruby
require 'abstract_unit'
|
|
require 'concurrent/atomics'
|
|
|
|
module ActionController
|
|
module Live
|
|
class ResponseTest < ActiveSupport::TestCase
|
|
def setup
|
|
@response = Live::Response.new
|
|
@response.request = ActionDispatch::Request.new({}) #yolo
|
|
end
|
|
|
|
def test_header_merge
|
|
header = @response.header.merge('Foo' => 'Bar')
|
|
assert_kind_of(ActionController::Live::Response::Header, header)
|
|
assert_not_equal header, @response.header
|
|
end
|
|
|
|
def test_initialize_with_default_headers
|
|
r = Class.new(Live::Response) do
|
|
def self.default_headers
|
|
{ 'omg' => 'g' }
|
|
end
|
|
end
|
|
|
|
header = r.new.header
|
|
assert_kind_of(ActionController::Live::Response::Header, header)
|
|
end
|
|
|
|
def test_parallel
|
|
latch = Concurrent::CountDownLatch.new
|
|
|
|
t = Thread.new {
|
|
@response.stream.write 'foo'
|
|
latch.wait
|
|
@response.stream.close
|
|
}
|
|
|
|
@response.await_commit
|
|
@response.each do |part|
|
|
assert_equal 'foo', part
|
|
latch.count_down
|
|
end
|
|
assert t.join
|
|
end
|
|
|
|
def test_setting_body_populates_buffer
|
|
@response.body = 'omg'
|
|
@response.close
|
|
assert_equal ['omg'], @response.body_parts
|
|
end
|
|
|
|
def test_cache_control_is_set
|
|
@response.stream.write 'omg'
|
|
assert_equal 'no-cache', @response.headers['Cache-Control']
|
|
end
|
|
|
|
def test_content_length_is_removed
|
|
@response.headers['Content-Length'] = "1234"
|
|
@response.stream.write 'omg'
|
|
assert_nil @response.headers['Content-Length']
|
|
end
|
|
|
|
def test_headers_cannot_be_written_after_webserver_reads
|
|
@response.stream.write 'omg'
|
|
latch = Concurrent::CountDownLatch.new
|
|
|
|
t = Thread.new {
|
|
@response.stream.each do |chunk|
|
|
latch.count_down
|
|
end
|
|
}
|
|
|
|
latch.wait
|
|
assert @response.headers.frozen?
|
|
e = assert_raises(ActionDispatch::IllegalStateError) do
|
|
@response.headers['Content-Length'] = "zomg"
|
|
end
|
|
|
|
assert_equal 'header already sent', e.message
|
|
@response.stream.close
|
|
t.join
|
|
end
|
|
|
|
def test_headers_cannot_be_written_after_close
|
|
@response.stream.close
|
|
|
|
e = assert_raises(ActionDispatch::IllegalStateError) do
|
|
@response.headers['Content-Length'] = "zomg"
|
|
end
|
|
assert_equal 'header already sent', e.message
|
|
end
|
|
end
|
|
end
|
|
end
|