class TestWorker < Test::Unit::TestCase context "See Kthxbye::Worker" do setup do Kthxbye::Config.setup(:verbose => false, :redis_port => 9876, :attempts => 1) Kthxbye.redis.flushall @id = Kthxbye.enqueue( "test", SimpleJob, 1, 2 ) @worker = Kthxbye::Worker.new("test", 0) end should "register and show a worker" do @worker.run do assert_equal [@worker], Kthxbye.workers end end should "find a registered worker and its queues" do @worker.run do found = Kthxbye::Worker.find(@worker.id) assert_equal @worker, found end end should "startup a worker and register it as working" do @worker.run do assert_equal [[@worker.to_s, {'job_id' => @id, 'started' => Time.now.to_s}]], Kthxbye.working end end should "pull jobs from given queue and run them" do @worker.run assert_equal 0, Kthxbye.size("test") end should "store a job's results and retrieve them" do id = Kthxbye.enqueue("new", GoodJob, "Lukas") worker = Kthxbye::Worker.new("new", 0) worker.run assert_equal 1, Kthxbye.job_results("new").size assert_equal "Good job, Lukas", Kthxbye.job_results("new", id) end should "capture and report failed jobs" do id = Kthxbye.enqueue( "bad", BadJob ) id2 = Kthxbye.enqueue( "bad", BadJobWithSyntaxError ) assert_equal 0, Kthxbye::Failure.count worker = Kthxbye::Worker.new("bad", 0 ) worker.run assert_equal 2, Kthxbye::Failure.count assert_equal "Bad job!", Kthxbye::Failure.find(id)["error"] assert_equal "Extra Bad job!", Kthxbye::Failure.find(id2)["error"] assert_equal ["Bad job!", "Extra Bad job!"], Kthxbye::Failure.all.map{|x| x['error']} assert_equal 1, Kthxbye::Failure.count_type(RuntimeError) end should "report queues available to workers when added via csv (alphabetical)" do worker = Kthxbye::Worker.new("good,bad,ugly") assert_equal ["good", "bad", "ugly"].sort, worker.queues end should "report queues available to workers when using * (alphabetical)" do Kthxbye.register_queue("hello") Kthxbye.register_queue("world") worker = Kthxbye::Worker.new("*") assert_equal ["hello", "test", "world"].sort, worker.queues end should "report queues available to worker when passed an array (alphabetical)" do worker = Kthxbye::Worker.new(["one", "two", "three"]) assert_equal ["one", "two", "three"].sort, worker.queues end should "work jobs from multiple queues" do Kthxbye.enqueue("other", GoodJob, "Boieee") Kthxbye.enqueue("more", GoodJob, "Lukas") worker = Kthxbye::Worker.new("*", 0) worker.run assert_equal 0, Kthxbye.size("test") assert_equal 0, Kthxbye.size("other") assert_equal 0, Kthxbye.size("more") end should "return job active once working it" do job = Kthxbye::Job.find(@id, "test") @worker.working(job) assert @worker.working? assert_equal @worker.current_job, job assert job.active? end should "know when it is working" do @worker.run do assert @worker.working? assert Kthxbye::Job.find(@id, @worker.current_queue ).active? end end should "know which is the current queue" do worker = Kthxbye::Worker.new("*", 0) worker.run do assert_equal "test", worker.current_queue end end should "know which is the current job id" do worker = Kthxbye::Worker.new("*", 0) worker.run do assert_equal Kthxbye::Job.find(@id, worker.current_queue), worker.current_job end end should "find the current job" do @worker.run do assert_equal ({'job_id' => @id, 'started' => Time.now.to_s}), Kthxbye::Worker.working_on(@worker.id) end end should "report system stats" do Kthxbye.enqueue( "test", BadJob ) assert_equal 0, @worker.processed assert_equal 0, @worker.failed @worker.run assert_equal 2, Kthxbye::Stats["processed"] assert_equal 1, Kthxbye::Stats["failures"] end should "track failures" do Kthxbye.enqueue( "test", BadJob ) Kthxbye.enqueue( "test", BadJob ) 3.times do job = @worker.grab_job @worker.perform(job) end assert_equal 2, @worker.failed end should "requeue a job and report error if worker is killed mid-process" do @worker.working( Kthxbye::Job.find( @id, "test" ) ) @worker.unregister_worker assert_equal 1, Kthxbye::Failure.all.size assert_equal "Kthxbye::ActiveWorkerKilled", Kthxbye::Failure.find(@id)['type'] assert_not_nil job = Kthxbye::Job.find( @id, "test" ) assert_equal 1, job.failed_attempts end should "show a pretty inspect" do assert_equal "#", @worker.inspect end end end