rails_cache_spec.rb 6.61 KB
Newer Older
Yorick Peterse's avatar
Yorick Peterse committed
1 2 3
require 'spec_helper'

describe Gitlab::Metrics::Subscribers::RailsCache do
Paweł Chojnacki's avatar
Paweł Chojnacki committed
4
  let(:env) { {} }
5
  let(:transaction) { Gitlab::Metrics::WebTransaction.new(env) }
Yorick Peterse's avatar
Yorick Peterse committed
6 7 8 9 10 11
  let(:subscriber) { described_class.new }

  let(:event) { double(:event, duration: 15.2) }

  describe '#cache_read' do
    it 'increments the cache_read duration' do
Paweł Chojnacki's avatar
Paweł Chojnacki committed
12 13
      expect(subscriber).to receive(:observe)
                              .with(:read, event.duration)
Yorick Peterse's avatar
Yorick Peterse committed
14 15 16

      subscriber.cache_read(event)
    end
17 18 19

    context 'with a transaction' do
      before do
20
        allow(subscriber).to receive(:current_transaction)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
21
                               .and_return(transaction)
22 23 24 25 26 27
      end

      context 'with hit event' do
        let(:event) { double(:event, duration: 15.2, payload: { hit: true }) }

        it 'increments the cache_read_hit count' do
28
          expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
29
                                   .with(:cache_read_hit_count, 1, false)
30
          expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
31
                                   .with(any_args).at_least(1) # Other calls
32 33 34 35 36 37 38 39

          subscriber.cache_read(event)
        end

        context 'when super operation is fetch' do
          let(:event) { double(:event, duration: 15.2, payload: { hit: true, super_operation: :fetch }) }

          it 'does not increment cache read miss' do
40
            expect(transaction).not_to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
41
                                         .with(:cache_read_hit_count, 1)
42 43 44 45 46 47 48 49 50 51

            subscriber.cache_read(event)
          end
        end
      end

      context 'with miss event' do
        let(:event) { double(:event, duration: 15.2, payload: { hit: false }) }

        it 'increments the cache_read_miss count' do
52
          expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
53
                                   .with(:cache_read_miss_count, 1, false)
54
          expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
55
                                   .with(any_args).at_least(1) # Other calls
56 57 58 59

          subscriber.cache_read(event)
        end

60
        it 'increments the cache_read_miss total' do
61
          expect(subscriber.send(:metric_cache_misses_total)).to receive(:increment).with({})
62 63 64 65

          subscriber.cache_read(event)
        end

66 67 68 69
        context 'when super operation is fetch' do
          let(:event) { double(:event, duration: 15.2, payload: { hit: false, super_operation: :fetch }) }

          it 'does not increment cache read miss' do
70
            expect(transaction).not_to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
71
                                         .with(:cache_read_miss_count, 1)
72 73 74

            subscriber.cache_read(event)
          end
75 76

          it 'does not increment cache_read_miss total' do
77
            expect(subscriber.send(:metric_cache_misses_total)).not_to receive(:increment).with({})
78 79 80

            subscriber.cache_read(event)
          end
81 82 83
        end
      end
    end
Yorick Peterse's avatar
Yorick Peterse committed
84 85 86
  end

  describe '#cache_write' do
87
    it 'observes write duration' do
Paweł Chojnacki's avatar
Paweł Chojnacki committed
88 89
      expect(subscriber).to receive(:observe)
                              .with(:write, event.duration)
Yorick Peterse's avatar
Yorick Peterse committed
90 91 92 93 94 95

      subscriber.cache_write(event)
    end
  end

  describe '#cache_delete' do
96
    it 'observes delete duration' do
Paweł Chojnacki's avatar
Paweł Chojnacki committed
97 98
      expect(subscriber).to receive(:observe)
                              .with(:delete, event.duration)
Yorick Peterse's avatar
Yorick Peterse committed
99 100 101 102 103 104

      subscriber.cache_delete(event)
    end
  end

  describe '#cache_exist?' do
105
    it 'observes the exists duration' do
