diff --git a/app/models/execution_environment.rb b/app/models/execution_environment.rb index 3db35c35..9d420715 100644 --- a/app/models/execution_environment.rb +++ b/app/models/execution_environment.rb @@ -51,6 +51,9 @@ class ExecutionEnvironment < ApplicationRecord Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}") false + rescue Faraday::Error => e + Rails.logger.warn("Could not create execution environment because of Faraday error: #{e.inspect}") + false end def to_json(*_args) diff --git a/app/models/submission.rb b/app/models/submission.rb index 7810cbb8..4bd2653b 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -197,7 +197,7 @@ class Submission < ApplicationRecord runner = Runner.for(user, exercise) copy_files_to runner waiting_duration = Time.zone.now - request_time - yield(runner, waiting_duration) if block_given? + yield(runner, waiting_duration) end def command_for(template, file) diff --git a/spec/lib/runner/strategy/poseidon_spec.rb b/spec/lib/runner/strategy/poseidon_spec.rb index 15795701..d3acf5f9 100644 --- a/spec/lib/runner/strategy/poseidon_spec.rb +++ b/spec/lib/runner/strategy/poseidon_spec.rb @@ -100,6 +100,20 @@ describe Runner::Strategy::Poseidon do end end + # All requests handle a Faraday error the same way. + shared_examples 'Faraday error handling' do + context 'when Faraday throws an error' do + # The response status is not needed in this context but the describes block this context is embedded + # into expect this variable to be set in order to properly stub requests to the runner management. + let(:response_status) { -1 } + + it 'raises an error' do + %i[post patch delete].each {|message| allow(Faraday).to receive(message).and_raise(Faraday::TimeoutError) } + expect { action.call }.to raise_error(Runner::Error::Unknown, /Faraday/) + end + end + end + describe '::request_from_management' do let(:action) { -> { described_class.request_from_management(execution_environment) } } let!(:request_runner_stub) do @@ -158,6 +172,7 @@ describe Runner::Strategy::Poseidon do include_examples 'InternalServerError (500) error handling' include_examples 'unknown response status error handling' + include_examples 'Faraday error handling' end describe '#execute_command' do @@ -213,6 +228,7 @@ describe Runner::Strategy::Poseidon do include_examples 'NotFound (404) error handling' include_examples 'InternalServerError (500) error handling' include_examples 'unknown response status error handling' + include_examples 'Faraday error handling' end describe '#destroy_at_management' do @@ -236,6 +252,7 @@ describe Runner::Strategy::Poseidon do include_examples 'NotFound (404) error handling' include_examples 'InternalServerError (500) error handling' include_examples 'unknown response status error handling' + include_examples 'Faraday error handling' end describe '#copy_files' do @@ -268,6 +285,7 @@ describe Runner::Strategy::Poseidon do include_examples 'NotFound (404) error handling' include_examples 'InternalServerError (500) error handling' include_examples 'unknown response status error handling' + include_examples 'Faraday error handling' end describe '#attach_to_execution' do diff --git a/spec/models/execution_environment_spec.rb b/spec/models/execution_environment_spec.rb index 54e01111..6441b896 100644 --- a/spec/models/execution_environment_spec.rb +++ b/spec/models/execution_environment_spec.rb @@ -227,5 +227,10 @@ describe ExecutionEnvironment do [400, 500].each do |status| include_examples 'returns false when the api request failed', status end + + it 'returns false if Faraday raises an error' do + allow(Faraday).to receive(:put).and_raise(Faraday::TimeoutError) + expect(execution_environment.copy_to_poseidon).to be_falsey + end end end