Skip to content

Commit

Permalink
Merge pull request #2116 from osela/messagepackeventstream-issue
Browse files Browse the repository at this point in the history
Fix MessagePackEventStream issue with Enumerable methods
  • Loading branch information
ashie authored Mar 18, 2021
2 parents 9fe81ca + 4316de5 commit 5844f72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
16 changes: 3 additions & 13 deletions lib/fluent/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/test_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5844f72

Please sign in to comment.