Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(timecop gem): add support for Timecop 0.9.9 so that we could track proper tests' execution time when Process.clock_gettime is mocked #262

Merged
merged 8 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

### UNRELEASED

### 7.6.1

* Add support for the Timecop 0.9.9 gem version so that we could track proper tests' execution time when `Process.clock_gettime` is mocked.

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/262

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v7.6.0...v7.6.1

### 7.6.0

* Avoid starting an unnecessary process in Queue Mode.
Expand Down
2 changes: 1 addition & 1 deletion knapsack_pro.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'pry', '~> 0'
spec.add_development_dependency 'vcr', '>= 6.0'
spec.add_development_dependency 'webmock', '>= 3.13'
spec.add_development_dependency 'timecop', '>= 0.9.4'
spec.add_development_dependency 'timecop', '>= 0.9.9'
end
2 changes: 1 addition & 1 deletion lib/knapsack_pro/formatters/time_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def merge(h1, h2)
end

def now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
KnapsackPro::Utils.time_now
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/knapsack_pro/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def update_test_file_time(execution_time)
end

def now_without_mock_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
KnapsackPro::Utils.time_now
end
end
end
8 changes: 8 additions & 0 deletions lib/knapsack_pro/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ class Utils
def self.unsymbolize(obj)
JSON.parse(obj.to_json)
end

def self.time_now
if defined?(Timecop) && Process.respond_to?(:clock_gettime_without_mock)
Process.clock_gettime_without_mock(Process::CLOCK_MONOTONIC)
else
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
end
end
end
36 changes: 36 additions & 0 deletions spec/knapsack_pro/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,40 @@
])
end
end

describe '.time_now' do
subject { described_class.time_now }

context 'when Timecop does not mock the time' do
it do
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
expect(subject).to be_within(0.001).of(now)
end
end

context 'when Timecop does mock the process clock' do
around(:example) do |ex|
unless Gem::Version.new(Timecop::VERSION) >= Gem::Version.new('0.9.9')
raise 'Timecop >= 0.9.9 is required to run this test. Please run: bundle update'
end

if Gem::Version.new(Timecop::VERSION) >= Gem::Version.new('0.9.10')
Timecop.mock_process_clock = true
ex.run
Timecop.mock_process_clock = false
else
ex.run
end
end

it do
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)

time = Time.local(2020, 1, 31)
Timecop.travel(time) do
expect(subject).to be_within(0.001).of(now)
end
end
end
end
end