require 'spec_helper' require 'timecop' require 'active_support/core_ext/numeric/time' describe ::Protobuf::Rpc::Stat do before(:all) do unless defined?(BarService) class BarService < ::Struct.new(:method_name); end end end describe 'server mode' do it 'describes a server response to a client' do ::Timecop.freeze(10.minutes.ago) do stats = ::Protobuf::Rpc::Stat.new(:SERVER) stats.client = 'myserver1' stats.dispatcher = double('dispatcher', :service => BarService.new(:find_bars)) stats.request_size = 43 stats.response_size = 1302 ::Timecop.freeze(1.62.seconds.from_now) do stats.stop stats.to_s.should eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - #{::Time.now.iso8601}" end end end context 'when request is still running' do it 'omits response size, duration, and timestamp' do stats = ::Protobuf::Rpc::Stat.new(:SERVER) stats.client = 'myserver1' stats.dispatcher = double('dispatcher', :service => BarService.new(:find_bars)) stats.request_size = 43 stats.to_s.should eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/-" end end end describe 'client mode' do it 'describes a client request to a server' do ::Timecop.freeze(10.minutes.ago) do stats = ::Protobuf::Rpc::Stat.new(:CLIENT) stats.server = ['30000', 'myserver1.myhost.com'] stats.service = 'Foo::BarService' stats.method_name = 'find_bars' stats.request_size = 37 stats.response_size = 12345 ::Timecop.freeze(0.832.seconds.from_now) do stats.stop stats.to_s.should eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - #{::Time.now.iso8601}" end end end context 'when request is still running' do it 'omits response size, duration, and timestamp' do stats = ::Protobuf::Rpc::Stat.new(:CLIENT) stats.server = ['30000', 'myserver1.myhost.com'] stats.service = 'Foo::BarService' stats.method_name = 'find_bars' stats.request_size = 37 stats.to_s.should eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/-" end end end end