Skip to content

Commit 9b7ea19

Browse files
committed
test: add support for topology description related operations
1 parent 05eabba commit 9b7ea19

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
HostAddress,
3030
MongoClient,
3131
MongoCredentials,
32-
ServerDescriptionChangedEvent
32+
ServerDescriptionChangedEvent,
33+
TopologyDescription
3334
} from '../../../src/index';
3435
import { ReadConcern } from '../../../src/read_concern';
3536
import { ReadPreference } from '../../../src/read_preference';
@@ -273,6 +274,7 @@ export type Entity =
273274
| UnifiedChangeStream
274275
| GridFSBucket
275276
| ClientEncryption
277+
| TopologyDescription // From recordTopologyDescription operation
276278
| Document; // Results from operations
277279

278280
export type EntityCtor =

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import {
99
Document,
1010
GridFSFile,
1111
MongoClient,
12-
ObjectId
12+
ObjectId,
13+
ServerType,
14+
TopologyDescription,
15+
TopologyType
1316
} from '../../../src';
1417
import { CommandStartedEvent } from '../../../src/cmap/command_monitoring_events';
18+
import { SERVER_DESCRIPTION_CHANGED } from '../../../src/constants';
1519
import { ReadConcern } from '../../../src/read_concern';
1620
import { ReadPreference } from '../../../src/read_preference';
1721
import { WriteConcern } from '../../../src/write_concern';
@@ -499,6 +503,75 @@ operations.set('assertEventCount', async ({ entities, operation }) => {
499503
expect(actualEventCount, `Error in assertEventCount for ${eventName}`).to.equal(count);
500504
});
501505

506+
operations.set('recordTopologyDescription', async ({ entities, operation }) => {
507+
const { client, id }: { client: string; id: string } = operation.arguments! as any;
508+
expect(id).to.be.a('string');
509+
const mongoClient = entities.getEntity('client', client, true);
510+
const description = mongoClient.topology?.description;
511+
expect(description, `Undefined topology description for client ${client}`).to.exist;
512+
513+
entities.set(id, description!);
514+
});
515+
516+
operations.set('assertTopologyType', async ({ entities, operation }) => {
517+
const {
518+
topologyDescription,
519+
topologyType
520+
}: { topologyDescription: string; topologyType: TopologyType } = operation.arguments! as any;
521+
expect(topologyDescription).to.be.a('string');
522+
const actualDescription = entities.get(topologyDescription) as TopologyDescription;
523+
expect(actualDescription, `Failed to retrieve description for topology ${topologyDescription}`).to
524+
.exist;
525+
expect(actualDescription).to.have.property('type', topologyType);
526+
});
527+
528+
operations.set('waitForPrimaryChange', async ({ entities, operation }) => {
529+
const {
530+
client,
531+
priorTopologyDescription,
532+
timeoutMS
533+
}: { client: string; priorTopologyDescription: TopologyType; timeoutMS?: number } =
534+
operation.arguments! as any;
535+
536+
const mongoClient = entities.getEntity('client', client, true);
537+
538+
expect(priorTopologyDescription).to.be.a('string');
539+
const priorTopologyDescriptionObject = entities.get(
540+
priorTopologyDescription
541+
) as TopologyDescription;
542+
expect(
543+
priorTopologyDescriptionObject,
544+
`Failed to retrieve description for topology ${priorTopologyDescription}`
545+
).to.exist;
546+
547+
const priorPrimary = Array.from(priorTopologyDescriptionObject.servers.values()).find(
548+
serverDescription => serverDescription.type === ServerType.RSPrimary
549+
);
550+
551+
const newPrimaryPromise = new Promise<void>(resolve => {
552+
function checkForNewPrimary() {
553+
const currentPrimary = Array.from(mongoClient.topology!.description.servers.values()).find(
554+
serverDescription => serverDescription.type === ServerType.RSPrimary
555+
);
556+
if (
557+
(!priorPrimary && currentPrimary) ||
558+
(currentPrimary && !currentPrimary.equals(priorPrimary))
559+
) {
560+
return resolve();
561+
}
562+
563+
mongoClient.once(SERVER_DESCRIPTION_CHANGED, checkForNewPrimary);
564+
}
565+
checkForNewPrimary();
566+
});
567+
await Promise.race([
568+
newPrimaryPromise,
569+
sleep(timeoutMS ?? 10000).then(() =>
570+
Promise.reject(new Error(`Timed out waiting for primary change on ${client}`))
571+
)
572+
]);
573+
});
574+
502575
operations.set('withTransaction', async ({ entities, operation, client, testConfig }) => {
503576
const session = entities.getEntity('session', operation.object);
504577

0 commit comments

Comments
 (0)