Skip to content

Commit

Permalink
refactor to support testing from_json and legacy parser
Browse files Browse the repository at this point in the history
  • Loading branch information
colinsurprenant committed Feb 8, 2016
1 parent d0bb6a5 commit 5a737f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
19 changes: 14 additions & 5 deletions lib/logstash/codecs/json_lines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def register
@buffer = FileWatch::BufferedTokenizer.new(@delimiter)
@converter = LogStash::Util::Charset.new(@charset)
@converter.logger = @logger

# pick the correct parser here and not at class evaluation so that we can control this
# mechanism in rspec and actually force use both strategies.
self.class.module_eval do
# keep compatibility with all v2.x distributions. only in 2.3 will the Event#from_json method be introduced
# and we need to keep compatibility for all v2 releases.
alias_method :parse, from_json_exposed? ? :from_json_parse : :legacy_parse
end
end

def decode(data, &block)
Expand All @@ -48,6 +56,12 @@ def encode(event)
@on_event.call(event, "#{event.to_json}#{@delimiter}")
end # def encode

# this method is made public to that we can mock it in rspec and control
# testing using from_json or legacy mechanism.
def self.from_json_exposed?
LogStash::Event.respond_to?(:from_json)
end

private

# from_json_parse uses the Event#from_json method to deserialize and directly produce events
Expand All @@ -63,9 +77,4 @@ def legacy_parse(json, &block)
rescue LogStash::Json::ParserError
yield LogStash::Event.new("message" => json, "tags" => ["_jsonparsefailure"])
end

# keep compatibility with all v2.x distributions. only in 2.3 will the Event#from_json method be introduced
# and we need to keep compatibility for all v2 releases.
alias_method :parse, LogStash::Event.respond_to?(:from_json) ? :from_json_parse : :legacy_parse

end # class LogStash::Codecs::JSONLines
38 changes: 29 additions & 9 deletions spec/codecs/json_lines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
require "insist"

describe LogStash::Codecs::JSONLines do
subject do
next LogStash::Codecs::JSONLines.new
end

let(:codec_options) { {} }

shared_examples :codec do

context "#decode" do
it "should return an event from json data" do
Expand Down Expand Up @@ -37,9 +38,7 @@
context "when using custom delimiter" do
let(:delimiter) { "|" }
let(:line) { "{\"hey\":1}|{\"hey\":2}|{\"hey\":3}|" }
subject do
next LogStash::Codecs::JSONLines.new("delimiter" => delimiter)
end
let(:codec_options) { { "delimiter" => delimiter } }

it "should decode multiple lines separated by the delimiter" do
result = []
Expand Down Expand Up @@ -122,9 +121,7 @@

context "when using custom delimiter" do
let(:delimiter) { "|" }
subject do
next LogStash::Codecs::JSONLines.new("delimiter" => delimiter)
end
let(:codec_options) { { "delimiter" => delimiter } }

it "should decode multiple lines separated by the delimiter" do
subject.on_event do |e, d|
Expand Down Expand Up @@ -170,4 +167,27 @@
expect(collector.last['field']).to eq('value2')
end
end

end

context "forcing legacy parsing" do
it_behaves_like :codec do
subject do
expect(LogStash::Codecs::JSONLines).to receive(:from_json_exposed?).and_return(false)
# register method is called in the constructor
LogStash::Codecs::JSONLines.new(codec_options)
end
end
end

context "default parser choice" do
# here we cannot force the use of the Event#from_json since if this test is run in the
# legacy context (no Java Event) it will fail but if in the new context, it will be picked up.
it_behaves_like :codec do
subject do
# register method is called in the constructor
LogStash::Codecs::JSONLines.new(codec_options)
end
end
end
end

0 comments on commit 5a737f5

Please sign in to comment.