diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-05 18:07:52 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-05 18:07:52 +0300 |
commit | afe2b984524ae4b0c8a0636db7ec5b2c452f0734 (patch) | |
tree | 3de39f954c7239e09a9afe84263a64e7042b2b60 /spec/lib/gitlab/database | |
parent | 5a6b36b60502c50ab59c0bc3c345793b70a3d548 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/database')
-rw-r--r-- | spec/lib/gitlab/database/connection_timer_spec.rb | 100 | ||||
-rw-r--r-- | spec/lib/gitlab/database/postgresql_adapter/force_disconnectable_mixin_spec.rb | 55 |
2 files changed, 155 insertions, 0 deletions
diff --git a/spec/lib/gitlab/database/connection_timer_spec.rb b/spec/lib/gitlab/database/connection_timer_spec.rb new file mode 100644 index 00000000000..c9e9d770343 --- /dev/null +++ b/spec/lib/gitlab/database/connection_timer_spec.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Database::ConnectionTimer do + let(:current_clock_value) { 1234.56 } + + before do + allow(described_class).to receive(:current_clock_value).and_return(current_clock_value) + end + + describe '.starting_now' do + let(:default_interval) { described_class::DEFAULT_INTERVAL } + let(:random_value) { 120 } + + before do + allow(described_class).to receive(:rand).and_return(random_value) + end + + context 'when the configured interval is positive' do + before do + allow(described_class).to receive(:interval).and_return(default_interval) + end + + it 'randomizes the interval of the created timer' do + timer = described_class.starting_now + + expect(timer.interval).to eq(default_interval + random_value) + end + end + + context 'when the configured interval is not positive' do + before do + allow(described_class).to receive(:interval).and_return(0) + end + + it 'sets the interval of the created timer to nil' do + timer = described_class.starting_now + + expect(timer.interval).to be_nil + end + end + end + + describe '.expired?' do + context 'when the interval is positive' do + context 'when the interval has elapsed' do + it 'returns true' do + timer = described_class.new(20, current_clock_value - 30) + + expect(timer).to be_expired + end + end + + context 'when the interval has not elapsed' do + it 'returns false' do + timer = described_class.new(20, current_clock_value - 10) + + expect(timer).not_to be_expired + end + end + end + + context 'when the interval is not positive' do + context 'when the interval has elapsed' do + it 'returns false' do + timer = described_class.new(0, current_clock_value - 30) + + expect(timer).not_to be_expired + end + end + + context 'when the interval has not elapsed' do + it 'returns false' do + timer = described_class.new(0, current_clock_value + 10) + + expect(timer).not_to be_expired + end + end + end + + context 'when the interval is nil' do + it 'returns false' do + timer = described_class.new(nil, current_clock_value - 30) + + expect(timer).not_to be_expired + end + end + end + + describe '.reset!' do + it 'updates the timer clock value' do + timer = described_class.new(20, current_clock_value - 20) + expect(timer.starting_clock_value).not_to eql(current_clock_value) + + timer.reset! + expect(timer.starting_clock_value).to eql(current_clock_value) + end + end +end diff --git a/spec/lib/gitlab/database/postgresql_adapter/force_disconnectable_mixin_spec.rb b/spec/lib/gitlab/database/postgresql_adapter/force_disconnectable_mixin_spec.rb new file mode 100644 index 00000000000..0523066b593 --- /dev/null +++ b/spec/lib/gitlab/database/postgresql_adapter/force_disconnectable_mixin_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin do + describe 'checking in a connection to the pool' do + let(:model) do + Class.new(ActiveRecord::Base) do + self.abstract_class = true + + def self.name + 'ForceDisconnectTestModel' + end + end + end + let(:config) { Rails.application.config_for(:database).merge(pool: 1) } + let(:pool) { model.establish_connection(config) } + + it 'calls the force disconnect callback on checkin' do + connection = pool.connection + + expect(pool.active_connection?).to be_truthy + expect(connection).to receive(:force_disconnect_if_old!).and_call_original + + model.clear_active_connections! + end + end + + describe 'disconnecting from the database' do + let(:connection) { ActiveRecord::Base.connection_pool.connection } + let(:timer) { connection.force_disconnect_timer } + + context 'when the timer is expired' do + it 'disconnects from the database' do + allow(timer).to receive(:expired?).and_return(true) + + expect(connection).to receive(:disconnect!).and_call_original + expect(timer).to receive(:reset!).and_call_original + + connection.force_disconnect_if_old! + end + end + + context 'when the timer is not expired' do + it 'does not disconnect from the database' do + allow(timer).to receive(:expired?).and_return(false) + + expect(connection).not_to receive(:disconnect!) + expect(timer).not_to receive(:reset!) + + connection.force_disconnect_if_old! + end + end + end +end |