From 4316de500fd2407323cceef922199503c7339690 Mon Sep 17 00:00:00 2001 From: Or Sela Date: Tue, 28 Aug 2018 22:13:44 +0300 Subject: [PATCH] Fix MessagePackEventStream issue with Enumerable methods Signed-off-by: Or Sela Signed-off-by: Takuro Ashie --- lib/fluent/event.rb | 16 +++------------- test/test_event.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/fluent/event.rb b/lib/fluent/event.rb index d3cd13e02a..97711f4621 100644 --- a/lib/fluent/event.rb +++ b/lib/fluent/event.rb @@ -254,19 +254,9 @@ def slice(index, num) end def each(unpacker: nil, &block) - if @unpacked_times - @unpacked_times.each_with_index do |time, i| - block.call(time, @unpacked_records[i]) - end - else - @unpacked_times = [] - @unpacked_records = [] - (unpacker || Fluent::MessagePackFactory.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!(unpacker: unpacker) + @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 627fde37f1..b70f863dec 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