diff --git a/lib/buildkite/test_collector/session.rb b/lib/buildkite/test_collector/session.rb index 16404f0..8c37623 100644 --- a/lib/buildkite/test_collector/session.rb +++ b/lib/buildkite/test_collector/session.rb @@ -7,6 +7,10 @@ class Session MAX_RECONNECTION_ATTEMPTS = ENV.fetch("BUILDKITE_ANALYTICS_RECONNECTION_ATTEMPTS") { 3 }.to_i WAIT_BETWEEN_RECONNECTIONS = ENV.fetch("BUILDKITE_ANALYTICS_RECONNECTION_WAIT") { 5 }.to_i + # We keep a private reference so that mocking libraries won't break JSON + JSON_PARSE = JSON.method(:parse) + private_constant :JSON_PARSE + class RejectedSubscription < StandardError; end class InitialConnectionFailure < StandardError; end @@ -120,7 +124,7 @@ def close(examples_count) end def handle(_connection, data) - data = JSON.parse(data) + data = JSON_PARSE.call(data) case data["type"] when "ping" # In absence of other message, the server sends us a ping every 3 seconds diff --git a/spec/test_collector/session_spec.rb b/spec/test_collector/session_spec.rb index 7ee1dbf..dfbbe07 100644 --- a/spec/test_collector/session_spec.rb +++ b/spec/test_collector/session_spec.rb @@ -53,6 +53,12 @@ session.handle(socket_double, {"type"=> "message", "identifier"=> "fake_channel", "message" => {"confirm"=> ["./spec/analytics/session_spec.rb[1:1]"]}}.to_json) expect(session.unconfirmed_idents_count).to be 0 end + + it "is immune to JSON.parse being mocked" do + allow(JSON).to receive(:parse).and_raise(JSON::ParserError) + + session.handle(socket_double, {"type"=> "message", "identifier"=> "fake_channel", "message" => {"confirm"=> ["./spec/analytics/session_spec.rb[1:1]"]}}.to_json) + end end describe "#close" do