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:
parent
6e58dc19b3
commit
cadc235795
30
lib/rackstash/helpers/time.rb
Normal file
30
lib/rackstash/helpers/time.rb
Normal 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
|
||||
29
spec/rackstash/helpers/time_spec.rb
Normal file
29
spec/rackstash/helpers/time_spec.rb
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user