class ActionController::LiveStreamTest

Public Instance Methods

assert_stream_closed() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 266
def assert_stream_closed
  assert response.stream.closed?, "stream should be closed"
  assert response.committed?,     "response should be committed"
  assert response.sent?,          "response should be sent"
end
capture_log_output() { |output| ... } click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 272
def capture_log_output
  output = StringIO.new
  old_logger, ActionController::Base.logger = ActionController::Base.logger, ActiveSupport::Logger.new(output)

  begin
    yield output
  ensure
    ActionController::Base.logger = old_logger
  end
end
setup() click to toggle source
Calls superclass method
# File actionpack/test/controller/live_stream_test.rb, line 283
def setup
  super

  def @controller.new_controller_thread
    Thread.new { yield }
  end
end
test_abort_with_full_buffer() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 332
def test_abort_with_full_buffer
  @controller.latch = Concurrent::CountDownLatch.new
  @controller.error_latch = Concurrent::CountDownLatch.new

  capture_log_output do |output|
    get :overfill_buffer_and_die, format: "plain"

    t = Thread.new(response) { |resp|
      resp.await_commit
      _, _, body = resp.to_a
      body.each do
        @controller.latch.wait
        body.close
        break
      end
    }

    t.join
    @controller.error_latch.wait
    assert_match "Error while streaming", output.rewind && output.read
  end
end
test_async_stream() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 311
def test_async_stream
  rubinius_skip "https://github.com/rubinius/rubinius/issues/2934"

  @controller.latch = Concurrent::CountDownLatch.new
  parts             = ["hello", "world"]

  get :blocking_stream

  t = Thread.new(response) { |resp|
    resp.await_commit
    resp.stream.each do |part|
      assert_equal parts.shift, part
      ol = @controller.latch
      @controller.latch = Concurrent::CountDownLatch.new
      ol.count_down
    end
  }

  assert t.join(3), "timeout expired before the thread terminated"
end
test_bad_request_in_controller_before_streaming() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 446
def test_bad_request_in_controller_before_streaming
  assert_raises(ActionController::BadRequest) do
    get :bad_request_error, format: "text/event-stream"
  end
end
test_delayed_autoload_after_write_within_interlock_hook() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 303
def test_delayed_autoload_after_write_within_interlock_hook
  # Simulate InterlockHook
  ActiveSupport::Dependencies.interlock.start_running
  res = get :write_sleep_autoload
  res.each {}
  ActiveSupport::Dependencies.interlock.done_running
end
test_exception_callback_when_committed() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 425
def test_exception_callback_when_committed
  current_threads = Thread.list

  capture_log_output do |output|
    get :exception_with_callback, format: "text/event-stream"

    # Wait on the execution of all threads
    (Thread.list - current_threads).each(&:join)

    assert_equal %(data: "500 Internal Server Error"\n\n), response.body
    assert_match "An exception occurred...", output.rewind && output.read
    assert_stream_closed
  end
end
test_exception_handling_html() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 397
def test_exception_handling_html
  assert_raises(ActionView::MissingTemplate) do
    get :exception_in_view
  end

  capture_log_output do |output|
    get :exception_in_view_after_commit
    assert_match %r((window\.location = "/500\.html"</script></html>)$), response.body
    assert_match "Missing template test/doesntexist", output.rewind && output.read
    assert_stream_closed
  end
  assert response.body
  assert_stream_closed
end
test_exception_handling_plain_text() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 412
def test_exception_handling_plain_text
  assert_raises(ActionView::MissingTemplate) do
    get :exception_in_view, format: :json
  end

  capture_log_output do |output|
    get :exception_in_view_after_commit, format: :json
    assert_equal "", response.body
    assert_match "Missing template test/doesntexist", output.rewind && output.read
    assert_stream_closed
  end
end
test_exception_in_controller_before_streaming() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 440
def test_exception_in_controller_before_streaming
  assert_raises(ActionController::LiveStreamTest::Exception) do
    get :exception_in_controller, format: "text/event-stream"
  end
end
test_exceptions_raised_handling_exceptions_and_committed() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 452
def test_exceptions_raised_handling_exceptions_and_committed
  capture_log_output do |output|
    get :exception_in_exception_callback, format: "text/event-stream"
    assert_equal "", response.body
    assert_match "We need to go deeper", output.rewind && output.read
    assert_stream_closed
  end
end
test_ignore_client_disconnect() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 355
def test_ignore_client_disconnect
  @controller.latch = Concurrent::CountDownLatch.new

  capture_log_output do |output|
    get :ignore_client_disconnect

    t = Thread.new(response) { |resp|
      resp.await_commit
      _, _, body = resp.to_a
      body.each do
        body.close
        break
      end
    }

    t.join
    Timeout.timeout(3) do
      @controller.latch.wait
    end
    assert_match "Work complete", output.rewind && output.read
  end
end
test_live_stream_default_header() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 386
def test_live_stream_default_header
  get :default_header
  assert response.headers["Content-Type"]
end
test_render_text() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 391
def test_render_text
  get :render_text
  assert_equal "zomg", response.body
  assert_stream_closed
end
test_stale_with_etag() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 466
def test_stale_with_etag
  @request.if_none_match = %(W/"#{Digest::MD5.hexdigest('123')}")
  get :with_stale
  assert_equal 304, response.status.to_i
end
test_stale_without_etag() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 461
def test_stale_without_etag
  get :with_stale
  assert_equal 200, response.status.to_i
end
test_thread_locals_get_copied() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 378
def test_thread_locals_get_copied
  @controller.tc = self
  Thread.current[:originating_thread] = Thread.current.object_id
  Thread.current[:setting]            = "aaron"

  get :thread_locals
end
test_write_to_stream() click to toggle source
# File actionpack/test/controller/live_stream_test.rb, line 297
def test_write_to_stream
  get :basic_stream
  assert_equal "helloworld", @response.body
  assert_equal "text/event-stream", @response.headers["Content-Type"]
end