diff options
Diffstat (limited to 'jstests/sharding/now_variable_replset.js')
-rw-r--r-- | jstests/sharding/now_variable_replset.js | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/jstests/sharding/now_variable_replset.js b/jstests/sharding/now_variable_replset.js index 9cd185b748f..ad5104a0695 100644 --- a/jstests/sharding/now_variable_replset.js +++ b/jstests/sharding/now_variable_replset.js @@ -12,16 +12,28 @@ var db = replTest.getPrimary().getDB("test"); const coll = db[jsTest.name()]; + const otherColl = db[coll.getName() + "_other"]; + otherColl.drop(); coll.drop(); db["viewWithNow"].drop(); db["viewWithClusterTime"].drop(); + // Insert simple documents into the main test collection. Aggregation and view pipelines will + // augment these docs with time-based fields. const numdocs = 1000; - const bulk = coll.initializeUnorderedBulkOp(); + let bulk = coll.initializeUnorderedBulkOp(); for (let i = 0; i < numdocs; ++i) { bulk.insert({_id: i}); } - assert.writeOK(bulk.execute()); + assert.commandWorked(bulk.execute()); + + // Insert into another collection with pre-made fields for testing the find() command. + bulk = otherColl.initializeUnorderedBulkOp(); + const timeFieldValue = new Date(); + for (let i = 0; i < numdocs; ++i) { + bulk.insert({_id: i, timeField: timeFieldValue, clusterTimeField: new Timestamp(0, 1)}); + } + assert.commandWorked(bulk.execute()); assert.commandWorked( db.createView("viewWithNow", coll.getName(), [{$addFields: {timeField: "$$NOW"}}])); @@ -31,8 +43,12 @@ "viewWithClusterTime", coll.getName(), [{$addFields: {timeField: "$$CLUSTER_TIME"}}])); const viewWithClusterTime = db["viewWithClusterTime"]; + function toResultsArray(queryRes) { + return Array.isArray(queryRes) ? queryRes : queryRes.toArray(); + } + function runTests(query) { - const results = query().toArray(); + const results = toResultsArray(query()); assert.eq(results.length, numdocs); // Make sure the values are the same for all documents @@ -43,18 +59,32 @@ // Sleep for a while and then rerun. sleep(3000); - const resultsLater = query().toArray(); + const resultsLater = toResultsArray(query()); assert.eq(resultsLater.length, numdocs); // Later results should be later in time. assert.lte(results[0].timeField, resultsLater[0].timeField); } - function baseCollectionNow() { + function baseCollectionNowFind() { + return otherColl.find({$expr: {$lte: ["$timeField", "$$NOW"]}}); + } + + function baseCollectionClusterTimeFind() { + // The test validator examines 'timeField', so we copy clusterTimeField into timeField here. + const results = + otherColl.find({$expr: {$lt: ["$clusterTimeField", "$$CLUSTER_TIME"]}}).toArray(); + results.forEach((val, idx) => { + results[idx].timeField = results[idx].clusterTimeField; + }); + return results; + } + + function baseCollectionNowAgg() { return coll.aggregate([{$addFields: {timeField: "$$NOW"}}]); } - function baseCollectionClusterTime() { + function baseCollectionClusterTimeAgg() { return coll.aggregate([{$addFields: {timeField: "$$CLUSTER_TIME"}}]); } @@ -75,14 +105,29 @@ } // $$NOW - runTests(baseCollectionNow); + runTests(baseCollectionNowFind); + runTests(baseCollectionNowAgg); runTests(fromViewWithNow); runTests(withExprNow); + // Test that $$NOW can be used in explain for both find and aggregate. + assert.commandWorked(coll.explain().find({$expr: {$lte: ["$timeField", "$$NOW"]}}).finish()); + assert.commandWorked( + viewWithNow.explain().find({$expr: {$eq: ["$timeField", "$$NOW"]}}).finish()); + assert.commandWorked(coll.explain().aggregate([{$addFields: {timeField: "$$NOW"}}])); + // $$CLUSTER_TIME - runTests(baseCollectionClusterTime); + runTests(baseCollectionClusterTimeFind); + runTests(baseCollectionClusterTimeAgg); runTests(fromViewWithClusterTime); runTests(withExprClusterTime); + // Test that $$CLUSTER_TIME can be used in explain for both find and aggregate. + assert.commandWorked( + coll.explain().find({$expr: {$lte: ["$timeField", "$$CLUSTER_TIME"]}}).finish()); + assert.commandWorked( + viewWithNow.explain().find({$expr: {$eq: ["$timeField", "$$CLUSTER_TIME"]}}).finish()); + assert.commandWorked(coll.explain().aggregate([{$addFields: {timeField: "$$CLUSTER_TIME"}}])); + replTest.stopSet(); }()); |