|
| 1 | +'use strict'; |
| 2 | +const MongoClient = require('../../..').MongoClient, |
| 3 | + Timestamp = require('bson').Timestamp, |
| 4 | + expect = require('chai').expect, |
| 5 | + mock = require('../../mock'); |
| 6 | + |
| 7 | +const test = {}; |
| 8 | +describe('Sessions', function() { |
| 9 | + describe('Collection', function() { |
| 10 | + afterEach(() => mock.cleanup()); |
| 11 | + beforeEach(() => { |
| 12 | + return mock.createServer().then(server => { |
| 13 | + test.server = server; |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should include `afterClusterTime` in read command with causal consistency', { |
| 18 | + metadata: { requires: { topology: 'single' } }, |
| 19 | + |
| 20 | + test: function() { |
| 21 | + let findCommand; |
| 22 | + let insertOperationTime = Timestamp.fromNumber(Date.now()); |
| 23 | + test.server.setMessageHandler(request => { |
| 24 | + const doc = request.document; |
| 25 | + if (doc.ismaster) { |
| 26 | + request.reply(mock.DEFAULT_ISMASTER_36); |
| 27 | + } else if (doc.insert) { |
| 28 | + request.reply({ ok: 1, operationTime: insertOperationTime }); |
| 29 | + } else if (doc.find) { |
| 30 | + findCommand = doc; |
| 31 | + request.reply({ ok: 1 }); |
| 32 | + } else if (doc.endSessions) { |
| 33 | + request.reply({ ok: 1 }); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + return MongoClient.connect(`mongodb://${test.server.uri()}/test`).then(client => { |
| 38 | + const session = client.startSession({ causalConsistency: true }); |
| 39 | + const coll = client.db('foo').collection('bar'); |
| 40 | + |
| 41 | + return coll |
| 42 | + .insert({ a: 42 }, { session: session }) |
| 43 | + .then(() => |
| 44 | + coll.findOne({}, null, { session: session, readConcern: { level: 'majoroy' } }) |
| 45 | + ) |
| 46 | + .then(() => { |
| 47 | + expect(findCommand.readConcern).to.have.keys(['level', 'afterClusterTime']); |
| 48 | + expect(findCommand.readConcern.afterClusterTime).to.eql(insertOperationTime); |
| 49 | + return client.close(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments