summaryrefslogtreecommitdiff
path: root/jstests/replsets/command_response_operation_time.js
blob: cd5fbcd070ddc250b5022266c31b09aba6e1f35f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
 * Tests that reads and writes in a replica set return the correct operationTime for their
 * read/write concern level. Majority reads and writes return the last committed optime's timestamp
 * and local reads and writes return the last applied optime's timestamp.
 * @tags: [requires_majority_read_concern]
 */
(function() {
"use strict";

function assertCorrectOperationTime(operationTime, expectedTimestamp, opTimeType) {
    assert.eq(0,
              timestampCmp(operationTime, expectedTimestamp),
              "operationTime in command response, " + tojson(operationTime) +
                  ", does not equal the last " + opTimeType + " timestamp, " +
                  tojson(expectedTimestamp));
}

var name = "command_response_operation_time";

var replTest = new ReplSetTest(
    {name: name, nodes: 3, nodeOptions: {enableMajorityReadConcern: ""}, waitForKeys: true});
replTest.startSet();
replTest.initiate();

var res, statusRes;
var testDB = replTest.getPrimary().getDB(name);

jsTestLog("Executing majority write.");
res = assert.commandWorked(
    testDB.runCommand({insert: "foo", documents: [{x: 1}], writeConcern: {w: "majority"}}));
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
assertCorrectOperationTime(
    res.operationTime, statusRes.optimes.lastCommittedOpTime.ts, "committed");

jsTestLog("Executing local write.");
res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 2}]}));
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
assertCorrectOperationTime(res.operationTime, statusRes.optimes.appliedOpTime.ts, "applied");

replTest.awaitLastOpCommitted();

jsTestLog("Executing majority read.");
res = assert.commandWorked(
    testDB.runCommand({find: "foo", filter: {x: 1}, readConcern: {level: "majority"}}));
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
assertCorrectOperationTime(
    res.operationTime, statusRes.optimes.lastCommittedOpTime.ts, "committed");

jsTestLog("Executing local read.");
res = assert.commandWorked(testDB.runCommand({find: "foo", filter: {x: 1}}));
statusRes = assert.commandWorked(testDB.adminCommand({replSetGetStatus: 1}));
assertCorrectOperationTime(res.operationTime, statusRes.optimes.appliedOpTime.ts, "applied");

replTest.stopSet();
})();