spec/hotseat/queue_spec.rb in hotseat-0.1.2 vs spec/hotseat/queue_spec.rb in hotseat-0.2.0

- old
+ new

@@ -26,77 +26,92 @@ describe "#initialize" do it "should create a Hotseat design doc in the database if one does not exist" do reset_test_db! q = Hotseat::Queue.new(DB) - q.db.get(Hotseat.design_doc_id).should_not be_nil + q.db.get(q.design_doc_id).should_not be_nil end end describe "#patch" do + before(:each) do + reset_test_queue! + end + it "should add a queue object on a document" do doc = sample_doc - Queue.patch doc - doc.should have_key(Hotseat.config[:object_name]) + @q.patch doc + doc.should have_key(@q.config[:object_name]) end it "should return the patched document with original data intact" do - doc = Queue.patch sample_doc + doc = @q.patch sample_doc sample_doc.each do |k,v| doc[k].should == v end end end describe "#unpatch" do it "should remove the queue object from a document" do - doc = Queue.unpatch( Queue.patch( sample_doc ) ) - doc.should_not have_key(Hotseat.config[:object_name]) + reset_test_queue! + doc = @q.unpatch( @q.patch( sample_doc ) ) + doc.should_not have_key(@q.config[:object_name]) end end describe "#add_lock" do it "should add a lock object on a patched document" do - doc = Queue.add_lock( Queue.patch( sample_doc ) ) - patch = doc[Hotseat.config[:object_name]] + reset_test_queue! + doc = @q.add_lock( @q.patch( sample_doc ) ) + patch = doc[@q.config[:object_name]] patch.should have_key('lock') end end describe "#remove_lock" do - before(:each) { @doc = Queue.remove_lock( Queue.add_lock( Queue.patch( sample_doc ) ) ) } + before(:each) do + reset_test_queue! + @doc = @q.remove_lock( @q.add_lock( @q.patch( sample_doc ) ) ) + end it "should remove a lock object added by #add_lock" do - patch = @doc[Hotseat.config[:object_name]] + patch = @doc[@q.config[:object_name]] patch.should_not have_key('lock') end it "should leave the queue patch intact" do - @doc.should have_key(Hotseat.config[:object_name]) + @doc.should have_key(@q.config[:object_name]) end end describe "#locked?" do + before(:each) do + reset_test_queue! + end it "should be true for a locked document" do - doc = Queue.add_lock( Queue.patch( sample_doc ) ) - Queue.locked?(doc).should be_true + doc = @q.add_lock( @q.patch( sample_doc ) ) + @q.locked?(doc).should be_true end it "should be false for a patched, but not locked document" do - doc = Queue.patch( sample_doc ) - Queue.locked?(doc).should be_false + doc = @q.patch( sample_doc ) + @q.locked?(doc).should be_false end it "should be false for an unlocked document" do - doc = Queue.remove_lock( Queue.add_lock( Queue.patch( sample_doc ) ) ) - Queue.locked?(doc).should be_false + doc = @q.remove_lock( @q.add_lock( @q.patch( sample_doc ) ) ) + @q.locked?(doc).should be_false end end describe "#mark_done" do - before(:each) { @doc = Queue.mark_done( Queue.patch( sample_doc ) ) } + before(:each) do + reset_test_queue! + @doc = @q.mark_done( @q.patch( sample_doc ) ) + end it "should leave the queue patch intact" do - @doc.should have_key(Hotseat.config[:object_name]) + @doc.should have_key(@q.config[:object_name]) end it "should add a 'done' object on a patched document" do - patch = @doc[Hotseat.config[:object_name]] + patch = @doc[@q.config[:object_name]] patch.should have_key('done') end end describe "#add" do @@ -105,11 +120,11 @@ @doc_id = DB.save_doc(sample_doc)['id'] end it "should add a document to the queue, given a doc id" do @q.add @doc_id - DB.get(@doc_id).should have_key(Hotseat.config[:object_name]) + DB.get(@doc_id).should have_key(@q.config[:object_name]) end it "should save changes made in the block" do @q.add(@doc_id) do |doc| doc['field'] = 'changed value' @@ -125,11 +140,11 @@ it "should add multiple documents in bulk, given multiple doc ids" do reset_test_queue! doc_ids = create_some_docs @q.add_bulk doc_ids doc_ids.each do |doc_id| - DB.get(doc_id).should have_key(Hotseat.config[:object_name]) + DB.get(doc_id).should have_key(@q.config[:object_name]) end end end describe "#num_pending" do @@ -151,19 +166,19 @@ docs.should_not be_nil docs.should have(2).items docs.each do |doc| db_doc = DB.get(doc['_id']) db_doc.should be_kind_of CouchRest::Document - db_doc.should have_key(Hotseat.config[:object_name]) + db_doc.should have_key(@q.config[:object_name]) end end it "should lock a pending document" do doc_id = @q.lease.first['_id'] doc = DB.get(doc_id) - doc.should have_key(Hotseat.config[:object_name]) - doc[Hotseat.config[:object_name]].should have_key('lock') + doc.should have_key(@q.config[:object_name]) + doc[@q.config[:object_name]].should have_key('lock') end it "should lock and return up to the specified number of documents" do ids = @q.lease 4 ids.should have(3).items @@ -192,11 +207,11 @@ enqueue( create_some_docs(3) ) end it "should not be pending" do locked_id = @q.lease.first['_id'] - pending_ids = @q.db.view(Hotseat.pending_view_name)['rows'].map{|row| row['id']} + pending_ids = @q.db.view(@q.pending_view_name)['rows'].map{|row| row['id']} pending_ids.should_not include(locked_id) end it "should be counted by #num_locked" do @q.lease 2 @@ -214,19 +229,19 @@ docs = @q.get 2 docs.should_not be_nil docs.should have(2).items docs.each do |doc| db_doc = DB.get(doc['_id']) - db_doc.should have_key(Hotseat.config[:object_name]) + db_doc.should have_key(@q.config[:object_name]) end end it "should not lock the documents it returns" do doc_id = @q.get.first['_id'] doc = DB.get(doc_id) - doc.should have_key(Hotseat.config[:object_name]) - doc[Hotseat.config[:object_name]].should_not have_key('lock') + doc.should have_key(@q.config[:object_name]) + doc[@q.config[:object_name]].should_not have_key('lock') end it "should return up to the specified number of documents" do docs = @q.get 4 docs.should have(3).items @@ -242,12 +257,12 @@ end it "should unlock a leased document" do @q.remove @doc_id doc = DB.get(@doc_id) - doc.should have_key(Hotseat.config[:object_name]) - doc[Hotseat.config[:object_name]].should_not have_key('lock') + doc.should have_key(@q.config[:object_name]) + doc[@q.config[:object_name]].should_not have_key('lock') end it "should remove a document from the queue" do @q.remove @doc_id pending_docs = @q.get 3 # ensure we get all remaining pending docs @@ -272,18 +287,18 @@ end it "should leave queue history in the document (mark as done) by default" do @q.remove @doc_id doc = DB.get(@doc_id) - doc.should have_key(Hotseat.config[:object_name]) - doc[Hotseat.config[:object_name]].should have_key('done') + doc.should have_key(@q.config[:object_name]) + doc[@q.config[:object_name]].should have_key('done') end it "should delete queue history from the document when forget=true" do @q.remove @doc_id, :forget => true doc = DB.get(@doc_id) - doc.should_not have_key(Hotseat.config[:object_name]) + doc.should_not have_key(@q.config[:object_name]) end it "should save any changes made in the block" do @q.remove(@doc_id) do |doc| doc['field'] = 'changed value' @@ -305,12 +320,12 @@ it "should unlock leased documents" do @q.remove_bulk @doc_ids docs = DB.get_bulk(@doc_ids)['rows'].map{|row| row['doc']} docs.each do |doc| - doc.should have_key(Hotseat.config[:object_name]) - doc[Hotseat.config[:object_name]].should_not have_key('lock') + doc.should have_key(@q.config[:object_name]) + doc[@q.config[:object_name]].should_not have_key('lock') end end it "should remove multiple documents from the queue" do @q.remove_bulk @doc_ids @@ -338,20 +353,20 @@ it "should leave queue history in the document (mark as done) by default" do @q.remove_bulk @doc_ids docs = DB.get_bulk(@doc_ids)['rows'].map{|row| row['doc']} docs.each do |doc| - doc.should have_key(Hotseat.config[:object_name]) - doc[Hotseat.config[:object_name]].should have_key('done') + doc.should have_key(@q.config[:object_name]) + doc[@q.config[:object_name]].should have_key('done') end end it "should delete queue history from the document when forget=true" do @q.remove_bulk @doc_ids, :forget => true docs = DB.get_bulk(@doc_ids)['rows'].map{|row| row['doc']} docs.each do |doc| - doc.should_not have_key(Hotseat.config[:object_name]) + doc.should_not have_key(@q.config[:object_name]) end end end describe "#num_done" do @@ -381,22 +396,22 @@ reset_test_queue! enqueue( create_some_docs(1) ) doc_id = @q.get.first['_id'] @q.forget doc_id doc = DB.get(doc_id) - doc.should_not have_key(Hotseat.config[:object_name]) + doc.should_not have_key(@q.config[:object_name]) end end describe "#forget_bulk" do it "should delete queue history from multiple document" do reset_test_queue! enqueue( create_some_docs(3) ) doc_ids = @q.get(3).map{|doc| doc['_id'] } @q.forget_bulk doc_ids @q.db.bulk_load(doc_ids)['rows'].map{|row| row['doc']}.each do |doc| - doc.should_not have_key(Hotseat.config[:object_name]) + doc.should_not have_key(@q.config[:object_name]) end end end describe "#purge" do @@ -410,8 +425,35 @@ it "should remove (and forget) all documents from the queue" do @q.purge @q.num_all.should == 0 end end - end + context "when two queues are defined on the same DB" do + before do + reset_test_db! + @doc_ids = create_some_docs(10) + @q1 = Hotseat.make_queue(DB, :design_doc_name => 'hotseat1_queue', :object_name => 'hotseat1') + @q2 = Hotseat.make_queue(DB, :design_doc_name => 'hotseat2_queue', :object_name => 'hotseat2') + end + + it "#add should add a doc to the specified queue only" do + @q1.add @doc_ids[0] + @q2.add @doc_ids[1] + DB.get(@doc_ids[0]).should have_key(@q1.config[:object_name]) + DB.get(@doc_ids[0]).should_not have_key(@q2.config[:object_name]) + DB.get(@doc_ids[1]).should have_key(@q2.config[:object_name]) + DB.get(@doc_ids[1]).should_not have_key(@q1.config[:object_name]) + end + + it "#lease should lease docs from the specified queue" do + @q1.add_bulk @doc_ids[0, 5] + @q2.add_bulk @doc_ids[5, 5] + doc1 = @q1.lease.first + @doc_ids[0, 5].should include(doc1['_id']) + @q2.lease(3).each do |doc| + @doc_ids[5, 5].should include(doc['_id']) + end + end + end + end end