Skip to content

Commit

Permalink
fix(mongos): bubble up close events after the first one (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored and mbroadst committed May 31, 2018
1 parent cc46750 commit 3e91d77
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/topologies/mongos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
56 changes: 56 additions & 0 deletions test/functional/sharding_connection_tests.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
});
});

0 comments on commit 3e91d77

Please sign in to comment.