BigW Consortium Gitlab

sidekiq_middleware_spec.rb 1.79 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe Gitlab::Metrics::SidekiqMiddleware do
  let(:middleware) { described_class.new }
5
  let(:message) { { 'args' => ['test'], 'enqueued_at' => Time.new(2016, 6, 23, 6, 59).to_f } }
6 7 8

  describe '#call' do
    it 'tracks the transaction' do
9 10 11 12 13
      worker = double(:worker, class: double(:class, name: 'TestWorker'))

      expect(Gitlab::Metrics::Transaction).to receive(:new).
        with('TestWorker#perform').
        and_call_original
14

15 16 17
      expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set).
        with(:sidekiq_queue_duration, instance_of(Float))

18 19
      expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)

20 21 22 23 24 25 26 27 28 29
      middleware.call(worker, message, :test) { nil }
    end

    it 'tracks the transaction (for messages without `enqueued_at`)' do
      worker = double(:worker, class: double(:class, name: 'TestWorker'))

      expect(Gitlab::Metrics::Transaction).to receive(:new).
        with('TestWorker#perform').
        and_call_original

30 31 32
      expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:set).
        with(:sidekiq_queue_duration, instance_of(Float))

33 34 35
      expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)

      middleware.call(worker, {}, :test) { nil }
36
    end
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    it 'tracks any raised exceptions' do
      worker = double(:worker, class: double(:class, name: 'TestWorker'))

      expect_any_instance_of(Gitlab::Metrics::Transaction).
        to receive(:run).and_raise(RuntimeError)

      expect_any_instance_of(Gitlab::Metrics::Transaction).
        to receive(:add_event).with(:sidekiq_exception)

      expect_any_instance_of(Gitlab::Metrics::Transaction).
        to receive(:finish)

      expect { middleware.call(worker, message, :test) }.
        to raise_error(RuntimeError)
    end
53 54
  end
end