Skip to content
This repository was archived by the owner on Sep 25, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions lib/coursemology/evaluator/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,36 @@ def run
Signal.trap('SIGTERM', method(:on_sig_term))

loop do
allocate_evaluations
break if @terminate
evaluations = allocate_evaluations

if evaluations && !evaluations.empty?
on_allocate(evaluations)
else
# :nocov:
# This sleep might not be triggered in the specs, because interruptions to the thread is
# nondeterministically run by the OS scheduler.
sleep(1.minute)
# :nocov:
end

# :nocov:
# This sleep might not be triggered in the specs, because interruptions to the thread is
# nondeterministically run by the OS scheduler.
sleep(1.minute)
# :nocov:
break if @terminate
end
end

private

# Requests evaluations from the server.
#
# @return [Array<Coursemology::Evaluator::Models::ProgrammingEvaluation>] The evaluations
# retrieved from the server.
def allocate_evaluations
evaluations =
ActiveSupport::Notifications.instrument('allocate.client.evaluator.coursemology') do
languages = Coursemology::Polyglot::Language.concrete_languages.map(&:display_name)
Coursemology::Evaluator::Models::ProgrammingEvaluation.allocate(language: languages)
end

on_allocate(evaluations)
ActiveSupport::Notifications.instrument('allocate.client.evaluator.coursemology') do
languages = Coursemology::Polyglot::Language.concrete_languages.map(&:display_name)
Coursemology::Evaluator::Models::ProgrammingEvaluation.allocate(language: languages)
end
rescue Flexirest::HTTPUnauthorisedClientException => e
ActiveSupport::Notifications.publish('allocate_fail.client.evaluator.coursemology', e: e)
nil
end

# The callback for handling an array of allocated evaluations.
Expand Down
26 changes: 15 additions & 11 deletions spec/coursemology/evaluator/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@
end

describe '#run' do
let(:dummy_evaluation) { build_stubbed(:programming_evaluation) }

it 'loops until @terminate is set' do
expect(subject).to receive(:allocate_evaluations).at_least(:once)
allow(subject).to receive(:sleep) { sleep(0.1.seconds) }

Thread.new { subject.instance_variable_set(:@terminate, true) }
subject.run
end

it 'calls #on_allocate with the evaluation' do
called = false
expect(subject).to receive(:allocate_evaluations) do
called ? [] : [dummy_evaluation]
end.at_least(:once)

expect(subject).to receive(:on_allocate).with([dummy_evaluation]).at_least(:once)
Thread.new { subject.send(:on_sig_term) }
subject.run
end
end

describe '#allocate_evaluations' do
context 'when an evaluation is provided' do
let(:dummy_evaluation) { build_stubbed(:programming_evaluation) }
before do
expect(Coursemology::Evaluator::Models::ProgrammingEvaluation).to \
receive(:allocate).and_return([dummy_evaluation])
end

it 'calls #on_evaluation with the evaluation' do
expect(subject).to receive(:on_evaluation).with(dummy_evaluation)
subject.send(:allocate_evaluations)
end

it 'instruments the allocation request' do
expect(Coursemology::Evaluator::Models::ProgrammingEvaluation).to \
receive(:allocate).and_return(nil)
expect { subject.send(:allocate_evaluations) }.to \
instrument_notification('allocate.client.evaluator.coursemology')
end
Expand Down