test/minion_test.rb in parallel_minion-1.1.0 vs test/minion_test.rb in parallel_minion-1.2.0
- old
+ new
@@ -1,159 +1,176 @@
-require File.join(File.dirname(__FILE__), 'test_helper')
+require_relative './test_helper'
# Test ParallelMinion standalone without Rails
# Run this test standalone to verify it has no Rails dependencies
class MinionTest < Minitest::Test
include SemanticLogger::Loggable
- context ParallelMinion::Minion do
+ describe ParallelMinion::Minion do
[false, true].each do |enabled|
- context ".new with enabled: #{enabled.inspect}" do
- setup do
+ describe enabled ? 'enabled' : 'disabled' do
+ before do
ParallelMinion::Minion.enabled = enabled
- $log_struct = nil
+ $log_structs = []
end
- should 'without parameters' do
+ it 'without parameters' do
minion = ParallelMinion::Minion.new { 196 }
assert_equal 196, minion.result
end
- should 'with a description' do
+ it 'with a description' do
minion = ParallelMinion::Minion.new(description: 'Test') { 197 }
assert_equal 197, minion.result
end
- should 'with an argument' do
- p1 = { name: 198 }
+ it 'with an argument' do
+ p1 = {name: 198}
minion = ParallelMinion::Minion.new(p1, description: 'Test') do |v|
v[:name]
end
assert_equal 198, minion.result
end
- should 'raise exception' do
+ it 'raise exception' do
minion = ParallelMinion::Minion.new(description: 'Test') { raise "An exception" }
assert_raises RuntimeError do
minion.result
end
end
+ it 'has correct logger name' do
+ minion = ParallelMinion::Minion.new { 196 }
+ name = enabled ? 'Minion' : 'Inline'
+ assert_equal name, minion.logger.name
+ end
+
# TODO Blocks still have access to their original scope if variables cannot be
# resolved first by the parameters, then by the values in Minion itself
- # should 'not have access to local variables' do
+ # it 'not have access to local variables' do
# name = 'Jack'
# minion = ParallelMinion::Minion.new(description: 'Test') { puts name }
# assert_raises NameError do
# minion.result
# end
# end
- should 'run minion' do
- hash = { value: 23 }
- value = 47
+ it 'run minion' do
+ hash = {value: 23}
+ value = 47
minion = ParallelMinion::Minion.new(hash, description: 'Test') do |h|
- value = 321
+ value = 321
h[:value] = 123
456
end
assert_equal 456, minion.result
assert_equal 123, hash[:value]
assert_equal 321, value
end
- should 'copy across logging tags' do
+ it 'copy across logging tags' do
minion = nil
- logger.tagged('TAG') do
- assert_equal 'TAG', logger.tags.last
+ SemanticLogger.tagged('TAG') do
+ assert_equal 'TAG', SemanticLogger.tags.last
minion = ParallelMinion::Minion.new(description: 'Tag Test') do
logger.tags.last
end
end
assert_equal 'TAG', minion.result
end
- should 'include metric' do
- metric_name = '/Custom/metric'
- hash = { value: 23 }
- value = 47
- minion = ParallelMinion::Minion.new(hash, description: 'Test', metric: metric_name) do |h|
- value = 321
+ it 'include metric' do
+ metric_name = 'model/method'
+ hash = {value: 23}
+ value = 47
+ minion = ParallelMinion::Minion.new(hash, description: 'Test', metric: metric_name) do |h|
+ value = 321
h[:value] = 123
+ sleep 1
456
end
assert_equal 456, minion.result
assert_equal 123, hash[:value]
assert_equal 321, value
SemanticLogger.flush
- assert_equal metric_name, $log_struct.metric
+ assert log = $log_structs.first, -> { $log_structs.ai }
+ if enabled
+ # Completed log message
+ assert_equal metric_name, log.metric, -> { $log_structs.ai }
+ # Wait log message
+ assert log = $log_structs.last, -> { $log_structs.ai }
+ assert_equal "#{metric_name}/wait", log.metric, -> { $log_structs.ai }
+ else
+ # Timeout and wait has no effect when run inline
+ assert_equal metric_name, log.metric, -> { $log_structs.ai }
+ end
end
- should 'handle multiple minions concurrently' do
+ it 'handle multiple minions concurrently' do
# Start 10 minions
minions = 10.times.collect do |i|
# Each Minion returns its index in the collection
- ParallelMinion::Minion.new(i, description: "Minion:#{i}") {|counter| counter }
+ ParallelMinion::Minion.new(i, description: "Minion:#{i}") { |counter| counter }
end
assert_equal 10, minions.count
# Fetch the result from each Minion
minions.each_with_index do |minion, index|
assert_equal index, minion.result
end
end
- should 'timeout' do
- minion = ParallelMinion::Minion.new(description: 'Test', timeout: 100) { sleep 1 }
+ it 'timeout' do
if enabled
- assert_equal nil, minion.result
+ minion = ParallelMinion::Minion.new(description: 'Test', timeout: 100) { sleep 1 }
+ assert_nil minion.result
end
end
- should 'timeout and terminate thread with Exception' do
- minion = ParallelMinion::Minion.new(description: 'Test', timeout: 100, on_timeout: Timeout::Error) { sleep 1 }
+ it 'timeout and terminate thread with Exception' do
if enabled
- assert_equal nil, minion.result
+ minion = ParallelMinion::Minion.new(description: 'Test', timeout: 100, on_timeout: Timeout::Error) { sleep 1 }
+ assert_nil minion.result
# Give time for thread to terminate
sleep 0.1
assert_equal Timeout::Error, minion.exception.class
assert_equal false, minion.working?
- assert_equal true, minion.completed?
- assert_equal true, minion.failed?
- assert_equal 0, minion.time_left
+ assert_equal true, minion.completed?
+ assert_equal true, minion.failed?
+ assert_equal 0, minion.time_left
end
end
- should 'make description instance variable available' do
+ it 'make description instance variable available' do
minion = ParallelMinion::Minion.new(description: 'Test') do
description
end
assert_equal 'Test', minion.result
end
- should 'make timeout instance variable available' do
- minion = ParallelMinion::Minion.new(description: 'Test', timeout: 1000 ) do
+ it 'make timeout instance variable available' do
+ minion = ParallelMinion::Minion.new(description: 'Test', timeout: 1000) do
timeout
end
assert_equal 1000, minion.result
end
- should 'make enabled? method available' do
+ it 'make enabled? method available' do
minion = ParallelMinion::Minion.new(description: 'Test') do
enabled?
end
assert_equal enabled, minion.result
end
- should 'keep the original arguments' do
- minion = ParallelMinion::Minion.new(1, 'data', 14.1, description: 'Test') do | num, str, float |
+ it 'keep the original arguments' do
+ minion = ParallelMinion::Minion.new(1, 'data', 14.1, description: 'Test') do |num, str, float|
num + float
end
assert_equal 15.1, minion.result
- assert_equal [ 1, 'data', 14.1 ], minion.arguments
+ assert_equal [1, 'data', 14.1], minion.arguments
end
end
end
end
-end
\ No newline at end of file
+end