2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-02-21 20:25:52 -05:00
|
|
|
|
|
|
|
class ReloaderTest < ActiveSupport::TestCase
|
|
|
|
def test_prepare_callback
|
2017-01-06 04:43:23 -05:00
|
|
|
prepared = completed = false
|
2016-02-21 20:25:52 -05:00
|
|
|
reloader.to_prepare { prepared = true }
|
2017-01-06 04:43:23 -05:00
|
|
|
reloader.to_complete { completed = true }
|
2016-02-21 20:25:52 -05:00
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not prepared
|
|
|
|
assert_not completed
|
2016-02-21 20:25:52 -05:00
|
|
|
reloader.prepare!
|
|
|
|
assert prepared
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not completed
|
2016-02-21 20:25:52 -05:00
|
|
|
|
|
|
|
prepared = false
|
|
|
|
reloader.wrap do
|
|
|
|
assert prepared
|
|
|
|
prepared = false
|
|
|
|
end
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not prepared
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
|
2017-01-06 04:43:23 -05:00
|
|
|
def test_prepend_prepare_callback
|
|
|
|
i = 10
|
|
|
|
reloader.to_prepare { i += 1 }
|
|
|
|
reloader.to_prepare(prepend: true) { i = 0 }
|
|
|
|
|
|
|
|
reloader.prepare!
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
|
|
|
|
2016-02-21 20:25:52 -05:00
|
|
|
def test_only_run_when_check_passes
|
|
|
|
r = new_reloader { true }
|
|
|
|
invoked = false
|
|
|
|
r.to_run { invoked = true }
|
2018-09-25 13:18:20 -04:00
|
|
|
r.wrap { }
|
2016-02-21 20:25:52 -05:00
|
|
|
assert invoked
|
|
|
|
|
|
|
|
r = new_reloader { false }
|
|
|
|
invoked = false
|
|
|
|
r.to_run { invoked = true }
|
2018-09-25 13:18:20 -04:00
|
|
|
r.wrap { }
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not invoked
|
2016-02-21 20:25:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_full_reload_sequence
|
|
|
|
called = []
|
|
|
|
reloader.to_prepare { called << :prepare }
|
|
|
|
reloader.to_run { called << :reloader_run }
|
|
|
|
reloader.to_complete { called << :reloader_complete }
|
|
|
|
reloader.executor.to_run { called << :executor_run }
|
|
|
|
reloader.executor.to_complete { called << :executor_complete }
|
|
|
|
|
2018-09-25 13:18:20 -04:00
|
|
|
reloader.wrap { }
|
2016-02-21 20:25:52 -05:00
|
|
|
assert_equal [:executor_run, :reloader_run, :prepare, :reloader_complete, :executor_complete], called
|
|
|
|
|
|
|
|
called = []
|
|
|
|
reloader.reload!
|
|
|
|
assert_equal [:executor_run, :reloader_run, :prepare, :reloader_complete, :executor_complete, :prepare], called
|
|
|
|
|
|
|
|
reloader.check = lambda { false }
|
|
|
|
|
|
|
|
called = []
|
2018-09-25 13:18:20 -04:00
|
|
|
reloader.wrap { }
|
2016-02-21 20:25:52 -05:00
|
|
|
assert_equal [:executor_run, :executor_complete], called
|
|
|
|
|
|
|
|
called = []
|
|
|
|
reloader.reload!
|
|
|
|
assert_equal [:executor_run, :reloader_run, :prepare, :reloader_complete, :executor_complete, :prepare], called
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_unload_block
|
|
|
|
called = []
|
|
|
|
reloader.before_class_unload { called << :before_unload }
|
|
|
|
reloader.after_class_unload { called << :after_unload }
|
|
|
|
reloader.to_run do
|
|
|
|
class_unload! do
|
|
|
|
called << :unload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
reloader.wrap { called << :body }
|
|
|
|
|
|
|
|
assert_equal [:before_unload, :unload, :after_unload, :body], called
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def new_reloader(&check)
|
|
|
|
Class.new(ActiveSupport::Reloader).tap do |r|
|
|
|
|
r.check = check
|
|
|
|
r.executor = Class.new(ActiveSupport::Executor)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reloader
|
|
|
|
@reloader ||= new_reloader { true }
|
|
|
|
end
|
|
|
|
end
|