diff options
author | Rémy Coutable <remy@rymai.me> | 2017-03-10 03:28:20 +0100 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-03-22 19:45:21 +0100 |
commit | 645a55f19b71bb951f3ce7c3a7915ce878f82e97 (patch) | |
tree | dc14ba7bcf596bf8ded773d31d4760acf6602e88 /spec/support/wait_for_requests.rb | |
parent | 3574963bc01097da418b62ae6f87d02359f36f12 (diff) | |
download | gitlab-ce-645a55f19b71bb951f3ce7c3a7915ce878f82e97.tar.gz |
Introduce a new middleware for the test environment that can block requests
The idea is that after each feature spec example, we block all incoming
requests at the Rack level, go to the 'about:blank' page, and wait until
the current requests reach 0.
This should solve the problem where a request would end after database
cleaner performed the database truncation. The problem was that a GET
request can still lead to records creation (e.g. namespaces or routes).
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'spec/support/wait_for_requests.rb')
-rw-r--r-- | spec/support/wait_for_requests.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/support/wait_for_requests.rb b/spec/support/wait_for_requests.rb new file mode 100644 index 00000000000..4a8958c7336 --- /dev/null +++ b/spec/support/wait_for_requests.rb @@ -0,0 +1,40 @@ +module WaitForRequests + extend self + + # This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests + def wait_for_requests_complete + stop_client + Gitlab::Testing::RequestBlockerMiddleware.block_requests! + wait_for('pending AJAX requests complete') do + Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? + end + ensure + Gitlab::Testing::RequestBlockerMiddleware.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: Capybara.default_max_wait_time, polling_interval: 0.01) + wait_until = Time.now + max_wait_time.seconds + loop do + break if yield + if Time.now > wait_until + raise "Condition not met: #{condition_name}" + else + sleep(polling_interval) + end + end + end +end + +RSpec.configure do |config| + config.after(:each, :js) do + wait_for_requests_complete + end +end |