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

event_reporter_spec.rb « watchdog « memory « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1d241249e2a91b8b5cc53f336304a990046bef2 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

require 'fast_spec_helper'
require 'prometheus/client'

RSpec.describe Gitlab::Memory::Watchdog::EventReporter, feature_category: :application_performance do
  let(:logger) { instance_double(::Logger) }
  let(:violations_counter) { instance_double(::Prometheus::Client::Counter) }
  let(:violations_handled_counter) { instance_double(::Prometheus::Client::Counter) }
  let(:reporter) { described_class.new(logger: logger) }

  def stub_prometheus_metrics
    allow(Gitlab::Metrics).to receive(:counter)
                                .with(:gitlab_memwd_violations_total, anything, anything)
                                .and_return(violations_counter)
    allow(Gitlab::Metrics).to receive(:counter)
                                .with(:gitlab_memwd_violations_handled_total, anything, anything)
                                .and_return(violations_handled_counter)

    allow(violations_counter).to receive(:increment)
    allow(violations_handled_counter).to receive(:increment)
  end

  before do
    stub_prometheus_metrics
    allow(Gitlab::Metrics::System).to receive(:memory_usage_rss).at_least(:once).and_return(
      total: 1024
    )
    allow(::Prometheus::PidProvider).to receive(:worker_id).and_return('worker_1')
  end

  describe '#logger' do
    context 'when logger is not provided' do
      let(:reporter) { described_class.new }

      it 'uses default Gitlab::AppLogger' do
        expect(reporter.logger).to eq(Gitlab::AppLogger)
      end
    end
  end

  describe '#started' do
    it 'logs start message once' do
      expect(logger).to receive(:info).once
        .with(
          pid: Process.pid,
          worker_id: 'worker_1',
          custom_label: 'dummy_label',
          memwd_rss_bytes: 1024,
          message: 'started')

      reporter.started(custom_label: 'dummy_label')
    end
  end

  describe '#stopped' do
    subject { reporter.stopped(custom_label: 'dummy_label') }

    it 'logs stop message once' do
      expect(logger).to receive(:info).once
        .with(
          pid: Process.pid,
          worker_id: 'worker_1',
          custom_label: 'dummy_label',
          memwd_rss_bytes: 1024,
          message: 'stopped')

      reporter.stopped(custom_label: 'dummy_label')
    end
  end

  describe '#threshold_violated' do
    subject { reporter.threshold_violated(:monitor_name) }

    it 'increments violations counter' do
      expect(violations_counter).to receive(:increment).with(reason: :monitor_name)

      subject
    end

    it 'does not increment handled violations counter' do
      expect(violations_handled_counter).not_to receive(:increment)

      subject
    end

    it 'does not log violation' do
      expect(logger).not_to receive(:warn)

      subject
    end
  end

  describe '#strikes_exceeded' do
    subject { reporter.strikes_exceeded(:monitor_name, { message: 'dummy_text' }) }

    before do
      allow(logger).to receive(:warn)
    end

    it 'increments handled violations counter' do
      expect(violations_handled_counter).to receive(:increment).with(reason: :monitor_name)

      subject
    end

    it 'logs violation' do
      expect(logger).to receive(:warn)
        .with({
          pid: Process.pid,
          worker_id: 'worker_1',
          memwd_rss_bytes: 1024,
          message: 'dummy_text'
        })

      subject
    end
  end
end