diff options
author | Jack Mulrow <jack.mulrow@mongodb.com> | 2017-04-10 11:55:13 -0400 |
---|---|---|
committer | Jack Mulrow <jack.mulrow@mongodb.com> | 2017-04-19 13:26:47 -0400 |
commit | 3483a1c42f9d5f3fd38e671684e9157821cf48df (patch) | |
tree | cdfb706389798ffb217f4955fc3dc40b8a2d926a /jstests/sharding/logical_time_api.js | |
parent | 41d5d369508592a2c1ebaaaac48fe2b3680e0615 (diff) | |
download | mongo-3483a1c42f9d5f3fd38e671684e9157821cf48df.tar.gz |
SERVER-28451 API integration test for logicalTime
Diffstat (limited to 'jstests/sharding/logical_time_api.js')
-rw-r--r-- | jstests/sharding/logical_time_api.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/jstests/sharding/logical_time_api.js b/jstests/sharding/logical_time_api.js new file mode 100644 index 00000000000..fc8a03ceb35 --- /dev/null +++ b/jstests/sharding/logical_time_api.js @@ -0,0 +1,98 @@ +/** + * Tests the logicalTime API for the following topologies: + * - mongos talking to a sharded replica set (sharded and unsharded collections) + * - mongod from a sharded replica set + * - mongod from a non-sharded replica set + * - standalone mongod + * + * Expects logicalTime to come in the commandReply from a mongos and the metadata from a mongod. + */ +(function() { + "use strict"; + + // Returns true if the given object contains a logicalTime BSON object in the following format: + // logicalTime: { + // clusterTime: <Timestamp> + // signature: { + // hash: <BinData> + // keyId: <NumberLong> + // } + // } + function containsValidLogicalTimeBson(obj) { + if (!obj) { + return false; + } + + var logicalTime = obj.logicalTime; + return logicalTime && isType(logicalTime, "BSON") && + isType(logicalTime.clusterTime, "Timestamp") && isType(logicalTime.signature, "BSON") && + isType(logicalTime.signature.hash, "BinData") && + isType(logicalTime.signature.keyId, "NumberLong"); + } + + function isType(val, typeString) { + assert.eq(Object.prototype.toString.call(val), + "[object " + typeString + "]", + "expected: " + val + ", to be of type: " + typeString); + return true; + } + + // A mongos that talks to a non-sharded collection on a sharded replica set returns a + // logicalTime BSON object that matches the expected format. + var st = new ShardingTest({name: "logical_time_api", shards: {rs0: {nodes: 1}}}); + + var testDB = st.s.getDB("test"); + var res = testDB.runCommandWithMetadata("insert", {insert: "foo", documents: [{x: 1}]}, {}); + assert.commandWorked(res.commandReply); + assert(containsValidLogicalTimeBson(res.commandReply), + "Expected commandReply from a mongos talking to a non-sharded collection on a sharded " + + "replica set to contain logicalTime, received: " + tojson(res.commandReply)); + + // A mongos that talks to a sharded collection on a sharded replica set returns a + // logicalTime BSON object that matches the expected format. + assert.commandWorked(st.s.adminCommand({enableSharding: "test"})); + assert.commandWorked(st.s.adminCommand({shardCollection: "test.bar", key: {x: 1}})); + + res = testDB.runCommandWithMetadata("insert", {insert: "bar", documents: [{x: 2}]}, {}); + assert.commandWorked(res.commandReply); + assert(containsValidLogicalTimeBson(res.commandReply), + "Expected commandReply from a mongos talking to a sharded collection on a sharded " + + "replica set to contain logicalTime, received: " + tojson(res.commandReply)); + + // A mongod in a sharded replica set returns a logicalTime bson that matches the expected + // format. + testDB = st.rs0.getPrimary().getDB("test"); + res = testDB.runCommandWithMetadata("insert", {insert: "foo", documents: [{x: 3}]}, {}); + assert.commandWorked(res.commandReply); + assert(containsValidLogicalTimeBson(res.metadata), + "Expected metadata in response from a mongod in a sharded replica set to contain " + + "logicalTime, received: " + tojson(res.metadata)); + + st.stop(); + + // A mongod from a non-sharded replica set does not return logicalTime. + var replTest = new ReplSetTest({name: "logical_time_api_non_sharded_replset", nodes: 1}); + replTest.startSet(); + replTest.initiate(); + + testDB = replTest.getPrimary().getDB("test"); + res = testDB.runCommandWithMetadata("insert", {insert: "foo", documents: [{x: 4}]}, {}); + assert.commandWorked(res.commandReply); + assert(!containsValidLogicalTimeBson(res.metadata), + "Expected metadata in response from a mongod in a non-sharded replica set to not " + + "contain logicalTime, received: " + tojson(res.metadata)); + + replTest.stopSet(); + + // A standalone mongod does not return logicalTime. + var standalone = MongoRunner.runMongod(); + + testDB = standalone.getDB("test"); + res = testDB.runCommandWithMetadata("insert", {insert: "foo", documents: [{x: 5}]}, {}); + assert.commandWorked(res.commandReply); + assert(!containsValidLogicalTimeBson(res.metadata), + "Expected metadata in response from a standalone mongod to not contain logicalTime, " + + "received: " + tojson(res.metadata)); + + MongoRunner.stopMongod(standalone); +})(); |