From 7ffb4bbe056777cbaadeab748941e2f47be0fb1f Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Wed, 12 Dec 2018 20:14:16 -0500 Subject: [PATCH] fix(mongos-replset): pass connect options to child server instances This allows for options determined during uri parsing to make it down to the creation of child Server instances using the legacy SDAM topology classes. NODE-1798 --- lib/topologies/mongos.js | 2 +- lib/topologies/replset.js | 4 +- test/tests/unit/replset/compression_tests.js | 53 ++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 test/tests/unit/replset/compression_tests.js diff --git a/lib/topologies/mongos.js b/lib/topologies/mongos.js index 0ebc0b8bd..cd5110b05 100644 --- a/lib/topologies/mongos.js +++ b/lib/topologies/mongos.js @@ -286,7 +286,7 @@ Mongos.prototype.connect = function(options) { // Create server instances var servers = this.s.seedlist.map(function(x) { const server = new Server( - Object.assign({}, self.s.options, x, { + Object.assign({}, self.s.options, x, options, { authProviders: self.authProviders, reconnect: false, monitoring: false, diff --git a/lib/topologies/replset.js b/lib/topologies/replset.js index bac745096..dbb160445 100644 --- a/lib/topologies/replset.js +++ b/lib/topologies/replset.js @@ -960,12 +960,14 @@ ReplSet.prototype.connect = function(options) { var self = this; // Add any connect level options to the internal state this.s.connectOptions = options || {}; + // Set connecting state stateTransition(this, CONNECTING); + // Create server instances var servers = this.s.seedlist.map(function(x) { return new Server( - Object.assign({}, self.s.options, x, { + Object.assign({}, self.s.options, x, options, { authProviders: self.authProviders, reconnect: false, monitoring: false, diff --git a/test/tests/unit/replset/compression_tests.js b/test/tests/unit/replset/compression_tests.js new file mode 100644 index 000000000..81918d489 --- /dev/null +++ b/test/tests/unit/replset/compression_tests.js @@ -0,0 +1,53 @@ +'use strict'; + +const ReplSet = require('../../../../lib/topologies/replset'); +const mock = require('mongodb-mock-server'); +const ReplSetFixture = require('../common').ReplSetFixture; +const expect = require('chai').expect; + +describe('Compression (ReplSet)', function() { + let test; + before(() => (test = new ReplSetFixture())); + afterEach(() => mock.cleanup()); + beforeEach(() => test.setup()); + + it('should pass compression information to child server instances on connect', function(done) { + const compressionData = []; + test.primaryServer.setMessageHandler(request => { + const doc = request.document; + if (doc.ismaster) { + compressionData.push(doc.compression); + request.reply(test.primaryStates[0]); + } + }); + + test.firstSecondaryServer.setMessageHandler(request => { + const doc = request.document; + if (doc.ismaster) { + compressionData.push(doc.compression); + request.reply(test.firstSecondaryStates[0]); + } + }); + + const replSet = new ReplSet( + [test.primaryServer.address(), test.firstSecondaryServer.address()], + { + setName: 'rs', + haInterval: 10000, + connectionTimeout: 3000, + secondaryOnlyConnectionAllowed: true, + size: 1 + } + ); + + replSet.on('fullsetup', () => { + compressionData.forEach(data => { + expect(data).to.eql(['zlib']); + }); + + done(); + }); + + replSet.connect({ compression: { compressors: ['zlib'] } }); + }); +});