summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/connection_timer_spec.rb
blob: 2b6746bae07dbbcb3e5bfc6c3281d8ee951767d6 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

require 'spec_helper'

RSpec.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