@@ -5,6 +5,7 @@ var expect = helper.expect;
55var eventStream = helper . eventStream ;
66var ObjectId = require ( 'bson' ) . ObjectId ;
77var mock = require ( 'mock-require' ) ;
8+ const EventEmitter = require ( 'events' ) ;
89
910var NativeClient = require ( '../lib/native-client' ) ;
1011
@@ -27,81 +28,114 @@ describe('NativeClient', function() {
2728
2829 describe ( '#connect' , function ( ) {
2930 context ( 'when mocking connection-model' , function ( ) {
31+ function mockedTopologyDescription (
32+ topologyType = 'Standalone' ,
33+ serverType = 'Single'
34+ ) {
35+ return {
36+ type : topologyType ,
37+ servers : new Map ( [ [ '127.0.0.1:27017' , { type : serverType } ] ] )
38+ } ;
39+ }
40+
3041 /*
3142 * pretends to be a connection-model providing every function call
32- * required in NativeClient#connect, but returns the ismaster of our
33- * choice.
43+ * required in NativeClient#connect, but returns topology of our choice
3444 */
35- var mockedConnectionModel = function ( ismaster ) {
45+ function mockedConnectionModel ( topologyDescription ) {
46+ const _topologyDescription =
47+ topologyDescription || mockedTopologyDescription ( ) ;
48+
3649 return {
37- connect : function ( model , cb ) {
38- var db = {
39- admin : function ( ) {
40- return {
41- command : function ( cmd , innerCb ) {
42- innerCb ( null , ismaster ) ;
43- }
44- } ;
45- }
46- } ;
47- cb ( null , db ) ;
48- return { on : function ( ) { } } ;
50+ connect ( _model , setupListeners , cb ) {
51+ const mockedClient = new EventEmitter ( ) ;
52+ mockedClient . db = ( ) => { } ;
53+ setupListeners ( mockedClient ) ;
54+ mockedClient . emit ( 'topologyDescriptionChanged' , {
55+ newDescription : _topologyDescription
56+ } ) ;
57+ cb ( null , mockedClient ) ;
4958 }
5059 } ;
51- } ;
60+ }
5261
5362 after ( function ( ) {
5463 mock . stop ( 'mongodb-connection-model' ) ;
5564 } ) ;
5665
57- it ( 'sets .isMongos to true when ismaster is from a mongos' , function ( ) {
58- mock ( 'mongodb-connection-model' , mockedConnectionModel ( { msg : 'isdbgrid' } ) ) ;
66+ it ( 'sets .isMongos to true when topology is sharded' , function ( done ) {
67+ mock (
68+ 'mongodb-connection-model' ,
69+ mockedConnectionModel ( mockedTopologyDescription ( 'Sharded' ) )
70+ ) ;
71+
5972 var MockedNativeClient = mock . reRequire ( '../lib/native-client' ) ;
6073 var mockedClient = new MockedNativeClient ( helper . connection ) ;
74+
6175 mockedClient . connect ( function ( ) {
62- /* eslint no-unused-expressions: 0 */
6376 expect ( mockedClient . isMongos ) . to . be . true ;
77+ done ( ) ;
6478 } ) ;
6579 } ) ;
6680
67- it ( 'sets .isMongos to false when ismaster is not from a mongos' , function ( ) {
68- mock ( 'mongodb-connection-model' , mockedConnectionModel ( { ismaster : true } ) ) ;
81+ it ( 'sets .isMongos to false when topology is not sharded' , function ( done ) {
82+ mock ( 'mongodb-connection-model' , mockedConnectionModel ( ) ) ;
83+
6984 var MockedNativeClient = mock . reRequire ( '../lib/native-client' ) ;
7085 var mockedClient = new MockedNativeClient ( helper . connection ) ;
86+
7187 mockedClient . connect ( function ( ) {
72- /* eslint no-unused-expressions: 0 */
7388 expect ( mockedClient . isMongos ) . to . be . false ;
89+ done ( ) ;
7490 } ) ;
75- mock . stop ( 'mongodb-connection-model' ) ;
7691 } ) ;
7792
78- it ( 'sets .isWritable to true when the node is a primary replset member' , function ( ) {
79- mock ( 'mongodb-connection-model' , mockedConnectionModel ( { ismaster : true } ) ) ;
93+ it ( 'sets .isWritable to true when the node is a primary replset member' , function ( done ) {
94+ mock (
95+ 'mongodb-connection-model' ,
96+ mockedConnectionModel (
97+ mockedTopologyDescription ( 'ReplicaSetWithPrimary' )
98+ )
99+ ) ;
100+
80101 var MockedNativeClient = mock . reRequire ( '../lib/native-client' ) ;
81102 var mockedClient = new MockedNativeClient ( helper . connection ) ;
103+
82104 mockedClient . connect ( function ( ) {
83- /* eslint no-unused-expressions: 0 */
84105 expect ( mockedClient . isWritable ) . to . be . true ;
106+ done ( ) ;
85107 } ) ;
86108 } ) ;
87109
88- it ( 'sets .isWritable to false when the node is a secondary replset member' , function ( ) {
89- mock ( 'mongodb-connection-model' , mockedConnectionModel ( { ismaster : false } ) ) ;
110+ it ( 'sets .isWritable to false when the node is a secondary replset member' , function ( done ) {
111+ mock (
112+ 'mongodb-connection-model' ,
113+ mockedConnectionModel (
114+ mockedTopologyDescription ( 'Single' , 'RSSecondary' )
115+ )
116+ ) ;
117+
90118 var MockedNativeClient = mock . reRequire ( '../lib/native-client' ) ;
91119 var mockedClient = new MockedNativeClient ( helper . connection ) ;
120+
92121 mockedClient . connect ( function ( ) {
93- /* eslint no-unused-expressions: 0 */
94122 expect ( mockedClient . isWritable ) . to . be . false ;
123+ done ( ) ;
95124 } ) ;
96125 } ) ;
97126
98- it ( 'sets .isWritable to true when the node is a mongos' , function ( ) {
99- mock ( 'mongodb-connection-model' , mockedConnectionModel ( { msg : 'isdbgrid' } ) ) ;
127+ it ( 'sets .isWritable to true when the node is a mongos' , function ( done ) {
128+ mock (
129+ 'mongodb-connection-model' ,
130+ mockedConnectionModel ( mockedTopologyDescription ( 'Single' , 'Mongos' ) )
131+ ) ;
132+
100133 var MockedNativeClient = mock . reRequire ( '../lib/native-client' ) ;
101134 var mockedClient = new MockedNativeClient ( helper . connection ) ;
135+
102136 mockedClient . connect ( function ( ) {
103- /* eslint no-unused-expressions: 0 */
104137 expect ( mockedClient . isWritable ) . to . be . true ;
138+ done ( ) ;
105139 } ) ;
106140 } ) ;
107141 } ) ;
0 commit comments