diff --git a/lib/fluent/event.rb b/lib/fluent/event.rb index b316da073f..04a013e5ac 100644 --- a/lib/fluent/event.rb +++ b/lib/fluent/event.rb @@ -251,19 +251,9 @@ def slice(index, num) end def each(&block) - if @unpacked_times - @unpacked_times.each_with_index do |time, i| - block.call(time, @unpacked_records[i]) - end - else - @unpacked_times = [] - @unpacked_records = [] - msgpack_unpacker.feed_each(@data) do |time, record| - @unpacked_times << time - @unpacked_records << record - block.call(time, record) - end - @size = @unpacked_times.size + ensure_unpacked! + @unpacked_times.each_with_index do |time, i| + block.call(time, @unpacked_records[i]) end nil end diff --git a/test/test_event.rb b/test/test_event.rb index 93e7eb38d9..a51bd65f1e 100644 --- a/test/test_event.rb +++ b/test/test_event.rb @@ -401,6 +401,22 @@ def setup i += 1 } end + + # `any?` represents an Enumerable method which calls `each` internally + test 'size_after_any' do + @es.any? + + assert_equal 2, @es.size + end + + # `any?` represents an Enumerable method which calls `each` internally + test 'each_after_any' do + @es.any? + + count = 0 + @es.each { |time, record| count += 1 } + assert_equal 2, count + end end class CompressedMessagePackEventStreamTest < ::Test::Unit::TestCase