2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2010-11-23 21:04:05 -05:00
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class ReloaderTest < ActiveSupport::TestCase
|
2014-06-05 10:30:53 -04:00
|
|
|
teardown do
|
2016-02-21 20:25:52 -05:00
|
|
|
ActiveSupport::Reloader.reset_callbacks :prepare
|
|
|
|
ActiveSupport::Reloader.reset_callbacks :complete
|
2014-06-05 10:30:53 -04:00
|
|
|
end
|
|
|
|
|
2010-11-23 21:04:05 -05:00
|
|
|
class MyBody < Array
|
|
|
|
def initialize(&block)
|
|
|
|
@on_close = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def foo
|
|
|
|
"foo"
|
|
|
|
end
|
|
|
|
|
|
|
|
def bar
|
|
|
|
"bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@on_close.call if @on_close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
def test_prepare_callbacks
|
|
|
|
a = b = c = nil
|
|
|
|
reloader.to_prepare { |*args| a = b = c = 1 }
|
|
|
|
reloader.to_prepare { |*args| b = c = 2 }
|
|
|
|
reloader.to_prepare { |*args| c = 3 }
|
|
|
|
|
|
|
|
# Ensure to_prepare callbacks are not run when defined
|
|
|
|
assert_nil a || b || c
|
|
|
|
|
|
|
|
# Run callbacks
|
|
|
|
call_and_return_body
|
|
|
|
|
|
|
|
assert_equal 1, a
|
|
|
|
assert_equal 2, b
|
|
|
|
assert_equal 3, c
|
|
|
|
end
|
|
|
|
|
2010-11-23 21:04:05 -05:00
|
|
|
def test_returned_body_object_always_responds_to_close
|
|
|
|
body = call_and_return_body
|
2010-12-20 06:41:49 -05:00
|
|
|
assert_respond_to body, :close
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
2012-01-13 15:40:34 -05:00
|
|
|
def test_returned_body_object_always_responds_to_close_even_if_called_twice
|
|
|
|
body = call_and_return_body
|
|
|
|
assert_respond_to body, :close
|
|
|
|
body.close
|
|
|
|
|
|
|
|
body = call_and_return_body
|
|
|
|
assert_respond_to body, :close
|
|
|
|
body.close
|
|
|
|
end
|
|
|
|
|
2011-12-12 13:23:13 -05:00
|
|
|
def test_condition_specifies_when_to_reload
|
|
|
|
i, j = 0, 0, 0, 0
|
2016-02-21 20:25:52 -05:00
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
reloader = reloader(lambda { i < 3 })
|
|
|
|
reloader.to_prepare { |*args| i += 1 }
|
|
|
|
reloader.to_complete { |*args| j += 1 }
|
2016-02-21 20:25:52 -05:00
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
app = middleware(lambda { |env| [200, {}, []] }, reloader)
|
2011-12-12 13:23:13 -05:00
|
|
|
5.times do
|
|
|
|
resp = app.call({})
|
|
|
|
resp[2].close
|
|
|
|
end
|
|
|
|
assert_equal 3, i
|
|
|
|
assert_equal 3, j
|
|
|
|
end
|
|
|
|
|
2010-11-23 21:04:05 -05:00
|
|
|
def test_returned_body_object_behaves_like_underlying_object
|
|
|
|
body = call_and_return_body do
|
|
|
|
b = MyBody.new
|
|
|
|
b << "hello"
|
|
|
|
b << "world"
|
|
|
|
[200, { "Content-Type" => "text/html" }, b]
|
|
|
|
end
|
|
|
|
assert_equal 2, body.size
|
|
|
|
assert_equal "hello", body[0]
|
|
|
|
assert_equal "world", body[1]
|
|
|
|
assert_equal "foo", body.foo
|
|
|
|
assert_equal "bar", body.bar
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_it_calls_close_on_underlying_object_when_close_is_called_on_body
|
|
|
|
close_called = false
|
|
|
|
body = call_and_return_body do
|
|
|
|
b = MyBody.new do
|
|
|
|
close_called = true
|
|
|
|
end
|
|
|
|
[200, { "Content-Type" => "text/html" }, b]
|
|
|
|
end
|
|
|
|
body.close
|
|
|
|
assert close_called
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_returned_body_object_responds_to_all_methods_supported_by_underlying_object
|
|
|
|
body = call_and_return_body do
|
|
|
|
[200, { "Content-Type" => "text/html" }, MyBody.new]
|
|
|
|
end
|
2010-12-20 06:41:49 -05:00
|
|
|
assert_respond_to body, :size
|
|
|
|
assert_respond_to body, :each
|
|
|
|
assert_respond_to body, :foo
|
|
|
|
assert_respond_to body, :bar
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
def test_complete_callbacks_are_called_when_body_is_closed
|
|
|
|
completed = false
|
|
|
|
reloader.to_complete { completed = true }
|
2010-11-23 21:04:05 -05:00
|
|
|
|
|
|
|
body = call_and_return_body
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not completed
|
2010-11-23 21:04:05 -05:00
|
|
|
|
|
|
|
body.close
|
2017-01-06 04:43:23 -05:00
|
|
|
assert completed
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_prepare_callbacks_arent_called_when_body_is_closed
|
|
|
|
prepared = false
|
2017-01-06 04:43:23 -05:00
|
|
|
reloader.to_prepare { prepared = true }
|
2010-11-23 21:04:05 -05:00
|
|
|
|
|
|
|
body = call_and_return_body
|
|
|
|
prepared = false
|
|
|
|
|
|
|
|
body.close
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not prepared
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
def test_complete_callbacks_are_called_on_exceptions
|
|
|
|
completed = false
|
|
|
|
reloader.to_complete { completed = true }
|
2010-12-23 13:17:02 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
call_and_return_body do
|
|
|
|
raise "error"
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
assert completed
|
2010-12-23 13:17:02 -05:00
|
|
|
end
|
|
|
|
|
2010-11-23 21:04:05 -05:00
|
|
|
private
|
|
|
|
def call_and_return_body(&block)
|
2017-01-06 04:43:23 -05:00
|
|
|
app = middleware(block || proc { [200, {}, "response"] })
|
|
|
|
_, _, body = app.call("rack.input" => StringIO.new(""))
|
|
|
|
body
|
|
|
|
end
|
|
|
|
|
|
|
|
def middleware(inner_app, reloader = reloader())
|
|
|
|
ActionDispatch::Reloader.new(inner_app, reloader)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reloader(check = lambda { true })
|
|
|
|
@reloader ||= begin
|
|
|
|
reloader = Class.new(ActiveSupport::Reloader)
|
|
|
|
reloader.check = check
|
|
|
|
reloader
|
|
|
|
end
|
2010-11-23 21:04:05 -05:00
|
|
|
end
|
|
|
|
end
|