summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/memory/watchdog/monitor_state_spec.rb
blob: 7802e274c538bb9c2c443adb2873c40744ad1199 (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::Memory::Watchdog::MonitorState do
  let(:max_strikes) { 2 }
  let(:payload) { { message: 'DummyMessage' } }
  let(:threshold_violated) { true }
  let(:monitor) { monitor_class.new(threshold_violated, payload) }
  let(:monitor_name) { :dummy_monitor_name }
  let(:monitor_class) do
    Struct.new(:threshold_violated, :payload) do
      def call
        { threshold_violated: threshold_violated, payload: payload }
      end

      def self.name
        'MonitorName'
      end
    end
  end

  subject(:monitor_state) { described_class.new(monitor, max_strikes: max_strikes, monitor_name: monitor_name) }

  shared_examples 'returns correct result' do
    it 'returns correct result', :aggregate_failures do
      result = monitor_state.call

      expect(result).to be_an_instance_of(described_class::Result)
      expect(result.strikes_exceeded?).to eq(strikes_exceeded)
      expect(result.threshold_violated?).to eq(threshold_violated)
      expect(result.payload).to eq(expected_payload)
      expect(result.monitor_name).to eq(monitor_name)
    end
  end

  describe '#call' do
    let(:strikes_exceeded) { false }
    let(:curr_strikes) { 0 }
    let(:expected_payload) do
      {
        memwd_max_strikes: max_strikes,
        memwd_cur_strikes: curr_strikes
      }.merge(payload)
    end

    context 'when threshold is not violated' do
      let(:threshold_violated) { false }

      include_examples 'returns correct result'
    end

    context 'when threshold is violated' do
      let(:curr_strikes) { 1 }
      let(:threshold_violated) { true }

      include_examples 'returns correct result'

      context 'when strikes_exceeded' do
        let(:max_strikes) { 0 }
        let(:strikes_exceeded) { true }

        include_examples 'returns correct result'
      end
    end
  end
end