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

require 'fast_spec_helper'
require 'support/shared_examples/lib/gitlab/memory/watchdog/monitor_result_shared_examples'
require 'prometheus/client'

RSpec.describe Gitlab::Memory::Watchdog::Monitor::HeapFragmentation do
  let(:heap_frag_limit_gauge) { instance_double(::Prometheus::Client::Gauge) }
  let(:max_heap_fragmentation) { 0.2 }
  let(:fragmentation) { 0.3 }

  subject(:monitor) do
    described_class.new(max_heap_fragmentation: max_heap_fragmentation)
  end

  before do
    allow(Gitlab::Metrics).to receive(:gauge)
      .with(:gitlab_memwd_heap_frag_limit, anything)
      .and_return(heap_frag_limit_gauge)
    allow(heap_frag_limit_gauge).to receive(:set)

    allow(Gitlab::Metrics::Memory).to receive(:gc_heap_fragmentation).and_return(fragmentation)
  end

  describe '#initialize' do
    it 'sets the heap fragmentation limit gauge' do
      expect(heap_frag_limit_gauge).to receive(:set).with({}, max_heap_fragmentation)

      monitor
    end
  end

  describe '#call' do
    it 'gets gc_heap_fragmentation' do
      expect(Gitlab::Metrics::Memory).to receive(:gc_heap_fragmentation)

      monitor.call
    end

    context 'when process exceeds threshold' do
      let(:fragmentation) { max_heap_fragmentation + 0.1 }
      let(:payload) do
        {
          message: 'heap fragmentation limit exceeded',
          memwd_cur_heap_frag: fragmentation,
          memwd_max_heap_frag: max_heap_fragmentation
        }
      end

      include_examples 'returns Watchdog Monitor result', threshold_violated: true
    end

    context 'when process does not exceed threshold' do
      let(:fragmentation) { max_heap_fragmentation - 0.1 }
      let(:payload) { {} }

      include_examples 'returns Watchdog Monitor result', threshold_violated: false
    end
  end
end