-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
change_stream_spec.test.js
289 lines (247 loc) · 8.17 KB
/
change_stream_spec.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'use strict';
const path = require('path');
const chai = require('chai');
const { loadSpecTests } = require('../spec');
const { runUnifiedSuite } = require('../tools/unified-spec-runner/runner');
const camelCase = require('lodash.camelcase');
const { setupDatabase } = require('./shared');
const { delay } = require('./shared');
const expect = chai.expect;
describe('Change Streams Spec - Unified', function () {
runUnifiedSuite(loadSpecTests(path.join('change-streams', 'unified')));
});
describe('Change Stream Spec - v1', function () {
let globalClient;
let ctx;
let events;
const TESTS_TO_SKIP = new Set([]);
before(function () {
const configuration = this.configuration;
return setupDatabase(configuration).then(() => {
globalClient = configuration.newClient();
return globalClient.connect();
});
});
after(function () {
const gc = globalClient;
globalClient = undefined;
return new Promise(r => gc.close(() => r()));
});
loadSpecTests(path.join('change-streams', 'legacy')).forEach(suite => {
const ALL_DBS = [suite.database_name, suite.database2_name];
describe(suite.name, () => {
beforeEach(function () {
const gc = globalClient;
const sDB = suite.database_name;
const sColl = suite.collection_name;
const configuration = this.configuration;
return Promise.all(
ALL_DBS.map(db => gc.db(db).dropDatabase({ writeConcern: { w: 'majority' } }))
)
.then(() => gc.db(sDB).createCollection(sColl))
.then(() => {
if (suite.database2_name && suite.collection2_name) {
return gc.db(suite.database2_name).createCollection(suite.collection2_name);
}
})
.then(() =>
configuration
.newClient({}, { monitorCommands: true, heartbeatFrequencyMS: 100 })
.connect()
)
.then(client => {
ctx = { gc, client };
events = [];
const _events = events;
ctx.database = ctx.client.db(sDB);
ctx.collection = ctx.database.collection(sColl);
ctx.client.on('commandStarted', e => {
console.log(e);
if (e.commandName !== 'ismaster') _events.push(e);
});
});
});
afterEach(function () {
const client = ctx.client;
ctx = undefined;
events = undefined;
client.removeAllListeners('commandStarted');
return client && client.close(true);
});
suite.tests.forEach(test => {
const shouldSkip = test.skip || TESTS_TO_SKIP.has(test.description);
const itFn = shouldSkip ? it.skip : test.only ? it.only : it;
const metadata = generateMetadata(test);
const testFn = generateTestFn(test);
itFn(test.description, { metadata, test: testFn });
});
});
});
// Fn Generator methods
function generateMetadata(test) {
const topology = test.topology;
const requires = {};
const versionLimits = [];
if (test.minServerVersion) {
versionLimits.push(`>=${test.minServerVersion}`);
}
if (test.maxServerVersion) {
versionLimits.push(`<=${test.maxServerVersion}`);
}
if (versionLimits.length) {
requires.mongodb = versionLimits.join(' ');
}
if (topology) {
requires.topology = topology;
}
return { requires };
}
function generateTestFn(test) {
const configureFailPoint = makeFailPointCommand(test);
const testFnRunOperations = makeTestFnRunOperations(test);
const testSuccess = makeTestSuccess(test);
const testFailure = makeTestFailure(test);
const testAPM = makeTestAPM(test);
return function testFn() {
return configureFailPoint(ctx)
.then(() => testFnRunOperations(ctx))
.then(testSuccess, testFailure)
.then(() => testAPM(ctx, events));
};
}
function makeFailPointCommand(test) {
if (!test.failPoint) {
return () => Promise.resolve();
}
return function (ctx) {
return ctx.gc.db('admin').command(test.failPoint);
};
}
function makeTestSuccess(test) {
const result = test.result;
return function testSuccess(value) {
if (result.error) {
throw new Error(`Expected test to return error ${result.error}`);
}
if (result.success) {
expect(value).to.have.a.lengthOf(result.success.length);
expect(value).to.matchMongoSpec(result.success);
}
};
}
function makeTestFailure(test) {
const result = test.result;
return function testFailure(err) {
if (!result.error) {
throw err;
}
expect(err).to.matchMongoSpec(result.error);
};
}
function makeTestAPM(test) {
const expectedEvents = test.expectations || [];
return function testAPM(ctx, events) {
console.log('events', events);
expectedEvents
.map(e => e.command_started_event)
.map(normalizeAPMEvent)
.forEach((expected, idx) => {
if (!events[idx]) {
throw new Error(
`Expected there to be an APM event at index ${idx}, but there was none`
);
}
// killCursors events should be skipped
// (see https://github.com/mongodb/specifications/blob/master/source/change-streams/tests/README.rst#spec-test-runner)
if (events[idx].commandName === 'killCursors') {
return;
}
expect(events[idx]).to.matchMongoSpec(expected);
});
};
}
function allSettled(promises) {
let err;
return Promise.all(promises.map(p => p.catch(x => (err = err || x)))).then(args => {
if (err) {
throw err;
}
return args;
});
}
function makeTestFnRunOperations(test) {
const target = test.target;
const operations = test.operations;
const success = test.result.success || [];
return function testFnRunOperations(ctx) {
const changeStreamPipeline = test.changeStreamPipeline;
const changeStreamOptions = test.changeStreamOptions;
ctx.changeStream = ctx[target].watch(changeStreamPipeline, changeStreamOptions);
const changeStreamPromise = readAndCloseChangeStream(ctx.changeStream, success.length);
const operationsPromise = runOperations(ctx.gc, operations);
return allSettled([changeStreamPromise, operationsPromise]).then(args => args[0]);
};
}
function readAndCloseChangeStream(changeStream, numChanges) {
const close = makeChangeStreamCloseFn(changeStream);
let changeStreamPromise = changeStream.next().then(r => [r]);
for (let i = 1; i < numChanges; i += 1) {
changeStreamPromise = changeStreamPromise.then(results => {
return changeStream.next().then(result => {
results.push(result);
return results;
});
});
}
return changeStreamPromise.then(
result => close(null, result),
err => close(err)
);
}
function runOperations(client, operations) {
return operations
.map(op => makeOperation(client, op))
.reduce((p, op) => p.then(op), delay(200));
}
function makeChangeStreamCloseFn(changeStream) {
return function close(error, value) {
return new Promise((resolve, reject) => {
changeStream.close(err => {
if (error || err) {
return reject(error || err);
}
return resolve(value);
});
});
};
}
function normalizeAPMEvent(raw) {
return Object.keys(raw).reduce((agg, key) => {
agg[camelCase(key)] = raw[key];
return agg;
}, {});
}
function makeOperation(client, op) {
const target = client.db(op.database).collection(op.collection);
const command = op.name;
const args = [];
if (op.arguments) {
if (op.arguments.document) {
args.push(op.arguments.document);
}
if (op.arguments.filter) {
args.push(op.arguments.filter);
}
if (op.arguments.update) {
args.push(op.arguments.update);
}
if (op.arguments.replacement) {
args.push(op.arguments.replacement);
}
if (op.arguments.to) {
args.push(op.arguments.to);
}
}
return () => target[command].apply(target, args);
}
});