Welcome to mirror list, hosted at ThFree Co, Russian Federation.

force_disconnectable_mixin_spec.rb « postgresql_adapter « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3650ca1d9044ae8173adc649f78c681581090281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::PostgresqlAdapter::ForceDisconnectableMixin, :delete, :reestablished_active_record_base 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) { ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash.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
      before do
        allow(timer).to receive(:expired?).and_return(true)
      end

      it 'disconnects from the database' do
        expect(connection).to receive(:disconnect!).and_call_original
        expect(timer).to receive(:reset!).and_call_original

        connection.force_disconnect_if_old!
      end

      context 'when the connection has an open transaction' do
        it 'does not disconnect from the database' do
          connection.begin_transaction

          expect(connection).not_to receive(:disconnect!)
          expect(timer).not_to receive(:reset!)

          connection.force_disconnect_if_old!

          connection.rollback_transaction
        end
      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