Skip to content

Instantly share code, notes, and snippets.

@kemenaran
Created November 9, 2022 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemenaran/f66f38fc5bb1a028a008ad0248e8fa40 to your computer and use it in GitHub Desktop.
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.
# 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