-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
sessions.test.js
275 lines (248 loc) · 9.68 KB
/
sessions.test.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict';
const path = require('path');
const expect = require('chai').expect;
const { setupDatabase, withMonitoredClient } = require('./shared');
const { TestRunnerContext, generateTopologyTests } = require('./spec-runner');
const { loadSpecTests } = require('../spec');
const { runUnifiedTest } = require('./unified-spec-runner/runner');
const ignoredCommands = ['ismaster'];
const test = {
commands: { started: [], succeeded: [] },
setup: function (config) {
this.commands = { started: [], succeeded: [] };
this.client = config.newClient({ w: 1 }, { maxPoolSize: 1, monitorCommands: true });
this.client.on('commandStarted', event => {
if (ignoredCommands.indexOf(event.commandName) === -1) {
this.commands.started.push(event);
}
});
this.client.on('commandSucceeded', event => {
if (ignoredCommands.indexOf(event.commandName) === -1) {
this.commands.succeeded.push(event);
}
});
return this.client.connect();
}
};
describe('Sessions - functional', function () {
before(function () {
return setupDatabase(this.configuration);
});
describe('endSessions', function () {
beforeEach(function () {
return test.setup(this.configuration);
});
it('should send endSessions for multiple sessions', {
metadata: {
requires: { topology: ['single'], mongodb: '>=3.6.0' },
// Skipping session leak tests b/c these are explicit sessions
sessions: { skipLeakTests: true }
},
test: function (done) {
const client = test.client;
let sessions = [client.startSession(), client.startSession()].map(s => s.id);
client.close(err => {
expect(err).to.not.exist;
expect(test.commands.started).to.have.length(1);
expect(test.commands.started[0].commandName).to.equal('endSessions');
expect(test.commands.started[0].command.endSessions).to.include.deep.members(sessions);
expect(client.s.sessions.size).to.equal(0);
done();
});
}
});
});
describe('withSession', {
metadata: { requires: { mongodb: '>=3.6.0' } },
test: function () {
beforeEach(function () {
return test.setup(this.configuration);
});
[
{
description: 'should support operations that return promises',
operation: client => session => {
return client.db('test').collection('foo').find({}, { session }).toArray();
}
},
// {
// nodeVersion: '>=8.x',
// description: 'should support async operations',
// operation: client => session =>
// async function() {
// await client
// .db('test')
// .collection('foo')
// .find({}, { session })
// .toArray();
// }
// },
{
description: 'should support operations that return rejected promises',
operation: (/* client */) => (/* session */) => {
return Promise.reject(new Error('something awful'));
}
},
{
description: "should support operations that don't return promises",
operation: (/* client */) => (/* session */) => {
setTimeout(() => {});
}
},
{
description: 'should support operations that throw exceptions',
operation: (/* client */) => (/* session */) => {
throw new Error('something went wrong!');
}
}
].forEach(testCase => {
it(testCase.description, function () {
const client = test.client;
return client
.withSession(testCase.operation(client))
.then(
() => expect(client.topology.s.sessionPool.sessions).to.have.length(1),
() => expect(client.topology.s.sessionPool.sessions).to.have.length(1)
)
.then(() => client.close())
.then(() => {
// verify that the `endSessions` command was sent
const lastCommand = test.commands.started[test.commands.started.length - 1];
expect(lastCommand.commandName).to.equal('endSessions');
expect(client.topology).to.not.exist;
});
});
});
it('supports passing options to ClientSession', function () {
const client = test.client;
const promise = client.withSession({ causalConsistency: false }, session => {
expect(session.supports.causalConsistency).to.be.false;
return client.db('test').collection('foo').find({}, { session }).toArray();
});
return promise
.then(() => expect(client.topology.s.sessionPool.sessions).to.have.length(1))
.then(() => client.close())
.then(() => {
// verify that the `endSessions` command was sent
const lastCommand = test.commands.started[test.commands.started.length - 1];
expect(lastCommand.commandName).to.equal('endSessions');
expect(client.topology).to.not.exist;
});
});
}
});
describe('legacy spec tests', function () {
class SessionSpecTestContext extends TestRunnerContext {
assertSessionNotDirty(options) {
const session = options.session;
expect(session.serverSession.isDirty).to.be.false;
}
assertSessionDirty(options) {
const session = options.session;
expect(session.serverSession.isDirty).to.be.true;
}
assertSameLsidOnLastTwoCommands() {
expect(this.commandEvents).to.have.length.of.at.least(2);
const lastTwoCommands = this.commandEvents.slice(-2).map(c => c.command);
lastTwoCommands.forEach(command => expect(command).to.have.property('lsid'));
expect(lastTwoCommands[0].lsid).to.eql(lastTwoCommands[1].lsid);
}
assertDifferentLsidOnLastTwoCommands() {
expect(this.commandEvents).to.have.length.of.at.least(2);
const lastTwoCommands = this.commandEvents.slice(-2).map(c => c.command);
lastTwoCommands.forEach(command => expect(command).to.have.property('lsid'));
expect(lastTwoCommands[0].lsid).to.not.eql(lastTwoCommands[1].lsid);
}
}
const testContext = new SessionSpecTestContext();
const testSuites = loadSpecTests(path.join('sessions', 'legacy'));
after(() => testContext.teardown());
before(function () {
return testContext.setup(this.configuration);
});
function testFilter(spec) {
const SKIP_TESTS = [
// These two tests need to run against multiple mongoses
'Dirty explicit session is discarded',
'Dirty implicit session is discarded (write)'
];
return SKIP_TESTS.indexOf(spec.description) === -1;
}
generateTopologyTests(testSuites, testContext, testFilter);
});
describe('unified spec tests', function () {
for (const sessionTests of loadSpecTests(path.join('sessions', 'unified'))) {
expect(sessionTests).to.be.an('object');
context(String(sessionTests.description), function () {
// TODO: NODE-3393 fix test runner to apply session to all operations
const skipTestMap = {
'snapshot-sessions': [
'countDocuments operation with snapshot',
'Distinct operation with snapshot',
'Mixed operation with snapshot'
],
'snapshot-sessions-not-supported-client-error': [
'Client error on distinct with snapshot'
],
'snapshot-sessions-not-supported-server-error': [
'Server returns an error on distinct with snapshot'
],
'snapshot-sessions-unsupported-ops': [
'Server returns an error on listCollections with snapshot',
'Server returns an error on listDatabases with snapshot',
'Server returns an error on listIndexes with snapshot',
'Server returns an error on runCommand with snapshot'
]
};
const testsToSkip = skipTestMap[sessionTests.description] || [];
for (const test of sessionTests.tests) {
it(String(test.description), {
metadata: { sessions: { skipLeakTests: true } },
test: async function () {
await runUnifiedTest(this, sessionTests, test, testsToSkip);
}
});
}
});
}
});
context('unacknowledged writes', () => {
it('should not include session for unacknowledged writes', {
metadata: { requires: { topology: 'single', mongodb: '>=3.6.0' } },
test: withMonitoredClient('insert', { clientOptions: { writeConcern: { w: 0 } } }, function (
client,
events,
done
) {
client
.db('test')
.collection('foo')
.insertOne({ foo: 'bar' }, err => {
expect(err).to.not.exist;
const event = events[0];
expect(event).nested.property('command.writeConcern.w').to.equal(0);
expect(event).to.not.have.nested.property('command.lsid');
done();
});
})
});
it('should throw error with explicit session', {
metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6.0' } },
test: withMonitoredClient('insert', { clientOptions: { writeConcern: { w: 0 } } }, function (
client,
events,
done
) {
const session = client.startSession({ causalConsistency: true });
client
.db('test')
.collection('foo')
.insertOne({ foo: 'bar' }, { session }, err => {
expect(err).to.exist;
expect(err.message).to.equal('Cannot have explicit session with unacknowledged writes');
client.close(done);
});
})
});
});
});