@@ -463,6 +463,8 @@ describe('ReadPreference', function () {
463
463
it ( 'should attach a read preference of primaryPreferred to the read command for replicaset' , {
464
464
metadata : { requires : { topology : 'replicaset' , mongodb : '>=3.6' } } ,
465
465
test : async function ( ) {
466
+ let checkedPrimary = false ;
467
+
466
468
for ( const server of this . configuration . options . hostAddresses ) {
467
469
const { host, port } = server . toHostPort ( ) ;
468
470
const client = this . configuration . newClient (
@@ -482,23 +484,39 @@ describe('ReadPreference', function () {
482
484
events . push ( event ) ;
483
485
}
484
486
} ) ;
485
- let serverError ;
487
+ let serverStatus ;
488
+
486
489
try {
487
- await client . db ( 'test' ) . collection ( 'test' ) . findOne ( { a : 1 } ) ;
488
- } catch ( error ) {
489
- serverError = error ;
490
+ const admin = client . db ( ) . admin ( ) ;
491
+ serverStatus = await admin . serverStatus ( ) ;
492
+ } catch ( serverStatusError ) {
493
+ expect ( serverStatusError ) . to . not . exist ;
490
494
}
491
495
492
- expect ( serverError ) . to . not . exist ;
493
- expect ( events [ 0 ] ) . to . containSubset ( {
494
- commandName : 'find' ,
495
- command : {
496
- $readPreference : { mode : 'primaryPreferred' }
496
+ if ( serverStatus . repl . ismaster ) {
497
+ let serverError ;
498
+
499
+ try {
500
+ await client . db ( 'test' ) . collection ( 'test' ) . findOne ( { a : 1 } ) ;
501
+ } catch ( error ) {
502
+ serverError = error ;
497
503
}
498
- } ) ;
504
+
505
+ expect ( serverError ) . to . not . exist ;
506
+ expect ( events [ 0 ] ) . to . containSubset ( {
507
+ commandName : 'find' ,
508
+ command : {
509
+ $readPreference : { mode : 'primaryPreferred' }
510
+ }
511
+ } ) ;
512
+
513
+ checkedPrimary = true ;
514
+ }
499
515
500
516
await client . close ( ) ;
501
517
}
518
+
519
+ expect ( checkedPrimary ) . to . be . equal ( true ) ;
502
520
}
503
521
} ) ;
504
522
@@ -550,6 +568,8 @@ describe('ReadPreference', function () {
550
568
it ( 'should attach a read preference of secondary to the read command for replicaset' , {
551
569
metadata : { requires : { topology : 'replicaset' , mongodb : '>=3.6' } } ,
552
570
test : async function ( ) {
571
+ let checkedSecondary = false ;
572
+
553
573
for ( const server of this . configuration . options . hostAddresses ) {
554
574
const { host, port } = server . toHostPort ( ) ;
555
575
const client = this . configuration . newClient (
@@ -569,23 +589,39 @@ describe('ReadPreference', function () {
569
589
events . push ( event ) ;
570
590
}
571
591
} ) ;
572
- let serverError ;
592
+ let serverStatus ;
593
+
573
594
try {
574
- await client . db ( 'test' ) . collection ( 'test' ) . findOne ( { a : 1 } ) ;
575
- } catch ( error ) {
576
- serverError = error ;
595
+ const admin = client . db ( ) . admin ( ) ;
596
+ serverStatus = await admin . serverStatus ( ) ;
597
+ } catch ( serverStatusError ) {
598
+ expect ( serverStatusError ) . to . not . exist ;
577
599
}
578
600
579
- expect ( serverError ) . to . not . exist ;
580
- expect ( events [ 0 ] ) . to . containSubset ( {
581
- commandName : 'find' ,
582
- command : {
583
- $readPreference : { mode : 'secondary' }
601
+ if ( serverStatus . repl . secondary ) {
602
+ let serverError ;
603
+
604
+ try {
605
+ await client . db ( 'test' ) . collection ( 'test' ) . findOne ( { a : 1 } ) ;
606
+ } catch ( error ) {
607
+ serverError = error ;
584
608
}
585
- } ) ;
609
+
610
+ expect ( serverError ) . to . not . exist ;
611
+ expect ( events [ 0 ] ) . to . containSubset ( {
612
+ commandName : 'find' ,
613
+ command : {
614
+ $readPreference : { mode : 'secondary' }
615
+ }
616
+ } ) ;
617
+
618
+ checkedSecondary = true ;
619
+ }
586
620
587
621
await client . close ( ) ;
588
622
}
623
+
624
+ expect ( checkedSecondary ) . to . be . equal ( true ) ;
589
625
}
590
626
} ) ;
591
627
0 commit comments