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
1 change: 1 addition & 0 deletions coursemology-evaluator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'vcr'

spec.add_dependency 'activesupport', '~> 4.2.0', '>= 4.2.2'
spec.add_dependency 'iso8601', '~> 0.9.1'
spec.add_dependency 'flexirest', '~> 1.2', '>= 1.2.6'
spec.add_dependency 'faraday_middleware'

Expand Down
1 change: 1 addition & 0 deletions lib/coursemology/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'coursemology/polyglot'
require 'coursemology/polyglot/extensions'
require 'coursemology/evaluator/version'
require 'iso8601'

module Coursemology::Evaluator
extend ActiveSupport::Autoload
Expand Down
13 changes: 11 additions & 2 deletions lib/coursemology/evaluator/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'optparse'

class Coursemology::Evaluator::CLI
Options = Struct.new(:host, :api_token, :api_user_email, :one_shot)
Options = Struct.new(:host, :api_token, :api_user_email, :one_shot, :poll_interval)

def self.start(argv)
new.start(argv)
Expand All @@ -16,7 +16,7 @@ def run(argv)
options = optparse!(argv)
Coursemology::Evaluator::Client.initialize(options.host, options.api_user_email,
options.api_token)
Coursemology::Evaluator::Client.new(options.one_shot).run
Coursemology::Evaluator::Client.new(options.one_shot, options.poll_interval).run
end

private
Expand All @@ -27,6 +27,11 @@ def run(argv)
# @return [Coursemology::Evaluator::CLI::Options]
def optparse!(argv) # rubocop:disable Metrics/MethodLength
options = Options.new

# default options for optional parameters
options.poll_interval = '10S'
options.one_shot = false

option_parser = OptionParser.new do |parser|
parser.banner = "Usage: #{parser.program_name} [options]"
parser.on('-hHOST', '--host=HOST', 'Coursemology host to connect to') do |host|
Expand All @@ -41,6 +46,10 @@ def optparse!(argv) # rubocop:disable Metrics/MethodLength
options.api_user_email = user
end

parser.on('-iINTERVAL', '--interval=INTERVAL') do |interval|
options.poll_interval = interval
end

parser.on('-o', '--one-shot') do
options.one_shot = true
end
Expand Down
7 changes: 5 additions & 2 deletions lib/coursemology/evaluator/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ def self.initialize(host, api_user_email, api_token)
end

# @param [Boolean] one_shot If the client should only fire one request.
def initialize(one_shot = false)
# @param [Float] poll_time Sleep time between checks for task allocation.
# @param [String] poll_interval Units for poll_time.
def initialize(one_shot = false, poll_interval = '10S')
@poll_interval = ::ISO8601::Duration.new("PT#{poll_interval}".upcase)
@terminate = one_shot
end

Expand All @@ -31,7 +34,7 @@ def client_loop
# :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)
sleep(@poll_interval.to_seconds)
# :nocov:
end

Expand Down
22 changes: 21 additions & 1 deletion spec/coursemology/evaluator/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
let!(:original_api_token) { Coursemology::Evaluator::Models::Base.api_token }
let(:api_token) { 'abcd' }
let(:api_user_email) { 'test@example.org' }
let(:poll_interval) { '10S' }
let(:argv) do
["--host=#{host}", "--api-token=#{api_token}", "--api-user-email=#{api_user_email}",
'--one-shot', "--interval=#{poll_interval}"]
end
let(:argv_missing) do
["--host=#{host}", "--api-token=#{api_token}", "--api-user-email=#{api_user_email}",
'--one-shot']
end

describe Coursemology::Evaluator::CLI::Options do
it { is_expected.to have_attributes(host: nil, api_token: nil, api_user_email: nil) }
it 'checks Options attributes' do
expect(subject).to have_attributes(host: nil, api_token: nil, api_user_email: nil,
poll_interval: nil)
end
end

with_mock_client do
Expand Down Expand Up @@ -70,5 +78,17 @@
it 'parses api-user-email' do
expect(subject.api_user_email).to eq(api_user_email)
end

it 'parses poll-interval' do
expect(subject.poll_interval).to eq(poll_interval)
end
end

describe '#optparse! defaults' do
subject { Coursemology::Evaluator::CLI.new.send(:optparse!, argv_missing) }

it 'sets default value for poll_interval' do
expect(subject.poll_interval).to eq('10S')
end
end
end
35 changes: 35 additions & 0 deletions spec/coursemology/evaluator/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@
describe '#run' do
let(:dummy_evaluation) { build_stubbed(:programming_evaluation) }

context 'with custom poll time' do
# Client with custom poll interval
let(:one_shot) { false }
let(:poll_interval) { '5m' }
subject(:custom_poll) do
Coursemology::Evaluator::Client.new(one_shot, poll_interval)
end

it 'sleeps with a specified poll time' do
# Simulate no evaluations
expect(custom_poll).to receive(:allocate_evaluations) { [] }

# Stub sleep to terminate after 1 mock sleep
allow(custom_poll).to receive(:sleep) do
custom_poll.instance_variable_set(:@terminate, true)
end

expect(custom_poll).to receive(:sleep).with(300)
custom_poll.run
end
end

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) }
Expand All @@ -38,6 +60,19 @@
Thread.new { subject.send(:on_sig_term) }
subject.run
end

it 'sleeps with the default poll time' do
# Simulate no evaluations
expect(subject).to receive(:allocate_evaluations) { [] }

# Stub sleep to terminate after 1 mock sleep
allow(subject).to receive(:sleep) do
subject.instance_variable_set(:@terminate, true)
end

expect(subject).to receive(:sleep).with(10)
subject.run
end
end

describe '#allocate_evaluations' do
Expand Down