Last active
August 29, 2015 13:56
-
-
Save jturkel/9317269 to your computer and use it in GitHub Desktop.
This file contains 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
RSpec.configure do |config| | |
... | |
config.after(:each) do | |
wait_for_ajax_completion if example.metadata[:type] == :feature | |
end | |
end | |
def wait_for_ajax_completion | |
wait_for('pending AJAX requests complete') do | |
num_pending_ajax_calls == 0 | |
end | |
end | |
def num_pending_ajax_calls | |
page.evaluate_script("window.$.active").to_i | |
end | |
# Waits until the passed block returns true | |
def wait_for(condition_name, max_wait_time: 30, polling_interval: 0.01) | |
wait_until = Time.now + max_wait_time.seconds | |
while true | |
return if yield | |
if Time.now > wait_until | |
raise "Condition not met: #{condition_name}" | |
else | |
sleep(polling_interval) | |
end | |
end | |
end |
This file contains 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
group :test do | |
gem 'atomic' | |
end |
This file contains 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
require 'atomic' | |
# Rack middleware that keeps track of the number of active requests and can block new requests. | |
class RackRequestBlocker | |
@@num_active_requests = Atomic.new(0) | |
@@block_requests = Atomic.new(false) | |
# Returns the number of requests the server is currently processing. | |
def self.num_active_requests | |
@@num_active_requests.value | |
end | |
# Prevents the server from accepting new requests. Any new requests will return an HTTP | |
# 503 status. | |
def self.block_requests! | |
@@block_requests.value = true | |
end | |
# Allows the server to accept requests again. | |
def self.allow_requests! | |
@@block_requests.value = false | |
end | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
increment_active_requests | |
if block_requests? | |
block_request(env) | |
else | |
@app.call(env) | |
end | |
ensure | |
decrement_active_requests | |
end | |
private | |
def block_requests? | |
@@block_requests.value | |
end | |
def block_request(env) | |
[503, {}, []] | |
end | |
def increment_active_requests | |
@@num_active_requests.update { |v| v + 1 } | |
end | |
def decrement_active_requests | |
@@num_active_requests.update { |v| v - 1 } | |
end | |
end |
This file contains 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
RSpec.configure do |config| | |
... | |
config.after(:each) do | |
wait_for_requests_complete if example.metadata[:type] == :feature | |
end | |
end | |
def wait_for_requests_complete | |
stop_client | |
RackRequestBlocker.block_requests! | |
wait_for('pending AJAX requests complete') do | |
RackRequestBlocker.num_active_requests == 0 | |
end | |
ensure | |
RackRequestBlocker.allow_requests! | |
end | |
# Navigate away from the current page which will prevent any new requests from being started | |
def stop_client | |
page.execute_script %Q{ | |
window.location = "about:blank"; | |
} | |
end | |
# Waits until the passed block returns true | |
def wait_for(condition_name, max_wait_time: 30, polling_interval: 0.01) | |
wait_until = Time.now + max_wait_time.seconds | |
while true | |
return if yield | |
if Time.now > wait_until | |
raise "Condition not met: #{condition_name}" | |
else | |
sleep(polling_interval) | |
end | |
end | |
end | |
This file contains 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
MyProject::Application.configure do | |
... | |
require 'rack_request_blocker' | |
# Make sure the middleware is inserted first in middleware chain | |
config.middleware.insert_before('ActionDispatch::Static', 'RackRequestBlocker') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment