Skip to content

Commit 3a41fac

Browse files
W-A-Jamesnbbeeken
authored andcommitted
WIP: Fixing test failures
1 parent 2116955 commit 3a41fac

File tree

6 files changed

+50
-52
lines changed

6 files changed

+50
-52
lines changed

src/sdam/server.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
MongoInvalidArgumentError,
3131
MongoNetworkError,
3232
MongoNetworkTimeoutError,
33-
MongoRuntimeError,
3433
MongoServerClosedError,
3534
type MongoServerError,
3635
needsRetryableWriteLabel
@@ -315,56 +314,53 @@ export class Server extends TypedEventEmitter<ServerEvents> {
315314
// require special logic to decrement it again, or would double increment (the load
316315
// balanced code makes a recursive call). Instead, we increment the count after this
317316
// check.
318-
319317
if (this.loadBalanced && session && conn == null && isPinnableCommand(cmd, session)) {
320318
conn = await this.pool.checkOut();
321319
session.pin(conn);
322320
return this.command(ns, cmd, finalOptions);
323321
}
324322

325-
const runCommand = async (conn: Connection) => {
326-
this.incrementOperationCount();
327-
try {
328-
return await conn.command(ns, cmd, finalOptions);
329-
} catch (e) {
330-
throw this.decorateAndHandleError(conn, cmd, finalOptions, e);
331-
} finally {
332-
this.decrementOperationCount();
333-
}
334-
};
335-
336-
const shouldReauth = (e: AnyError): boolean => {
337-
return e instanceof MongoError && e.code === MONGODB_ERROR_CODES.Reauthenticate;
338-
};
339-
340-
// FIXME: This is where withConnection logic should go
323+
this.incrementOperationCount();
341324
if (!conn) {
342325
try {
343326
conn = await this.pool.checkOut();
344-
} catch (e) {
345-
if (!e) throw new MongoRuntimeError('Failed to create connection without error');
346-
if (!(e instanceof PoolClearedError)) this.handleError(e);
327+
} catch (checkoutError) {
328+
if (!(checkoutError instanceof PoolClearedError)) this.handleError(checkoutError);
347329

348-
throw e;
330+
throw checkoutError;
349331
}
350332
}
351333

352334
// Reauth flow
353-
let rv;
354335
try {
355-
rv = await runCommand(conn);
356-
} catch (e) {
357-
if (shouldReauth(e)) {
336+
return await this.executeCommand(conn, ns, cmd, finalOptions);
337+
} catch (operationError) {
338+
if (
339+
operationError instanceof MongoError &&
340+
operationError.code === MONGODB_ERROR_CODES.Reauthenticate
341+
) {
358342
conn = await this.pool.reauthenticateAsync(conn);
359-
rv = await runCommand(conn);
343+
return await this.executeCommand(conn, ns, cmd, finalOptions);
360344
} else {
361-
throw e;
345+
throw operationError;
362346
}
363347
} finally {
364348
this.pool.checkIn(conn);
349+
this.decrementOperationCount();
365350
}
351+
}
366352

367-
return rv;
353+
private async executeCommand(
354+
conn: Connection,
355+
ns: MongoDBNamespace,
356+
cmd: Document,
357+
options?: CommandOptions
358+
): Promise<Document> {
359+
try {
360+
return await conn.command(ns, cmd, options);
361+
} catch (commandOptions) {
362+
throw this.decorateAndHandleError(conn, cmd, options, commandOptions);
363+
}
368364
}
369365

370366
/**
@@ -415,6 +411,10 @@ export class Server extends TypedEventEmitter<ServerEvents> {
415411
}
416412
}
417413

414+
/**
415+
* Ensure that error is properly decorated and internal state is updated before throwing
416+
* @internal
417+
*/
418418
private decorateAndHandleError(
419419
connection: Connection,
420420
cmd: Document,

test/integration/retryable-writes/retryable_writes.spec.prose.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,19 @@ describe('Retryable Writes Spec Prose', () => {
278278
const serverCommandStub = sinon.stub(Server.prototype, 'command');
279279
serverCommandStub
280280
.onCall(0)
281-
.yieldsRight(
282-
new MongoWriteConcernError({ errorLabels: ['RetryableWriteError'], code: 91 }, {})
281+
.returns(
282+
Promise.reject(
283+
new MongoWriteConcernError({ errorLabels: ['RetryableWriteError'], code: 91 }, {})
284+
)
283285
);
284286
serverCommandStub
285287
.onCall(1)
286-
.yieldsRight(
287-
new MongoWriteConcernError(
288-
{ errorLabels: ['RetryableWriteError', 'NoWritesPerformed'], errorCode: 10107 },
289-
{}
288+
.returns(
289+
Promise.reject(
290+
new MongoWriteConcernError(
291+
{ errorLabels: ['RetryableWriteError', 'NoWritesPerformed'], errorCode: 10107 },
292+
{}
293+
)
290294
)
291295
);
292296

test/integration/server-selection/operation_count.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ describe('Server Operation Count Tests', function () {
103103
const server = Array.from(client.topology.s.servers.values())[0];
104104
expect(server.s.operationCount).to.equal(0);
105105

106-
sinon.stub(ConnectionPool.prototype, 'checkOut').callsFake(function (cb) {
107-
cb(new Error('unable to checkout connection'), undefined);
108-
});
106+
sinon
107+
.stub(ConnectionPool.prototype, 'checkOut')
108+
.returns(Promise.reject(new Error('unable to checkout connection')));
109109
const commandSpy = sinon.spy(server, 'command');
110110

111111
const error = await collection.findOne({ count: 1 }).catch(e => e);

test/tools/spec-runner/context.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,17 @@ class TestRunnerContext {
143143
return Promise.all(cleanupPromises).then(cleanup, cleanup);
144144
}
145145

146-
targetedFailPoint(options) {
146+
async targetedFailPoint(options) {
147147
const session = options.session;
148148
const failPoint = options.failPoint;
149149
expect(session.isPinned).to.be.true;
150150

151-
return new Promise((resolve, reject) => {
152-
const serverOrConnection = session.loadBalanced
153-
? session.pinnedConnection
154-
: session.transaction.server;
151+
const serverOrConnection = session.loadBalanced
152+
? session.pinnedConnection
153+
: session.transaction.server;
155154

156-
serverOrConnection.command(ns('admin.$cmd'), failPoint, undefined, err => {
157-
if (err) return reject(err);
158-
159-
this.appliedFailPoints.push(failPoint);
160-
resolve();
161-
});
162-
});
155+
await serverOrConnection.command(ns('admin.$cmd'), failPoint, {});
156+
this.appliedFailPoints.push(failPoint);
163157
}
164158

165159
enableFailPoint(failPoint) {

test/unit/operations/get_more.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe('GetMoreOperation', function () {
202202
new ServerDescription('a:1'),
203203
{} as any
204204
);
205-
sinon.stub(server, 'command').yieldsRight();
205+
sinon.stub(server, 'command');
206206

207207
it('should throw if the cursorId is undefined', async () => {
208208
const getMoreOperation = new GetMoreOperation(

test/unit/operations/kill_cursors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('class KillCursorsOperation', () => {
9191
server,
9292
options
9393
) as any;
94-
const stub = sinon.stub(server, 'command').yieldsRight();
94+
const stub = sinon.stub(server, 'command').returns(Promise.resolve({}));
9595
await killCursorsOperation.execute(server, undefined);
9696
expect(stub).to.have.been.calledOnceWith(namespace, {
9797
killCursors: namespace.collection,

0 commit comments

Comments
 (0)