@@ -4,7 +4,8 @@ const test = require('./shared').assert;
4
4
const setupDatabase = require ( './shared' ) . setupDatabase ;
5
5
const withMonitoredClient = require ( './shared' ) . withMonitoredClient ;
6
6
const expect = require ( 'chai' ) . expect ;
7
- const { ReadPreference } = require ( '../../src' ) ;
7
+ const { ReadPreference, Topology } = require ( '../../src' ) ;
8
+ const { withClient } = require ( './shared' ) ;
8
9
9
10
describe ( 'ReadPreference' , function ( ) {
10
11
before ( function ( ) {
@@ -587,4 +588,74 @@ describe('ReadPreference', function () {
587
588
} )
588
589
} ) ;
589
590
} ) ;
591
+
592
+ context ( 'should enforce fixed primary read preference' , function ( ) {
593
+ const collectionName = 'ddl_collection' ;
594
+
595
+ beforeEach ( function ( ) {
596
+ const configuration = this . configuration ;
597
+ const client = this . configuration . newClient ( configuration . writeConcernMax ( ) , {
598
+ useUnifiedTopology : true ,
599
+ readPreference : 'primaryPreferred'
600
+ } ) ;
601
+ return withClient ( client , ( client , done ) => {
602
+ const db = client . db ( configuration . db ) ;
603
+ db . addUser ( 'default' , 'pass' , { roles : 'readWrite' } , ( ) => {
604
+ db . createCollection ( 'before_collection' , ( ) => {
605
+ db . createIndex ( collectionName , { aloha : 1 } , done ) ;
606
+ } ) ;
607
+ } ) ;
608
+ } ) ;
609
+ } ) ;
610
+
611
+ const methods = {
612
+ 'Collection#createIndex' : [ { quote : 'text' } ] ,
613
+ 'Db#createIndex' : [ collectionName , { quote : 'text' } ] ,
614
+ 'Db#addUser' : [ 'thomas' , 'pass' , { roles : 'readWrite' } ] ,
615
+ 'Db#removeUser' : [ 'default' ] ,
616
+ 'Db#createCollection' : [ 'created_collection' ] ,
617
+ 'Db#dropCollection' : [ 'before_collection' ] ,
618
+ 'Collection#dropIndex' : [ 'aloha_1' ] ,
619
+ 'Collection#rename' : [ 'new_name' ] ,
620
+ 'Db#dropDatabase' : [ ]
621
+ } ;
622
+
623
+ Object . keys ( methods ) . forEach ( operation => {
624
+ it ( `${ operation } ` , {
625
+ metadata : {
626
+ requires : { topology : [ 'replicaset' , 'sharded' ] }
627
+ } ,
628
+ test : function ( ) {
629
+ const configuration = this . configuration ;
630
+ const client = this . configuration . newClient ( configuration . writeConcernMax ( ) , {
631
+ useUnifiedTopology : true ,
632
+ readPreference : 'primaryPreferred'
633
+ } ) ;
634
+ return withClient ( client , ( client , done ) => {
635
+ const db = client . db ( configuration . db ) ;
636
+ const args = methods [ operation ] ;
637
+ const [ parentId , method ] = operation . split ( '#' ) ;
638
+ const collection = db . collection ( collectionName ) ;
639
+ const parent = parentId === 'Collection' ? collection : parentId === 'Db' ? db : null ;
640
+ const selectServerSpy = this . sinon . spy ( Topology . prototype , 'selectServer' ) ;
641
+ const callback = err => {
642
+ expect ( err ) . to . not . exist ;
643
+ expect ( selectServerSpy . called ) . to . equal ( true ) ;
644
+ if ( typeof selectServerSpy . args [ 0 ] [ 0 ] === 'function' ) {
645
+ expect ( selectServerSpy )
646
+ . nested . property ( 'args[0][1].readPreference.mode' )
647
+ . to . equal ( ReadPreference . PRIMARY ) ;
648
+ } else {
649
+ expect ( selectServerSpy )
650
+ . nested . property ( 'args[0][0].readPreference.mode' )
651
+ . to . equal ( ReadPreference . PRIMARY ) ;
652
+ }
653
+ done ( ) ;
654
+ } ;
655
+ parent [ method ] . apply ( parent , [ ...args , callback ] ) ;
656
+ } ) ;
657
+ }
658
+ } ) ;
659
+ } ) ;
660
+ } ) ;
590
661
} ) ;
0 commit comments