require File.expand_path("../../test_helper", __FILE__) class BBQueue::ConsumerTest < MiniTest::Test class ForkingConsumer < BBQueue::Consumer attr_accessor :tempfile def initialize(tempfile, *args) self.tempfile = tempfile super *args end def fork? true end def before_fork tempfile.puts "before_fork" end def after_fork tempfile.puts "after_fork" end end class NonForkingConsumer < ForkingConsumer def fork? false end end class LoggingJob attr_accessor :tempfile_path def initialize(tempfile_path) self.tempfile_path = tempfile_path end def work open(tempfile_path, "w+") { |stream| stream.puts "work" } end end class NullJob def work # Nothing end end class RaisingJob def work raise "BLUBB" end end def test_non_forking_job consumer_tempfile = Tempfile.new("consumer") consumer = NonForkingConsumer.new(consumer_tempfile, "default") job_tempfile = Tempfile.new("job") Stalking::Consumer.instances.last.run_job "default", { "object" => BBQueue::Serializer.dump(LoggingJob.new(job_tempfile.path)) } refute_includes File.read(consumer_tempfile.path), "before_fork" refute_includes File.read(consumer_tempfile.path), "after_fork" assert_includes File.read(job_tempfile.path), "work" end def test_forking_job consumer_tempfile = Tempfile.new("consumer") consumer = ForkingConsumer.new(consumer_tempfile, "default") job_tempfile = Tempfile.new("job") Stalking::Consumer.instances.last.run_job "default", { "object" => BBQueue::Serializer.dump(LoggingJob.new(job_tempfile.path)) } assert_includes File.read(consumer_tempfile.path), "before_fork" assert_includes File.read(consumer_tempfile.path), "after_fork" assert_includes File.read(job_tempfile.path), "work" end def test_forking_job_with_raise logger = BBQueue::TestLogger.new consumer = ForkingConsumer.new(Tempfile.new("consumer"), "default", :logger => logger) assert_difference "logger.count /^error:/", 2 do Stalking::Consumer.instances.last.run_job "default", { "object" => BBQueue::Serializer.dump(RaisingJob.new) } end end def test_error logger = BBQueue::TestLogger.new consumer = BBQueue::Consumer.new("default", :logger => logger) assert_difference "logger.count /^error:/", 2 do Stalking::Consumer.instances.last.run_error "Error", "default", { "object" => BBQueue::Serializer.dump(NullJob.new) } end end def test_before logger = BBQueue::TestLogger.new consumer = BBQueue::Consumer.new("default", :logger => logger) assert_difference "logger.count /^info:/" do Stalking::Consumer.instances.last.run_before "default", { "object" => BBQueue::Serializer.dump(NullJob.new) } end end def test_after logger = BBQueue::TestLogger.new consumer = BBQueue::Consumer.new("default", :logger => logger) assert_difference "logger.count /^info:/" do Stalking::Consumer.instances.last.run_after "default", { "object" => BBQueue::Serializer.dump(NullJob.new) } end end end