Skip to content
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
4 changes: 3 additions & 1 deletion lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def self.from_time(time)
end

def self.eq?(a, b)
if a.is_a?(Fluent::EventTime) && b.is_a?(Fluent::EventTime)
return false unless a.class == b.class

if a.is_a?(Fluent::EventTime)
a.sec == b.sec && a.nsec == b.nsec
else
a == b
Expand Down
4 changes: 2 additions & 2 deletions test/test_event_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ class EventTimeTest < Test::Unit::TestCase
refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), Fluent::EventTime.new(3, 2)))
refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), Fluent::EventTime.new(3, 4)))

assert(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), 1))
refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), 1))
refute(Fluent::EventTime.eq?(Fluent::EventTime.new(1, 2), 2))

assert(Fluent::EventTime.eq?(1, Fluent::EventTime.new(1, 2)))
refute(Fluent::EventTime.eq?(1, Fluent::EventTime.new(1, 2)))
refute(Fluent::EventTime.eq?(2, Fluent::EventTime.new(1, 2)))
end

Expand Down
21 changes: 19 additions & 2 deletions test/test_time_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ def test_format_negative_timestamp
assert_equal("1969-12-31T23:59:59Z", formatter.format(Fluent::EventTime.new(-1)))
end

# Here the slot is compared with EventTime.eq?, which falls back to comparing
# seconds alone, so the whole first second of the epoch matched an empty slot.
# Here the slot is compared with EventTime.eq?, which used to fall back to
# comparing seconds alone, so the whole first second of the epoch matched an
# empty slot.
def test_format_epoch_with_subsec
# Only the tc2 slot is used here, so every timestamp is a miss.
formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%N", false, nil)
Expand All @@ -241,6 +242,22 @@ def test_format_epoch_with_subsec
assert_equal("19700101 000000.999999999", formatter.format(Fluent::EventTime.new(0, 999999999)))
end

# A cached EventTime must not answer for an Integer time of the same second:
# EventTime.eq? used to compare the seconds alone then, so the cached string
# carried the subsecond part of the earlier event.
def test_format_with_subsec_integer_and_event_time_of_the_same_second
time = Time.new(2014, 9, 27, 0, 0, 0, 0).to_i
formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%N", false, nil)
assert_equal("20140927 000000.123456789", formatter.format(Fluent::EventTime.new(time, 123456789)))
assert_equal("20140927 000000.000000000", formatter.format(time))
assert_equal("20140927 000000.123456789", formatter.format(Fluent::EventTime.new(time, 123456789)))
assert_equal("20140927 000000.000000000", formatter.format(time))

formatter = Fluent::TimeFormatter.new("%Y%m%d %H%M%S.%3N", false, nil)
assert_equal("20140927 000000.000", formatter.format(time))
assert_equal("20140927 000000.500", formatter.format(Fluent::EventTime.new(time, 500_000_000)))
end

sub_test_case 'TimeMixin::Formatter' do
class DummyForTimeFormatter
include Fluent::Configurable
Expand Down
Loading