debian-mirror-gitlab/spec/support/helpers/wait_for_requests.rb

58 lines
1.6 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module WaitForRequests
extend self
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
2017-09-10 17:25:29 +05:30
def block_and_wait_for_requests_complete
2018-03-17 18:26:18 +05:30
block_requests { wait_for_all_requests }
end
# Block all requests inside block with 503 response
def block_requests
2017-08-17 22:00:37 +05:30
Gitlab::Testing::RequestBlockerMiddleware.block_requests!
2018-03-17 18:26:18 +05:30
yield
ensure
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end
# Slow down requests inside block by injecting `sleep 0.2` before each response
def slow_requests
Gitlab::Testing::RequestBlockerMiddleware.slow_requests!
yield
2017-08-17 22:00:37 +05:30
ensure
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end
2018-03-17 18:26:18 +05:30
# Wait for client-side AJAX requests
2017-09-10 17:25:29 +05:30
def wait_for_requests
2018-11-08 19:23:39 +05:30
wait_for('JS requests complete', max_wait_time: 2 * Capybara.default_max_wait_time) do
finished_all_js_requests?
end
2018-03-17 18:26:18 +05:30
end
# Wait for active Rack requests and client-side AJAX requests
def wait_for_all_requests
wait_for('pending requests complete') do
2020-04-08 14:13:33 +05:30
finished_all_rack_requests? &&
2018-03-17 18:26:18 +05:30
finished_all_js_requests?
end
2017-09-10 17:25:29 +05:30
end
private
2020-04-08 14:13:33 +05:30
def finished_all_rack_requests?
2020-10-24 23:57:45 +05:30
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests == 0
2018-03-17 18:26:18 +05:30
end
def finished_all_js_requests?
2017-09-10 17:25:29 +05:30
return true unless javascript_test?
2021-01-03 14:25:43 +05:30
finished_all_ajax_requests?
2017-09-10 17:25:29 +05:30
end
def finished_all_ajax_requests?
2021-03-11 19:13:27 +05:30
Capybara.page.evaluate_script('window.pendingRequests || window.pendingApolloRequests || window.pendingRailsUJSRequests || 0').zero? # rubocop:disable Style/NumericPredicate
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
end