require 'spec_helper' require 'rack-timer' describe RackTimer::Middleware do let(:buffer) { StringIO.new } let(:output) { buffer.rewind ; buffer.read } let(:statsd) { double(timing: true) } let(:app) { build_stack *stack } let(:stack) { [Test::App, Test::FastMiddleware, RackTimer::Middleware] } before do RackTimer.output = buffer RackTimer.statsd = statsd end def build_stack(app_class, *middlewares) app = app_class.new middlewares.each do |m| app = m.new(app) end app end describe 'assimilation' do it 'gets logged' do app output.should =~ /assimilating: RackTimer::Middleware/ output.should =~ /assimilating: Test::FastMiddleware/ end end describe 'middleware timing' do it 'logs app timing' do app.call({}) output.should =~ /Test::App took \d+ us/ end it 'logs middleware timing' do statsd.should_receive(:timing).with("rack-timer.time.test_app", kind_of(Integer)) statsd.should_receive(:timing).with("rack-timer.time.test_fastmiddleware", kind_of(Integer)) statsd.should_receive(:timing).with("rack-timer.time.racktimer_middleware", kind_of(Integer)) app.call({}) output.should =~ /Test::FastMiddleware took \d+ us/ end context 'with a slow middleware' do let(:stack) { [Test::App, Test::SlowMiddleware, Test::FastMiddleware, RackTimer::Middleware] } it 'times the slow middleware' do app.call({}) output.should =~ /Test::SlowMiddleware took \d+ us/ end it 'does not include the slow middleware time in the outer middleware' do app.call({}) time_slow = /Test::SlowMiddleware took (?