Commit ed8c0960 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'qmnguyen0711/fix-a-flakky-database-test' into 'master'

Flaky test: Release DB proxy's host when clearing configuration

See merge request gitlab-org/gitlab!57320
parents 062f75a4 5debe584
...@@ -137,12 +137,6 @@ module Gitlab ...@@ -137,12 +137,6 @@ module Gitlab
ActiveRecord::Base.singleton_class.prepend(ActiveRecordProxy) ActiveRecord::Base.singleton_class.prepend(ActiveRecordProxy)
end end
# Clear configuration
def self.clear_configuration
@proxy = nil
remove_instance_variable(:@feature_available) if defined?(@feature_available)
end
def self.active_record_models def self.active_record_models
ActiveRecord::Base.descendants ActiveRecord::Base.descendants
end end
......
...@@ -87,7 +87,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do ...@@ -87,7 +87,7 @@ RSpec.describe Gitlab::Database::LoadBalancing::ConnectionProxy do
end end
end end
it 'inserts data in bulk', quarantine: 'https://gitlab.com/gitlab-org/gitlab/-/issues/325786' do it 'inserts data in bulk' do
expect(model_class).to receive(:connection) expect(model_class).to receive(:connection)
.at_least(:once) .at_least(:once)
.and_return(proxy) .and_return(proxy)
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Database::LoadBalancing do RSpec.describe Gitlab::Database::LoadBalancing do
include_context 'clear DB Load Balancing configuration'
describe '.proxy' do describe '.proxy' do
context 'when configured' do context 'when configured' do
before do before do
...@@ -10,10 +12,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -10,10 +12,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
subject.configure_proxy subject.configure_proxy
end end
after do
subject.clear_configuration
end
it 'returns the connection proxy' do it 'returns the connection proxy' do
expect(subject.proxy).to be_an_instance_of(subject::ConnectionProxy) expect(subject.proxy).to be_an_instance_of(subject::ConnectionProxy)
end end
...@@ -133,7 +131,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -133,7 +131,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do
let!(:license) { create(:license, plan: ::License::PREMIUM_PLAN) } let!(:license) { create(:license, plan: ::License::PREMIUM_PLAN) }
before do before do
subject.clear_configuration clear_load_balancing_configuration
allow(described_class).to receive(:hosts).and_return(%w(foo)) allow(described_class).to receive(:hosts).and_return(%w(foo))
end end
...@@ -176,7 +174,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -176,7 +174,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do
context 'without a license' do context 'without a license' do
before do before do
License.destroy_all # rubocop: disable Cop/DestroyAll License.destroy_all # rubocop: disable Cop/DestroyAll
subject.clear_configuration clear_load_balancing_configuration
end end
it 'is disabled' do it 'is disabled' do
...@@ -207,10 +205,13 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -207,10 +205,13 @@ RSpec.describe Gitlab::Database::LoadBalancing do
describe '.configured?' do describe '.configured?' do
let!(:license) { create(:license, plan: ::License::PREMIUM_PLAN) } let!(:license) { create(:license, plan: ::License::PREMIUM_PLAN) }
before do
clear_load_balancing_configuration
end
it 'returns true when Sidekiq is being used' do it 'returns true when Sidekiq is being used' do
allow(described_class).to receive(:hosts).and_return(%w(foo)) allow(described_class).to receive(:hosts).and_return(%w(foo))
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true) allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
expect(described_class.configured?).to eq(true) expect(described_class.configured?).to eq(true)
end end
...@@ -238,7 +239,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -238,7 +239,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do
context 'without a license' do context 'without a license' do
before do before do
License.destroy_all # rubocop: disable Cop/DestroyAll License.destroy_all # rubocop: disable Cop/DestroyAll
subject.clear_configuration clear_load_balancing_configuration
end end
it 'is not configured' do it 'is not configured' do
...@@ -248,10 +249,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -248,10 +249,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
describe '.configure_proxy' do describe '.configure_proxy' do
after do
described_class.clear_configuration
end
it 'configures the connection proxy' do it 'configures the connection proxy' do
allow(ActiveRecord::Base.singleton_class).to receive(:prepend) allow(ActiveRecord::Base.singleton_class).to receive(:prepend)
...@@ -370,10 +367,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -370,10 +367,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
subject.configure_proxy(proxy) subject.configure_proxy(proxy)
end end
after do
subject.clear_configuration
end
context 'when the load balancer returns :replica' do context 'when the load balancer returns :replica' do
it 'returns :replica' do it 'returns :replica' do
allow(load_balancer).to receive(:db_role_for_connection).and_return(:replica) allow(load_balancer).to receive(:db_role_for_connection).and_return(:replica)
...@@ -445,7 +438,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -445,7 +438,7 @@ RSpec.describe Gitlab::Database::LoadBalancing do
model.singleton_class.prepend ::Gitlab::Database::LoadBalancing::ActiveRecordProxy model.singleton_class.prepend ::Gitlab::Database::LoadBalancing::ActiveRecordProxy
# Setup load balancing # Setup load balancing
subject.clear_configuration clear_load_balancing_configuration
allow(ActiveRecord::Base.singleton_class).to receive(:prepend) allow(ActiveRecord::Base.singleton_class).to receive(:prepend)
subject.configure_proxy(::Gitlab::Database::LoadBalancing::ConnectionProxy.new(hosts)) subject.configure_proxy(::Gitlab::Database::LoadBalancing::ConnectionProxy.new(hosts))
allow(ActiveRecord::Base.configurations[Rails.env]) allow(ActiveRecord::Base.configurations[Rails.env])
...@@ -454,10 +447,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -454,10 +447,6 @@ RSpec.describe Gitlab::Database::LoadBalancing do
.and_return('hosts' => hosts) .and_return('hosts' => hosts)
::Gitlab::Database::LoadBalancing::Session.clear_session ::Gitlab::Database::LoadBalancing::Session.clear_session
end end
after do
subject.clear_configuration
end
end end
where(:queries, :include_transaction, :expected_results) do where(:queries, :include_transaction, :expected_results) do
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe ProjectFeatureUsage, :request_store do RSpec.describe ProjectFeatureUsage, :request_store do
include_context 'clear DB Load Balancing configuration'
describe '#log_jira_dvcs_integration_usage' do describe '#log_jira_dvcs_integration_usage' do
let!(:project) { create(:project) } let!(:project) { create(:project) }
...@@ -17,10 +19,6 @@ RSpec.describe ProjectFeatureUsage, :request_store do ...@@ -17,10 +19,6 @@ RSpec.describe ProjectFeatureUsage, :request_store do
::Gitlab::Database::LoadBalancing::Session.clear_session ::Gitlab::Database::LoadBalancing::Session.clear_session
end end
after do
::Gitlab::Database::LoadBalancing.clear_configuration
end
it 'logs Jira DVCS Cloud last sync' do it 'logs Jira DVCS Cloud last sync' do
freeze_time do freeze_time do
subject.log_jira_dvcs_integration_usage subject.log_jira_dvcs_integration_usage
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Users::ActivityService, '#execute', :request_store, :redis, :clean_gitlab_redis_shared_state do RSpec.describe Users::ActivityService, '#execute', :request_store, :redis, :clean_gitlab_redis_shared_state do
include_context 'clear DB Load Balancing configuration'
let(:user) { create(:user, last_activity_on: last_activity_on) } let(:user) { create(:user, last_activity_on: last_activity_on) }
context 'when last activity is in the past' do context 'when last activity is in the past' do
...@@ -33,10 +35,6 @@ RSpec.describe Users::ActivityService, '#execute', :request_store, :redis, :clea ...@@ -33,10 +35,6 @@ RSpec.describe Users::ActivityService, '#execute', :request_store, :redis, :clea
expect(::Gitlab::Database::LoadBalancing::Session.current).to be_performed_write expect(::Gitlab::Database::LoadBalancing::Session.current).to be_performed_write
expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_using_primary expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_using_primary
end end
after do
::Gitlab::Database::LoadBalancing.clear_configuration
end
end end
context 'database load balancing is not configured' do context 'database load balancing is not configured' do
......
# frozen_string_literal: true
RSpec.shared_context 'clear DB Load Balancing configuration' do
def clear_load_balancing_configuration
proxy = ::Gitlab::Database::LoadBalancing.instance_variable_get(:@proxy)
proxy.load_balancer.release_host if proxy
::Gitlab::Database::LoadBalancing.instance_variable_set(:@proxy, nil)
::Gitlab::Database::LoadBalancing.remove_instance_variable(:@feature_available) if ::Gitlab::Database::LoadBalancing.instance_variable_defined?(:@feature_available)
::Gitlab::Database::LoadBalancing::Session.clear_session
end
around do |example|
clear_load_balancing_configuration
example.run
clear_load_balancing_configuration
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