From c1e05a6eace4e73ebd61a72e1a6fe72c956b879f Mon Sep 17 00:00:00 2001 From: Keith Wiersema Date: Tue, 12 Sep 2017 14:55:11 -0500 Subject: [PATCH] allow client to rethrow connection reset exception --- lib/backbeat/client.rb | 2 +- lib/backbeat/errors.rb | 8 ++++++++ lib/backbeat/workers/async_worker.rb | 2 +- spec/unit/client_spec.rb | 26 ++++++++++++++++++++++++-- spec/unit/workers/async_worker_spec.rb | 6 ++++-- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/backbeat/client.rb b/lib/backbeat/client.rb index 0da4344..ae05750 100644 --- a/lib/backbeat/client.rb +++ b/lib/backbeat/client.rb @@ -76,7 +76,7 @@ def post(url, params = {}) headers: { "Content-Type" => "application/json" } }) rescue => e - raise HttpError.new("Could not POST #{url}, error: #{e.class}, #{e.message}") + raise NetworkError.new("Could not POST #{url}, error: #{e.class}, #{e.message}") end end end diff --git a/lib/backbeat/errors.rb b/lib/backbeat/errors.rb index 698f408..2ab42c6 100644 --- a/lib/backbeat/errors.rb +++ b/lib/backbeat/errors.rb @@ -63,4 +63,12 @@ def initialize(message, response = nil) super(message) end end + + class NetworkError < StandardError + attr_reader :response + def initialize(message, response = nil) + @response = response + super(message) + end + end end diff --git a/lib/backbeat/workers/async_worker.rb b/lib/backbeat/workers/async_worker.rb index d8b4581..3123a2f 100644 --- a/lib/backbeat/workers/async_worker.rb +++ b/lib/backbeat/workers/async_worker.rb @@ -76,7 +76,7 @@ def business_perform(event_class, node_data, options) rescue DeserializeError => e error(status: :deserialize_node_error, node: node_data["node_id"], error: e, backtrace: e.backtrace) raise e - rescue Errno::ECONNRESET => e + rescue NetworkError => e Kernel.sleep(Config.options[:connection_error_wait]) if (node.reload.current_client_status != :complete) handle_processing_error(e, event_class, node, options) diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index 1c8620a..2337d21 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -142,6 +142,17 @@ expect { Backbeat::Client.perform(node) }.to raise_error(Backbeat::HttpError, "HTTP request for decision failed") end + + it "raises a network error if the connection is reset" do + node.legacy_type = :decision + + WebMock.stub_request(:post, "http://decisions.com/api/v1/workflows/make_decision").with( + body: { decision: Backbeat::NodePresenter.present(node) }.to_json, + headers: { 'Content-Type'=>'application/json'} + ).to_raise(Errno::ECONNRESET) + + expect { Backbeat::Client.perform(node) }.to raise_error(Backbeat::NetworkError) + end end context ".perform_activity" do @@ -169,12 +180,23 @@ expect { Backbeat::Client.perform(node) }.to raise_error(Backbeat::HttpError, "HTTP request for activity failed") end - it "raises an http error if HTTParty fails" do + it "raises a network error if the connection is reset" do + node.legacy_type = :activity + + stub = WebMock.stub_request(:post, "http://activity.com/api/v1/workflows/perform_activity").with( + :body => { activity: Backbeat::NodePresenter.present(node) }.to_json, + :headers => { 'Content-Type'=>'application/json' } + ).to_raise(Errno::ECONNRESET) + + expect { Backbeat::Client.perform(node) }.to raise_error(Backbeat::NetworkError) + end + + it "raises an network error if HTTParty fails" do node.legacy_type = :activity expect(HTTParty).to receive(:post).and_raise(HTTParty::Error.new("Request failure")) - expect { Backbeat::Client.perform(node) }.to raise_error(Backbeat::HttpError, "Could not POST http://activity.com/api/v1/workflows/perform_activity, error: HTTParty::Error, Request failure") + expect { Backbeat::Client.perform(node) }.to raise_error(Backbeat::NetworkError, "Could not POST http://activity.com/api/v1/workflows/perform_activity, error: HTTParty::Error, Request failure") end end end diff --git a/spec/unit/workers/async_worker_spec.rb b/spec/unit/workers/async_worker_spec.rb index f5b6db8..4670300 100644 --- a/spec/unit/workers/async_worker_spec.rb +++ b/spec/unit/workers/async_worker_spec.rb @@ -80,13 +80,15 @@ end it "waits for a client complete response if the server receives a connection reset" do - allow(Backbeat::Server).to receive(:fire_event) { raise Errno::ECONNRESET } + node = FactoryGirl.create(:node, user: user, workflow: workflow, current_server_status: :started, current_client_status: :ready) + + allow(HTTParty).to receive(:post) { raise Errno::ECONNRESET } allow(Backbeat::Config).to receive(:options).and_return({connection_error_wait: 0.01}) expect(Kernel).to receive(:sleep).with(0.01) Backbeat::Workers::AsyncWorker.new.perform( - Backbeat::Events::MarkChildrenReady.name, + Backbeat::Events::StartNode.name, { "node_class" => node.class.name, "node_id" => node.id }, { "retries" => 1 } )