Skip to content

Commit 3e02faa

Browse files
committed
test: update prose tests to await events
1 parent de23ac1 commit 3e02faa

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
import { once } from 'node:events';
3+
24
import { expect } from 'chai';
35
import * as sinon from 'sinon';
46

@@ -304,22 +306,10 @@ describe('Retryable Writes Spec Prose', () => {
304306
'emits a command succeeded event for write concern errors with ok: 1',
305307
{ requires: { topology: 'replicaset', mongodb: '>=4.2.9' } },
306308
async () => {
307-
const successListener = event => {
308-
if (event.commandName === 'insert' && event.reply.writeConcernError) {
309-
expect(event.reply.writeConcernError.code).to.equal(91);
310-
}
311-
};
312-
313-
const failureListener = event => {
314-
if (event.commandName === 'insert') {
315-
expect.fail('the insert must be retried after the first suceeded event');
316-
}
317-
};
318-
319309
// Generate a write concern error to assert that we get a command
320310
// suceeded event but the operation will retry because it was an
321311
// actual write concern error.
322-
client.db('admin').command({
312+
await client.db('admin').command({
323313
configureFailPoint: 'failCommand',
324314
mode: { times: 1 },
325315
data: {
@@ -331,15 +321,21 @@ describe('Retryable Writes Spec Prose', () => {
331321
}
332322
});
333323

334-
client.addListener('commandSucceeded', successListener);
335-
client.addListener('commandFailed', failureListener);
324+
const willBeCommandSucceeded = once(client, 'commandSucceeded').catch(error => error);
325+
const willBeCommandFailed = once(client, 'commandFailed', {
326+
signal: AbortSignal.timeout(1000)
327+
}).catch(error => error);
336328

337329
const insertResult = await collection.insertOne({ _id: 1 }).catch(error => error);
338330

339-
// sinon.restore();
340-
client.removeListener('commandSucceeded', successListener);
341-
client.removeListener('commandFailed', failureListener);
342-
331+
const [commandSucceeded] = await willBeCommandSucceeded;
332+
expect(commandSucceeded.commandName).to.equal('insert');
333+
expect(commandSucceeded.reply).to.have.nested.property('writeConcernError.code', 91);
334+
const noCommandFailedEvent = await willBeCommandFailed;
335+
expect(
336+
noCommandFailedEvent.message,
337+
'expected timeout, since no failure event should emit'
338+
).to.include('operation was aborted');
343339
expect(insertResult).to.deep.equal({ acknowledged: true, insertedId: 1 });
344340
}
345341
);

0 commit comments

Comments
 (0)