Paweł Chojnacki's avatar
Paweł Chojnacki committed
106 107
      expect(subscriber).to receive(:observe)
                              .with(:exists, event.duration)
Yorick Peterse's avatar
Yorick Peterse committed
108 109 110 111 112

      subscriber.cache_exist?(event)
    end
  end

113 114 115 116 117 118 119 120 121 122 123
  describe '#cache_fetch_hit' do
    context 'without a transaction' do
      it 'returns' do
        expect(transaction).not_to receive(:increment)

        subscriber.cache_fetch_hit(event)
      end
    end

    context 'with a transaction' do
      before do
124
        allow(subscriber).to receive(:current_transaction)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
125
                               .and_return(transaction)
126 127 128
      end

      it 'increments the cache_read_hit count' do
129
        expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
130
                                 .with(:cache_read_hit_count, 1)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

        subscriber.cache_fetch_hit(event)
      end
    end
  end

  describe '#cache_generate' do
    context 'without a transaction' do
      it 'returns' do
        expect(transaction).not_to receive(:increment)

        subscriber.cache_generate(event)
      end
    end

    context 'with a transaction' do
147 148
      let(:metric_cache_misses_total) { double('metric_cache_misses_total', increment: nil) }

149
      before do
150
        allow(subscriber).to receive(:metric_cache_misses_total).and_return(metric_cache_misses_total)
151
        allow(subscriber).to receive(:current_transaction)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
152
                               .and_return(transaction)
153 154 155
      end

      it 'increments the cache_fetch_miss count' do
156
        expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
157
                                 .with(:cache_read_miss_count, 1)
158 159 160

        subscriber.cache_generate(event)
      end
161 162 163

      it 'increments the cache_read_miss total' do
        subscriber.cache_generate(event)
164 165

        expect(metric_cache_misses_total).to have_received(:increment).with({})
166
      end
167 168 169
    end
  end

Paweł Chojnacki's avatar
Paweł Chojnacki committed
170
  describe '#observe' do
Yorick Peterse's avatar
Yorick Peterse committed
171 172 173 174
    context 'without a transaction' do
      it 'returns' do
        expect(transaction).not_to receive(:increment)

Paweł Chojnacki's avatar
Paweł Chojnacki committed
175
        subscriber.observe(:foo, 15.2)
Yorick Peterse's avatar
Yorick Peterse committed
176 177 178 179 180
      end
    end

    context 'with a transaction' do
      before do
181
        allow(subscriber).to receive(:current_transaction)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
182
                               .and_return(transaction)
Yorick Peterse's avatar
Yorick Peterse committed
183 184 185
      end

      it 'increments the total and specific cache duration' do
186
        expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
187
                                 .with(:cache_duration, event.duration, false)
Yorick Peterse's avatar
Yorick Peterse committed
188

189
        expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
190
                                 .with(:cache_count, 1, false)
191

192
        expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
193
                                 .with(:cache_delete_duration, event.duration, false)
Yorick Peterse's avatar
Yorick Peterse committed
194

195
        expect(transaction).to receive(:increment)
Paweł Chojnacki's avatar
Paweł Chojnacki committed
196
                                 .with(:cache_delete_count, 1, false)
197

Paweł Chojnacki's avatar
Paweł Chojnacki committed
198
        subscriber.observe(:delete, event.duration)
Yorick Peterse's avatar
Yorick Peterse committed
199
      end
200 201

      it 'observes cache metric' do
202
        expect(subscriber.send(:metric_cache_operation_duration_seconds))
203
          .to receive(:observe)
Ben Kochie's avatar
Ben Kochie committed
204 205 206 207 208 209 210 211 212
          .with({ operation: :delete }, event.duration / 1000.0)

        subscriber.observe(:delete, event.duration)
      end

      it 'increments the operations total' do
        expect(subscriber.send(:metric_cache_operations_total))
          .to receive(:increment)
          .with(transaction.labels.merge(operation: :delete))
213 214 215

        subscriber.observe(:delete, event.duration)
      end
Yorick Peterse's avatar
Yorick Peterse committed
216 217 218
    end
  end
end