Skip to content

Commit 191a2ea

Browse files
committed
Add process extenstion to use Procss.clock_gettime with timecop
Signed-off-by: Yuta Iwama <[email protected]>
1 parent 578c07d commit 191a2ea

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

test/helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def to_masked_element
5050
require 'fluent/time'
5151
require 'serverengine'
5252
require 'helpers/fuzzy_assert'
53+
require 'helpers/process_extenstion'
5354

5455
module Fluent
5556
module Plugin

test/helpers/process_extenstion.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'timecop'
2+
3+
module Process
4+
class << self
5+
alias_method :clock_gettime_original, :clock_gettime
6+
7+
# this can be used with timecop
8+
def clock_gettime(clock_id, unit = :float_second)
9+
if Process::CLOCK_REALTIME == clock_id
10+
t = Time.now
11+
12+
case unit
13+
when :float_second
14+
t.to_i + t.nsec / 1_000_000_000.0
15+
when :float_millisecond
16+
t.to_i * 1_000 + t.nsec / 1_000_000.0
17+
when :float_microsecond
18+
t.to_i * 1_000_000 + t.nsec / 1_000.0
19+
when :second
20+
t.to_i
21+
when :millisecond
22+
t.to_i * 1000 + t.nsec / 1_000_000
23+
when :microsecond
24+
t.to_i * 1_000_000 + t.nsec / 1_000
25+
when :nanosecond
26+
t.to_i * 1_000_000_000 + t.nsec
27+
end
28+
else
29+
Process.clock_gettime_original(clock_id, unit)
30+
end
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)