From aaf2969861e882624132c5d6b6141acfafc15aa7 Mon Sep 17 00:00:00 2001 From: "Yunhe (John) Wang" Date: Tue, 6 Oct 2015 13:41:59 -0400 Subject: SERVER-20720 mongos now returns dead tailable cursor over an empty capped collection --- jstests/core/tailable_skip_limit.js | 141 ++++++++++++++++++++---------------- 1 file changed, 80 insertions(+), 61 deletions(-) (limited to 'jstests/core') diff --git a/jstests/core/tailable_skip_limit.js b/jstests/core/tailable_skip_limit.js index d0da0a15b28..1fae7263a8f 100644 --- a/jstests/core/tailable_skip_limit.js +++ b/jstests/core/tailable_skip_limit.js @@ -1,63 +1,82 @@ // Test that tailable cursors work correctly with skip and limit. +(function() { + "use strict"; -// Setup the capped collection. -var collname = "jstests_tailable_skip_limit" -var t = db[collname]; -t.drop(); -db.createCollection(collname, {capped: true, size: 1024}); - -t.save({_id: 1}); -t.save({_id: 2}); - -// Non-tailable with skip -var cursor = t.find().skip(1); -assert.eq(2, cursor.next()["_id"]); -assert(!cursor.hasNext()); -t.save({_id: 3}); -assert(!cursor.hasNext()); - -// Non-tailable with limit -var cursor = t.find().limit(100); -for (var i = 1; i <= 3; i++) { - assert.eq(i, cursor.next()["_id"]); -} -assert(!cursor.hasNext()); -t.save({_id: 4}); -assert(!cursor.hasNext()); - -// Non-tailable with negative limit -var cursor = t.find().limit(-100); -for (var i = 1; i <= 4; i++) { - assert.eq(i, cursor.next()["_id"]); -} -assert(!cursor.hasNext()); -t.save({_id: 5}); -assert(!cursor.hasNext()); - -// Tailable with skip -cursor = t.find().addOption(2).skip(4); -assert.eq(5, cursor.next()["_id"]); -assert(!cursor.hasNext()); -t.save({_id: 6}); -assert(cursor.hasNext()); -assert.eq(6, cursor.next()["_id"]); - -// Tailable with limit -var cursor = t.find().addOption(2).limit(100); -for (var i = 1; i <= 6; i++) { - assert.eq(i, cursor.next()["_id"]); -} -assert(!cursor.hasNext()); -t.save({_id: 7}); -assert(cursor.hasNext()); -assert.eq(7, cursor.next()["_id"]); - -// Tailable with negative limit -var cursor = t.find().addOption(2).limit(-100); -for (var i = 1; i <= 7; i++) { - assert.eq(i, cursor.next()["_id"]); -} -assert(!cursor.hasNext()); -t.save({_id: 8}); -assert(cursor.hasNext()); -assert.eq(8, cursor.next()["_id"]); + // Setup the capped collection. + var collname = "jstests_tailable_skip_limit" + var t = db[collname]; + t.drop(); + db.createCollection(collname, {capped: true, size: 1024}); + + t.save({_id: 1}); + t.save({_id: 2}); + + // Non-tailable with skip + var cursor = t.find().skip(1); + assert.eq(2, cursor.next()["_id"]); + assert(!cursor.hasNext()); + t.save({_id: 3}); + assert(!cursor.hasNext()); + + // Non-tailable with limit + var cursor = t.find().limit(100); + for (var i = 1; i <= 3; i++) { + assert.eq(i, cursor.next()["_id"]); + } + assert(!cursor.hasNext()); + t.save({_id: 4}); + assert(!cursor.hasNext()); + + // Non-tailable with negative limit + var cursor = t.find().limit(-100); + for (var i = 1; i <= 4; i++) { + assert.eq(i, cursor.next()["_id"]); + } + assert(!cursor.hasNext()); + t.save({_id: 5}); + assert(!cursor.hasNext()); + + // Tailable with skip + cursor = t.find().addOption(2).skip(4); + assert.eq(5, cursor.next()["_id"]); + assert(!cursor.hasNext()); + t.save({_id: 6}); + assert(cursor.hasNext()); + assert.eq(6, cursor.next()["_id"]); + + // Tailable with limit + var cursor = t.find().addOption(2).limit(100); + for (var i = 1; i <= 6; i++) { + assert.eq(i, cursor.next()["_id"]); + } + assert(!cursor.hasNext()); + t.save({_id: 7}); + assert(cursor.hasNext()); + assert.eq(7, cursor.next()["_id"]); + + // Tailable with negative limit + var cursor = t.find().addOption(2).limit(-100); + for (var i = 1; i <= 7; i++) { + assert.eq(i, cursor.next()["_id"]); + } + assert(!cursor.hasNext()); + t.save({_id: 8}); + assert(cursor.hasNext()); + assert.eq(8, cursor.next()["_id"]); + + // Tests that a tailable cursor over an empty capped collection produces a dead cursor, intended + // to be run on both mongod and mongos. For SERVER-20720. + t.drop(); + db.createCollection(t.getName(), {capped: true, size: 1024}); + + var cmdRes = db.runCommand({find: t.getName(), tailable: true}); + assert.commandWorked(cmdRes); + assert.eq(cmdRes.cursor.id, NumberLong(0)); + assert.eq(cmdRes.cursor.ns, t.getFullName()); + assert.eq(cmdRes.cursor.firstBatch.length, 0); + + // Test that the cursor works in the shell. + assert.eq(t.find().addOption(2).itcount(), 0); + assert.writeOK(t.insert({a: 1})); + assert.eq(t.find().addOption(2).itcount(), 1); +})(); -- cgit v1.2.1