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

sampler_shared_examples.rb « metrics « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cec540cd12036e4aec60367cfd84dda3e96e2b80 (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
# frozen_string_literal: true

RSpec.shared_examples 'metrics sampler' do |env_prefix|
  context 'when sampling interval is passed explicitly' do
    subject(:sampler) { described_class.new(interval: 42, logger: double) }

    specify { expect(sampler.interval).to eq(42) }
  end

  context 'when sampling interval is passed through the environment' do
    subject(:sampler) { described_class.new(logger: double) }

    before do
      stub_env("#{env_prefix}_INTERVAL_SECONDS", '42')
    end

    specify { expect(sampler.interval).to eq(42) }
  end

  context 'when no sampling interval is passed anywhere' do
    subject(:sampler) { described_class.new(logger: double) }

    it 'uses the hardcoded default' do
      expect(sampler.interval).to eq(described_class::DEFAULT_SAMPLING_INTERVAL_SECONDS)
    end
  end

  describe '#start' do
    include WaitHelpers

    subject(:sampler) { described_class.new(interval: 0.1) }

    it 'calls the sample method on the sampler thread' do
      sampling_threads = []
      expect(sampler).to receive(:sample).at_least(:once) { sampling_threads << Thread.current }

      sampler.start

      wait_for('sampler has sampled', max_wait_time: 3) { sampling_threads.any? }
      expect(sampling_threads.first.name).to eq(sampler.thread_name)

      sampler.stop
    end

    context 'with warmup set to true' do
      subject(:sampler) { described_class.new(interval: 0.1, warmup: true) }

      it 'calls the sample method first on the caller thread' do
        sampling_threads = []
        current_thread = Thread.current
        # Instead of sampling, we're keeping track of which thread the sampling happened on.
        # We want the first sample to be on the spec thread, which would mean a blocking sample
        # before the actual sampler thread starts.
        expect(sampler).to receive(:sample).at_least(:once) { sampling_threads << Thread.current }

        sampler.start

        wait_for('sampler has sampled', max_wait_time: 3) { sampling_threads.size == 2 }

        expect(sampling_threads.first).to be(current_thread)
        expect(sampling_threads.last.name).to eq(sampler.thread_name)

        sampler.stop
      end
    end
  end

  describe '#safe_sample' do
    let(:logger) { Logger.new(File::NULL) }

    subject(:sampler) { described_class.new(logger: logger) }

    it 'calls #sample once' do
      expect(sampler).to receive(:sample)

      sampler.safe_sample
    end

    context 'when sampling fails with error' do
      before do
        expect(sampler).to receive(:sample).and_raise "something failed"
      end

      it 'recovers from errors' do
        expect { sampler.safe_sample }.not_to raise_error
      end

      context 'with logger' do
        let(:logger) { double('logger') }

        it 'logs errors' do
          expect(logger).to receive(:warn).with(an_instance_of(String))

          expect { sampler.safe_sample }.not_to raise_error
        end
      end
    end
  end
end