Sha256: 616a3e393e9b9396b872b73a64f81f020b63d9533ff91eec0b856d7ad7508123

Contents?: true

Size: 1.75 KB

Versions: 18

Compression:

Stored size: 1.75 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
        with_instrumentation('testevent') { 1 + 1 }.should == 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

18 entries across 18 versions & 1 rubygems

Version Path
roqua-support-0.1.34 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.33 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.32 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.31 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.30 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.29 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.28 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.27 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.26 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.25 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.24 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.23 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.22 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.21 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.20 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.19 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.18 spec/roqua/support/helpers_spec.rb
roqua-support-0.1.17 spec/roqua/support/helpers_spec.rb