summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/postgresql_adapter/force_disconnectable_mixin_spec.rb
blob: 0523066b593eafc294f39f34e77b076e15155c42 (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
# 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