1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Add Helpers::Time module to access a monotonic clock

This commit is contained in:
Holger Just 2017-08-30 01:01:15 +02:00
parent 6e58dc19b3
commit cadc235795
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,30 @@
# frozen_string_literal: true
# Copyright 2017 Holger Just
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
module Rackstash
module Helpers
module Time
protected
if defined?(Process::CLOCK_MONOTONIC)
# Get the current timestamp as a numeric value. If supported by the
# current platform, we use a monitonic clock.
#
# @return [Float] the current timestamp
def clock_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
else
# Get the current timestamp as a numeric value
#
# @return [Float] the current timestamp
def clock_time
Time.now.to_f
end
end
end
end
end

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
# Copyright 2017 Holger Just
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.
require 'spec_helper'
require 'rackstash/helpers/time'
describe Rackstash::Helpers::Time do
it 'only defines protected methods' do
expect(described_class.public_instance_methods(false)).to be_empty
end
describe '#clock_time' do
def clock_time(*args)
Object.new.extend(described_class).send(:clock_time, *args)
end
it 'returns the numeric timestamp' do
expect(clock_time).to be_a Float
end
it 'is monotinically increasing' do
expect(clock_time).to be < clock_time
end
end
end