Skip to content

Commit 6e9fd32

Browse files
committed
chore: fix tests and remove changes
1 parent af2e9a9 commit 6e9fd32

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

src/mongo_client.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import { readPreferenceServerSelector } from './sdam/server_selection';
4747
import type { SrvPoller } from './sdam/srv_polling';
4848
import { Topology, type TopologyEvents } from './sdam/topology';
4949
import { ClientSession, type ClientSessionOptions, ServerSessionPool } from './sessions';
50-
import { Timeout } from './timeout';
5150
import {
5251
COSMOS_DB_CHECK,
5352
COSMOS_DB_MSG,
@@ -528,19 +527,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
528527
return await this.connectionLock;
529528
}
530529

531-
const timeoutMS = this[kOptions].timeoutMS;
532-
533-
this.connectionLock = this._connect().finally(() => {
534-
// Clear the lock only when this promise finishes:
530+
try {
531+
this.connectionLock = this._connect();
532+
await this.connectionLock;
533+
} finally {
534+
// release
535535
this.connectionLock = undefined;
536-
});
537-
538-
await (timeoutMS == null
539-
? this.connectionLock
540-
: Promise.race([
541-
this.connectionLock,
542-
Timeout.expires(timeoutMS).catch(Timeout.convertError)
543-
]));
536+
}
544537

545538
return this;
546539
}

src/timeout.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,6 @@ export class Timeout extends Promise<never> {
126126
static override reject(rejection?: Error): Timeout {
127127
return new Timeout(undefined, { duration: 0, unref: true, rejection });
128128
}
129-
130-
static convertError(this: void, cause: unknown): never {
131-
if (TimeoutError.is(cause)) throw new MongoOperationTimeoutError('Timed out');
132-
if (cause instanceof Error) throw cause;
133-
throw new MongoRuntimeError('Unknown error', { cause: cause as any });
134-
}
135129
}
136130

137131
/** @internal */

test/integration/node-specific/auto_connect.test.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { once } from 'events';
3+
import * as sinon from 'sinon';
34

45
import {
56
BSONType,
@@ -8,12 +9,11 @@ import {
89
type Collection,
910
type MongoClient,
1011
MongoNotConnectedError,
11-
MongoOperationTimeoutError,
1212
ProfilingLevel,
1313
Topology,
1414
TopologyType
1515
} from '../../mongodb';
16-
import { type FailPoint } from '../../tools/utils';
16+
import { type FailPoint, sleep } from '../../tools/utils';
1717

1818
describe('When executing an operation for the first time', () => {
1919
let client: MongoClient;
@@ -824,7 +824,7 @@ describe('When executing an operation for the first time', () => {
824824
});
825825
});
826826

827-
describe.only('and CSOT is enabled', function () {
827+
describe('and CSOT is enabled', function () {
828828
let client: MongoClient;
829829

830830
beforeEach(async function () {
@@ -836,7 +836,7 @@ describe('When executing an operation for the first time', () => {
836836
});
837837

838838
describe('and nothing is wrong', function () {
839-
it('should connect the client', async function () {
839+
it('connects the client', async function () {
840840
await client.connect();
841841
expect(client).to.have.property('topology').that.is.instanceOf(Topology);
842842
});
@@ -856,24 +856,45 @@ describe('When executing an operation for the first time', () => {
856856
});
857857

858858
it(
859-
'should throw an MongoOperationTimeoutError error from connect',
859+
'takes as long as ping is delayed for and does not throw a timeout error',
860860
{ requires: { auth: 'enabled' } },
861861
async function () {
862862
const start = performance.now();
863-
const error = await client.connect().catch(error => error);
863+
const returnedClient = await client.connect();
864864
const end = performance.now();
865-
expect(error).to.be.instanceOf(MongoOperationTimeoutError);
866-
expect(end - start).to.be.within(1000, 1500);
865+
expect(returnedClient).to.equal(client);
866+
expect(end - start).to.be.within(2000, 2500); // timeoutMS is 1000, did not apply.
867867
}
868868
);
869+
});
870+
871+
describe('when server selection takes longer than the timeout', function () {
872+
beforeEach(async function () {
873+
const selectServerStub = sinon
874+
.stub(Topology.prototype, 'selectServer')
875+
.callsFake(async function (selector, options) {
876+
await sleep(2000);
877+
const result = selectServerStub.wrappedMethod.call(this, selector, options);
878+
sinon.restore(); // restore after connect selection
879+
return result;
880+
});
881+
});
882+
883+
// restore sinon stub after test
884+
afterEach(() => {
885+
sinon.restore();
886+
});
869887

870888
it(
871-
'still have a pending connect promise',
889+
'takes as long as selectServer is delayed for and does not throw a timeout error',
872890
{ requires: { auth: 'enabled' } },
873891
async function () {
874-
const error = await client.connect().catch(error => error);
875-
expect(client).to.have.property('connectionLock').that.is.instanceOf(Promise);
876-
expect(error).to.be.instanceOf(MongoOperationTimeoutError);
892+
const start = performance.now();
893+
expect(client.topology).to.not.exist; // make sure not connected.
894+
const res = await client.db().collection('test').insertOne({ a: 1 }, { timeoutMS: 1000 }); // auto-connect
895+
const end = performance.now();
896+
expect(res).to.have.property('acknowledged', true);
897+
expect(end - start).to.be.within(2000, 2500); // timeoutMS is 1000, did not apply.
877898
}
878899
);
879900
});

0 commit comments

Comments
 (0)