Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 0740c23

Browse files
gribnoysupaddaleax
andauthored
fix: Fix isWritable and isMongos check and fix tests for the properties (#276)
* fix: Fix isWritable and isMongos check and fix tests for the properties * fix: Get first element of the Map from the iterator Co-authored-by: Anna Henningsen <anna.henningsen@mongodb.com>
1 parent 198b6bf commit 0740c23

File tree

2 files changed

+78
-35
lines changed

2 files changed

+78
-35
lines changed

lib/native-client.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ const SYSTEM = 'system';
6666
*/
6767
const ADMIN = 'admin';
6868

69+
/**
70+
*
71+
* @param {Map<K, V>} _map
72+
* @returns {V}
73+
*/
74+
function getFirstFromMap(_map) {
75+
return _map.values().next().value;
76+
}
77+
6978
/**
7079
* The native client class.
7180
*/
@@ -1134,10 +1143,10 @@ class NativeClient extends EventEmitter {
11341143
* @returns {Boolean} If the server is writable.
11351144
*/
11361145
_isWritable(evt) {
1137-
const topologyType = evt.newDescription.topologyType;
1146+
const topologyType = evt.newDescription.type;
11381147
// If type is SINGLE we must be connected to primary, standalone or mongos.
11391148
if (topologyType === SINGLE) {
1140-
const server = evt.newDescription.servers[0];
1149+
const server = getFirstFromMap(evt.newDescription.servers);
11411150
return server && WRITABLE_SERVER_TYPES.includes(server.type);
11421151
}
11431152
return WRITABLE_TYPES.includes(topologyType);
@@ -1151,7 +1160,7 @@ class NativeClient extends EventEmitter {
11511160
* @returns {Boolean} If the server is a mongos.
11521161
*/
11531162
_isMongos(evt) {
1154-
return evt.newDescription.topologyType === SHARDED;
1163+
return evt.newDescription.type === SHARDED;
11551164
}
11561165

11571166
/**

test/native-client.test.js

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var expect = helper.expect;
55
var eventStream = helper.eventStream;
66
var ObjectId = require('bson').ObjectId;
77
var mock = require('mock-require');
8+
const EventEmitter = require('events');
89

910
var 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

Comments
 (0)