Skip to content

Commit

Permalink
fix(NODE-6151): MongoClient connect does not keep Node.js running (#4101
Browse files Browse the repository at this point in the history
)
  • Loading branch information
W-A-James authored May 6, 2024
1 parent 2609953 commit 7e0d9e6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Timeout extends Promise<never> {
public timedOut = false;

/** Create a new timeout that expires in `duration` ms */
private constructor(executor: Executor = () => null, duration: number) {
private constructor(executor: Executor = () => null, duration: number, unref = false) {
let reject!: Reject;

if (duration < 0) {
Expand All @@ -63,8 +63,8 @@ export class Timeout extends Promise<never> {
this.timedOut = true;
reject(new TimeoutError(`Expired after ${duration}ms`));
}, this.duration);
// Ensure we do not keep the Node.js event loop running
if (typeof this.id.unref === 'function') {
if (typeof this.id.unref === 'function' && unref) {
// Ensure we do not keep the Node.js event loop running
this.id.unref();
}
}
Expand All @@ -78,8 +78,8 @@ export class Timeout extends Promise<never> {
this.id = undefined;
}

public static expires(durationMS: number): Timeout {
return new Timeout(undefined, durationMS);
public static expires(durationMS: number, unref?: boolean): Timeout {
return new Timeout(undefined, durationMS, unref);
}

static is(timeout: unknown): timeout is Timeout {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ describe('Timeout', function () {
beforeEach(() => {
timeout = Timeout.expires(2000);
});
it('creates a timeout instance that will not keep the Node.js event loop active', function () {
it.skip('creates a timeout instance that will not keep the Node.js event loop active', function () {
expect(timeout).to.have.property('id');
// @ts-expect-error: accessing private property
const id = timeout.id;
expect(id?.hasRef()).to.be.false;
});
}).skipReason = 'Skipping until further work during CSOT implementation';
it('throws a TimeoutError when it expires', async function () {
try {
await timeout;
Expand Down

0 comments on commit 7e0d9e6

Please sign in to comment.