From 0ea75950098ff11e1c217119dd44181a8500c6d8 Mon Sep 17 00:00:00 2001 From: Colin Kelley Date: Mon, 9 Nov 2020 08:24:35 -0800 Subject: [PATCH] issue #510: move if/else constant testing outside now method --- lib/listen/monotonic_time.rb | 18 ++++++++++++++---- spec/lib/listen/monotonic_time_spec.rb | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/listen/monotonic_time.rb b/lib/listen/monotonic_time.rb index 424073af..51b0fc9d 100644 --- a/lib/listen/monotonic_time.rb +++ b/lib/listen/monotonic_time.rb @@ -3,14 +3,24 @@ module Listen module MonotonicTime class << self - def now - if defined?(Process::CLOCK_MONOTONIC) + if defined?(Process::CLOCK_MONOTONIC) + + def now Process.clock_gettime(Process::CLOCK_MONOTONIC) - elsif defined?(Process::CLOCK_MONOTONIC_RAW) + end + + elsif defined?(Process::CLOCK_MONOTONIC_RAW) + + def now Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW) - else + end + + else + + def now Time.now.to_f end + end end end diff --git a/spec/lib/listen/monotonic_time_spec.rb b/spec/lib/listen/monotonic_time_spec.rb index 0ae8db96..4c21f46a 100644 --- a/spec/lib/listen/monotonic_time_spec.rb +++ b/spec/lib/listen/monotonic_time_spec.rb @@ -3,6 +3,11 @@ require 'listen/monotonic_time' RSpec.describe Listen::MonotonicTime do + after(:all) do + # load once more with constants unstubbed/unhidden + load './lib/listen/monotonic_time.rb' + end + context 'module methods' do describe '.now' do subject { described_class.now } @@ -11,6 +16,7 @@ context 'when CLOCK_MONOTONIC defined' do before do stub_const('Process::CLOCK_MONOTONIC', 10) + load './lib/listen/monotonic_time.rb' end it 'returns the CLOCK_MONOTONIC tick count' do @@ -23,6 +29,7 @@ before do hide_const('Process::CLOCK_MONOTONIC') stub_const('Process::CLOCK_MONOTONIC_RAW', 11) + load './lib/listen/monotonic_time.rb' end it 'returns the floating point Time.now' do @@ -37,6 +44,7 @@ before do hide_const('Process::CLOCK_MONOTONIC') hide_const('Process::CLOCK_MONOTONIC_RAW') + load './lib/listen/monotonic_time.rb' end it 'returns the floating point Time.now' do