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
gem 'test_after_commit', '~> 1.1'
gem 'sham_rack', '~> 1.3.6'
gem 'timecop', '~> 0.8.0'
gem 'concurrent-ruby', '~> 1.0.5'
end
gem 'octokit', '~> 4.6.2'
......
......@@ -128,7 +128,7 @@ GEM
execjs
coffee-script-source (1.10.0)
colorize (0.7.7)
concurrent-ruby (1.0.4)
concurrent-ruby (1.0.5)
connection_pool (2.2.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
......@@ -868,6 +868,7 @@ DEPENDENCIES
chronic (~> 0.10.2)
chronic_duration (~> 0.10.6)
coffee-rails (~> 4.1.0)
concurrent-ruby (~> 1.0.5)
connection_pool (~> 2.0)
creole (~> 0.5.0)
d3_rails (~> 3.5.0)
......
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
# 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'
feature 'Member autocomplete', feature: true do
include WaitForAjax
feature 'Member autocomplete', :js do
let(:project) { create(:project, :public) }
let(:user) { create(:user) }
let(:participant) { create(:user) }
let(:author) { create(:user) }
let(:note) { create(:note, noteable: noteable, project: noteable.project) }
before do
allow_any_instance_of(Commit).to receive(:author).and_return(author)
login_as user
end
shared_examples "open suggestions" do
it 'displays suggestions' do
expect(page).to have_selector('.atwho-view', visible: true)
note # actually create the note
login_as(user)
end
it 'suggests author' do
page.within('.atwho-view', visible: true) do
expect(page).to have_content(author.username)
shared_examples "open suggestions when typing @" do
before do
page.within('.new-note') do
find('#note_note').send_keys('@')
end
end
it 'suggests participant' do
it 'suggests noteable author and note author' 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
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
issue = create(:issue, author: author, project: project)
create(:note, note: 'Ultralight Beam', noteable: issue,
project: project, author: participant)
visit_issue(project, issue)
visit namespace_project_issue_path(project.namespace, project, noteable)
end
context 'when typing @' do
include_examples "open suggestions"
before do
open_member_suggestions
end
end
include_examples "open suggestions when typing @"
end
context 'adding a new note on a Merge Request ', js: true do
before do
merge = create(:merge_request, source_project: project, target_project: project, author: author)
create(:note, note: 'Ultralight Beam', noteable: merge,
project: project, author: participant)
visit_merge_request(project, merge)
context 'adding a new note on a Merge Request' do
let(:noteable) do
create(:merge_request, source_project: project,
target_project: project, author: author)
end
context 'when typing @' do
include_examples "open suggestions"
before do
open_member_suggestions
end
visit namespace_project_merge_request_path(project.namespace, project, noteable)
end
end
context 'adding a new note on a Commit ', js: true do
let(:commit) { project.commit }
before do
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)
include_examples "open suggestions when typing @"
end
context 'when typing @' do
include_examples "open suggestions"
before do
open_member_suggestions
end
end
end
context 'adding a new note on a Commit' do
let(:noteable) { project.commit }
let(:note) { create(:note_on_commit, project: project, commit_id: project.commit.id) }
def open_member_suggestions
page.within('.new-note') do
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
before do
allow_any_instance_of(Commit).to receive(:author).and_return(author)
def visit_merge_request(project, merge)
visit namespace_project_merge_request_path(project.namespace, project, merge)
visit namespace_project_commit_path(project.namespace, project, noteable)
end
def visit_commit(project, commit)
visit namespace_project_commit_path(project.namespace, project, commit)
include_examples "open suggestions when typing @"
end
end
......@@ -35,7 +35,8 @@ RSpec.configure do |config|
config.include Warden::Test::Helpers, type: :request
config.include LoginHelpers, 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 EmailHelpers, type: :mailer
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