2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "concurrent/atomic/count_down_latch"
|
2014-02-28 18:22:35 -05:00
|
|
|
Thread.abort_on_exception = true
|
2012-07-29 20:26:07 -04:00
|
|
|
|
|
|
|
module ActionController
|
2013-07-14 18:48:57 -04:00
|
|
|
class SSETest < ActionController::TestCase
|
|
|
|
class SSETestController < ActionController::Base
|
|
|
|
include ActionController::Live
|
|
|
|
|
|
|
|
def basic_sse
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2013-07-14 18:48:57 -04:00
|
|
|
sse = SSE.new(response.stream)
|
|
|
|
sse.write("{\"name\":\"John\"}")
|
2016-08-06 13:44:11 -04:00
|
|
|
sse.write(name: "Ryan")
|
2013-07-14 18:48:57 -04:00
|
|
|
ensure
|
|
|
|
sse.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def sse_with_event
|
|
|
|
sse = SSE.new(response.stream, event: "send-name")
|
|
|
|
sse.write("{\"name\":\"John\"}")
|
2016-08-06 13:44:11 -04:00
|
|
|
sse.write(name: "Ryan")
|
2013-07-14 18:48:57 -04:00
|
|
|
ensure
|
|
|
|
sse.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def sse_with_retry
|
|
|
|
sse = SSE.new(response.stream, retry: 1000)
|
|
|
|
sse.write("{\"name\":\"John\"}")
|
|
|
|
sse.write({ name: "Ryan" }, retry: 1500)
|
|
|
|
ensure
|
|
|
|
sse.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def sse_with_id
|
|
|
|
sse = SSE.new(response.stream)
|
|
|
|
sse.write("{\"name\":\"John\"}", id: 1)
|
|
|
|
sse.write({ name: "Ryan" }, id: 2)
|
|
|
|
ensure
|
|
|
|
sse.close
|
|
|
|
end
|
2014-05-12 11:44:26 -04:00
|
|
|
|
|
|
|
def sse_with_multiple_line_message
|
|
|
|
sse = SSE.new(response.stream)
|
|
|
|
sse.write("first line.\nsecond line.")
|
|
|
|
ensure
|
|
|
|
sse.close
|
|
|
|
end
|
2013-07-14 18:48:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
tests SSETestController
|
|
|
|
|
|
|
|
def wait_for_response_stream_close
|
2014-03-12 20:40:08 -04:00
|
|
|
response.body
|
2013-07-14 18:48:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_basic_sse
|
|
|
|
get :basic_sse
|
|
|
|
|
|
|
|
wait_for_response_stream_close
|
|
|
|
assert_match(/data: {\"name\":\"John\"}/, response.body)
|
|
|
|
assert_match(/data: {\"name\":\"Ryan\"}/, response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sse_with_event_name
|
|
|
|
get :sse_with_event
|
|
|
|
|
|
|
|
wait_for_response_stream_close
|
|
|
|
assert_match(/data: {\"name\":\"John\"}/, response.body)
|
|
|
|
assert_match(/data: {\"name\":\"Ryan\"}/, response.body)
|
|
|
|
assert_match(/event: send-name/, response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sse_with_retry
|
|
|
|
get :sse_with_retry
|
|
|
|
|
|
|
|
wait_for_response_stream_close
|
|
|
|
first_response, second_response = response.body.split("\n\n")
|
|
|
|
assert_match(/data: {\"name\":\"John\"}/, first_response)
|
|
|
|
assert_match(/retry: 1000/, first_response)
|
|
|
|
|
|
|
|
assert_match(/data: {\"name\":\"Ryan\"}/, second_response)
|
|
|
|
assert_match(/retry: 1500/, second_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sse_with_id
|
|
|
|
get :sse_with_id
|
|
|
|
|
|
|
|
wait_for_response_stream_close
|
|
|
|
first_response, second_response = response.body.split("\n\n")
|
|
|
|
assert_match(/data: {\"name\":\"John\"}/, first_response)
|
|
|
|
assert_match(/id: 1/, first_response)
|
|
|
|
|
|
|
|
assert_match(/data: {\"name\":\"Ryan\"}/, second_response)
|
|
|
|
assert_match(/id: 2/, second_response)
|
|
|
|
end
|
2014-05-12 11:44:26 -04:00
|
|
|
|
|
|
|
def test_sse_with_multiple_line_message
|
|
|
|
get :sse_with_multiple_line_message
|
|
|
|
|
|
|
|
wait_for_response_stream_close
|
|
|
|
first_response, second_response = response.body.split("\n")
|
|
|
|
assert_match(/data: first line/, first_response)
|
|
|
|
assert_match(/data: second line/, second_response)
|
|
|
|
end
|
2013-07-14 18:48:57 -04:00
|
|
|
end
|
|
|
|
|
2012-07-29 22:23:19 -04:00
|
|
|
class LiveStreamTest < ActionController::TestCase
|
2014-02-28 18:39:08 -05:00
|
|
|
class Exception < StandardError
|
|
|
|
end
|
|
|
|
|
2012-07-29 20:26:07 -04:00
|
|
|
class TestController < ActionController::Base
|
2012-07-29 22:23:19 -04:00
|
|
|
include ActionController::Live
|
|
|
|
|
2015-09-23 19:40:56 -04:00
|
|
|
attr_accessor :latch, :tc, :error_latch
|
2012-07-29 22:23:19 -04:00
|
|
|
|
2012-07-29 20:26:07 -04:00
|
|
|
def self.controller_path
|
2016-08-06 12:54:50 -04:00
|
|
|
"test"
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
|
|
|
|
2014-03-12 19:07:04 -04:00
|
|
|
def set_cookie
|
|
|
|
cookies[:hello] = "world"
|
|
|
|
response.stream.write "hello world"
|
|
|
|
response.close
|
|
|
|
end
|
|
|
|
|
2012-07-29 23:33:16 -04:00
|
|
|
def render_text
|
2016-08-06 12:54:50 -04:00
|
|
|
render plain: "zomg"
|
2012-07-29 23:33:16 -04:00
|
|
|
end
|
|
|
|
|
2012-07-29 23:26:40 -04:00
|
|
|
def default_header
|
|
|
|
response.stream.write "<html><body>hi</body></html>"
|
|
|
|
response.stream.close
|
|
|
|
end
|
|
|
|
|
2012-07-29 20:26:07 -04:00
|
|
|
def basic_stream
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2012-07-29 22:23:19 -04:00
|
|
|
%w{ hello world }.each do |word|
|
|
|
|
response.stream.write word
|
|
|
|
end
|
|
|
|
response.stream.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocking_stream
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2012-07-29 22:23:19 -04:00
|
|
|
%w{ hello world }.each do |word|
|
|
|
|
response.stream.write word
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.wait
|
2012-07-29 22:23:19 -04:00
|
|
|
end
|
|
|
|
response.stream.close
|
|
|
|
end
|
|
|
|
|
2016-08-15 17:08:46 -04:00
|
|
|
def write_sleep_autoload
|
|
|
|
path = File.join(File.dirname(__FILE__), "../fixtures")
|
|
|
|
ActiveSupport::Dependencies.autoload_paths << path
|
|
|
|
|
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
|
|
|
response.stream.write "before load"
|
|
|
|
sleep 0.01
|
2016-10-07 15:21:48 -04:00
|
|
|
silence_warning do
|
|
|
|
::LoadMe
|
|
|
|
end
|
2016-08-15 17:08:46 -04:00
|
|
|
response.stream.close
|
|
|
|
latch.count_down
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
ActiveSupport::Dependencies.autoload_paths.reject! { |p| p == path }
|
2016-08-15 17:08:46 -04:00
|
|
|
end
|
|
|
|
|
2012-07-29 22:23:19 -04:00
|
|
|
def thread_locals
|
2016-08-06 12:54:50 -04:00
|
|
|
tc.assert_equal "aaron", Thread.current[:setting]
|
2012-07-29 22:23:19 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2012-07-29 20:26:07 -04:00
|
|
|
%w{ hello world }.each do |word|
|
|
|
|
response.stream.write word
|
|
|
|
end
|
|
|
|
response.stream.close
|
|
|
|
end
|
2013-03-14 04:13:10 -04:00
|
|
|
|
|
|
|
def with_stale
|
2016-08-06 12:54:50 -04:00
|
|
|
render plain: "stale" if stale?(etag: "123", template: false)
|
2013-03-14 04:13:10 -04:00
|
|
|
end
|
2013-03-07 17:49:03 -05:00
|
|
|
|
|
|
|
def exception_in_view
|
2016-08-06 12:54:50 -04:00
|
|
|
render "doesntexist"
|
2013-03-07 17:49:03 -05:00
|
|
|
end
|
|
|
|
|
2014-02-17 21:41:21 -05:00
|
|
|
def exception_in_view_after_commit
|
|
|
|
response.stream.write ""
|
2016-08-06 12:54:50 -04:00
|
|
|
render "doesntexist"
|
2014-02-17 21:41:21 -05:00
|
|
|
end
|
|
|
|
|
2013-03-07 17:49:03 -05:00
|
|
|
def exception_with_callback
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2013-03-07 17:49:03 -05:00
|
|
|
|
|
|
|
response.stream.on_error do
|
|
|
|
response.stream.write %(data: "500 Internal Server Error"\n\n)
|
|
|
|
response.stream.close
|
|
|
|
end
|
|
|
|
|
2014-02-28 18:39:08 -05:00
|
|
|
response.stream.write "" # make sure the response is committed
|
2016-08-06 12:54:50 -04:00
|
|
|
raise "An exception occurred..."
|
2013-03-07 17:49:03 -05:00
|
|
|
end
|
|
|
|
|
2014-02-15 16:05:00 -05:00
|
|
|
def exception_in_controller
|
2016-08-06 12:54:50 -04:00
|
|
|
raise Exception, "Exception in controller"
|
2014-02-15 16:05:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def bad_request_error
|
|
|
|
raise ActionController::BadRequest
|
|
|
|
end
|
|
|
|
|
2013-03-07 17:49:03 -05:00
|
|
|
def exception_in_exception_callback
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2013-03-07 17:49:03 -05:00
|
|
|
response.stream.on_error do
|
2016-08-06 12:54:50 -04:00
|
|
|
raise "We need to go deeper."
|
2013-03-07 17:49:03 -05:00
|
|
|
end
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write ""
|
2013-03-07 17:49:03 -05:00
|
|
|
response.stream.write params[:widget][:didnt_check_for_nil]
|
|
|
|
end
|
2014-05-31 21:05:00 -04:00
|
|
|
|
|
|
|
def overfill_buffer_and_die
|
2015-09-23 19:40:56 -04:00
|
|
|
logger = ActionController::Base.logger || Logger.new($stdout)
|
|
|
|
response.stream.on_error do
|
2016-08-06 12:54:50 -04:00
|
|
|
logger.warn "Error while streaming."
|
2015-09-23 19:40:56 -04:00
|
|
|
error_latch.count_down
|
|
|
|
end
|
|
|
|
|
2014-05-31 21:05:00 -04:00
|
|
|
# Write until the buffer is full. It doesn't expose that
|
|
|
|
# information directly, so we must hard-code its size:
|
|
|
|
10.times do
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write "."
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
|
|
|
# .. plus one more, because the #each frees up a slot:
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write "."
|
2014-05-31 21:05:00 -04:00
|
|
|
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.count_down
|
2014-05-31 21:05:00 -04:00
|
|
|
|
|
|
|
# This write will block, and eventually raise
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write "x"
|
2014-05-31 21:05:00 -04:00
|
|
|
|
|
|
|
20.times do
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write "."
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignore_client_disconnect
|
|
|
|
response.stream.ignore_disconnect = true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write "" # commit
|
2014-05-31 21:05:00 -04:00
|
|
|
|
|
|
|
# These writes will be ignored
|
|
|
|
15.times do
|
2016-08-06 12:54:50 -04:00
|
|
|
response.stream.write "x"
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
logger.info "Work complete"
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.count_down
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
tests TestController
|
|
|
|
|
2013-03-07 17:49:03 -05:00
|
|
|
def assert_stream_closed
|
2016-08-06 12:54:50 -04:00
|
|
|
assert response.stream.closed?, "stream should be closed"
|
|
|
|
assert response.committed?, "response should be committed"
|
|
|
|
assert response.sent?, "response should be sent"
|
2013-03-07 17:49:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-02-05 14:42:06 -05:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
|
|
|
def @controller.new_controller_thread
|
|
|
|
Thread.new { yield }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-12 19:07:04 -04:00
|
|
|
def test_set_cookie
|
|
|
|
get :set_cookie
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hello" => "world" }, @response.cookies)
|
2014-03-12 19:07:04 -04:00
|
|
|
assert_equal "hello world", @response.body
|
|
|
|
end
|
|
|
|
|
2012-07-29 20:26:07 -04:00
|
|
|
def test_write_to_stream
|
|
|
|
get :basic_stream
|
2012-07-29 22:23:19 -04:00
|
|
|
assert_equal "helloworld", @response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "text/event-stream", @response.headers["Content-Type"]
|
2012-07-29 22:23:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-15 17:08:46 -04:00
|
|
|
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
|
|
|
|
|
2012-07-29 22:23:19 -04:00
|
|
|
def test_async_stream
|
2015-03-02 11:14:45 -05:00
|
|
|
rubinius_skip "https://github.com/rubinius/rubinius/issues/2934"
|
|
|
|
|
2015-07-13 14:22:54 -04:00
|
|
|
@controller.latch = Concurrent::CountDownLatch.new
|
2016-08-06 12:54:50 -04:00
|
|
|
parts = ["hello", "world"]
|
2012-07-29 22:23:19 -04:00
|
|
|
|
2015-09-23 18:59:32 -04:00
|
|
|
get :blocking_stream
|
2012-07-29 22:23:19 -04:00
|
|
|
|
2015-09-23 18:59:32 -04:00
|
|
|
t = Thread.new(response) { |resp|
|
2014-03-12 20:40:08 -04:00
|
|
|
resp.await_commit
|
2012-07-29 22:23:19 -04:00
|
|
|
resp.stream.each do |part|
|
|
|
|
assert_equal parts.shift, part
|
|
|
|
ol = @controller.latch
|
2015-07-13 14:22:54 -04:00
|
|
|
@controller.latch = Concurrent::CountDownLatch.new
|
|
|
|
ol.count_down
|
2012-07-29 22:23:19 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert t.join(3), "timeout expired before the thread terminated"
|
2012-07-29 22:23:19 -04:00
|
|
|
end
|
|
|
|
|
2014-05-31 21:05:00 -04:00
|
|
|
def test_abort_with_full_buffer
|
2015-07-13 14:22:54 -04:00
|
|
|
@controller.latch = Concurrent::CountDownLatch.new
|
2015-09-23 19:40:56 -04:00
|
|
|
@controller.error_latch = Concurrent::CountDownLatch.new
|
2014-05-31 21:05:00 -04:00
|
|
|
|
|
|
|
capture_log_output do |output|
|
2016-08-06 13:35:13 -04:00
|
|
|
get :overfill_buffer_and_die, format: "plain"
|
2015-09-23 19:40:56 -04:00
|
|
|
|
|
|
|
t = Thread.new(response) { |resp|
|
|
|
|
resp.await_commit
|
|
|
|
_, _, body = resp.to_a
|
|
|
|
body.each do
|
|
|
|
@controller.latch.wait
|
|
|
|
body.close
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2014-05-31 21:05:00 -04:00
|
|
|
t.join
|
2015-09-23 19:40:56 -04:00
|
|
|
@controller.error_latch.wait
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_match "Error while streaming", output.rewind && output.read
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ignore_client_disconnect
|
2015-07-13 14:22:54 -04:00
|
|
|
@controller.latch = Concurrent::CountDownLatch.new
|
2014-05-31 21:05:00 -04:00
|
|
|
|
2015-09-23 19:44:00 -04:00
|
|
|
capture_log_output do |output|
|
|
|
|
get :ignore_client_disconnect
|
2014-05-31 21:05:00 -04:00
|
|
|
|
2015-09-23 19:44:00 -04:00
|
|
|
t = Thread.new(response) { |resp|
|
|
|
|
resp.await_commit
|
|
|
|
_, _, body = resp.to_a
|
|
|
|
body.each do
|
|
|
|
body.close
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
2014-05-31 21:05:00 -04:00
|
|
|
|
|
|
|
t.join
|
|
|
|
Timeout.timeout(3) do
|
2015-07-13 14:22:54 -04:00
|
|
|
@controller.latch.wait
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_match "Work complete", output.rewind && output.read
|
2014-05-31 21:05:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-29 22:23:19 -04:00
|
|
|
def test_thread_locals_get_copied
|
|
|
|
@controller.tc = self
|
|
|
|
Thread.current[:originating_thread] = Thread.current.object_id
|
2016-08-06 12:54:50 -04:00
|
|
|
Thread.current[:setting] = "aaron"
|
2012-07-29 22:23:19 -04:00
|
|
|
|
|
|
|
get :thread_locals
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
2012-07-29 23:26:40 -04:00
|
|
|
|
|
|
|
def test_live_stream_default_header
|
2015-09-23 19:42:15 -04:00
|
|
|
get :default_header
|
2016-08-06 12:54:50 -04:00
|
|
|
assert response.headers["Content-Type"]
|
2012-07-29 23:26:40 -04:00
|
|
|
end
|
2012-07-29 23:33:16 -04:00
|
|
|
|
|
|
|
def test_render_text
|
|
|
|
get :render_text
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "zomg", response.body
|
2013-03-07 17:49:03 -05:00
|
|
|
assert_stream_closed
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exception_handling_html
|
2014-02-28 18:39:08 -05:00
|
|
|
assert_raises(ActionView::MissingTemplate) do
|
2013-03-07 17:49:03 -05:00
|
|
|
get :exception_in_view
|
|
|
|
end
|
2014-02-17 21:41:21 -05:00
|
|
|
|
|
|
|
capture_log_output do |output|
|
|
|
|
get :exception_in_view_after_commit
|
|
|
|
assert_match %r((window\.location = "/500\.html"</script></html>)$), response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_match "Missing template test/doesntexist", output.rewind && output.read
|
2014-02-17 21:41:21 -05:00
|
|
|
assert_stream_closed
|
|
|
|
end
|
2014-03-12 20:40:08 -04:00
|
|
|
assert response.body
|
2014-02-28 18:39:08 -05:00
|
|
|
assert_stream_closed
|
2013-03-07 17:49:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_exception_handling_plain_text
|
2014-02-28 18:39:08 -05:00
|
|
|
assert_raises(ActionView::MissingTemplate) do
|
2013-03-07 17:49:03 -05:00
|
|
|
get :exception_in_view, format: :json
|
|
|
|
end
|
2014-02-17 21:41:21 -05:00
|
|
|
|
|
|
|
capture_log_output do |output|
|
|
|
|
get :exception_in_view_after_commit, format: :json
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "", response.body
|
|
|
|
assert_match "Missing template test/doesntexist", output.rewind && output.read
|
2014-02-17 21:41:21 -05:00
|
|
|
assert_stream_closed
|
|
|
|
end
|
2013-03-07 17:49:03 -05:00
|
|
|
end
|
|
|
|
|
2014-02-28 18:39:08 -05:00
|
|
|
def test_exception_callback_when_committed
|
2015-10-23 02:33:33 -04:00
|
|
|
current_threads = Thread.list
|
|
|
|
|
2013-03-07 17:49:03 -05:00
|
|
|
capture_log_output do |output|
|
2016-08-06 12:54:50 -04:00
|
|
|
get :exception_with_callback, format: "text/event-stream"
|
2015-10-23 02:33:33 -04:00
|
|
|
|
|
|
|
# Wait on the execution of all threads
|
|
|
|
(Thread.list - current_threads).each(&:join)
|
|
|
|
|
2013-03-07 17:49:03 -05:00
|
|
|
assert_equal %(data: "500 Internal Server Error"\n\n), response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_match "An exception occurred...", output.rewind && output.read
|
2013-03-07 17:49:03 -05:00
|
|
|
assert_stream_closed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-15 16:05:00 -05:00
|
|
|
def test_exception_in_controller_before_streaming
|
2014-02-28 18:39:08 -05:00
|
|
|
assert_raises(ActionController::LiveStreamTest::Exception) do
|
2016-08-06 12:54:50 -04:00
|
|
|
get :exception_in_controller, format: "text/event-stream"
|
2014-02-28 18:39:08 -05:00
|
|
|
end
|
2014-02-15 16:05:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_request_in_controller_before_streaming
|
2014-02-28 18:39:08 -05:00
|
|
|
assert_raises(ActionController::BadRequest) do
|
2016-08-06 12:54:50 -04:00
|
|
|
get :bad_request_error, format: "text/event-stream"
|
2014-02-28 18:39:08 -05:00
|
|
|
end
|
2014-02-15 16:05:00 -05:00
|
|
|
end
|
|
|
|
|
2014-02-28 18:39:08 -05:00
|
|
|
def test_exceptions_raised_handling_exceptions_and_committed
|
2013-03-07 17:49:03 -05:00
|
|
|
capture_log_output do |output|
|
2016-08-06 12:54:50 -04:00
|
|
|
get :exception_in_exception_callback, format: "text/event-stream"
|
|
|
|
assert_equal "", response.body
|
|
|
|
assert_match "We need to go deeper", output.rewind && output.read
|
2013-03-07 17:49:03 -05:00
|
|
|
assert_stream_closed
|
|
|
|
end
|
2012-07-29 23:33:16 -04:00
|
|
|
end
|
2013-03-14 04:13:10 -04:00
|
|
|
|
|
|
|
def test_stale_without_etag
|
|
|
|
get :with_stale
|
2015-09-23 19:42:15 -04:00
|
|
|
assert_equal 200, response.status.to_i
|
2013-03-14 04:13:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_stale_with_etag
|
2014-11-10 00:23:31 -05:00
|
|
|
@request.if_none_match = %(W/"#{Digest::MD5.hexdigest('123')}")
|
2013-03-14 04:13:10 -04:00
|
|
|
get :with_stale
|
2015-09-23 19:42:15 -04:00
|
|
|
assert_equal 304, response.status.to_i
|
2013-03-14 04:13:10 -04:00
|
|
|
end
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
2014-02-28 14:57:15 -05:00
|
|
|
|
|
|
|
class BufferTest < ActionController::TestCase
|
|
|
|
def test_nil_callback
|
|
|
|
buf = ActionController::Live::Buffer.new nil
|
|
|
|
assert buf.call_on_error
|
|
|
|
end
|
|
|
|
end
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
2015-12-09 07:51:02 -05:00
|
|
|
|
|
|
|
class LiveStreamRouterTest < ActionDispatch::IntegrationTest
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
include ActionController::Live
|
|
|
|
|
|
|
|
def index
|
2016-08-06 12:54:50 -04:00
|
|
|
response.headers["Content-Type"] = "text/event-stream"
|
2015-12-09 07:51:02 -05:00
|
|
|
sse = SSE.new(response.stream)
|
|
|
|
sse.write("{\"name\":\"John\"}")
|
2016-08-06 13:44:11 -04:00
|
|
|
sse.write(name: "Ryan")
|
2015-12-09 07:51:02 -05:00
|
|
|
ensure
|
|
|
|
sse.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
routes.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
|
|
|
routes.draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/test" => "live_stream_router_test/test#index"
|
2015-12-09 07:51:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
self.class
|
|
|
|
end
|
|
|
|
|
|
|
|
test "streaming served through the router" do
|
|
|
|
get "/test"
|
|
|
|
|
|
|
|
assert_response :ok
|
|
|
|
assert_match(/data: {\"name\":\"John\"}/, response.body)
|
|
|
|
assert_match(/data: {\"name\":\"Ryan\"}/, response.body)
|
|
|
|
end
|
|
|
|
end
|