@@ -9,9 +9,13 @@ import {
9
9
Document ,
10
10
GridFSFile ,
11
11
MongoClient ,
12
- ObjectId
12
+ ObjectId ,
13
+ ServerType ,
14
+ TopologyDescription ,
15
+ TopologyType
13
16
} from '../../../src' ;
14
17
import { CommandStartedEvent } from '../../../src/cmap/command_monitoring_events' ;
18
+ import { SERVER_DESCRIPTION_CHANGED } from '../../../src/constants' ;
15
19
import { ReadConcern } from '../../../src/read_concern' ;
16
20
import { ReadPreference } from '../../../src/read_preference' ;
17
21
import { WriteConcern } from '../../../src/write_concern' ;
@@ -499,6 +503,75 @@ operations.set('assertEventCount', async ({ entities, operation }) => {
499
503
expect ( actualEventCount , `Error in assertEventCount for ${ eventName } ` ) . to . equal ( count ) ;
500
504
} ) ;
501
505
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
+
502
575
operations . set ( 'withTransaction' , async ( { entities, operation, client, testConfig } ) => {
503
576
const session = entities . getEntity ( 'session' , operation . object ) ;
504
577
0 commit comments