From 9150b2e0aca4e4de20dba031d123529200fd3b89 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 5 Dec 2017 19:25:54 +0100 Subject: [PATCH] Properly test every code path for Rackstash::Helpers::Time Even if it doesn't look like it, there might (and in fact did) lure unexpected errors even in seemlingly obvious code-paths. --- lib/rackstash/helpers/time.rb | 2 +- spec/rackstash/helpers/time_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/rackstash/helpers/time.rb b/lib/rackstash/helpers/time.rb index 4724012..bb99784 100644 --- a/lib/rackstash/helpers/time.rb +++ b/lib/rackstash/helpers/time.rb @@ -23,7 +23,7 @@ module Rackstash # # @return [Float] the current timestamp def clock_time - Time.now.to_f + ::Time.now.to_f end end end diff --git a/spec/rackstash/helpers/time_spec.rb b/spec/rackstash/helpers/time_spec.rb index 8481afb..b6b9867 100644 --- a/spec/rackstash/helpers/time_spec.rb +++ b/spec/rackstash/helpers/time_spec.rb @@ -20,11 +20,34 @@ describe Rackstash::Helpers::Time do end it 'returns the numeric timestamp' do + expect(::Process::CLOCK_MONOTONIC).to_not be_nil + expect(::Time).not_to receive(:now) expect(clock_time).to be_a Float end it 'is monotinically increasing' do expect(clock_time).to be < clock_time end + + context 'without a monotonic clock' do + around do |example| + clock_monotic = ::Process.send(:remove_const, :CLOCK_MONOTONIC) + verbose, $VERBOSE = $VERBOSE, false + load File.expand_path('../../../lib/rackstash/helpers/time.rb', __dir__) + $VERBOSE = verbose + + example.run + + ::Process::CLOCK_MONOTONIC = clock_monotic + verbose, $VERBOSE = $VERBOSE, false + load File.expand_path('../../../lib/rackstash/helpers/time.rb', __dir__) + $VERBOSE = verbose + end + + it 'returns a float' do + expect(::Time).to receive(:now).and_call_original + expect(clock_time).to be_a Float + end + end end end