Sha256: 940570733734e4502832708eab5be4d4487145f772022938d074d4fd8dd9d86a

Contents?: true

Size: 1.76 KB

Versions: 11

Compression:

Stored size: 1.76 KB

Contents

require 'roqua/support/instrumentation'

describe 'Helper methods' do
  describe '#with_instrumentation' do
    include Roqua::Support::Instrumentation

    let(:logger) { double("Logger", info: nil, error: nil) }
    let(:stats)  { double("Stats", submit: nil) }

    before do
      allow(Roqua).to receive(:logger).and_return(logger)
      allow(Roqua).to receive(:stats).and_return(stats)
    end

    context 'when the block returns a value' do
      it 'logs the start and finish lifecycle of a block' do
        expect(logger).to receive(:info).with('testevent:started', {extra: "params"}).ordered
        expect(logger).to receive(:info).with('testevent:finished', hash_including({extra: "params"})).ordered
        expect(stats).to receive(:submit).with('testevent.finished', 1)
        expect(stats).to receive(:submit).with('testevent.duration', an_instance_of(Float))

        with_instrumentation('testevent', extra: 'params') { 1 + 1 }
      end

      it 'returns the value returned by the block' do
        expect(with_instrumentation('testevent') { 1 + 1 }).to eq 2
      end
    end

    context 'when an exception happens during the block' do
      it 'logs the start and failure of a block if it raises' do
        expect(logger).to receive(:info).with('testevent:started', instance_of(Hash)).ordered
        expect(logger).to receive(:error).with('testevent:failed', instance_of(Hash)).ordered
        expect(stats).to receive(:submit).with('testevent.failed', 1)

        with_instrumentation 'testevent' do
          raise StandardError, "Foo"
        end rescue nil
      end

      it 'reraises the exception' do
        expect {
          with_instrumentation 'testevent' do
            raise "Foo"
          end
        }.to raise_error('Foo')
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
roqua-support-0.4.4 spec/roqua/support/helpers_spec.rb
roqua-support-0.4.3 spec/roqua/support/helpers_spec.rb
roqua-support-0.4.2 spec/roqua/support/helpers_spec.rb
roqua-support-0.4.1 spec/roqua/support/helpers_spec.rb
roqua-support-0.4.0 spec/roqua/support/helpers_spec.rb
roqua-support-0.3.5 spec/roqua/support/helpers_spec.rb
roqua-support-0.3.4 spec/roqua/support/helpers_spec.rb
roqua-support-0.3.3 spec/roqua/support/helpers_spec.rb
roqua-support-0.3.2 spec/roqua/support/helpers_spec.rb
roqua-support-0.3.1 spec/roqua/support/helpers_spec.rb
roqua-support-0.3.0 spec/roqua/support/helpers_spec.rb