|
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 | 20 | describe('CSOT driver tests', () => {
|
15 | 21 | describe('timeoutMS inheritance', () => {
|
@@ -161,4 +167,147 @@ describe('CSOT driver tests', () => {
|
161 | 167 | });
|
162 | 168 | });
|
163 | 169 | });
|
| 170 | + |
| 171 | + describe( |
| 172 | + 'server-side maxTimeMS errors are transformed', |
| 173 | + { requires: { mongodb: '>=4.4' } }, |
| 174 | + () => { |
| 175 | + let client: MongoClient; |
| 176 | + let commandsSucceeded; |
| 177 | + let commandsFailed; |
| 178 | + |
| 179 | + beforeEach(async function () { |
| 180 | + client = this.configuration.newClient({ timeoutMS: 500_000, monitorCommands: true }); |
| 181 | + commandsSucceeded = []; |
| 182 | + commandsFailed = []; |
| 183 | + client.on('commandSucceeded', event => { |
| 184 | + if (event.commandName === 'configureFailPoint') return; |
| 185 | + commandsSucceeded.push(event); |
| 186 | + }); |
| 187 | + client.on('commandFailed', event => commandsFailed.push(event)); |
| 188 | + }); |
| 189 | + |
| 190 | + afterEach(async function () { |
| 191 | + await client |
| 192 | + .db() |
| 193 | + .collection('a') |
| 194 | + .drop() |
| 195 | + .catch(() => null); |
| 196 | + await client.close(); |
| 197 | + commandsSucceeded = undefined; |
| 198 | + commandsFailed = undefined; |
| 199 | + }); |
| 200 | + |
| 201 | + describe('when a maxTimeExpired error is returned at the top-level', () => { |
| 202 | + // {ok: 0, code: 50, codeName: "MaxTimeMSExpired", errmsg: "operation time limit exceeded"} |
| 203 | + const failpoint: FailPoint = { |
| 204 | + configureFailPoint: 'failCommand', |
| 205 | + mode: { times: 1 }, |
| 206 | + data: { |
| 207 | + failCommands: ['ping'], |
| 208 | + errorCode: 50 |
| 209 | + } |
| 210 | + }; |
| 211 | + |
| 212 | + beforeEach(async () => { |
| 213 | + await client.db('admin').command(failpoint); |
| 214 | + }); |
| 215 | + |
| 216 | + afterEach(async () => { |
| 217 | + await client.db('admin').command({ ...failpoint, mode: 'off' }); |
| 218 | + }); |
| 219 | + |
| 220 | + it('throws a MongoOperationTimeoutError error and emits command failed', async () => { |
| 221 | + const error = await client |
| 222 | + .db() |
| 223 | + .command({ ping: 1 }) |
| 224 | + .catch(error => error); |
| 225 | + expect(error).to.be.instanceOf(MongoOperationTimeoutError); |
| 226 | + expect(error.cause).to.be.instanceOf(MongoServerError); |
| 227 | + expect(error.cause).to.have.property('code', 50); |
| 228 | + |
| 229 | + expect(commandsFailed).to.have.lengthOf(1); |
| 230 | + expect(commandsFailed).to.have.nested.property('[0].failure.cause.code', 50); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + describe('when a maxTimeExpired error is returned inside a writeErrors array', () => { |
| 235 | + // Okay so allegedly this can never happen. |
| 236 | + // But the spec says it can, so let's be defensive and support it. |
| 237 | + // {ok: 1, writeErrors: [{code: 50, codeName: "MaxTimeMSExpired", errmsg: "operation time limit exceeded"}]} |
| 238 | + |
| 239 | + beforeEach(async () => { |
| 240 | + const writeErrorsReply = BSON.serialize({ |
| 241 | + ok: 1, |
| 242 | + writeErrors: [ |
| 243 | + { code: 50, codeName: 'MaxTimeMSExpired', errmsg: 'operation time limit exceeded' } |
| 244 | + ] |
| 245 | + }); |
| 246 | + const commandSpy = sinon.spy(Connection.prototype, 'command'); |
| 247 | + const readManyStub = sinon |
| 248 | + // @ts-expect-error: readMany is private |
| 249 | + .stub(Connection.prototype, 'readMany') |
| 250 | + .callsFake(async function* (...args) { |
| 251 | + const realIterator = readManyStub.wrappedMethod.call(this, ...args); |
| 252 | + const cmd = commandSpy.lastCall.args.at(1); |
| 253 | + if ('giveMeWriteErrors' in cmd) { |
| 254 | + await realIterator.next().catch(() => null); // dismiss response |
| 255 | + yield { parse: () => writeErrorsReply }; |
| 256 | + } else { |
| 257 | + yield (await realIterator.next()).value; |
| 258 | + } |
| 259 | + }); |
| 260 | + }); |
| 261 | + |
| 262 | + afterEach(() => sinon.restore()); |
| 263 | + |
| 264 | + it('throws a MongoOperationTimeoutError error and emits command succeeded', async () => { |
| 265 | + const error = await client |
| 266 | + .db('admin') |
| 267 | + .command({ giveMeWriteErrors: 1 }) |
| 268 | + .catch(error => error); |
| 269 | + expect(error).to.be.instanceOf(MongoOperationTimeoutError); |
| 270 | + expect(error.cause).to.be.instanceOf(MongoServerError); |
| 271 | + expect(error.cause).to.have.nested.property('writeErrors[0].code', 50); |
| 272 | + |
| 273 | + expect(commandsSucceeded).to.have.lengthOf(1); |
| 274 | + expect(commandsSucceeded).to.have.nested.property('[0].reply.writeErrors[0].code', 50); |
| 275 | + }); |
| 276 | + }); |
| 277 | + |
| 278 | + describe('when a maxTimeExpired error is returned inside a writeConcernError embedded document', () => { |
| 279 | + // {ok: 1, writeConcernError: {code: 50, codeName: "MaxTimeMSExpired"}} |
| 280 | + const failpoint: FailPoint = { |
| 281 | + configureFailPoint: 'failCommand', |
| 282 | + mode: { times: 1 }, |
| 283 | + data: { |
| 284 | + failCommands: ['insert'], |
| 285 | + writeConcernError: { code: 50, errmsg: 'times up buster', errorLabels: [] } |
| 286 | + } |
| 287 | + }; |
| 288 | + |
| 289 | + beforeEach(async () => { |
| 290 | + await client.db('admin').command(failpoint); |
| 291 | + }); |
| 292 | + |
| 293 | + afterEach(async () => { |
| 294 | + await client.db('admin').command({ ...failpoint, mode: 'off' }); |
| 295 | + }); |
| 296 | + |
| 297 | + it('throws a MongoOperationTimeoutError error and emits command succeeded', async () => { |
| 298 | + const error = await client |
| 299 | + .db() |
| 300 | + .collection('a') |
| 301 | + .insertOne({}) |
| 302 | + .catch(error => error); |
| 303 | + expect(error).to.be.instanceOf(MongoOperationTimeoutError); |
| 304 | + expect(error.cause).to.be.instanceOf(MongoServerError); |
| 305 | + expect(error.cause).to.have.nested.property('writeConcernError.code', 50); |
| 306 | + |
| 307 | + expect(commandsSucceeded).to.have.lengthOf(1); |
| 308 | + expect(commandsSucceeded).to.have.nested.property('[0].reply.writeConcernError.code', 50); |
| 309 | + }); |
| 310 | + }); |
| 311 | + } |
| 312 | + ); |
164 | 313 | });
|
0 commit comments