diff --git a/lib/fluent/time.rb b/lib/fluent/time.rb index 793dd58e61..691db93e9a 100644 --- a/lib/fluent/time.rb +++ b/lib/fluent/time.rb @@ -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 diff --git a/test/test_event_time.rb b/test/test_event_time.rb index 23343bfa22..26aa0ee773 100644 --- a/test/test_event_time.rb +++ b/test/test_event_time.rb @@ -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 diff --git a/test/test_time_formatter.rb b/test/test_time_formatter.rb index 3780fd7e8b..ec808b959f 100644 --- a/test/test_time_formatter.rb +++ b/test/test_time_formatter.rb @@ -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) @@ -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