test/minion_test.rb in parallel_minion-0.4.0 vs test/minion_test.rb in parallel_minion-0.4.1

- old
+ new

@@ -7,10 +7,15 @@ # Register an appender if one is not already registered SemanticLogger.default_level = :trace SemanticLogger.add_appender('test.log', &SemanticLogger::Appender::Base.colorized_formatter) if SemanticLogger.appenders.size == 0 +# Setup global callback for metric so that it can be tested below +SemanticLogger.on_metric do |log_struct| + $log_struct = log_struct.dup +end + # Test ParallelMinion standalone without Rails # Run this test standalone to verify it has no Rails dependencies class MinionTest < Test::Unit::TestCase include SemanticLogger::Loggable @@ -18,10 +23,11 @@ [false, true].each do |enabled| context ".new with enabled: #{enabled.inspect}" do setup do ParallelMinion::Minion.enabled = enabled + $log_struct = nil end should 'without parameters' do minion = ParallelMinion::Minion.new { 196 } assert_equal 196, minion.result @@ -45,19 +51,19 @@ assert_raise RuntimeError do minion.result end 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 -# name = 'Jack' -# minion = ParallelMinion::Minion.new(description: 'Test') { puts name } -# assert_raise NameError do -# minion.result -# end -# 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 + # name = 'Jack' + # minion = ParallelMinion::Minion.new(description: 'Test') { puts name } + # assert_raise NameError do + # minion.result + # end + # end should 'run minion' do hash = { value: 23 } value = 47 minion = ParallelMinion::Minion.new(hash, description: 'Test') do |h| @@ -77,9 +83,25 @@ 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 + h[:value] = 123 + 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 end should 'handle multiple minions concurrently' do # Start 10 minions minions = 10.times.collect do |i| \ No newline at end of file