test/minion_test.rb in parallel_minion-1.2.1 vs test/minion_test.rb in parallel_minion-1.3.0
- old
+ new
@@ -4,16 +4,39 @@
# Run this test standalone to verify it has no Rails dependencies
class MinionTest < Minitest::Test
include SemanticLogger::Loggable
describe ParallelMinion::Minion do
+ class InMemoryAppender < SemanticLogger::Subscriber
+ attr_reader :messages
+ def initialize
+ @messages = []
+ self.name = 'Minion'
+ end
+
+ def log(log)
+ messages << log.dup
+ end
+ end
+
+ let :log_messages do
+ appender.messages
+ end
+
+ let :appender do
+ InMemoryAppender.new
+ end
+
+ before do
+ ParallelMinion::Minion.logger = appender
+ end
+
[false, true].each do |enabled|
describe enabled ? 'enabled' : 'disabled' do
before do
ParallelMinion::Minion.enabled = enabled
- $log_structs.clear
end
it 'without parameters' do
minion = ParallelMinion::Minion.new { 196 }
assert_equal 196, minion.result
@@ -31,11 +54,11 @@
end
assert_equal 198, minion.result
end
it 'raise exception' do
- minion = ParallelMinion::Minion.new(description: 'Test') { raise "An exception" }
+ minion = ParallelMinion::Minion.new(description: 'Test') { raise 'An exception' }
assert_raises RuntimeError do
minion.result
end
end
@@ -43,11 +66,11 @@
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
+ # 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
# it 'not have access to local variables' do
# name = 'Jack'
# minion = ParallelMinion::Minion.new(description: 'Test') { puts name }
# assert_raises NameError do
@@ -71,11 +94,11 @@
it 'copy across logging tags' do
minion = nil
SemanticLogger.tagged('TAG') do
assert_equal 'TAG', SemanticLogger.tags.last
minion = ParallelMinion::Minion.new(description: 'Tag Test') do
- logger.info "Tag Test"
+ logger.info 'Tag Test'
logger.tags.last
end
end
assert_equal 'TAG', minion.result
end
@@ -83,11 +106,11 @@
it 'copy across named tags' do
minion = nil
SemanticLogger.named_tagged(tag: 'TAG') do
assert_equal({tag: 'TAG'}, SemanticLogger.named_tags)
minion = ParallelMinion::Minion.new(description: 'Named Tags Test') do
- logger.info "Named Tags Test"
+ logger.info 'Named Tags Test'
SemanticLogger.named_tags
end
end
assert_equal({tag: 'TAG'}, minion.result)
end
@@ -97,19 +120,18 @@
SemanticLogger.tagged('TAG') do
SemanticLogger.named_tagged(tag: 'TAG') do
assert_equal({tag: 'TAG'}, SemanticLogger.named_tags)
assert_equal 'TAG', SemanticLogger.tags.last
minion = ParallelMinion::Minion.new(description: 'Tags Test') do
- logger.info "Tags Test"
+ logger.info 'Tags Test'
[SemanticLogger.named_tags, SemanticLogger.tags.last]
end
assert_equal({tag: 'TAG'}, minion.result.first)
assert_equal 'TAG', minion.result.last
end
end
-
end
it 'include metric' do
metric_name = 'model/method'
hash = {value: 23}
@@ -121,27 +143,45 @@
456
end
assert_equal 456, minion.result
assert_equal 123, hash[:value]
assert_equal 321, value
- SemanticLogger.flush
- assert log = $log_structs.first, -> { $log_structs.ai }
+
+ assert log_messages.shift, -> { log_messages.ai }
+ assert completed_log = log_messages.shift, -> { log_messages.ai }
+ # Completed log message
+ assert_equal metric_name, completed_log.metric, -> { completed_log.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 }
+ assert waited_log = log_messages.shift, -> { log_messages.ai }
+ assert_equal "#{metric_name}/wait", waited_log.metric, -> { waited_log.ai }
end
end
+ it ':on_exception_level' do
+ minion = ParallelMinion::Minion.new(
+ description: 'Test',
+ on_exception_level: :error
+ ) do |_h|
+ raise 'Oh No'
+ end
+ # Wait for thread to complete
+ assert_raises RuntimeError do
+ minion.result
+ end
+
+ assert log_messages.shift, -> { log_messages.ai }
+ assert completed_log = log_messages.shift, -> { log_messages.ai }
+
+ assert_equal :error, completed_log.level
+ assert_equal 'Completed Test -- Exception: RuntimeError: Oh No', completed_log.message
+ refute completed_log.backtrace.empty?
+ end
+
it 'handle multiple minions concurrently' do
# Start 10 minions
- minions = 10.times.collect do |i|
+ minions = Array.new(10) do |i|
# Each Minion returns its index in the collection
ParallelMinion::Minion.new(i, description: "Minion:#{i}") { |counter| counter }
end
assert_equal 10, minions.count
# Fetch the result from each Minion
@@ -191,17 +231,15 @@
end
assert_equal enabled, minion.result
end
it 'keep the original arguments' do
- minion = ParallelMinion::Minion.new(1, 'data', 14.1, description: 'Test') do |num, str, float|
+ 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
end
end
-
end
-
end
end