require 'test_helper' class TestFailure < Test::Unit::TestCase context "See Kthxbye Failures" do setup do Kthxbye.redis.flushall end should "create a failure" do id = Kthxbye.enqueue("test", SimpleJob, 1, 2) Kthxbye::Failure.create(Kthxbye::Job.find(id, "test"), Exception.new("Test!")) assert_not_nil f = Kthxbye::Failure.all.first assert_equal "Exception", f['type'] assert_equal "Test!", f['error'] assert_equal id, f['job'] end should "insert and clear an exception" do id = Kthxbye.enqueue("test", SimpleJob, 1, 2) Kthxbye::Failure.create(Kthxbye::Job.find(id, "test"), Exception.new("Test!")) assert_equal 1, Kthxbye::Failure.all.size assert Kthxbye::Failure.clear_exception(id) assert_equal 0, Kthxbye::Failure.all.size end should "retry a job that failed" do id = Kthxbye.enqueue("test", BadJob) worker = Kthxbye::Worker.new("test", 0) worker.run assert_equal 1, Kthxbye::Failure.all.size assert_equal 1, Kthxbye::Failure.find(id)['attempts'] Kthxbye::Job.find(id, "test").rerun assert_equal 1, Kthxbye.size("test") worker.run do assert_equal Kthxbye::Job.find(id, "test"), worker.current_job end # note, we only store one error for a job failure. we will increment the failure count assert_equal 1, Kthxbye::Failure.all.size assert_equal 2, Kthxbye::Failure.find(id)['attempts'] end end end