require 'test_helper' class TestKthxbye < Test::Unit::TestCase context "See Kthxbye Configuration" do should "configure an app with given params" do k = Kthxbye::Config.setup(:redis_server => "localhost", :redis_port => 8080, :verbose => true) assert_equal 'localhost', Kthxbye::Config.options[:redis_server] assert_equal 8080, Kthxbye::Config.options[:redis_port] assert_equal true, Kthxbye::Config.options[:verbose] end should "configure an app with default params" do Kthxbye::Config.setup assert_equal '127.0.0.1', Kthxbye::Config.options[:redis_server] assert_equal 9876, Kthxbye::Config.options[:redis_port] assert_equal false, Kthxbye::Config.options[:verbose] end end context "See Kthxbye" do setup do Kthxbye::Config.setup Kthxbye.redis.flushall end should "register unregister and delete queues" do assert_equal 0, Kthxbye.queues.size assert Kthxbye.register_queue "dogs" assert Kthxbye.register_queue "cats" assert_equal 2, Kthxbye.queues.size assert_equal ["cats", "dogs"], Kthxbye.queues assert Kthxbye.unregister_queue("dogs") assert_equal ["cats"], Kthxbye.queues assert Kthxbye.delete_queue("cats") assert_equal [], Kthxbye.queues end should "store a couple of jobs and return good ids" do assert id = Kthxbye.enqueue("test", SimpleJob, {:hello => "world"}, "test params") assert id2 = Kthxbye.enqueue("test", GoodJob, "I am a sentence to print") assert_equal 1, id assert_equal 2, id2 end should "enqueue a job and show a queue size of one" do assert_equal 0, Kthxbye.size("test") Kthxbye.enqueue("test", SimpleJob, {:hello => "world"}, "test params") assert_equal 1, Kthxbye.size("test") end should "peek at the datastore for a specific job" do id = Kthxbye.enqueue("test", SimpleJob, {:hello => "world"}, "test params") assert_equal [{'hello' => "world"}, "test params"], Kthxbye.data_peek("test", id)['payload'] end should "peek at all the data stores" do Kthxbye.enqueue("test", SimpleJob, "first", "job") Kthxbye.enqueue("test", SimpleJob, "second", "job") assert_equal ({'1' => {'klass' => 'SimpleJob', 'payload' => ["first", "job"]}, '2' => {'klass' => 'SimpleJob', 'payload' => ["second", "job"]}}), Kthxbye.data_peek("test") end should "peek at the resultstore for a specific job" do id = Kthxbye.enqueue("test", GoodJob, "Jim") worker = Kthxbye::Worker.new("test", 0) worker.run assert_equal "Good job, Jim", Kthxbye.result_peek("test", id) end should "peek at all the result stores" do Kthxbye.enqueue("test", GoodJob, "Jim") Kthxbye.enqueue("test", GoodJob, "Dwight") worker = Kthxbye::Worker.new("test", 0) worker.run assert_equal ({'1' => "Good job, Jim", '2' => "Good job, Dwight"}), Kthxbye.result_peek("test") end should "show its queues correctly" do Kthxbye.enqueue("these-jobs", SimpleJob, {:hello => "world"}, "test params") Kthxbye.enqueue("those-jobs", SimpleJob, {:hello => "world"}, "test params") assert_equal ["these-jobs", "those-jobs"], Kthxbye.queues Kthxbye.delete_queue("these-jobs") assert_equal ["those-jobs"], Kthxbye.queues end should "grab a job off the queue" do Kthxbye.enqueue("these-jobs", SimpleJob, {:hello => "world"}, "test params") assert_not_nil job = Kthxbye.salvage("these-jobs") assert_equal Kthxbye::Job, job.class assert_equal 1, job.id assert_equal "these-jobs", job.queue assert_equal "SimpleJob", job.data['klass'] end should "return a job" do id = Kthxbye.enqueue( "test", SimpleJob, 1, 2 ) assert job = Kthxbye::Job.find( id, "test" ) assert_equal Kthxbye::Job, job.class assert_equal [1, 2], job.data['payload'] end should "perform a job" do id = Kthxbye.enqueue( "test", GoodJob, "Lukas" ) job = Kthxbye::Job.find(id, "test") assert result = job.perform assert "Good job, Lukas!", result end should "retry a job if attempts setting is > 1" do Kthxbye::Config.options[:attempts] = 2 id = Kthxbye.enqueue( "test", BadJob ) job = Kthxbye::Job.find(id, "test") job.perform assert_equal 2, job.instance_variable_get(:@failed_attempts) end should "delete a job" do id = Kthxbye.enqueue("test", SimpleJob, 1, 2 ) assert_equal :inactive, Kthxbye::Job.destroy(id, "test") assert_equal nil, Kthxbye::Job.find(id, "test") end should "return all keys" do Kthxbye.enqueue("test", SimpleJob, 1, 2 ) assert_equal ["data-store:test", "jobs:inactive", "queue:test", "queues", "uniq_id"], Kthxbye.keys.sort end should "register and unregister workers" do assert_equal [], Kthxbye.workers worker = Kthxbye::Worker.new("bill") worker.register_worker assert_equal [worker], Kthxbye.workers worker2 = Kthxbye::Worker.new("holiday") worker2.register_worker assert_equal [worker, worker2], Kthxbye.workers.sort worker2.unregister_worker assert_equal [worker], Kthxbye.workers worker.unregister_worker assert_equal [], Kthxbye.workers end should "show all active workers" do worker = Kthxbye::Worker.new( "test" ) id = Kthxbye.enqueue( "test", SimpleJob, 1, 2 ) worker.register_worker worker.working( Kthxbye::Job.find( id, "test" ) ) assert_equal [[worker, {'job_id' => id, 'started' => Time.now.to_s }]], Kthxbye.working end should "return the correct status of a job" do worker1 = Kthxbye::Worker.new( "good", 0 ) worker2 = Kthxbye::Worker.new( "bad", 0 ) good_job = Kthxbye.enqueue( "good", GoodJob, "Dave" ) bad_job = Kthxbye.enqueue( "bad", BadJob ) job = Kthxbye::Job.find(good_job, "good") assert_equal :inactive, job.status job.dequeue assert_equal :inactive, job.status job.rerun worker1.working(job) assert_equal :active, job.status worker1.run assert_equal :succeeded, job.status job = Kthxbye::Job.find(bad_job, "bad") worker2.run assert_equal :failed, job.status end should "display nice inspect" do i = Kthxbye.inspect assert_equal Kthxbye::Version, i[:version] assert_equal 0, i[:keys] assert_equal 0, i[:workers] assert_equal 0, i[:queues] assert_equal 0, i[:working] assert_equal 0, i[:pending] end end end