From ed3b23fec9f030fe7868c70265324a732bf34fe9 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Thu, 2 Nov 2017 15:34:30 -0500 Subject: [PATCH] refactor wait_for_requests, add slow_requests block helper --- .../filtered_search/dropdown_assignee_spec.rb | 8 ++--- .../filtered_search/dropdown_author_spec.rb | 8 ++--- .../filtered_search/dropdown_emoji_spec.rb | 8 ++--- .../filtered_search/dropdown_label_spec.rb | 8 ++--- .../dropdown_milestone_spec.rb | 8 ++--- .../projects/user_browses_files_spec.rb | 2 +- .../uploads/user_uploads_file_to_note_spec.rb | 34 +++++++----------- spec/support/inspect_requests.rb | 2 +- spec/support/wait_for_requests.rb | 36 +++++++++++++++---- 9 files changed, 64 insertions(+), 50 deletions(-) diff --git a/spec/features/issues/filtered_search/dropdown_assignee_spec.rb b/spec/features/issues/filtered_search/dropdown_assignee_spec.rb index 9fa26327d7e..2e4a25ee15d 100644 --- a/spec/features/issues/filtered_search/dropdown_assignee_spec.rb +++ b/spec/features/issues/filtered_search/dropdown_assignee_spec.rb @@ -43,11 +43,11 @@ describe 'Dropdown assignee', :js do end it 'should show loading indicator when opened' do - Gitlab::Testing::RequestBlockerMiddleware.slow_requests! - filtered_search.set('assignee:') + slow_requests do + filtered_search.set('assignee:') - expect(page).to have_css('#js-dropdown-assignee .filter-dropdown-loading', visible: true) - Gitlab::Testing::RequestBlockerMiddleware.allow_requests! + expect(page).to have_css('#js-dropdown-assignee .filter-dropdown-loading', visible: true) + end end it 'should hide loading indicator when loaded' do diff --git a/spec/features/issues/filtered_search/dropdown_author_spec.rb b/spec/features/issues/filtered_search/dropdown_author_spec.rb index 305b6830faa..2fb5e7cdba4 100644 --- a/spec/features/issues/filtered_search/dropdown_author_spec.rb +++ b/spec/features/issues/filtered_search/dropdown_author_spec.rb @@ -51,11 +51,11 @@ describe 'Dropdown author', :js do end it 'should show loading indicator when opened' do - Gitlab::Testing::RequestBlockerMiddleware.slow_requests! - filtered_search.set('author:') + slow_requests do + filtered_search.set('author:') - expect(page).to have_css('#js-dropdown-author .filter-dropdown-loading', visible: true) - Gitlab::Testing::RequestBlockerMiddleware.allow_requests! + expect(page).to have_css('#js-dropdown-author .filter-dropdown-loading', visible: true) + end end it 'should hide loading indicator when loaded' do diff --git a/spec/features/issues/filtered_search/dropdown_emoji_spec.rb b/spec/features/issues/filtered_search/dropdown_emoji_spec.rb index f502219484f..8db435634fd 100644 --- a/spec/features/issues/filtered_search/dropdown_emoji_spec.rb +++ b/spec/features/issues/filtered_search/dropdown_emoji_spec.rb @@ -70,11 +70,11 @@ describe 'Dropdown emoji', :js do end it 'should show loading indicator when opened' do - Gitlab::Testing::RequestBlockerMiddleware.slow_requests! - filtered_search.set('my-reaction:') + slow_requests do + filtered_search.set('my-reaction:') - expect(page).to have_css('#js-dropdown-my-reaction .filter-dropdown-loading', visible: true) - Gitlab::Testing::RequestBlockerMiddleware.allow_requests! + expect(page).to have_css('#js-dropdown-my-reaction .filter-dropdown-loading', visible: true) + end end it 'should hide loading indicator when loaded' do diff --git a/spec/features/issues/filtered_search/dropdown_label_spec.rb b/spec/features/issues/filtered_search/dropdown_label_spec.rb index 223f6e9de16..18cdb199c70 100644 --- a/spec/features/issues/filtered_search/dropdown_label_spec.rb +++ b/spec/features/issues/filtered_search/dropdown_label_spec.rb @@ -66,12 +66,12 @@ describe 'Dropdown label', :js do end it 'shows loading indicator when opened and hides it when loaded' do - Gitlab::Testing::RequestBlockerMiddleware.slow_requests! - filtered_search.set('label:') + slow_requests do + filtered_search.set('label:') - expect(page).to have_css("#{js_dropdown_label} .filter-dropdown-loading", visible: true) + expect(page).to have_css("#{js_dropdown_label} .filter-dropdown-loading", visible: true) + end expect(find(js_dropdown_label)).not_to have_css('.filter-dropdown-loading') - Gitlab::Testing::RequestBlockerMiddleware.allow_requests! end it 'loads all the labels when opened' do diff --git a/spec/features/issues/filtered_search/dropdown_milestone_spec.rb b/spec/features/issues/filtered_search/dropdown_milestone_spec.rb index 4a531b8d568..031eb06723a 100644 --- a/spec/features/issues/filtered_search/dropdown_milestone_spec.rb +++ b/spec/features/issues/filtered_search/dropdown_milestone_spec.rb @@ -50,11 +50,11 @@ describe 'Dropdown milestone', :js do end it 'should show loading indicator when opened' do - Gitlab::Testing::RequestBlockerMiddleware.slow_requests! - filtered_search.set('milestone:') + slow_requests do + filtered_search.set('milestone:') - expect(page).to have_css('#js-dropdown-milestone .filter-dropdown-loading', visible: true) - Gitlab::Testing::RequestBlockerMiddleware.allow_requests! + expect(page).to have_css('#js-dropdown-milestone .filter-dropdown-loading', visible: true) + end end it 'should hide loading indicator when loaded' do diff --git a/spec/features/projects/user_browses_files_spec.rb b/spec/features/projects/user_browses_files_spec.rb index 1d7c8a731f0..f5e4d7f5130 100644 --- a/spec/features/projects/user_browses_files_spec.rb +++ b/spec/features/projects/user_browses_files_spec.rb @@ -179,7 +179,7 @@ describe 'User browses files' do click_button('Upload file') end - block_and_wait_for_requests_complete + wait_for_all_requests visit(project_blob_path(project, 'new_branch_name/logo_sample.svg')) diff --git a/spec/features/uploads/user_uploads_file_to_note_spec.rb b/spec/features/uploads/user_uploads_file_to_note_spec.rb index 47b89d262f3..972c10aaf23 100644 --- a/spec/features/uploads/user_uploads_file_to_note_spec.rb +++ b/spec/features/uploads/user_uploads_file_to_note_spec.rb @@ -21,24 +21,12 @@ feature 'User uploads file to note' do end context 'uploading is in progress' do - before do - Gitlab::Testing::RequestBlockerMiddleware.slow_requests! - end - - after do - Gitlab::Testing::RequestBlockerMiddleware.allow_requests! - end - - it 'shows "Cancel" button on uploading', :js do - dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) - - expect(page).to have_button('Cancel') - end - it 'cancels uploading on clicking to "Cancel" button', :js do - dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) + slow_requests do + dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) - click_button 'Cancel' + click_button 'Cancel' + end expect(page).to have_button('Attach a file') expect(page).not_to have_button('Cancel') @@ -46,16 +34,20 @@ feature 'User uploads file to note' do end it 'shows "Attaching a file" message on uploading 1 file', :js do - dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) + slow_requests do + dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) - expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching a file -') + expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching a file -') + end end it 'shows "Attaching 2 files" message on uploading 2 file', :js do - dropzone_file([Rails.root.join('spec', 'fixtures', 'video_sample.mp4'), - Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) + slow_requests do + dropzone_file([Rails.root.join('spec', 'fixtures', 'video_sample.mp4'), + Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) - expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching 2 files -') + expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching 2 files -') + end end it 'shows error message, "retry" and "attach a new file" link a if file is too big', :js do diff --git a/spec/support/inspect_requests.rb b/spec/support/inspect_requests.rb index e121a1078f4..88ddc5c7f6c 100644 --- a/spec/support/inspect_requests.rb +++ b/spec/support/inspect_requests.rb @@ -9,7 +9,7 @@ module InspectRequests yield - block_and_wait_for_requests_complete + wait_for_all_requests Gitlab::Testing::RequestInspectorMiddleware.requests ensure Gitlab::Testing::RequestInspectorMiddleware.stop_logging! diff --git a/spec/support/wait_for_requests.rb b/spec/support/wait_for_requests.rb index b5c3c0f55b8..f4130d68271 100644 --- a/spec/support/wait_for_requests.rb +++ b/spec/support/wait_for_requests.rb @@ -1,25 +1,47 @@ -require_relative './wait_for_requests' - module WaitForRequests extend self # This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests def block_and_wait_for_requests_complete + block_requests { wait_for_all_requests } + end + + # Block all requests inside block with 503 response + def block_requests Gitlab::Testing::RequestBlockerMiddleware.block_requests! - wait_for('pending requests complete') do - Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? && finished_all_requests? - end + 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 + ensure + Gitlab::Testing::RequestBlockerMiddleware.allow_requests! + end + + # Wait for client-side AJAX requests def wait_for_requests - wait_for('JS requests') { finished_all_requests? } + wait_for('JS requests complete') { finished_all_js_requests? } + end + + # Wait for active Rack requests and client-side AJAX requests + def wait_for_all_requests + wait_for('pending requests complete') do + finished_all_rack_reqiests? && + finished_all_js_requests? + end end private - def finished_all_requests? + def finished_all_rack_reqiests? + Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? + end + + def finished_all_js_requests? return true unless javascript_test? finished_all_ajax_requests? &&