From 1f658b3522c486d3f1a2733e64008f15a59190cf Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 31 May 2018 10:21:24 -0400 Subject: [PATCH] fix(mongos): bubble up close events after the first one Fix Automattic/mongoose#6249 Re: #1685 --- lib/topologies/mongos.js | 6 +-- test/functional/sharding_connection_tests.js | 56 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/lib/topologies/mongos.js b/lib/topologies/mongos.js index 90c0dee9c1..72fb3aa156 100644 --- a/lib/topologies/mongos.js +++ b/lib/topologies/mongos.js @@ -268,9 +268,9 @@ class Mongos extends TopologyBase { }); // Set up listeners - self.s.coreTopology.once('timeout', errorHandler('timeout')); - self.s.coreTopology.once('error', errorHandler('error')); - self.s.coreTopology.once('close', errorHandler('close')); + self.s.coreTopology.on('timeout', errorHandler('timeout')); + self.s.coreTopology.on('error', errorHandler('error')); + self.s.coreTopology.on('close', errorHandler('close')); // Set up serverConfig listeners self.s.coreTopology.on('fullsetup', function() { diff --git a/test/functional/sharding_connection_tests.js b/test/functional/sharding_connection_tests.js index a543fb01af..fef10225cf 100644 --- a/test/functional/sharding_connection_tests.js +++ b/test/functional/sharding_connection_tests.js @@ -1,4 +1,6 @@ 'use strict'; + +var co = require('co'); var f = require('util').format; var test = require('./shared').assert; var setupDatabase = require('./shared').setupDatabase; @@ -168,4 +170,58 @@ describe('Sharding (Connection)', function() { ); } }); + + /** + * @ignore + */ + it('Should emit close event when mongos is stopped', { + metadata: { requires: { topology: 'sharded' } }, + + // The actual test we wish to run + test: function(done) { + var configuration = this.configuration; + var mongo = configuration.require; + var MongoClient = mongo.MongoClient; + var manager = configuration.manager; + var mongos = manager.proxies; + + co(function*() { + var url = f( + 'mongodb://%s:%s,%s:%s/sharded_test_db', + configuration.host, + configuration.port, + configuration.host, + configuration.port + 1 + ); + + var client = yield MongoClient.connect(url); + + var doc = { answer: 42 }; + var db = client.db('Test'); + var coll = db.collection('docs'); + yield coll.insertOne(doc); + + doc = yield coll.findOne({ answer: 42 }); + test.ok(!!doc); + + var waitForClose = new Promise(resolve => db.once('close', resolve)); + + yield mongos.map(p => p.stop()); + yield waitForClose; + yield mongos.map(p => p.start()); + + doc = yield coll.findOne({ answer: 42 }); + test.ok(!!doc); + + waitForClose = new Promise(resolve => db.once('close', resolve)); + + yield mongos.map(p => p.stop()); + yield waitForClose; + yield mongos.map(p => p.start()); + + doc = yield coll.findOne({ answer: 42 }); + test.ok(!!doc); + }).then(() => done(), done); + } + }); });