summaryrefslogtreecommitdiff
path: root/jstests/replsets/command_response_operation_time.js
blob: a9ae4d6ef705615222e4e1af4eed2e67c973172c (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
56
57
58
59
60
61
/**
 * 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";

load("jstests/replsets/rslib.js");  // For startSetIfSupportsReadMajority.

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

var name = "command_response_operation_time";

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

if (!startSetIfSupportsReadMajority(replTest)) {
    jsTestLog("Skipping test since storage engine doesn't support majority read concern.");
    replTest.stopSet();
    return;
}
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();
})();