|
1 | 1 | /* Anything javascript specific relating to timeouts */
|
2 | 2 | import { expect } from 'chai';
|
| 3 | +import * as semver from 'semver'; |
| 4 | +import * as sinon from 'sinon'; |
3 | 5 |
|
4 | 6 | import {
|
| 7 | + BSON, |
5 | 8 | type ClientSession,
|
6 | 9 | type Collection,
|
| 10 | + Connection, |
7 | 11 | type Db,
|
8 | 12 | type FindCursor,
|
9 | 13 | LEGACY_HELLO_COMMAND,
|
10 | 14 | type MongoClient,
|
11 |
| - MongoOperationTimeoutError |
| 15 | + MongoOperationTimeoutError, |
| 16 | + MongoServerError |
12 | 17 | } from '../../mongodb';
|
| 18 | +import { type FailPoint } from '../../tools/utils'; |
13 | 19 |
|
14 |
| -describe('CSOT driver tests', () => { |
| 20 | +describe('CSOT driver tests', { requires: { mongodb: '>=4.4' } }, () => { |
15 | 21 | describe('timeoutMS inheritance', () => {
|
16 | 22 | let client: MongoClient;
|
17 | 23 | let db: Db;
|
@@ -161,4 +167,157 @@ describe('CSOT driver tests', () => {
|
161 | 167 | });
|
162 | 168 | });
|
163 | 169 | });
|
| 170 | + |
| 171 | + describe('server-side maxTimeMS errors are transformed', () => { |
| 172 | + let client: MongoClient; |
| 173 | + let commandsSucceeded; |
| 174 | + let commandsFailed; |
| 175 | + |
| 176 | + beforeEach(async function () { |
| 177 | + client = this.configuration.newClient({ timeoutMS: 500_000, monitorCommands: true }); |
| 178 | + commandsSucceeded = []; |
| 179 | + commandsFailed = []; |
| 180 | + client.on('commandSucceeded', event => { |
| 181 | + if (event.commandName === 'configureFailPoint') return; |
| 182 | + commandsSucceeded.push(event); |
| 183 | + }); |
| 184 | + client.on('commandFailed', event => commandsFailed.push(event)); |
| 185 | + }); |
| 186 | + |
| 187 | + afterEach(async function () { |
| 188 | + await client |
| 189 | + .db() |
| 190 | + .collection('a') |
| 191 | + .drop() |
| 192 | + .catch(() => null); |
| 193 | + await client.close(); |
| 194 | + commandsSucceeded = undefined; |
| 195 | + commandsFailed = undefined; |
| 196 | + }); |
| 197 | + |
| 198 | + describe('when a maxTimeExpired error is returned at the top-level', () => { |
| 199 | + // {ok: 0, code: 50, codeName: "MaxTimeMSExpired", errmsg: "operation time limit exceeded"} |
| 200 | + const failpoint: FailPoint = { |
| 201 | + configureFailPoint: 'failCommand', |
| 202 | + mode: { times: 1 }, |
| 203 | + data: { |
| 204 | + failCommands: ['ping'], |
| 205 | + errorCode: 50 |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + beforeEach(async function () { |
| 210 | + if (semver.satisfies(this.configuration.version, '>=4.4')) |
| 211 | + await client.db('admin').command(failpoint); |
| 212 | + else { |
| 213 | + this.skipReason = 'Requires server version later than 4.4'; |
| 214 | + this.skip(); |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + afterEach(async function () { |
| 219 | + if (semver.satisfies(this.configuration.version, '>=4.4')) |
| 220 | + await client.db('admin').command({ ...failpoint, mode: 'off' }); |
| 221 | + }); |
| 222 | + |
| 223 | + it('throws a MongoOperationTimeoutError error and emits command failed', async () => { |
| 224 | + const error = await client |
| 225 | + .db() |
| 226 | + .command({ ping: 1 }) |
| 227 | + .catch(error => error); |
| 228 | + expect(error).to.be.instanceOf(MongoOperationTimeoutError); |
| 229 | + expect(error.cause).to.be.instanceOf(MongoServerError); |
| 230 | + expect(error.cause).to.have.property('code', 50); |
| 231 | + |
| 232 | + expect(commandsFailed).to.have.lengthOf(1); |
| 233 | + expect(commandsFailed).to.have.nested.property('[0].failure.cause.code', 50); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + describe('when a maxTimeExpired error is returned inside a writeErrors array', () => { |
| 238 | + // The server should always return one maxTimeExpiredError at the front of the writeErrors array |
| 239 | + // But for the sake of defensive programming we will find any maxTime error in the array. |
| 240 | + |
| 241 | + beforeEach(async () => { |
| 242 | + const writeErrorsReply = BSON.serialize({ |
| 243 | + ok: 1, |
| 244 | + writeErrors: [ |
| 245 | + { code: 2, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' }, |
| 246 | + { code: 3, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' }, |
| 247 | + { code: 4, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' }, |
| 248 | + { code: 50, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' } |
| 249 | + ] |
| 250 | + }); |
| 251 | + const commandSpy = sinon.spy(Connection.prototype, 'command'); |
| 252 | + const readManyStub = sinon |
| 253 | + // @ts-expect-error: readMany is private |
| 254 | + .stub(Connection.prototype, 'readMany') |
| 255 | + .callsFake(async function* (...args) { |
| 256 | + const realIterator = readManyStub.wrappedMethod.call(this, ...args); |
| 257 | + const cmd = commandSpy.lastCall.args.at(1); |
| 258 | + if ('giveMeWriteErrors' in cmd) { |
| 259 | + await realIterator.next().catch(() => null); // dismiss response |
| 260 | + yield { parse: () => writeErrorsReply }; |
| 261 | + } else { |
| 262 | + yield (await realIterator.next()).value; |
| 263 | + } |
| 264 | + }); |
| 265 | + }); |
| 266 | + |
| 267 | + afterEach(() => sinon.restore()); |
| 268 | + |
| 269 | + it('throws a MongoOperationTimeoutError error and emits command succeeded', async () => { |
| 270 | + const error = await client |
| 271 | + .db('admin') |
| 272 | + .command({ giveMeWriteErrors: 1 }) |
| 273 | + .catch(error => error); |
| 274 | + expect(error).to.be.instanceOf(MongoOperationTimeoutError); |
| 275 | + expect(error.cause).to.be.instanceOf(MongoServerError); |
| 276 | + expect(error.cause).to.have.nested.property('writeErrors[3].code', 50); |
| 277 | + |
| 278 | + expect(commandsSucceeded).to.have.lengthOf(1); |
| 279 | + expect(commandsSucceeded).to.have.nested.property('[0].reply.writeErrors[3].code', 50); |
| 280 | + }); |
| 281 | + }); |
| 282 | + |
| 283 | + describe('when a maxTimeExpired error is returned inside a writeConcernError embedded document', () => { |
| 284 | + // {ok: 1, writeConcernError: {code: 50, codeName: "MaxTimeMSExpired"}} |
| 285 | + const failpoint: FailPoint = { |
| 286 | + configureFailPoint: 'failCommand', |
| 287 | + mode: { times: 1 }, |
| 288 | + data: { |
| 289 | + failCommands: ['insert'], |
| 290 | + writeConcernError: { code: 50, errmsg: 'times up buster', errorLabels: [] } |
| 291 | + } |
| 292 | + }; |
| 293 | + |
| 294 | + beforeEach(async function () { |
| 295 | + if (semver.satisfies(this.configuration.version, '>=4.4')) |
| 296 | + await client.db('admin').command(failpoint); |
| 297 | + else { |
| 298 | + this.skipReason = 'Requires server version later than 4.4'; |
| 299 | + this.skip(); |
| 300 | + } |
| 301 | + }); |
| 302 | + |
| 303 | + afterEach(async function () { |
| 304 | + if (semver.satisfies(this.configuration.version, '>=4.4')) |
| 305 | + await client.db('admin').command({ ...failpoint, mode: 'off' }); |
| 306 | + }); |
| 307 | + |
| 308 | + it('throws a MongoOperationTimeoutError error and emits command succeeded', async () => { |
| 309 | + const error = await client |
| 310 | + .db() |
| 311 | + .collection('a') |
| 312 | + .insertOne({}) |
| 313 | + .catch(error => error); |
| 314 | + expect(error).to.be.instanceOf(MongoOperationTimeoutError); |
| 315 | + expect(error.cause).to.be.instanceOf(MongoServerError); |
| 316 | + expect(error.cause).to.have.nested.property('writeConcernError.code', 50); |
| 317 | + |
| 318 | + expect(commandsSucceeded).to.have.lengthOf(1); |
| 319 | + expect(commandsSucceeded).to.have.nested.property('[0].reply.writeConcernError.code', 50); |
| 320 | + }); |
| 321 | + }); |
| 322 | + }); |
164 | 323 | });
|
0 commit comments