summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sidekiq_middleware/memory_killer_spec.rb
blob: 8fdbbacd04d9744e7acaf69ab4aab3d53ce3051e (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
require 'spec_helper'

describe Gitlab::SidekiqMiddleware::MemoryKiller do
  subject { described_class.new }
  let(:pid) { 999 }

  let(:worker) { double(:worker, class: 'TestWorker') }
  let(:job) { { 'jid' => 123 } }
  let(:queue) { 'test_queue' }

  def run
    thread = subject.call(worker, job, queue) { nil }
    thread&.join
  end

  before do
    allow(subject).to receive(:get_rss).and_return(10.kilobytes)
    allow(subject).to receive(:pid).and_return(pid)
  end

  context 'when MAX_RSS is set to 0' do
    before do
      stub_const("#{described_class}::MAX_RSS", 0)
    end

    it 'does nothing' do
      expect(subject).not_to receive(:sleep)

      run
    end
  end

  context 'when MAX_RSS is exceeded' do
    before do
      stub_const("#{described_class}::MAX_RSS", 5.kilobytes)
    end

    it 'sends the STP, TERM and KILL signals at expected times' do
      expect(subject).to receive(:sleep).with(15 * 60).ordered
      expect(Process).to receive(:kill).with('SIGSTP', pid).ordered

      expect(subject).to receive(:sleep).with(30).ordered
      expect(Process).to receive(:kill).with('SIGTERM', pid).ordered

      expect(subject).to receive(:sleep).with(10).ordered
      expect(Process).to receive(:kill).with('SIGKILL', pid).ordered

      run
    end
  end

  context 'when MAX_RSS is not exceeded' do
    before do
      stub_const("#{described_class}::MAX_RSS", 15.kilobytes)
    end

    it 'does nothing' do
      expect(subject).not_to receive(:sleep)

      run
    end
  end
end