Commit 73c98a34 authored by Sean McGivern's avatar Sean McGivern

Merge branch '28307-fix-sporadic-feature-spec-failures' into 'master'

Introduce a new middleware for the test environment that can block requests

Closes #28307

See merge request !9846
parents ae1b4998 709a5667
...@@ -327,6 +327,7 @@ group :test do ...@@ -327,6 +327,7 @@ group :test do
gem 'test_after_commit', '~> 1.1' gem 'test_after_commit', '~> 1.1'
gem 'sham_rack', '~> 1.3.6' gem 'sham_rack', '~> 1.3.6'
gem 'timecop', '~> 0.8.0' gem 'timecop', '~> 0.8.0'
gem 'concurrent-ruby', '~> 1.0.5'
end end
gem 'octokit', '~> 4.6.2' gem 'octokit', '~> 4.6.2'
......
...@@ -128,7 +128,7 @@ GEM ...@@ -128,7 +128,7 @@ GEM
execjs execjs
coffee-script-source (1.10.0) coffee-script-source (1.10.0)
colorize (0.7.7) colorize (0.7.7)
concurrent-ruby (1.0.4) concurrent-ruby (1.0.5)
connection_pool (2.2.1) connection_pool (2.2.1)
crack (0.4.3) crack (0.4.3)
safe_yaml (~> 1.0.0) safe_yaml (~> 1.0.0)
...@@ -868,6 +868,7 @@ DEPENDENCIES ...@@ -868,6 +868,7 @@ DEPENDENCIES
chronic (~> 0.10.2) chronic (~> 0.10.2)
chronic_duration (~> 0.10.6) chronic_duration (~> 0.10.6)
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
concurrent-ruby (~> 1.0.5)
connection_pool (~> 2.0) connection_pool (~> 2.0)
creole (~> 0.5.0) creole (~> 0.5.0)
d3_rails (~> 3.5.0) d3_rails (~> 3.5.0)
......
Rails.application.configure do Rails.application.configure do
# Make sure the middleware is inserted first in middleware chain
config.middleware.insert_before('ActionDispatch::Static', 'Gitlab::Testing::RequestBlockerMiddleware')
# Settings specified here will take precedence over those in config/application.rb # Settings specified here will take precedence over those in config/application.rb
# The test environment is used exclusively to run your application's # The test environment is used exclusively to run your application's
......
# rubocop:disable Style/ClassVars
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
# Rack middleware that keeps track of the number of active requests and can block new requests.
module Gitlab
module Testing
class RequestBlockerMiddleware
@@num_active_requests = Concurrent::AtomicFixnum.new(0)
@@block_requests = Concurrent::AtomicBoolean.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.true?
end
def block_request(env)
[503, {}, []]
end
def increment_active_requests
@@num_active_requests.increment
end
def decrement_active_requests
@@num_active_requests.decrement
end
end
end
end
require 'spec_helper' require 'spec_helper'
feature 'Member autocomplete', feature: true do feature 'Member autocomplete', :js do
include WaitForAjax
let(:project) { create(:project, :public) } let(:project) { create(:project, :public) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:participant) { create(:user) }
let(:author) { create(:user) } let(:author) { create(:user) }
let(:note) { create(:note, noteable: noteable, project: noteable.project) }
before do before do
allow_any_instance_of(Commit).to receive(:author).and_return(author) note # actually create the note
login_as user login_as(user)
end
shared_examples "open suggestions" do
it 'displays suggestions' do
expect(page).to have_selector('.atwho-view', visible: true)
end end
it 'suggests author' do shared_examples "open suggestions when typing @" do
page.within('.atwho-view', visible: true) do before do
expect(page).to have_content(author.username) page.within('.new-note') do
find('#note_note').send_keys('@')
end end
end end
it 'suggests participant' do it 'suggests noteable author and note author' do
page.within('.atwho-view', visible: true) do page.within('.atwho-view', visible: true) do
expect(page).to have_content(participant.username) expect(page).to have_content(author.username)
expect(page).to have_content(note.author.username)
end end
end end
end end
context 'adding a new note on a Issue', js: true do context 'adding a new note on a Issue' do
let(:noteable) { create(:issue, author: author, project: project) }
before do before do
issue = create(:issue, author: author, project: project) visit namespace_project_issue_path(project.namespace, project, noteable)
create(:note, note: 'Ultralight Beam', noteable: issue,
project: project, author: participant)
visit_issue(project, issue)
end end
context 'when typing @' do include_examples "open suggestions when typing @"
include_examples "open suggestions"
before do
open_member_suggestions
end
end
end end
context 'adding a new note on a Merge Request ', js: true do context 'adding a new note on a Merge Request' do
before do let(:noteable) do
merge = create(:merge_request, source_project: project, target_project: project, author: author) create(:merge_request, source_project: project,
create(:note, note: 'Ultralight Beam', noteable: merge, target_project: project, author: author)
project: project, author: participant)
visit_merge_request(project, merge)
end end
context 'when typing @' do
include_examples "open suggestions"
before do before do
open_member_suggestions visit namespace_project_merge_request_path(project.namespace, project, noteable)
end
end end
end
context 'adding a new note on a Commit ', js: true do
let(:commit) { project.commit }
before do include_examples "open suggestions when typing @"
allow(commit).to receive(:author).and_return(author)
create(:note_on_commit, author: participant, project: project, commit_id: project.repository.commit.id, note: 'No More Parties in LA')
visit_commit(project, commit)
end end
context 'when typing @' do context 'adding a new note on a Commit' do
include_examples "open suggestions" let(:noteable) { project.commit }
before do let(:note) { create(:note_on_commit, project: project, commit_id: project.commit.id) }
open_member_suggestions
end
end
end
def open_member_suggestions before do
page.within('.new-note') do allow_any_instance_of(Commit).to receive(:author).and_return(author)
find('#note_note').send_keys('@')
end
wait_for_ajax
end
def visit_issue(project, issue)
visit namespace_project_issue_path(project.namespace, project, issue)
end
def visit_merge_request(project, merge) visit namespace_project_commit_path(project.namespace, project, noteable)
visit namespace_project_merge_request_path(project.namespace, project, merge)
end end
def visit_commit(project, commit) include_examples "open suggestions when typing @"
visit namespace_project_commit_path(project.namespace, project, commit)
end end
end end
...@@ -35,7 +35,8 @@ RSpec.configure do |config| ...@@ -35,7 +35,8 @@ RSpec.configure do |config|
config.include Warden::Test::Helpers, type: :request config.include Warden::Test::Helpers, type: :request
config.include LoginHelpers, type: :feature config.include LoginHelpers, type: :feature
config.include SearchHelpers, type: :feature config.include SearchHelpers, type: :feature
config.include WaitForAjax, type: :feature config.include WaitForRequests, :js
config.include WaitForAjax, :js
config.include StubConfiguration config.include StubConfiguration
config.include EmailHelpers, type: :mailer config.include EmailHelpers, type: :mailer
config.include TestEnv config.include TestEnv
......
module WaitForRequests
extend self
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
def wait_for_requests_complete
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
# 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment