Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flush support #35

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions lib/logstash/codecs/json_lines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def encode(event)
@on_event.call(event, "#{event.to_json}#{@delimiter}")
end

def flush(&block)
remainder = @buffer.flush
if !remainder.empty?
parse(@converter.convert(remainder), &block)
end
end

private

# from_json_parse uses the Event#from_json method to deserialize and directly produce events
Expand Down
19 changes: 19 additions & 0 deletions spec/codecs/json_lines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,24 @@
LogStash::Codecs::JSONLines.new(codec_options)
end
end

context "flush" do
subject do
LogStash::Codecs::JSONLines.new(codec_options)
end

let(:input) { "{\"foo\":\"bar\"}" }

it "should flush buffered data'" do
result = []
subject.decode(input) { |e| result << e }
expect(result.size).to eq(0)

subject.flush { |e| result << e }
expect(result.size).to eq(1)

expect(result[0].get("foo")).to eq("bar")
end
end
end
end