File: counter_spec.rb

package info (click to toggle)
ruby-prometheus-client-mmap 1.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 700 kB
  • sloc: ruby: 3,149; sh: 54; makefile: 21
file content (67 lines) | stat: -rw-r--r-- 1,779 bytes parent folder | download
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
require 'prometheus/client/counter'
require 'prometheus/client'
require 'examples/metric_example'

describe Prometheus::Client::Counter do
  before do
    allow(Prometheus::Client.configuration).to receive(:multiprocess_files_dir).and_return('tmp/')
  end

  let(:counter) { Prometheus::Client::Counter.new(:foo, 'foo description') }

  it_behaves_like Prometheus::Client::Metric do
    let(:type) { Float }
  end

  describe 'Memory Error tests' do
    it "creating many counters shouldn't cause a SIGBUS" do
      4.times do |j|
        9999.times do |i|
          counter = Prometheus::Client::Counter.new("foo#{j}_z#{i}".to_sym, 'some string')
          counter.increment
        end
        GC.start
      end
    end
  end

  describe '#increment' do
    it 'increments the counter' do
      expect { counter.increment }.to change { counter.get }.by(1)
    end

    it 'increments the counter for a given label set' do
      expect do
        expect do
          counter.increment(test: 'label')
        end.to change { counter.get(test: 'label') }.by(1)
      end.to_not change { counter.get(test: 'other_label') }
    end

    it 'increments the counter by a given value' do
      expect do
        counter.increment({}, 5)
      end.to change { counter.get }.by(5)
    end

    it 'raises an ArgumentError on negative increments' do
      expect do
        counter.increment({}, -1)
      end.to raise_error ArgumentError
    end

    it 'returns the new counter value' do
      expect(counter.increment).to eql(counter.get)
    end

    it 'is thread safe' do
      expect do
        Array.new(10) do
          Thread.new do
            10.times { counter.increment }
          end
        end.each(&:join)
      end.to change { counter.get }.by(100)
    end
  end
end