Created
November 9, 2022 12:01
-
-
Save kemenaran/f66f38fc5bb1a028a008ad0248e8fa40 to your computer and use it in GitHub Desktop.
A test controller that holds a request for the given time, then returns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A test controller that holds a request for the given time, then returns. | |
# Useful for testing requests that time out in Capybara. | |
# | |
# Usage: | |
# mock_posts_controller = HoldingController.new | |
# PostsController.stubs(:new).returns(mock_posts_controller) | |
# mock_posts_controller.holding_actions do | |
# # do something that should time out | |
# assert_text "The request timed out" | |
# end | |
# PostsController.unstub(:new) | |
class HoldingController < ActionController::Base | |
def holding_actions | |
@hold = true | |
yield | |
ensure | |
@hold = false | |
end | |
def create | |
Rails.logger.debug "HOLDING ACTION #{self.class.name}##{__method__.to_s}" | |
wait_until_release | |
Rails.logger.debug "RELEASED ACTION #{self.class.name}##{__method__.to_s}" | |
end | |
private | |
def wait_until_release | |
total_sleep = 0 | |
while @hold | |
sleep 0.1 | |
total_sleep += 0.1 | |
end | |
Rails.logger.debug "ACTION HELD FOR #{total_sleep.round(2)}s" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment