Skip to content

Commit

Permalink
Merge pull request #24 from keithwiersema/allow-client-to-catch-conne…
Browse files Browse the repository at this point in the history
…ction-reset

allow client to rethrow connection reset exception
  • Loading branch information
sydneycodes authored Sep 19, 2017
2 parents d1a8d24 + c1e05a6 commit d95fcb4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/backbeat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/backbeat/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/backbeat/workers/async_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions spec/unit/workers/async_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
)
Expand Down

0 comments on commit d95fcb4

Please sign in to comment.