Skip to content

Commit 62239b8

Browse files
committed
test: implement timeout race
1 parent ed08f52 commit 62239b8

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

test/tools/unified-spec-runner/entities.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type ConnectionCheckOutStartedEvent,
1818
type ConnectionClosedEvent,
1919
type ConnectionCreatedEvent,
20+
type ConnectionPool,
2021
type ConnectionPoolClearedEvent,
2122
type ConnectionPoolClosedEvent,
2223
type ConnectionPoolCreatedEvent,
@@ -39,6 +40,8 @@ import {
3940
type ServerHeartbeatStartedEvent,
4041
type ServerHeartbeatSucceededEvent,
4142
type ServerOpeningEvent,
43+
Timeout,
44+
TimeoutError,
4245
type TopologyClosedEvent,
4346
type TopologyDescription,
4447
type TopologyDescriptionChangedEvent,
@@ -622,6 +625,26 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
622625
try {
623626
new EntityEventRegistry(client, entity.client, map).register();
624627
await client.connect();
628+
if (entity.client.awaitMinPoolSizeMS) {
629+
if (client.topology?.s?.servers) {
630+
for (const server of client.topology.s.servers.values()) {
631+
const pool = server.pool;
632+
const timeout = Timeout.expires(entity.client.awaitMinPoolSizeMS);
633+
try {
634+
await Promise.race([checkMinPoolSize(pool), timeout]);
635+
} catch (error) {
636+
if (TimeoutError.is(error)) {
637+
throw new AssertionError(
638+
`Timed out waiting for min pool size to be populated within ${entity.client.awaitMinPoolSizeMS}ms`
639+
);
640+
}
641+
throw error;
642+
} finally {
643+
timeout.clear();
644+
}
645+
}
646+
}
647+
}
625648
} catch (error) {
626649
console.error('failed to connect entity', entity);
627650
// In the case where multiple clients are defined in the test and any one of them failed
@@ -721,3 +744,12 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
721744
return map;
722745
}
723746
}
747+
748+
function checkMinPoolSize(pool: ConnectionPool): Promise<boolean> {
749+
return new Promise(resolve => {
750+
while (pool.options.minPoolSize < pool.totalConnectionCount) {
751+
// Just looping until the min pool size is reached.
752+
}
753+
resolve(true);
754+
});
755+
}

test/tools/unified-spec-runner/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export interface ClientEntity {
155155
ignoreCommandMonitoringEvents?: string[];
156156
serverApi?: ServerApi;
157157
observeSensitiveCommands?: boolean;
158+
awaitMinPoolSizeMS?: number;
158159
// Was optionally scheduled for removal in NODE-6783, but opted to keep it for potential future use.
159160
storeEventsAsEntities?: StoreEventsAsEntity[];
160161
autoEncryptOpts?: Pick<

0 commit comments

Comments
 (0)