diff --git a/.travis.yml b/.travis.yml index 821a8a6f0..18e157576 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,34 @@ sudo: required dist: trusty -language: node_js -node_js: - - "4" - - "6" - - "8" +language: node_js cache: yarn: true directories: - node_modules -env: - - MONGODB_VERSION=2.6.x - - MONGODB_VERSION=3.0.x - - MONGODB_VERSION=3.2.x - - MONGODB_VERSION=3.4.x +jobs: + include: + - stage: tests + node_js: 4 + env: MONGODB_VERSION=2.6.x + - stage: tests + node_js: 4 + env: MONGODB_VERSION=3.0.x + - stage: tests + node_js: 4 + env: MONGODB_VERSION=3.2.x + - stage: tests + node_js: 4 + env: MONGODB_VERSION=3.4.x + - stage: tests + node_js: 4 + env: MONGODB_VERSION=3.5.x + + - stage: tests + node_js: 6 + env: MONGODB_VERSION=3.4.x + - stage: tests + node_js: 8 + env: MONGODB_VERSION=3.4.x diff --git a/lib/connection/connection.js b/lib/connection/connection.js index d45c9d3be..78431bd12 100644 --- a/lib/connection/connection.js +++ b/lib/connection/connection.js @@ -36,6 +36,8 @@ var debugFields = [ 'promoteBuffers', 'checkServerIdentity' ]; + +var connectionAccountingSpy = undefined; var connectionAccounting = false; var connections = {}; @@ -185,13 +187,18 @@ Connection.prototype.resetSocketTimeout = function() { } }; -Connection.enableConnectionAccounting = function() { +Connection.enableConnectionAccounting = function(spy) { + if (spy) { + connectionAccountingSpy = spy; + } + connectionAccounting = true; connections = {}; }; Connection.disableConnectionAccounting = function() { connectionAccounting = false; + connectionAccountingSpy = undefined; }; Connection.connections = function() { @@ -201,11 +208,19 @@ Connection.connections = function() { function deleteConnection(id) { // console.log("=== deleted connection " + id + " :: " + (connections[id] ? connections[id].port : '')) delete connections[id]; + + if (connectionAccountingSpy) { + connectionAccountingSpy.deleteConnection(id); + } } function addConnection(id, connection) { // console.log("=== added connection " + id + " :: " + connection.port) connections[id] = connection; + + if (connectionAccountingSpy) { + connectionAccountingSpy.addConnection(id, connection); + } } // diff --git a/test/mock/index.js b/test/mock/index.js index f137a822d..e8ac8d160 100644 --- a/test/mock/index.js +++ b/test/mock/index.js @@ -1,5 +1,39 @@ var Server = require('./lib/server'); +const cleanup = (servers, spy, callback) => { + if (typeof spy === 'function') { + callback = spy; + spy = undefined; + } + + if (!Array.isArray(servers)) { + throw new Error('First argument must be an array of mock servers'); + } + + if (spy) { + const alreadyDrained = spy.connectionCount() === 0; + const finish = () => { + callback(null, null); + }; + + if (!alreadyDrained) { + spy.once('drained', () => finish()); + } + + const cleanupPromise = Promise.all(servers.map(server => server.destroy())).catch(err => + callback(err, null) + ); + + if (alreadyDrained) { + cleanupPromise.then(() => finish()); + } + } else { + Promise.all(servers.map(server => server.destroy())) + .then(() => callback(null, null)) + .catch(err => callback(err, null)); + } +}; + /* * Main module */ @@ -7,5 +41,7 @@ module.exports = { createServer: function(port, host, options) { options = options || {}; return new Server(port, host, options).start(); - } + }, + + cleanup: cleanup }; diff --git a/test/mock/lib/server.js b/test/mock/lib/server.js index 7b1af0c9b..eec2eb4c0 100644 --- a/test/mock/lib/server.js +++ b/test/mock/lib/server.js @@ -33,7 +33,7 @@ var Server = function(port, host, options) { this.host = host; this.port = port; // Create a server socket - this.socket = net.createServer(); + this.server = net.createServer(); // Responses this.messages = []; // state @@ -47,11 +47,21 @@ var Server = function(port, host, options) { inherits(Server, EventEmitter); Server.prototype.destroy = function() { - this.state = 'destroyed'; - this.socket.close(); + var self = this; + if (self.state === 'destroyed') { + return Promise.resolve(); + } + + return new Promise(function(resolve, reject) { + self.sockets.forEach(function(x) { + x.destroy(); + }); - this.sockets.forEach(function(x) { - x.destroy(); + self.server.close(function(err) { + if (err) return reject(err); + self.state = 'destroyed'; + resolve(); + }); }); }; @@ -60,12 +70,12 @@ Server.prototype.start = function() { // Return start promise return new Promise(function(resolve, reject) { - self.socket.on('error', function(err) { + self.server.on('error', function(err) { console.log('!!!!!!!!!!!!!!!!!!!! error reject'); reject(err); }); - self.socket.on('connection', function(c) { + self.server.on('connection', function(c) { self.connections = self.connections + 1; self.sockets.push(c); @@ -98,13 +108,17 @@ Server.prototype.start = function() { }); }); - self.socket.listen(self.port, self.host, function() { + self.server.listen(self.port, self.host, function() { resolve(self); }); self.on('message', function(message, connection) { var request = new Request(self, connection, message); - self.messages.push(request); + if (self.messageHandler) { + self.messageHandler(request); + } else { + self.messages.push(request); + } }); self.state = 'running'; @@ -133,6 +147,10 @@ Server.prototype.receive = function() { }); }; +Server.prototype.setMessageHandler = function(messageHandler) { + this.messageHandler = messageHandler; +}; + var protocol = function(self, message) { var index = 0; self.isCompressed = false; diff --git a/test/tests/functional/mongos_mocks/mixed_seed_list_tests.js b/test/tests/functional/mongos_mocks/mixed_seed_list_tests.js index d8a307373..d8573739c 100644 --- a/test/tests/functional/mongos_mocks/mixed_seed_list_tests.js +++ b/test/tests/functional/mongos_mocks/mixed_seed_list_tests.js @@ -2,7 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'); describe('Mongos Mixed Seed List (mocks)', function() { it('Should correctly print warning when non mongos proxy passed in seed list', { @@ -21,7 +21,6 @@ describe('Mongos Mixed Seed List (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Default message fields var defaultFields = { @@ -57,80 +56,63 @@ describe('Mongos Mixed Seed List (mocks)', function() { // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52005, 'localhost'); - mongos2 = yield mockupdb.createServer(52006, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(52005, 'localhost'); + mongos2 = yield mock.createServer(52006, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[1]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } - } - }).catch(function() {}); - - // Attempt to connect - var server = new Mongos( - [{ host: 'localhost', port: 52005 }, { host: 'localhost', port: 52006 }], - { - connectionTimeout: 3000, - socketTimeout: 1000, - haInterval: 1000, - localThresholdMS: 500, - size: 1 - } - ); + }); - var logger = Logger.currentLogger(); - Logger.setCurrentLogger(function(msg, state) { - expect(state.type).to.equal('warn'); - expect(state.message).to.equal( - 'expected mongos proxy, but found replicaset member mongod for server localhost:52006' - ); + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[1]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); + } }); + }); - // Add event listeners - server.once('connect', function() { - Logger.setCurrentLogger(logger); + // Attempt to connect + var server = new Mongos( + [{ host: 'localhost', port: 52005 }, { host: 'localhost', port: 52006 }], + { + connectionTimeout: 3000, + socketTimeout: 1000, + haInterval: 1000, + localThresholdMS: 500, + size: 1 + } + ); + + var logger = Logger.currentLogger(); + Logger.setCurrentLogger(function(msg, state) { + expect(state.type).to.equal('warn'); + expect(state.message).to.equal( + 'expected mongos proxy, but found replicaset member mongod for server localhost:52006' + ); + }); - running = false; - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - done(); - }); + // Add event listeners + server.once('connect', function() { + Logger.setCurrentLogger(logger); - server.on('error', done); - setTimeout(function() { - server.connect(); - }, 100); + mock.cleanup([server, mongos1, mongos2], () => done()); }); + + server.on('error', done); + setTimeout(function() { + server.connect(); + }, 100); } }); - /* change this to `it.skip` when that is fixed in metamocha - it('Should correctly print warning and error when no mongos proxies in seed list', { + it.skip('Should correctly print warning and error when no mongos proxies in seed list', { metadata: { requires: { generators: true, @@ -140,20 +122,27 @@ describe('Mongos Mixed Seed List (mocks)', function() { test: function(done) { var Mongos = this.configuration.mongo.Mongos, - Logger = this.configuration.mongo.Logger, - ObjectId = this.configuration.mongo.BSON.ObjectId; + Logger = this.configuration.mongo.Logger, + ObjectId = this.configuration.mongo.BSON.ObjectId; // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Default message fields var defaultRSFields = { - 'setName': 'rs', 'setVersion': 1, 'electionId': new ObjectId(), - 'maxBsonObjectSize': 16777216, 'maxMessageSizeBytes': 48000000, - 'maxWriteBatchSize': 1000, 'localTime': new Date(), 'maxWireVersion': 4, - 'minWireVersion': 0, 'ok': 1, 'hosts': ['localhost:32000', 'localhost:32001', 'localhost:32002'], 'arbiters': ['localhost:32002'] + setName: 'rs', + setVersion: 1, + electionId: new ObjectId(), + maxBsonObjectSize: 16777216, + maxMessageSizeBytes: 48000000, + maxWriteBatchSize: 1000, + localTime: new Date(), + maxWireVersion: 4, + minWireVersion: 0, + ok: 1, + hosts: ['localhost:32000', 'localhost:32001', 'localhost:32002'], + arbiters: ['localhost:32002'] }; // Primary server states @@ -161,90 +150,67 @@ describe('Mongos Mixed Seed List (mocks)', function() { // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52002, 'localhost'); - mongos2 = yield mockupdb.createServer(52003, 'localhost'); - - // Mongos - co(function*() { - console.log('running1'); - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ok: 1, n: doc.documents, lastOp: new Date()}); - } + mongos1 = yield mock.createServer(52002, 'localhost'); + mongos2 = yield mock.createServer(52003, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function(err) { }); - // Mongos - co(function*() { - console.log('running2'); - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[1]); - } else if (doc.insert) { - request.reply({ok: 1, n: doc.documents, lastOp: new Date()}); - } + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[1]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function(err) { }); + }); - // Attempt to connect - var server = new Mongos([ - { host: 'localhost', port: 52002 }, - { host: 'localhost', port: 52003 } - ], { + // Attempt to connect + var server = new Mongos( + [{ host: 'localhost', port: 52002 }, { host: 'localhost', port: 52003 }], + { connectionTimeout: 3000, socketTimeout: 1000, haInterval: 1000, localThresholdMS: 500, size: 1 - }); - - // Add event listeners - server.once('connect', function(_server) {}); + } + ); + + var warnings = []; + var logger = Logger.currentLogger(); + Logger.setCurrentLogger(function(msg, state) { + console.log('pushed: ', state); + expect(state.type).to.equal('warn'); + warnings.push(state); + }); - var warnings = []; - var logger = Logger.currentLogger(); - Logger.setCurrentLogger(function(msg, state) { - console.log('pushed: ', state); - expect(state.type).to.equal('warn'); - warnings.push(state); - }); + server.on('error', function() { + Logger.setCurrentLogger(logger); + var errors = [ + 'expected mongos proxy, but found replicaset member mongod for server localhost:52002', + 'expected mongos proxy, but found replicaset member mongod for server localhost:52003', + 'no mongos proxies found in seed list, did you mean to connect to a replicaset' + ]; - server.on('error', function(err) { - Logger.setCurrentLogger(logger); - var errors = [ - 'expected mongos proxy, but found replicaset member mongod for server localhost:52002', - 'expected mongos proxy, but found replicaset member mongod for server localhost:52003', - 'no mongos proxies found in seed list, did you mean to connect to a replicaset' - ]; - - expect(warnings).to.have.length(3); - expect(warnings[0].message).to.be.oneOf(errors); - expect(warnings[1].message).to.be.oneOf(errors); - expect(warnings[2].message).to.be.oneOf(errors); - - running = false; - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - done(); - }); + expect(warnings).to.have.length(3); + expect(warnings[0].message).to.be.oneOf(errors); + expect(warnings[1].message).to.be.oneOf(errors); + expect(warnings[2].message).to.be.oneOf(errors); - setTimeout(function() { server.connect(); }, 100); - }).catch(function(err) { + Promise.all([server.destroy(), mongos1.destroy(), mongos2.destroy()]).then(() => done()); }); + + setTimeout(function() { + server.connect(); + }, 100); } }); - */ }); diff --git a/test/tests/functional/mongos_mocks/multiple_proxies_tests.js b/test/tests/functional/mongos_mocks/multiple_proxies_tests.js index c91bc9a7c..e736f9dba 100644 --- a/test/tests/functional/mongos_mocks/multiple_proxies_tests.js +++ b/test/tests/functional/mongos_mocks/multiple_proxies_tests.js @@ -2,15 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); - -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); -}; + mock = require('../../../mock'); describe('Mongos Multiple Proxies (mocks)', function() { it('Should correctly load-balance the operations', { @@ -27,7 +19,6 @@ describe('Mongos Multiple Proxies (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Default message fields var defaultFields = { @@ -46,80 +37,66 @@ describe('Mongos Multiple Proxies (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(11000, 'localhost'); - mongos2 = yield mockupdb.createServer(11001, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(11000, 'localhost'); + mongos2 = yield mock.createServer(11001, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); + }); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); + }); + }); - // Attempt to connect - var server = new Mongos( - [{ host: 'localhost', port: 11000 }, { host: 'localhost', port: 11001 }], - { - connectionTimeout: 3000, - socketTimeout: 1000, - haInterval: 1000, - localThresholdMS: 500, - size: 1 - } - ); + // Attempt to connect + var server = new Mongos( + [{ host: 'localhost', port: 11000 }, { host: 'localhost', port: 11001 }], + { + connectionTimeout: 3000, + socketTimeout: 1000, + haInterval: 1000, + localThresholdMS: 500, + size: 1 + } + ); - // Add event listeners - server.once('connect', function(_server) { - _server.insert('test.test', [{ created: new Date() }], function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.be.oneOf([11000, 1001]); - global.port = r.connection.port === 11000 ? 11001 : 11000; + var lastPort; - _server.insert('test.test', [{ created: new Date() }], function(_err, _r) { - expect(_err).to.be.null; - expect(_r.connection.port).to.equal(global.port); - global.port = _r.connection.port === 11000 ? 11001 : 11000; + // Add event listeners + server.once('connect', function(_server) { + _server.insert('test.test', [{ created: new Date() }], function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.be.oneOf([11000, 11001]); + lastPort = r.connection.port === 11000 ? 11001 : 11000; - _server.insert('test.test', [{ created: new Date() }], function(__err, __r) { - expect(__err).to.be.null; - expect(__r.connection.port).to.equal(global.port); + _server.insert('test.test', [{ created: new Date() }], function(_err, _r) { + expect(_err).to.be.null; + expect(_r.connection.port).to.equal(lastPort); + lastPort = _r.connection.port === 11000 ? 11001 : 11000; - running = false; - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - done(); - }); + _server.insert('test.test', [{ created: new Date() }], function(__err, __r) { + expect(__err).to.be.null; + expect(__r.connection.port).to.equal(lastPort); + + mock.cleanup([server, mongos1, mongos2], () => done()); }); }); }); + }); - server.on('error', done); - server.connect(); - }).catch(function() {}); + server.on('error', done); + server.connect(); } }); @@ -137,7 +114,6 @@ describe('Mongos Multiple Proxies (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Default message fields var defaultFields = { @@ -156,38 +132,27 @@ describe('Mongos Multiple Proxies (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(11002, 'localhost'); - mongos2 = yield mockupdb.createServer(11003, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(11002, 'localhost'); + mongos2 = yield mock.createServer(11003, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } }); - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - // Delay all the operations by 500 ms - yield timeoutPromise(500); - // Get the document + mongos2.setMessageHandler(request => { + setTimeout(() => { var doc = request.document; if (doc.ismaster) { request.reply(serverIsMaster[0]); } else if (doc.insert) { request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - } + }, 500); }); }); @@ -236,11 +201,7 @@ describe('Mongos Multiple Proxies (mocks)', function() { expect(___err).to.be.null; expect(___r.connection.port).to.equal(11003); - server2.destroy(); - mongos1.destroy(); - mongos2.destroy(); - running = false; - done(); + mock.cleanup([server, server2, mongos1, mongos2], () => done()); }); }); }); diff --git a/test/tests/functional/mongos_mocks/proxy_failover_tests.js b/test/tests/functional/mongos_mocks/proxy_failover_tests.js index aa5437e2f..050796fa0 100644 --- a/test/tests/functional/mongos_mocks/proxy_failover_tests.js +++ b/test/tests/functional/mongos_mocks/proxy_failover_tests.js @@ -2,15 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); - -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); -}; + mock = require('../../../mock'); describe('Mongos Proxy Failover (mocks)', function() { it('Should correctly failover due to proxy going away causing timeout', { @@ -27,7 +19,6 @@ describe('Mongos Proxy Failover (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Default message fields var defaultFields = { @@ -46,40 +37,28 @@ describe('Mongos Proxy Failover (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52007, 'localhost'); - mongos2 = yield mockupdb.createServer(52008, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - mongos1.destroy(); - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(52007, 'localhost'); + mongos2 = yield mock.createServer(52008, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + mongos1.destroy(); + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + }); + + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - }).catch(function() {}); + }); + }); // Attempt to connect var server = new Mongos( @@ -101,11 +80,7 @@ describe('Mongos Proxy Failover (mocks)', function() { if (r) { clearInterval(intervalId); expect(r.connection.port).to.equal(52008); - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - running = false; - done(); + mock.cleanup([server, mongos1, mongos2], () => done()); } }); }, 500); @@ -132,7 +107,6 @@ describe('Mongos Proxy Failover (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Current index for the ismaster var currentStep = 0; @@ -153,42 +127,29 @@ describe('Mongos Proxy Failover (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52009, 'localhost'); - mongos2 = yield mockupdb.createServer(52010, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 0) { - yield timeoutPromise(1600); - request.connection.destroy(); - } else if (doc.insert && currentStep === 1) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(52009, 'localhost'); + mongos2 = yield mock.createServer(52010, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 0) { + setTimeout(() => request.connection.destroy(), 1600); + } else if (doc.insert && currentStep === 1) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + }); + + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - }).catch(function() {}); + }); + }); // Attempt to connect var server = new Mongos( @@ -227,12 +188,7 @@ describe('Mongos Proxy Failover (mocks)', function() { // Do we have both proxies answering if (Object.keys(proxies).length === 2) { clearInterval(intervalId2); - - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - running = false; - done(); + mock.cleanup([server, mongos1, mongos2], () => done()); } }); }, 500); @@ -262,7 +218,6 @@ describe('Mongos Proxy Failover (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Current index for the ismaster var currentStep = 0; @@ -283,45 +238,31 @@ describe('Mongos Proxy Failover (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52011, 'localhost'); - mongos2 = yield mockupdb.createServer(52012, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 0) { - yield timeoutPromise(1600); - request.connection.destroy(); - } else if (doc.insert && currentStep === 1) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(52011, 'localhost'); + mongos2 = yield mock.createServer(52012, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 0) { + setTimeout(() => request.connection.destroy(), 1600); + } else if (doc.insert && currentStep === 1) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 0) { - yield timeoutPromise(1600); - request.connection.destroy(); - } else if (doc.insert && currentStep === 1) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + }); + + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 0) { + setTimeout(() => request.connection.destroy(), 1600); + } else if (doc.insert && currentStep === 1) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - }).catch(function() {}); + }); + }); // Attempt to connect var server = new Mongos( @@ -362,11 +303,7 @@ describe('Mongos Proxy Failover (mocks)', function() { clearInterval(intervalId2); intervalId2 = null; - running = false; - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - done(); + mock.cleanup([server, mongos1, mongos2], () => done()); } }); }, 100); diff --git a/test/tests/functional/mongos_mocks/proxy_read_preference_tests.js b/test/tests/functional/mongos_mocks/proxy_read_preference_tests.js index 07c6ce7ef..10f15fae4 100644 --- a/test/tests/functional/mongos_mocks/proxy_read_preference_tests.js +++ b/test/tests/functional/mongos_mocks/proxy_read_preference_tests.js @@ -2,7 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'); describe('Mongos Proxy Read Preference (mocks)', function() { it('Should correctly set query and readpreference field on wire protocol for 3.2', { @@ -20,7 +20,6 @@ describe('Mongos Proxy Read Preference (mocks)', function() { // Contain mock server var mongos1 = null; - var running = true; // Default message fields var defaultFields = { @@ -41,33 +40,27 @@ describe('Mongos Proxy Read Preference (mocks)', function() { var command = null; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52013, 'localhost'); - - // Mongos - return co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.$query && doc.$readPreference) { - command = doc; - request.reply({ - waitedMS: Long.ZERO, - cursor: { - id: Long.ZERO, - ns: 'test.t', - firstBatch: [] - }, - ok: 1 - }); - } + mongos1 = yield mock.createServer(52013, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.$query && doc.$readPreference) { + command = doc; + request.reply({ + waitedMS: Long.ZERO, + cursor: { + id: Long.ZERO, + ns: 'test.t', + firstBatch: [] + }, + ok: 1 + }); } }); - }).catch(function() {}); + }); // Attempt to connect var server = new Mongos([{ host: 'localhost', port: 52013 }], { @@ -94,10 +87,7 @@ describe('Mongos Proxy Read Preference (mocks)', function() { expect(command).to.have.keys(['$query', '$readPreference']); expect(command.$readPreference.mode).to.equal('secondary'); - server.destroy(); - mongos1.destroy(); - running = false; - done(); + mock.cleanup([server, mongos1], () => done()); }); }); @@ -123,7 +113,6 @@ describe('Mongos Proxy Read Preference (mocks)', function() { // Contain mock server var mongos1 = null; - var running = true; // Default message fields var defaultFields = { @@ -144,33 +133,26 @@ describe('Mongos Proxy Read Preference (mocks)', function() { var command = null; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52014, 'localhost'); - - // Mongos - return co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.$query && doc.$readPreference) { - command = doc; - request.reply({ - waitedMS: Long.ZERO, - cursor: { - id: Long.ZERO, - ns: 'test.t', - firstBatch: [] - }, - ok: 1 - }); - } + mongos1 = yield mock.createServer(52014, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.$query && doc.$readPreference) { + command = doc; + request.reply({ + waitedMS: Long.ZERO, + cursor: { + id: Long.ZERO, + ns: 'test.t', + firstBatch: [] + }, + ok: 1 + }); } }); - }).catch(function() {}); + }); // Attempt to connect var server = new Mongos([{ host: 'localhost', port: 52014 }], { @@ -198,10 +180,7 @@ describe('Mongos Proxy Read Preference (mocks)', function() { expect(command.$readPreference.mode).to.equal('nearest'); expect(command.$readPreference.tags).to.eql([{ db: 'sf' }]); - server.destroy(); - mongos1.destroy(); - running = false; - done(); + mock.cleanup([server, mongos1], () => done()); }); }); @@ -226,7 +205,6 @@ describe('Mongos Proxy Read Preference (mocks)', function() { // Contain mock server var mongos1 = null; - var running = true; // Default message fields var defaultFields = { @@ -247,25 +225,18 @@ describe('Mongos Proxy Read Preference (mocks)', function() { var command = null; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(52015, 'localhost'); - - // Mongos - return co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.$query && doc.$readPreference) { - command = doc; - request.reply([]); - } + mongos1 = yield mock.createServer(52015, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.$query && doc.$readPreference) { + command = doc; + request.reply([]); } }); - }).catch(function() {}); + }); // Attempt to connect var server = new Mongos([{ host: 'localhost', port: 52015 }], { @@ -292,10 +263,7 @@ describe('Mongos Proxy Read Preference (mocks)', function() { expect(command).to.have.keys(['$query', '$readPreference']); expect(command.$readPreference.mode, 'secondary'); - server.destroy(); - mongos1.destroy(); - running = false; - done(); + mock.cleanup([server, mongos1], () => done()); }); }); diff --git a/test/tests/functional/mongos_mocks/single_proxy_connection_tests.js b/test/tests/functional/mongos_mocks/single_proxy_connection_tests.js index 97874b874..6f54dd8dd 100644 --- a/test/tests/functional/mongos_mocks/single_proxy_connection_tests.js +++ b/test/tests/functional/mongos_mocks/single_proxy_connection_tests.js @@ -3,15 +3,7 @@ var expect = require('chai').expect, f = require('util').format, co = require('co'), assign = require('../../../../lib/utils').assign, - mockupdb = require('../../../mock'); - -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); -}; + mock = require('../../../mock'); describe('Mongos Single Proxy Connection (mocks)', function() { it('Should correctly timeout mongos socket operation and then correctly re-execute', { @@ -27,7 +19,6 @@ describe('Mongos Single Proxy Connection (mocks)', function() { // Contain mock server var server = null; - var running = true; // Current index for the ismaster var currentStep = 0; // Primary stop responding @@ -51,44 +42,36 @@ describe('Mongos Single Proxy Connection (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(52017, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); + server = yield mock.createServer(52017, 'localhost'); - // Get the document - var doc = request.document; + server.setMessageHandler(request => { + var doc = request.document; - if (doc.ismaster && currentStep === 0) { - request.reply(serverIsMaster[0]); + if (doc.ismaster && currentStep === 0) { + request.reply(serverIsMaster[0]); + currentStep += 1; + } else if (doc.insert && currentStep === 1) { + // Stop responding to any calls (emulate dropping packets on the floor) + if (stopRespondingPrimary) { currentStep += 1; - } else if (doc.insert && currentStep === 1) { - // Stop responding to any calls (emulate dropping packets on the floor) - if (stopRespondingPrimary) { - currentStep += 1; - stopRespondingPrimary = false; - // Timeout after 1500 ms - yield timeoutPromise(1500); - request.connection.destroy(); - } - } else if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 2) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); + stopRespondingPrimary = false; + setTimeout(() => request.connection.destroy(), 1500); } + } else if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 2) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); + }); // Start dropping the packets setTimeout(function() { stopRespondingPrimary = true; }, 500); - }).catch(function() {}); + }); // Attempt to connect - var _server = new Mongos([{ host: 'localhost', port: 52017 }], { + var mongos = new Mongos([{ host: 'localhost', port: 52017 }], { connectionTimeout: 3000, socketTimeout: 1000, haInterval: 500, @@ -99,24 +82,23 @@ describe('Mongos Single Proxy Connection (mocks)', function() { var finished = false; // Add event listeners - _server.once('connect', function() { + mongos.once('connect', function() { // Run an interval var intervalId = setInterval(function() { - _server.insert('test.test', [{ created: new Date() }], function(err, r) { + mongos.insert('test.test', [{ created: new Date() }], function(err, r) { if (r && !finished) { finished = true; clearInterval(intervalId); expect(r.connection.port).to.equal(52017); - running = false; - server.destroy(); - done(); + + mock.cleanup([mongos, server], () => done()); } }); }, 500); }); - _server.on('error', done); - _server.connect(); + mongos.on('error', done); + mongos.connect(); } }); @@ -135,7 +117,6 @@ describe('Mongos Single Proxy Connection (mocks)', function() { // Contain mock server var server = null; - var running = true; // Default message fields var defaultFields = { @@ -155,20 +136,15 @@ describe('Mongos Single Proxy Connection (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(52018, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); + server = yield mock.createServer(52018, 'localhost'); - // Get the document - var doc = request.document; + server.setMessageHandler(request => { + var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.find) { - yield timeoutPromise(600); + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.find) { + setTimeout(() => { // Reply with first batch request.reply({ cursor: { @@ -178,23 +154,23 @@ describe('Mongos Single Proxy Connection (mocks)', function() { }, ok: 1 }); - } else if (doc.getMore) { - // Reply with first batch - request.reply({ - cursor: { - id: Long.fromNumber(1), - ns: f('%s.cursor1', 'test'), - nextBatch: [{ _id: new ObjectId(), a: 1 }] - }, - ok: 1 - }); - } + }, 600); + } else if (doc.getMore) { + // Reply with first batch + request.reply({ + cursor: { + id: Long.fromNumber(1), + ns: f('%s.cursor1', 'test'), + nextBatch: [{ _id: new ObjectId(), a: 1 }] + }, + ok: 1 + }); } - }).catch(function() {}); - }).catch(function() {}); + }); + }); // Attempt to connect - var _server = new Mongos([{ host: 'localhost', port: 52018 }], { + var mongos = new Mongos([{ host: 'localhost', port: 52018 }], { connectionTimeout: 30000, socketTimeout: 30000, haInterval: 500, @@ -202,9 +178,9 @@ describe('Mongos Single Proxy Connection (mocks)', function() { }); // Add event listeners - _server.once('connect', function() { + mongos.once('connect', function() { // Execute find - var cursor = _server.cursor('test.test', { + var cursor = mongos.cursor('test.test', { find: 'test', query: {}, batchSize: 2 @@ -219,16 +195,14 @@ describe('Mongos Single Proxy Connection (mocks)', function() { expect(_err).to.not.exist; expect(_d).to.exist; - running = false; - server.destroy(); - done(); + mock.cleanup([mongos, server], () => done()); }); }); }); - _server.on('error', done); + mongos.on('error', done); setTimeout(function() { - _server.connect(); + mongos.connect(); }, 100); } }); diff --git a/test/tests/functional/rs_mocks/add_remove_tests.js b/test/tests/functional/rs_mocks/add_remove_tests.js index 36dbde886..b52b01de4 100644 --- a/test/tests/functional/rs_mocks/add_remove_tests.js +++ b/test/tests/functional/rs_mocks/add_remove_tests.js @@ -4,7 +4,8 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Add Remove (mocks)', function() { it('Successfully add a new secondary server to the set', { @@ -24,7 +25,6 @@ describe('ReplSet Add Remove (mocks)', function() { var firstSecondaryServer = null; var secondSecondaryServer = null; var arbiterServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -118,69 +118,43 @@ describe('ReplSet Add Remove (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32003, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32003, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Arbiter state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[currentIsMasterIndex]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -192,7 +166,7 @@ describe('ReplSet Add Remove (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -212,29 +186,18 @@ describe('ReplSet Add Remove (mocks)', function() { expect(secondaries['localhost:32003']).to.not.be.null; expect(arbiters['localhost:32002']).to.not.be.null; - // Finish up the test - running = false; - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - - setTimeout(function() { - expect(Object.keys(Connection.connections()).length).to.equal(0); - Connection.disableConnectionAccounting(); - done(); - }, 3000); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } } }); - server.on('error', function() {}); - server.on('connect', function() { - server.__connected = true; - }); - - // Gives proxies a chance to boot up setTimeout(function() { server.connect(); }, 100); @@ -258,7 +221,6 @@ describe('ReplSet Add Remove (mocks)', function() { var firstSecondaryServer = null; var secondSecondaryServer = null; var arbiterServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -360,69 +322,43 @@ describe('ReplSet Add Remove (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32003, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32003, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(function(request) { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(function(request) { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterIndex]); - } + secondSecondaryServer.setMessageHandler(function(request) { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Arbiter state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[currentIsMasterIndex]); - } + arbiterServer.setMessageHandler(function(request) { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -434,7 +370,7 @@ describe('ReplSet Add Remove (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -471,26 +407,17 @@ describe('ReplSet Add Remove (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 2000); + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer, arbiterServer], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } }); - server.on('error', function() {}); - server.on('connect', function() { - server.__connected = true; - }); - // Gives proxies a chance to boot up setTimeout(function() { server.connect(); @@ -515,7 +442,6 @@ describe('ReplSet Add Remove (mocks)', function() { var firstSecondaryServer = null; var secondSecondaryServer = null; var arbiterServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -645,69 +571,43 @@ describe('ReplSet Add Remove (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32003, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32003, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterIndex]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Arbiter state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[currentIsMasterIndex]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -719,7 +619,7 @@ describe('ReplSet Add Remove (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -756,22 +656,16 @@ describe('ReplSet Add Remove (mocks)', function() { expect(hosts.indexOf('localhost:32002')).to.not.equal(-1); expect(hosts.indexOf('localhost:32003')).to.not.equal(-1); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 3000); - }, 6000); - }, 3000); - - server.on('error', function() {}); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); + }, 1000); + }, 500); server.on('left', function(_type, _server) { if (_type === 'secondary' && _server.name === 'localhost:32003') { @@ -789,10 +683,6 @@ describe('ReplSet Add Remove (mocks)', function() { } }); - server.on('connect', function() { - server.__connected = true; - }); - // Gives proxies a chance to boot up setTimeout(function() { server.connect(); @@ -817,7 +707,6 @@ describe('ReplSet Add Remove (mocks)', function() { var firstSecondaryServer = null; var secondSecondaryServer = null; var arbiterServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -911,69 +800,43 @@ describe('ReplSet Add Remove (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32003, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32003, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Arbiter state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[currentIsMasterIndex]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -985,7 +848,7 @@ describe('ReplSet Add Remove (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -998,18 +861,14 @@ describe('ReplSet Add Remove (mocks)', function() { allservers[description.connectionId] = true; if (allservers['localhost:32003']) { // Finish up the test - running = false; - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 3000); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } }); diff --git a/test/tests/functional/rs_mocks/all_servers_close_tests.js b/test/tests/functional/rs_mocks/all_servers_close_tests.js index bb8a05d6e..cc2374475 100644 --- a/test/tests/functional/rs_mocks/all_servers_close_tests.js +++ b/test/tests/functional/rs_mocks/all_servers_close_tests.js @@ -3,7 +3,8 @@ var expect = require('chai').expect, co = require('co'), assign = require('../../../../lib/utils').assign, Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet All Servers Close (mocks)', function() { it('Successful reconnect when driver loses touch with entire replicaset', { @@ -22,7 +23,6 @@ describe('ReplSet All Servers Close (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; var die = false; @@ -77,120 +77,100 @@ describe('ReplSet All Servers Close (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - if (die) { - request.connection.destroy(); - } else { - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + if (die) { + request.connection.destroy(); + } else { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: 1 }); } } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - if (die) { - request.connection.destroy(); - } else { - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + if (die) { + request.connection.destroy(); + } else { + var doc = request.document; + + if (doc.ismaster) { + request.reply(firstSecondary[0]); } } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - if (die) { - request.connection.destroy(); - } else { - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + if (die) { + request.connection.destroy(); + } else { + var doc = request.document; + + if (doc.ismaster) { + request.reply(arbiter[0]); } } - }).catch(function() { - // console.log(err.stack); }); + }); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 2000, - socketTimeout: 2000, - haInterval: 500, - size: 500 - } - ); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 2000, + socketTimeout: 2000, + haInterval: 100, + size: 500 + } + ); + + server.on('connect', function(_server) { + setTimeout(function() { + die = true; - server.on('connect', function(_server) { setTimeout(function() { - die = true; + die = false; setTimeout(function() { - die = false; - - setTimeout(function() { - _server.command('admin.$cmd', { ismaster: true }, function(err, r) { - expect(r).to.exist; - expect(err).to.be.null; - expect(_server.s.replicaSetState.primary).to.not.be.null; - expect(_server.s.replicaSetState.secondaries).to.have.length(1); - expect(_server.s.replicaSetState.arbiters).to.have.length(1); - - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + _server.command('admin.$cmd', { ismaster: true }, function(err, r) { + expect(r).to.exist; + expect(err).to.be.null; + expect(_server.s.replicaSetState.primary).to.not.be.null; + expect(_server.s.replicaSetState.secondaries).to.have.length(1); + expect(_server.s.replicaSetState.arbiters).to.have.length(1); + + mock.cleanup( + [primaryServer, firstSecondaryServer, arbiterServer, server], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - }); - }, 12000); - }, 2500); - }, 2500); - }); - - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); + } + ); + }); + }, 1500); + }, 1000); + }, 500); }); + + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -212,7 +192,6 @@ describe('ReplSet All Servers Close (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; var die = false; @@ -267,66 +246,47 @@ describe('ReplSet All Servers Close (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(34000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(34001, 'localhost'); - arbiterServer = yield mockupdb.createServer(34002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - if (die) { - request.connection.destroy(); - } else { - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(34000, 'localhost'); + firstSecondaryServer = yield mock.createServer(34001, 'localhost'); + arbiterServer = yield mock.createServer(34002, 'localhost'); + + primaryServer.setMessageHandler(request => { + if (die) { + request.connection.destroy(); + } else { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - if (die) { - request.connection.destroy(); - } else { - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + if (die) { + request.connection.destroy(); + } else { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - if (die) { - request.connection.destroy(); - } else { - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + if (die) { + request.connection.destroy(); + } else { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -338,7 +298,7 @@ describe('ReplSet All Servers Close (mocks)', function() { setName: 'rs', connectionTimeout: 5000, socketTimeout: 5000, - haInterval: 1000, + haInterval: 100, size: 1 } ); @@ -349,7 +309,7 @@ describe('ReplSet All Servers Close (mocks)', function() { var intervalId = setInterval(function() { server.command('admin.$cmd', { ismaster: true }, function() {}); - }, 2000); + }, 500); setTimeout(function() { die = false; @@ -363,21 +323,18 @@ describe('ReplSet All Servers Close (mocks)', function() { expect(server.s.replicaSetState.secondaries).to.have.length(1); expect(server.s.replicaSetState.arbiters).to.have.length(1); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); }); - }, 5000); - }, 25000); - }, 2500); + }, 1500); + }, 1000); + }, 500); }); // Gives proxies a chance to boot up diff --git a/test/tests/functional/rs_mocks/connection_tests.js b/test/tests/functional/rs_mocks/connection_tests.js index 5d857b097..1fead329d 100644 --- a/test/tests/functional/rs_mocks/connection_tests.js +++ b/test/tests/functional/rs_mocks/connection_tests.js @@ -3,7 +3,8 @@ var expect = require('chai').expect, co = require('co'), assign = require('../../../../lib/utils').assign, Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Connection Tests (mocks)', function() { it('Successful connection to replicaset of 1 primary, 1 secondary and 1 arbiter', { @@ -22,7 +23,6 @@ describe('ReplSet Connection Tests (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -76,54 +76,35 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -135,7 +116,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -156,17 +137,10 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([primaryServer, firstSecondaryServer, arbiterServer, server], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); } } }); @@ -200,7 +174,6 @@ describe('ReplSet Connection Tests (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -254,60 +227,41 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet([{ host: 'localhost', port: 32002 }], { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 }); @@ -327,17 +281,14 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } } }); @@ -369,7 +320,6 @@ describe('ReplSet Connection Tests (mocks)', function() { // Contain mock server var primaryServer = null; var firstSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -412,39 +362,27 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -456,7 +394,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -474,15 +412,10 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - server.destroy(); - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([primaryServer, firstSecondaryServer, server], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); } // Joined @@ -492,7 +425,6 @@ describe('ReplSet Connection Tests (mocks)', function() { }); server.on('failed', function() { - // console.log('== failed :: ' + server.name) numberOfEvents = numberOfEvents + 1; if (numberOfEvents === 3) validations(); }); @@ -518,7 +450,6 @@ describe('ReplSet Connection Tests (mocks)', function() { // Contain mock server var firstSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -550,24 +481,19 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -579,27 +505,20 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); - server.on('connect', function() {}); server.on('error', function() { - server.destroy(); - firstSecondaryServer.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([server, firstSecondaryServer], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); }); // Gives proxies a chance to boot up setTimeout(function() { - // console.log('--------------- connect 1') server.connect(); }, 100); } @@ -622,7 +541,6 @@ describe('ReplSet Connection Tests (mocks)', function() { // Contain mock server var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -665,39 +583,27 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -709,7 +615,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1, secondaryOnlyConnectionAllowed: true } @@ -728,16 +634,10 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.be.null; - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([firstSecondaryServer, arbiterServer, server], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); } }); @@ -771,7 +671,6 @@ describe('ReplSet Connection Tests (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -825,54 +724,35 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -884,7 +764,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1, secondaryOnlyConnectionAllowed: true } @@ -906,17 +786,14 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } } }); @@ -949,7 +826,6 @@ describe('ReplSet Connection Tests (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1003,54 +879,35 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -1068,17 +925,10 @@ describe('ReplSet Connection Tests (mocks)', function() { ); server.on('error', function() { - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([primaryServer, firstSecondaryServer, arbiterServer, server], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); }); // Gives proxies a chance to boot up @@ -1103,7 +953,6 @@ describe('ReplSet Connection Tests (mocks)', function() { // Contain mock server var primaryServer = null; var firstSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1146,39 +995,27 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [{ host: 'localhost', port: 32000 }, { host: 'localhost', port: 32001 }], @@ -1186,7 +1023,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 5000, socketTimeout: 10000, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -1203,16 +1040,10 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([primaryServer, firstSecondaryServer, server], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); } } }); @@ -1246,7 +1077,6 @@ describe('ReplSet Connection Tests (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1300,54 +1130,35 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [{ host: '127.0.0.1', port: 32002 }, { host: '127.0.0.1', port: 32001 }], @@ -1355,7 +1166,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -1376,17 +1187,14 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } } }); @@ -1418,7 +1226,6 @@ describe('ReplSet Connection Tests (mocks)', function() { // Contain mock server var primaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1459,45 +1266,33 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; + primaryServer = yield mock.createServer(32000, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet([{ host: 'localhost', port: 32000 }], { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 }); @@ -1510,16 +1305,10 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32000'); - primaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup([primaryServer, arbiterServer, server], spy, () => { Connection.disableConnectionAccounting(); done(); - }, 1000); + }); } } }); @@ -1554,7 +1343,6 @@ describe('ReplSet Connection Tests (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1608,60 +1396,41 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet([{ host: 'localhost', port: 32000 }], { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 }); @@ -1673,17 +1442,13 @@ describe('ReplSet Connection Tests (mocks)', function() { expect(server.__connected).to.be.true; expect(server.__fullsetup).to.be.true; - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - done(); - // server.__c = true; + mock.cleanup([primaryServer, firstSecondaryServer, arbiterServer, server], spy, () => { + Connection.disableConnectionAccounting(); + done(); + }); }); server.on('connect', function() { - // console.log('============= connect') server.__connected = true; }); @@ -1712,7 +1477,6 @@ describe('ReplSet Connection Tests (mocks)', function() { // Contain mock server var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1755,39 +1519,27 @@ describe('ReplSet Connection Tests (mocks)', function() { // Boot the mock co(function*() { - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[0]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[0]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -1799,7 +1551,7 @@ describe('ReplSet Connection Tests (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1, secondaryOnlyConnectionAllowed: true } @@ -1811,11 +1563,10 @@ describe('ReplSet Connection Tests (mocks)', function() { var result = server.lastIsMaster(); expect(result).to.exist; - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - done(); + mock.cleanup([firstSecondaryServer, arbiterServer, server], spy, () => { + Connection.disableConnectionAccounting(); + done(); + }); }); // Gives proxies a chance to boot up diff --git a/test/tests/functional/rs_mocks/failover_tests.js b/test/tests/functional/rs_mocks/failover_tests.js index 90942613a..e0815603d 100644 --- a/test/tests/functional/rs_mocks/failover_tests.js +++ b/test/tests/functional/rs_mocks/failover_tests.js @@ -3,7 +3,8 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Failover (mocks)', function() { it('Successfully failover to new primary', { @@ -23,7 +24,6 @@ describe('ReplSet Failover (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var currentIsMasterIndex = 0; // Election Ids @@ -127,66 +127,47 @@ describe('ReplSet Failover (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (die) { - request.connection.destroy(); - } else { - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (die) { + request.connection.destroy(); + } else { + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (die) { - request.connection.destroy(); - } else { - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (die) { + request.connection.destroy(); + } else { + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (die) { - request.connection.destroy(); - } else { - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterIndex]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (die) { + request.connection.destroy(); + } else { + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterIndex]); } } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -198,7 +179,7 @@ describe('ReplSet Failover (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -236,25 +217,22 @@ describe('ReplSet Failover (mocks)', function() { expect(server.s.replicaSetState.primary).to.not.be.null; expect(server.s.replicaSetState.primary.name).to.equal('localhost:32001'); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - Server.disableServerAccounting(); - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, server], + spy, + () => { + Server.disableServerAccounting(); + Connection.disableConnectionAccounting(); + done(); + } + ); } }); setTimeout(function() { die = false; currentIsMasterIndex = currentIsMasterIndex + 1; - }, 2500); + }, 500); }, 100); }); @@ -283,7 +261,6 @@ describe('ReplSet Failover (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var currentIsMasterIndex = 0; // Election Ids @@ -387,66 +364,47 @@ describe('ReplSet Failover (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (die) { - request.connection.destroy(); - } else { - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (die) { + request.connection.destroy(); + } else { + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (die) { - request.connection.destroy(); - } else { - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (die) { + request.connection.destroy(); + } else { + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (die) { - request.connection.destroy(); - } else { - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterIndex]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (die) { + request.connection.destroy(); + } else { + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterIndex]); } } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -458,7 +416,7 @@ describe('ReplSet Failover (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -474,25 +432,21 @@ describe('ReplSet Failover (mocks)', function() { currentIsMasterIndex = currentIsMasterIndex + 1; server.on('reconnect', function() { - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - Server.disableServerAccounting(); - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, server], + spy, + () => { + Server.disableServerAccounting(); + Connection.disableConnectionAccounting(); + done(); + } + ); }); setTimeout(function() { die = false; currentIsMasterIndex = currentIsMasterIndex + 1; - }, 2500); + }, 500); }, 100); }); diff --git a/test/tests/functional/rs_mocks/maintanance_mode_tests.js b/test/tests/functional/rs_mocks/maintanance_mode_tests.js index 4ea7e0cf9..e291ec07d 100644 --- a/test/tests/functional/rs_mocks/maintanance_mode_tests.js +++ b/test/tests/functional/rs_mocks/maintanance_mode_tests.js @@ -3,7 +3,8 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Maintenance Mode (mocks)', function() { it('Successfully detect server in maintanance mode', { @@ -23,7 +24,6 @@ describe('ReplSet Maintenance Mode (mocks)', function() { var firstSecondaryServer = null; var secondSecondaryServer = null; var arbiterServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -117,69 +117,43 @@ describe('ReplSet Maintenance Mode (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32003, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32003, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterIndex]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Arbiter state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[currentIsMasterIndex]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -191,7 +165,7 @@ describe('ReplSet Maintenance Mode (mocks)', function() { setName: 'rs', connectionTimeout: 3000, socketTimeout: 0, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -221,26 +195,17 @@ describe('ReplSet Maintenance Mode (mocks)', function() { server.on('left', function(_type, _server) { if (_type === 'secondary' && _server.name === 'localhost:32003') { - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 2000); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); } }); - server.on('error', done); - server.on('connect', function() { - server.__connected = true; - }); - // Gives proxies a chance to boot up setTimeout(function() { server.connect(); diff --git a/test/tests/functional/rs_mocks/monitoring_tests.js b/test/tests/functional/rs_mocks/monitoring_tests.js index 9e095a848..edf845277 100644 --- a/test/tests/functional/rs_mocks/monitoring_tests.js +++ b/test/tests/functional/rs_mocks/monitoring_tests.js @@ -3,14 +3,11 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); +var delay = function(timeout) { + return new Promise(resolve => setTimeout(() => resolve(), timeout)); }; describe('ReplSet Monitoring (mocks)', function() { @@ -32,7 +29,6 @@ describe('ReplSet Monitoring (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Current index for the ismaster var currentIsMasterState = 0; @@ -109,23 +105,21 @@ describe('ReplSet Monitoring (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - - // Stop responding to any calls (emulate dropping packets on the floor) - if (stopRespondingPrimary) { - yield timeoutPromise(10000); - continue; - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; - // Get the document - var doc = request.document; + // Stop responding to any calls (emulate dropping packets on the floor) + if (stopRespondingPrimary) { + delay(3000).then(() => handleMessage(doc)); + } else { + handleMessage(doc); + } + + function handleMessage(doc) { if (doc.ismaster && currentIsMasterState === 0) { request.reply(primary[currentIsMasterState]); } else if (doc.insert && currentIsMasterState === 0) { @@ -141,38 +135,28 @@ describe('ReplSet Monitoring (mocks)', function() { } }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterState]); - } else if (doc.insert && currentIsMasterState === 1) { - request.reply({ - ok: 1, - n: doc.documents, - lastOp: new Date(), - electionId: electionIds[currentIsMasterState] - }); - } else if (doc.insert && currentIsMasterState === 0) { - request.reply({ note: 'from execCommand', ok: 0, errmsg: 'not master' }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterState]); + } else if (doc.insert && currentIsMasterState === 1) { + request.reply({ + ok: 1, + n: doc.documents, + lastOp: new Date(), + electionId: electionIds[currentIsMasterState] + }); + } else if (doc.insert && currentIsMasterState === 0) { + request.reply({ note: 'from execCommand', ok: 0, errmsg: 'not master' }); } }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterState]); - } else if (doc.insert && currentIsMasterState === 0) { - request.reply({ note: 'from execCommand', ok: 0, errmsg: 'not master' }); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterState]); + } else if (doc.insert && currentIsMasterState === 0) { + request.reply({ note: 'from execCommand', ok: 0, errmsg: 'not master' }); } }); @@ -180,10 +164,12 @@ describe('ReplSet Monitoring (mocks)', function() { setTimeout(function() { stopRespondingPrimary = true; currentIsMasterState = 1; - }, 5000); + }, 500); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet( [ @@ -195,7 +181,7 @@ describe('ReplSet Monitoring (mocks)', function() { setName: 'rs', connectionTimeout: 5000, socketTimeout: 3000, - haInterval: 2000, + haInterval: 100, size: 1 } ); @@ -216,17 +202,15 @@ describe('ReplSet Monitoring (mocks)', function() { expect(joinedSecondaries).to.eql({ 'localhost:32001': 1, 'localhost:32002': 1 }); // Destroy mock - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); + return; } @@ -273,7 +257,6 @@ describe('ReplSet Monitoring (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Current index for the ismaster var currentIsMasterState = 0; @@ -343,44 +326,28 @@ describe('ReplSet Monitoring (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster && currentIsMasterState == 0) { - request.reply(primary[currentIsMasterState]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster && currentIsMasterState === 0) { + request.reply(primary[currentIsMasterState]); } }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterState]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterState]); } }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterState]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterState]); } }); }); @@ -396,7 +363,7 @@ describe('ReplSet Monitoring (mocks)', function() { setName: 'rs', connectionTimeout: 5000, socketTimeout: 60000, - haInterval: 200, + haInterval: 100, size: 1 } ); @@ -406,16 +373,11 @@ describe('ReplSet Monitoring (mocks)', function() { setTimeout(function() { expect(_server.intervalIds.length).to.be.greaterThan(1); - // Destroy mock - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - expect(_server.intervalIds.length).to.equal(0); - done(); - }, 1000); + mock.cleanup([primaryServer, firstSecondaryServer, secondSecondaryServer, server], () => { + expect(_server.intervalIds.length).to.equal(0); + done(); + }); + }, 100); }); // Gives proxies a chance to boot up diff --git a/test/tests/functional/rs_mocks/no_primary_found_tests.js b/test/tests/functional/rs_mocks/no_primary_found_tests.js index cae306985..d550b0667 100644 --- a/test/tests/functional/rs_mocks/no_primary_found_tests.js +++ b/test/tests/functional/rs_mocks/no_primary_found_tests.js @@ -2,15 +2,8 @@ var assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); - -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); -}; + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet No Primary Found (mocks)', function() { it('Should correctly connect to a replicaset where the arbiter hangs no primary found error', { @@ -30,7 +23,6 @@ describe('ReplSet No Primary Found (mocks)', function() { var firstSecondaryServer = null; var secondSecondaryServer = null; var arbiterServer = null; - var running = true; // Default message fields var defaultFields = { @@ -91,65 +83,47 @@ describe('ReplSet No Primary Found (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - arbiterServer = yield mockupdb.createServer(32003, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[0]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + arbiterServer = yield mock.createServer(32003, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); } }); - // First secondary state machine - co(function*() { - while (running) { - yield timeoutPromise(9000000); - var request = yield firstSecondaryServer.receive(); + firstSecondaryServer.setMessageHandler(request => { + setTimeout(() => { var doc = request.document; - if (doc.ismaster) { request.reply(firstSecondary[0]); } - } + }, 9000000); // never respond? }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); } }); - // Arbiter state machine - co(function*() { - while (running) { - yield timeoutPromise(9000000); - var request = yield arbiterServer.receive(); + arbiterServer.setMessageHandler(request => { + setTimeout(() => { var doc = request.document; - if (doc.ismaster) { request.reply(arbiter[0]); } - } - }).catch(function() { - // console.log(err.stack); + }, 9000000); // never respond? }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet([{ host: 'localhost', port: 32000 }], { setName: 'rs', @@ -162,15 +136,14 @@ describe('ReplSet No Primary Found (mocks)', function() { // Add event listeners server.on('connect', function() { // Destroy mock - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - arbiterServer.destroy(); - server.destroy(); - running = false; - - Connection.disableConnectionAccounting(); - done(); + mock.cleanup( + [primaryServer, firstSecondaryServer, secondSecondaryServer, arbiterServer, server], + spy, + () => { + Connection.disableConnectionAccounting(); + done(); + } + ); }); server.on('error', done); diff --git a/test/tests/functional/rs_mocks/operation_tests.js b/test/tests/functional/rs_mocks/operation_tests.js index 9c80fc208..06ae87ec0 100644 --- a/test/tests/functional/rs_mocks/operation_tests.js +++ b/test/tests/functional/rs_mocks/operation_tests.js @@ -2,7 +2,8 @@ var assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Operations (mocks)', function() { it('Correctly execute count command against replicaset with a single member', { @@ -19,7 +20,6 @@ describe('ReplSet Operations (mocks)', function() { // Contain mock server var primaryServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -50,28 +50,21 @@ describe('ReplSet Operations (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - // console.log('======== doc') - // console.dir(doc) - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } else if (doc.count) { - request.reply({ ok: 1, n: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); + } else if (doc.count) { + request.reply({ ok: 1, n: 1 }); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet([{ host: 'localhost', port: 32000 }], { setName: 'rs', @@ -87,10 +80,7 @@ describe('ReplSet Operations (mocks)', function() { server.on('connect', function(_server) { _server.command('test.test', { count: 'test' }, function() { - primaryServer.destroy(); - _server.destroy(); - running = false; - done(); + mock.cleanup([primaryServer, server], spy, () => done()); }); }); @@ -118,7 +108,6 @@ describe('ReplSet Operations (mocks)', function() { // Contain mock server var primaryServer = null; - var running = true; var currentIsMasterIndex = 0; // Default message fields @@ -149,26 +138,21 @@ describe('ReplSet Operations (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } else if (doc.count) { - request.reply({ ok: 1, n: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); + } else if (doc.count) { + request.reply({ ok: 1, n: 1 }); } - }).catch(function(err) { - console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + // Attempt to connect var server = new ReplSet([{ host: 'localhost', port: 32000 }], { setName: 'rs', @@ -188,10 +172,7 @@ describe('ReplSet Operations (mocks)', function() { { count: 'test' }, { readPreference: ReadPreference.secondaryPreferred }, function() { - primaryServer.destroy(); - _server.destroy(); - running = false; - done(); + mock.cleanup([primaryServer, _server], spy, () => done()); } ); }); diff --git a/test/tests/functional/rs_mocks/primary_loses_network_tests.js b/test/tests/functional/rs_mocks/primary_loses_network_tests.js index 359909982..8dca5ee99 100644 --- a/test/tests/functional/rs_mocks/primary_loses_network_tests.js +++ b/test/tests/functional/rs_mocks/primary_loses_network_tests.js @@ -2,10 +2,11 @@ var assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Primary Loses Network (mocks)', function() { - it('Recover from Primary loosing network connectivity', { + it('Recover from Primary losing network connectivity', { metadata: { requires: { generators: true, @@ -21,7 +22,6 @@ describe('ReplSet Primary Loses Network (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var currentIsMasterIndex = 0; var step = 0; @@ -96,57 +96,37 @@ describe('ReplSet Primary Loses Network (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; + primaryServer.setMessageHandler(request => { + var doc = request.document; + // Fail primary + if (step >= 1) return; - // Fail primary - if (step >= 1) return; - - if (doc.ismaster) { - request.reply(primary[currentIsMasterIndex]); - } + if (doc.ismaster) { + request.reply(primary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[currentIsMasterIndex]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[currentIsMasterIndex]); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[currentIsMasterIndex]); } - }).catch(function() { - // console.log(err.stack); }); }); - Connection.enableConnectionAccounting(); + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); // Attempt to connect var server = new ReplSet( @@ -164,18 +144,22 @@ describe('ReplSet Primary Loses Network (mocks)', function() { } ); + let cleaningUp = false; server.on('error', done); server.on('left', function(_type) { if (_type === 'primary') { server.on('joined', function(__type, __server) { if (__type === 'primary' && __server.name === 'localhost:32002') { - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - - running = false; - __server.destroy(); - done(); + if (cleaningUp) { + return; + } + + cleaningUp = true; + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => done() + ); } }); } diff --git a/test/tests/functional/rs_mocks/read_preferences_tests.js b/test/tests/functional/rs_mocks/read_preferences_tests.js index 74608d5f0..006177239 100644 --- a/test/tests/functional/rs_mocks/read_preferences_tests.js +++ b/test/tests/functional/rs_mocks/read_preferences_tests.js @@ -3,7 +3,8 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), Connection = require('../../../../lib/connection/connection'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'), + ConnectionSpy = require('../shared').ConnectionSpy; describe('ReplSet Read Preferences (mocks)', function() { it('Should correctly connect to a replicaset and select the correct tagged secondary server', { @@ -24,7 +25,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -77,118 +77,98 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } - } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + // Get the document + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 2000, - size: 1 + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - ); - - // Add event listeners - server.on('connect', function(_server) { - // Set up a write - function schedule() { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondary', { loc: 'dc' }) - }, - function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.equal(32002); - - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; + }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 2000, + size: 1 + } + ); + + // Add event listeners + server.on('connect', function(_server) { + // Set up a write + function schedule() { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondary', { loc: 'dc' }) + }, + function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.equal(32002); - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - return; - } - ); - } - - // Schedule an insert - setTimeout(function() { - schedule(); - }, 2000); - }); + } + ); + } + ); + } - // Gives proxies a chance to boot up + // Schedule an insert setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + schedule(); + }, 2000); + }); + + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -210,7 +190,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -263,118 +242,97 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } - } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 2000, - size: 1 + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - ); - - // Add event listeners - server.on('connect', function(_server) { - // Set up a write - function schedule() { - setTimeout(function() { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('primaryPreferred') - }, - function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.equal(32000); + }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 2000, + size: 1 + } + ); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; + // Add event listeners + server.on('connect', function(_server) { + // Set up a write + function schedule() { + setTimeout(function() { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('primaryPreferred') + }, + function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.equal(32000); - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - return; - } - ); - }, 500); - } + } + ); + } + ); + }, 500); + } - // Schedule an insert - schedule(); - }); + // Schedule an insert + schedule(); + }); - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -396,7 +354,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -449,152 +406,131 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; + }); - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 2000, + size: 1 + } + ); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 2000, - size: 1 - } - ); + // Add event listeners + var port = 0; + server.on('connect', function(_server) { + // Set up a write + function schedule() { + setTimeout(function() { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondary') + }, + function(err, r) { + expect(err).to.be.null; + port = r.connection.port; - // Add event listeners - var port = 0; - server.on('connect', function(_server) { - // Set up a write - function schedule() { - setTimeout(function() { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondary') - }, - function(err, r) { - expect(err).to.be.null; - port = r.connection.port; - - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondary') - }, - function(_err, _r) { - expect(_err).to.be.null; - expect(_r.connection.port).to.not.equal(port); - port = _r.connection.port; - - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondary') - }, - function(__err, __r) { - expect(__err).to.be.null; - expect(__r.connection.port).to.not.equal(port); - - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondary') + }, + function(_err, _r) { + expect(_err).to.be.null; + expect(_r.connection.port).to.not.equal(port); + port = _r.connection.port; + + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondary') + }, + function(__err, __r) { + expect(__err).to.be.null; + expect(__r.connection.port).to.not.equal(port); + + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - return; - } - ); - } - ); - } - ); - }, 500); - } + } + ); + } + ); + } + ); + } + ); + }, 500); + } - // Schedule an insert - schedule(); - }); + // Schedule an insert + schedule(); + }); - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -616,7 +552,6 @@ describe('ReplSet Read Preferences (mocks)', function() { // Contain mock server var primaryServer = null; var firstSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -656,142 +591,126 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; + }); - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); + }); + + // mock ops store from node-mongodb-native for handling repl set disconnects + var mockDisconnectHandler = { + add: function(opType, ns, ops, options, callback) { + // Command issued to replSet will fail immediately if !server.isConnected() + return callback(new MongoError({ message: 'no connection available', driver: true })); + }, + execute: function() { + // method needs to be called, so provide a dummy version + return; + }, + flush: function() { + // method needs to be called, so provide a dummy version + return; + } + }; - // mock ops store from node-mongodb-native for handling repl set disconnects - var mockDisconnectHandler = { - add: function(opType, ns, ops, options, callback) { - // Command issued to replSet will fail immediately if !server.isConnected() - return callback(new MongoError({ message: 'no connection available', driver: true })); - }, - execute: function() { - // method needs to be called, so provide a dummy version - return; - }, - flush: function() { - // method needs to be called, so provide a dummy version - return; - } - }; + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { - host: 'localhost', - port: 32000, - socketTimeout: 3000, - connectionTimeout: 3000 - }, - { host: 'localhost', port: 32001 } - ], + // Attempt to connect + var server = new ReplSet( + [ { - setName: 'rs', - // connectionTimeout: 10000, - // socketTimeout: 10000, - haInterval: 10000, - disconnectHandler: mockDisconnectHandler, - size: 1 - } - ); + host: 'localhost', + port: 32000, + socketTimeout: 3000, + connectionTimeout: 3000 + }, + { host: 'localhost', port: 32001 } + ], + { + setName: 'rs', + // connectionTimeout: 10000, + // socketTimeout: 10000, + haInterval: 10000, + disconnectHandler: mockDisconnectHandler, + size: 1 + } + ); - // Add event listeners - server.on('connect', function(_server) { - function schedule() { - setTimeout(function() { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('primaryPreferred') - }, - function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.equal(32000); - primaryServer.destroy(); - - _server.on( - 'left', - function() { - // Perform another find, after primary is gone - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('primaryPreferred') - }, - function(_err, _r) { - expect(_err).to.be.null; - expect(_r.connection.port).to.equal(32001); // reads from secondary while primary down - - firstSecondaryServer.destroy(); - _server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); - return; - } - ); - }, - 2500 - ); - } - ); - }, 500); - } + // Add event listeners + server.on('connect', function(_server) { + function schedule() { + setTimeout(function() { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('primaryPreferred') + }, + function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.equal(32000); + primaryServer; + + _server.on( + 'left', + function() { + // Perform another find, after primary is gone + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('primaryPreferred') + }, + function(_err, _r) { + expect(_err).to.be.null; + expect(_r.connection.port).to.equal(32001); // reads from secondary while primary down + + mock.cleanup([server, primaryServer, firstSecondaryServer], spy, () => { + Connection.disableConnectionAccounting(); + done(); + }); + } + ); + }, + 2500 + ); + } + ); + }, 500); + } - // Schedule a commands - schedule(); - }); + // Schedule a commands + schedule(); + }); - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -813,7 +732,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -866,150 +784,129 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.connection.destroy(); - break; - } - } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.connection.destroy(); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 2000, - size: 1 + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - ); + }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 2000, + size: 1 + } + ); + + // Add event listeners + server.on('connect', function(_server) { + // Set up a write + function schedule() { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('primaryPreferred') + }, + function(err) { + expect(err).to.exist; - // Add event listeners - server.on('connect', function(_server) { - // Set up a write - function schedule() { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('primaryPreferred') - }, - function(err) { - expect(err).to.exist; - - // Let all sockets properly close - process.nextTick(function() { - // Test primaryPreferred - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('primaryPreferred') - }, - function(_err, _r) { - expect(_err).to.be.null; - expect(_r.connection.port).to.not.equal(32000); - - // Test secondaryPreferred - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondaryPreferred') - }, - function(__err, __r) { - expect(__err).to.be.null; - expect(__r.connection.port).to.not.equal(32000); - - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + // Let all sockets properly close + process.nextTick(function() { + // Test primaryPreferred + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('primaryPreferred') + }, + function(_err, _r) { + expect(_err).to.be.null; + expect(_r.connection.port).to.not.equal(32000); + + // Test secondaryPreferred + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondaryPreferred') + }, + function(__err, __r) { + expect(__err).to.be.null; + expect(__r.connection.port).to.not.equal(32000); + + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - } - ); - } - ); - }); - } - ); - } + } + ); + } + ); + } + ); + }); + } + ); + } - // Schedule an insert - schedule(); - }); + // Schedule an insert + schedule(); + }); - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -1031,7 +928,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1084,125 +980,104 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } - } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 1000, - size: 1 + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - ); - - // Add event listeners - server.on('connect', function(_server) { - // Set up a write - function schedule() { - _server.s.replicaSetState.secondaries = _server.s.replicaSetState.secondaries.map( - function(x, i) { - x.lastIsMasterMS = i * 20; - return x; - } - ); - - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('nearest') - }, - function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.be.oneOf([32000, 32001]); + }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 1000, + size: 1 + } + ); + + // Add event listeners + server.on('connect', function(_server) { + // Set up a write + function schedule() { + _server.s.replicaSetState.secondaries = _server.s.replicaSetState.secondaries.map( + function(x, i) { + x.lastIsMasterMS = i * 20; + return x; + } + ); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('nearest') + }, + function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.be.oneOf([32000, 32001]); - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - return; - } - ); - } - - // Schedule an insert - setTimeout(function() { - schedule(); - }, 2000); - }); + } + ); + } + ); + } - // Gives proxies a chance to boot up + // Schedule an insert setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + schedule(); + }, 2000); + }); + + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -1224,7 +1099,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1277,126 +1151,104 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } - } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - // console.log('--------------------------------------------- -2') - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 1000, - size: 1 + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - ); - - // Add event listeners - server.on('connect', function(_server) { - // Set up a write - function schedule() { - _server.s.replicaSetState.secondaries = _server.s.replicaSetState.secondaries.map( - function(x, i) { - x.lastIsMasterMS = i * 20; - return x; - } - ); - - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('nearest', { loc: 'dc' }) - }, - function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.be.oneOf([32001, 32002]); + }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 1000, + size: 1 + } + ); + + // Add event listeners + server.on('connect', function(_server) { + // Set up a write + function schedule() { + _server.s.replicaSetState.secondaries = _server.s.replicaSetState.secondaries.map( + function(x, i) { + x.lastIsMasterMS = i * 20; + return x; + } + ); - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('nearest', { loc: 'dc' }) + }, + function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.be.oneOf([32001, 32002]); - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => { Connection.disableConnectionAccounting(); done(); - }, 1000); - return; - } - ); - } - - // Schedule an insert - setTimeout(function() { - schedule(); - }, 2000); - }); + } + ); + } + ); + } - // Gives proxies a chance to boot up + // Schedule an insert setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + schedule(); + }, 2000); + }); + + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); @@ -1418,7 +1270,6 @@ describe('ReplSet Read Preferences (mocks)', function() { // Contain mock server var primaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1449,75 +1300,66 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() {}); - - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet([{ host: 'localhost', port: 32000 }], { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 2000, - size: 1 }); + }); - // Add event listeners - server.on('connect', function(_server) { - // Set up a write - function schedule() { - setTimeout(function() { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondaryPreferred') - }, - function(err, r) { - expect(err).to.be.null; - expect(r.connection.port).to.equal(32000); - - primaryServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - expect(Object.keys(Connection.connections())).to.have.length(0); - Connection.disableConnectionAccounting(); - done(); - }, 1000); - return; - } - ); - }, 500); - } + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); - // Schedule an insert - schedule(); - }); + // Attempt to connect + var server = new ReplSet([{ host: 'localhost', port: 32000 }], { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 2000, + size: 1 + }); - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + // Add event listeners + server.on('connect', function(_server) { + // Set up a write + function schedule() { + setTimeout(function() { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondaryPreferred') + }, + function(err, r) { + expect(err).to.be.null; + expect(r.connection.port).to.equal(32000); + + mock.cleanup([server, primaryServer], spy, () => { + Connection.disableConnectionAccounting(); + done(); + }); + } + ); + }, 500); + } + + // Schedule an insert + schedule(); + }); + + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } } ); @@ -1540,7 +1382,6 @@ describe('ReplSet Read Preferences (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var secondSecondaryServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -1593,127 +1434,106 @@ describe('ReplSet Read Preferences (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - secondSecondaryServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(primary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + secondSecondaryServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() {}); - - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } - } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield secondSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(secondSecondary[0]); - } else if (doc.count) { - request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - }).catch(function() { - // console.log(err.stack); }); - Connection.enableConnectionAccounting(); - // Attempt to connect - var server = new ReplSet( - [ - { host: 'localhost', port: 32000 }, - { host: 'localhost', port: 32001 }, - { host: 'localhost', port: 32002 } - ], - { - setName: 'rs', - connectionTimeout: 3000, - socketTimeout: 0, - haInterval: 2000, - size: 1 + secondSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(secondSecondary[0]); + } else if (doc.count) { + request.reply({ waitedMS: Long.ZERO, n: 1, ok: 1 }); } - ); - - // Add event listeners - server.on('all', function(_server) { - // Execute more operations than there is servers connected - setTimeout(function() { - var count = 50; - var portsSeen = {}; - - var checkHandler = function(err, r) { - count = count - 1; - expect(err).to.be.null; - - // Add the port to the portsSeen - portsSeen[r.connection.port] = true; - - // Finished up - if (count === 0) { - // Should not contain the primary - expect(portsSeen).to.not.have.key('32000'); - - // Shut down mocks - primaryServer.destroy(); - firstSecondaryServer.destroy(); - secondSecondaryServer.destroy(); - server.destroy(); - running = false; - - setTimeout(function() { - done(); - }, 500); - } - }; + }); + }); + + const spy = new ConnectionSpy(); + Connection.enableConnectionAccounting(spy); + + // Attempt to connect + var server = new ReplSet( + [ + { host: 'localhost', port: 32000 }, + { host: 'localhost', port: 32001 }, + { host: 'localhost', port: 32002 } + ], + { + setName: 'rs', + connectionTimeout: 3000, + socketTimeout: 0, + haInterval: 2000, + size: 1 + } + ); - for (var i = 0; i < 50; i++) { - // Perform a find - _server.command( - 'test.test', - { - count: 'test.test', - batchSize: 2 - }, - { - readPreference: new ReadPreference('secondaryPreferred') - }, - checkHandler + // Add event listeners + server.on('all', function(_server) { + // Execute more operations than there is servers connected + setTimeout(function() { + var count = 50; + var portsSeen = {}; + + var checkHandler = function(err, r) { + count = count - 1; + expect(err).to.be.null; + + // Add the port to the portsSeen + portsSeen[r.connection.port] = true; + + // Finished up + if (count === 0) { + // Should not contain the primary + expect(portsSeen).to.not.have.key('32000'); + + // Shut down mocks + mock.cleanup( + [server, primaryServer, firstSecondaryServer, secondSecondaryServer], + spy, + () => done() ); } - }, 1000); - }); + }; - // Gives proxies a chance to boot up - setTimeout(function() { - server.connect(); - }, 100); - }).catch(function() {}); + for (var i = 0; i < 50; i++) { + // Perform a find + _server.command( + 'test.test', + { + count: 'test.test', + batchSize: 2 + }, + { + readPreference: new ReadPreference('secondaryPreferred') + }, + checkHandler + ); + } + }, 1000); + }); + + // Gives proxies a chance to boot up + setTimeout(function() { + server.connect(); + }, 100); } }); }); diff --git a/test/tests/functional/sdam_monitoring_mocks/mongos_topology_tests.js b/test/tests/functional/sdam_monitoring_mocks/mongos_topology_tests.js index 17011e2b2..bff3373a4 100644 --- a/test/tests/functional/sdam_monitoring_mocks/mongos_topology_tests.js +++ b/test/tests/functional/sdam_monitoring_mocks/mongos_topology_tests.js @@ -2,15 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); - -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); -}; + mock = require('../../../mock'); describe.skip('Mongos SDAM Monitoring (mocks)', function() { it('SDAM Monitoring Should correctly connect to two proxies', { @@ -27,7 +19,6 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Current index for the ismaster var currentStep = 0; @@ -48,38 +39,26 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(62000, 'localhost'); - mongos2 = yield mockupdb.createServer(62001, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 1) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos1 = yield mock.createServer(62000, 'localhost'); + mongos2 = yield mock.createServer(62001, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 1) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + }); + + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } - }).catch(function() {}); + }); }); // Attempt to connect @@ -120,70 +99,68 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { // Do we have both proxies answering if (Object.keys(proxies).length === 2) { clearInterval(intervalId2); - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - - setTimeout(function() { - var results = [ - { - topologyId: _server.s.id, - previousDescription: { - topologyType: 'Sharded', - servers: [] - }, - newDescription: { - topologyType: 'Sharded', - servers: [ - { - type: 'Mongos', - address: 'localhost:62000' - }, - { - type: 'Unknown', - address: 'localhost:62001' - } - ] - } - }, - { - topologyId: _server.s.id, - previousDescription: { - topologyType: 'Sharded', - servers: [ - { - type: 'Mongos', - address: 'localhost:62000' - }, - { - type: 'Unknown', - address: 'localhost:62001' - } - ] + mock.cleanup([server, mongos1, mongos2], () => { + + setTimeout(function() { + var results = [ + { + topologyId: _server.s.id, + previousDescription: { + topologyType: 'Sharded', + servers: [] + }, + newDescription: { + topologyType: 'Sharded', + servers: [ + { + type: 'Mongos', + address: 'localhost:62000' + }, + { + type: 'Unknown', + address: 'localhost:62001' + } + ] + } }, - newDescription: { - topologyType: 'Sharded', - servers: [ - { - type: 'Mongos', - address: 'localhost:62000' - }, - { - type: 'Mongos', - address: 'localhost:62001' - } - ] + { + topologyId: _server.s.id, + previousDescription: { + topologyType: 'Sharded', + servers: [ + { + type: 'Mongos', + address: 'localhost:62000' + }, + { + type: 'Unknown', + address: 'localhost:62001' + } + ] + }, + newDescription: { + topologyType: 'Sharded', + servers: [ + { + type: 'Mongos', + address: 'localhost:62000' + }, + { + type: 'Mongos', + address: 'localhost:62001' + } + ] + } } - } - ]; + ]; - for (var i = 0; i < responses.topologyDescriptionChanged.length; i++) { - expect(results[i]).to.eql(responses.topologyDescriptionChanged[i]); - } + for (var i = 0; i < responses.topologyDescriptionChanged.length; i++) { + expect(results[i]).to.eql(responses.topologyDescriptionChanged[i]); + } - running = false; - done(); - }, 1000); + done(); + }, 1000); + }); } }); }, 500); @@ -255,7 +232,6 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Default message fields var defaultFields = { @@ -274,38 +250,26 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(62002, 'localhost'); - mongos2 = yield mockupdb.createServer(62003, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - mongos1.destroy(); - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - return; - } + mongos1 = yield mock.createServer(62002, 'localhost'); + mongos2 = yield mock.createServer(62003, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + mongos1.destroy(); + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); + return; } }); - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } else if (doc.insert) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); + } else if (doc.insert) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } }); }); @@ -332,78 +296,75 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { // Wait to allow at least one heartbeat to pass setTimeout(function() { expect(r.connection.port).to.equal(62003); - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - - // Wait for a little bit to let all events fire - setTimeout(function() { - expect(responses.serverOpening.length).to.be.at.least(2); - expect(responses.serverClosed.length).to.be.at.least(2); - expect(responses.topologyOpening).to.have.length(1); - expect(responses.topologyClosed).to.have.length(1); - expect(responses.serverHeartbeatStarted.length).to.be.greaterThan(0); - expect(responses.serverHeartbeatSucceeded.length).to.be.greaterThan(0); - expect(responses.serverDescriptionChanged.length).to.be.greaterThan(0); - expect(responses.topologyDescriptionChanged).to.have.length(2); - - var results = [ - { - topologyId: _server.s.id, - previousDescription: { - topologyType: 'Sharded', - servers: [] - }, - newDescription: { - topologyType: 'Sharded', - servers: [ - { - type: 'Mongos', - address: 'localhost:62002' - }, - { - type: 'Unknown', - address: 'localhost:62003' - } - ] - } - }, - { - topologyId: _server.s.id, - previousDescription: { - topologyType: 'Sharded', - servers: [ - { - type: 'Mongos', - address: 'localhost:62002' - }, - { - type: 'Unknown', - address: 'localhost:62003' - } - ] + mock.cleanup([server, mongos1, mongos2], () => { + // Wait for a little bit to let all events fire + setTimeout(function() { + expect(responses.serverOpening.length).to.be.at.least(2); + expect(responses.serverClosed.length).to.be.at.least(2); + expect(responses.topologyOpening).to.have.length(1); + expect(responses.topologyClosed).to.have.length(1); + expect(responses.serverHeartbeatStarted.length).to.be.greaterThan(0); + expect(responses.serverHeartbeatSucceeded.length).to.be.greaterThan(0); + expect(responses.serverDescriptionChanged.length).to.be.greaterThan(0); + expect(responses.topologyDescriptionChanged).to.have.length(2); + + var results = [ + { + topologyId: _server.s.id, + previousDescription: { + topologyType: 'Sharded', + servers: [] + }, + newDescription: { + topologyType: 'Sharded', + servers: [ + { + type: 'Mongos', + address: 'localhost:62002' + }, + { + type: 'Unknown', + address: 'localhost:62003' + } + ] + } }, - newDescription: { - topologyType: 'Sharded', - servers: [ - { - type: 'Mongos', - address: 'localhost:62002' - }, - { - type: 'Mongos', - address: 'localhost:62003' - } - ] + { + topologyId: _server.s.id, + previousDescription: { + topologyType: 'Sharded', + servers: [ + { + type: 'Mongos', + address: 'localhost:62002' + }, + { + type: 'Unknown', + address: 'localhost:62003' + } + ] + }, + newDescription: { + topologyType: 'Sharded', + servers: [ + { + type: 'Mongos', + address: 'localhost:62002' + }, + { + type: 'Mongos', + address: 'localhost:62003' + } + ] + } } - } - ]; - - expect(results).to.eql(responses.topologyDescriptionChanged); - running = false; - done(); - }, 100); - }, 1100); + ]; + + expect(results).to.eql(responses.topologyDescriptionChanged); + done(); + }, 100); + }); + }); } }); }, 500); @@ -470,7 +431,6 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { // Contain mock server var mongos1 = null; var mongos2 = null; - var running = true; // Current index for the ismaster var currentStep = 0; @@ -491,35 +451,22 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { var serverIsMaster = [assign({}, defaultFields)]; // Boot the mock co(function*() { - mongos1 = yield mockupdb.createServer(62004, 'localhost'); - mongos2 = yield mockupdb.createServer(62005, 'localhost'); - - // Mongos - co(function*() { - while (running) { - var request = yield mongos1.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster && currentStep === 0) { - request.reply(serverIsMaster[0]); - } else if (doc.ismaster && currentStep === 1) { - yield timeoutPromise(1600); - request.connection.destroy(); - } + mongos1 = yield mock.createServer(62004, 'localhost'); + mongos2 = yield mock.createServer(62005, 'localhost'); + + mongos1.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster && currentStep === 0) { + request.reply(serverIsMaster[0]); + } else if (doc.ismaster && currentStep === 1) { + setTimeout(() => request.connection.destroy(), 1600); } }); - // Mongos - co(function*() { - while (running) { - var request = yield mongos2.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } + mongos2.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); } }); @@ -532,10 +479,7 @@ describe.skip('Mongos SDAM Monitoring (mocks)', function() { setTimeout(function() { expect(responses.topologyDescriptionChanged.length).to.be.greaterThan(0); - server.destroy(); - mongos1.destroy(); - mongos2.destroy(); - done(); + mock.cleanup([server, mongos1, mongos2], () => done()); }, 2000); }, 2000); }, 2000); diff --git a/test/tests/functional/sdam_monitoring_mocks/replset_topology_tests.js b/test/tests/functional/sdam_monitoring_mocks/replset_topology_tests.js index 181fbaa15..17773a227 100644 --- a/test/tests/functional/sdam_monitoring_mocks/replset_topology_tests.js +++ b/test/tests/functional/sdam_monitoring_mocks/replset_topology_tests.js @@ -2,7 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'); describe.skip('ReplSet SDAM Monitoring (mocks)', function() { it('Successful emit SDAM monitoring events for replicaset', { @@ -21,7 +21,6 @@ describe.skip('ReplSet SDAM Monitoring (mocks)', function() { var primaryServer = null; var firstSecondaryServer = null; var arbiterServer = null; - var running = true; var electionIds = [new ObjectId(), new ObjectId()]; // Default message fields @@ -117,50 +116,29 @@ describe.skip('ReplSet SDAM Monitoring (mocks)', function() { // Boot the mock co(function*() { - primaryServer = yield mockupdb.createServer(32000, 'localhost'); - firstSecondaryServer = yield mockupdb.createServer(32001, 'localhost'); - arbiterServer = yield mockupdb.createServer(32002, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield primaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(primary[step]); - } + primaryServer = yield mock.createServer(32000, 'localhost'); + firstSecondaryServer = yield mock.createServer(32001, 'localhost'); + arbiterServer = yield mock.createServer(32002, 'localhost'); + + primaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(primary[step]); } - }).catch(function() { - // console.log(err.stack); }); - // First secondary state machine - co(function*() { - while (running) { - var request = yield firstSecondaryServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(firstSecondary[step]); - } + firstSecondaryServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(firstSecondary[step]); } - }).catch(function() { - // console.log(err.stack); }); - // Second secondary state machine - co(function*() { - while (running) { - var request = yield arbiterServer.receive(); - var doc = request.document; - - if (doc.ismaster) { - request.reply(arbiter[step]); - } + arbiterServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(arbiter[step]); } - }).catch(function() { - // console.log(err.stack); }); }); @@ -255,15 +233,11 @@ describe.skip('ReplSet SDAM Monitoring (mocks)', function() { console.log(JSON.stringify(expectedResults[i], null, 2)); console.log('----------------------------- got '); console.log(JSON.stringify(responses.topologyDescriptionChanged[i], null, 2)); - process.exit(0); + done(e); } } - running = false; - primaryServer.destroy(); - firstSecondaryServer.destroy(); - arbiterServer.destroy(); - done(); + mock.cleanup([primaryServer, firstSecondaryServer, arbiterServer], () => done()); }, 1000); }, 2000); }); diff --git a/test/tests/functional/sdam_monitoring_mocks/single_topology_tests.js b/test/tests/functional/sdam_monitoring_mocks/single_topology_tests.js index 660e1b0a8..240ffeadf 100644 --- a/test/tests/functional/sdam_monitoring_mocks/single_topology_tests.js +++ b/test/tests/functional/sdam_monitoring_mocks/single_topology_tests.js @@ -2,7 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'); describe.skip('Single SDAM Monitoring (mocks)', function() { it('Should correctly emit sdam monitoring events for single server', { @@ -18,7 +18,6 @@ describe.skip('Single SDAM Monitoring (mocks)', function() { // Contain mock server var server = null; - var running = true; // Default message fields var defaultFields = { @@ -38,20 +37,14 @@ describe.skip('Single SDAM Monitoring (mocks)', function() { // Boot the mock var mockServer; co(function*() { - mockServer = yield mockupdb.createServer(37018, 'localhost'); + mockServer = yield mock.createServer(37018, 'localhost'); - // Primary state machine - co(function*() { - while (running) { - var request = yield mockServer.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } + mockServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); } - }).catch(function() {}); + }); }); // Attempt to connect @@ -151,9 +144,8 @@ describe.skip('Single SDAM Monitoring (mocks)', function() { ] } }).to.eql(flags[5]); - running = false; - mockServer.destroy(); - done(); + + mock.cleanup([mockServer], () => done()); }, 100); }); @@ -171,7 +163,6 @@ describe.skip('Single SDAM Monitoring (mocks)', function() { // Contain mock server var server = null; - var running = true; // Default message fields var defaultFields = { @@ -192,20 +183,14 @@ describe.skip('Single SDAM Monitoring (mocks)', function() { // Boot the mock var mockServer; co(function*() { - mockServer = yield mockupdb.createServer(37008, 'localhost'); + mockServer = yield mock.createServer(37008, 'localhost'); - // Primary state machine - co(function*() { - while (running) { - var request = yield mockServer.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } + mockServer.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster) { + request.reply(serverIsMaster[0]); } - }).catch(function() {}); + }); }); // Attempt to connect @@ -305,8 +290,7 @@ describe.skip('Single SDAM Monitoring (mocks)', function() { } }).to.eql(flags[5]); - mockServer.destroy(); - done(); + mock.cleanup([mockServer], () => done()); }, 100); }); diff --git a/test/tests/functional/server_tests.js b/test/tests/functional/server_tests.js index ae7991cb2..5037ff2a2 100644 --- a/test/tests/functional/server_tests.js +++ b/test/tests/functional/server_tests.js @@ -1001,13 +1001,11 @@ describe('Server tests', function() { ); it( - 'should correctly connect server to single instance and execute insert with snappy compression if supported by the server', + 'should correctly connect server to single instance and execute insert with snappy compression', { - metadata: { requires: { topology: ['single', 'snappyCompression'] } }, + metadata: { requires: { topology: ['single'], mongodb: '>=3.5.x' } }, test: function(done) { - var self = this; - // Attempt to connect to server var server = new Server({ host: this.configuration.host, @@ -1020,23 +1018,13 @@ describe('Server tests', function() { // Add event listeners server.on('connect', function() { - var envShouldSupportCompression = - self.configuration.manager.options.networkMessageCompressors === 'snappy' && - server.ismaster.maxWireVersion >= WIRE_PROTOCOL_COMPRESSION_SUPPORT_MIN_VERSION; - // Check compression has been negotiated - if (envShouldSupportCompression) { - expect(server.s.pool.options.agreedCompressor).to.equal('snappy'); - } + expect(server.s.pool.options.agreedCompressor).to.equal('snappy'); server.insert('integration_tests.inserts', { a: 1 }, function(insertOneErr, insertOneR) { expect(insertOneErr).to.be.null; expect(insertOneR.result.n).to.equal(1); - if (envShouldSupportCompression) { - expect(insertOneR.message.fromCompressed).to.be.true; - } else { - expect(insertOneR.message.fromCompressed).to.not.exist; - } + expect(insertOneR.message.fromCompressed).to.be.true; server.insert('integration_tests.inserts', { a: 2 }, { ordered: false }, function( err, @@ -1044,11 +1032,7 @@ describe('Server tests', function() { ) { expect(err).to.be.null; expect(r.result.n).to.equal(1); - if (envShouldSupportCompression) { - expect(r.message.fromCompressed).to.be.true; - } else { - expect(r.message.fromCompressed).to.not.exist; - } + expect(r.message.fromCompressed).to.be.true; server.destroy(); done(); diff --git a/test/tests/functional/shared.js b/test/tests/functional/shared.js index e3c3ee330..07f9a0587 100644 --- a/test/tests/functional/shared.js +++ b/test/tests/functional/shared.js @@ -1,4 +1,5 @@ 'use strict'; +const EventEmitter = require('events'); function executeCommand(configuration, db, cmd, options, cb) { var Pool = require('../../../lib/connection/pool'), @@ -96,5 +97,46 @@ function locateAuthMethod(configuration, cb) { pool.connect.apply(pool); } +const delay = function(timeout) { + return new Promise(function(resolve) { + setTimeout(function() { + resolve(); + }, timeout); + }); +}; + +class ConnectionSpy extends EventEmitter { + constructor() { + super(); + this.connections = {}; + } + + addConnection(id, connection) { + // console.log(`=== added connection ${id} :: ${connection.port}`); + + this.connections[id] = connection; + this.emit('connectionAdded'); + } + + deleteConnection(id) { + // console.log( + // `=== deleted connection ${id} :: ${this.connections[id] ? this.connections[id].port : ''}` + // ); + + delete this.connections[id]; + this.emit('connectionRemoved'); + + if (this.connectionCount() === 0) { + this.emit('drained'); + } + } + + connectionCount() { + return Object.keys(this.connections).length; + } +} + module.exports.executeCommand = executeCommand; module.exports.locateAuthMethod = locateAuthMethod; +module.exports.delay = delay; +module.exports.ConnectionSpy = ConnectionSpy; diff --git a/test/tests/functional/single_mocks/compression_tests.js b/test/tests/functional/single_mocks/compression_tests.js index 89108fede..8e571a4c6 100644 --- a/test/tests/functional/single_mocks/compression_tests.js +++ b/test/tests/functional/single_mocks/compression_tests.js @@ -2,7 +2,7 @@ var Server = require('../../../../lib/topologies/server'), expect = require('chai').expect, co = require('co'), - mockupdb = require('../../../mock'); + mock = require('../../../mock'); // NOTE: eventually use `this.configuration.mongo.Server` instead of direct import when // https://github.com/malexandert/mongodb-test-runner/issues/3 is fixed @@ -19,7 +19,6 @@ describe('Single Compression (mocks)', function() { test: function(done) { // Contain mock server var server = null; - var running = true; // Prepare the server's response var serverResponse = { @@ -35,18 +34,13 @@ describe('Single Compression (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(37046, 'localhost'); + server = yield mock.createServer(37046, 'localhost'); - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); - expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); - request.reply(serverResponse); - running = false; - } + server.setMessageHandler(request => { + expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); + request.reply(serverResponse); }); - }).catch(done); + }); // Attempt to connect var client = new Server({ @@ -59,9 +53,7 @@ describe('Single Compression (mocks)', function() { }); client.on('connect', function() { - client.destroy(); - running = false; - setTimeout(done, 1000); + mock.cleanup([client, server], () => done()); }); setTimeout(function() { @@ -83,7 +75,6 @@ describe('Single Compression (mocks)', function() { test: function(done) { // Contain mock server var server = null; - var running = true; var currentStep = 0; // Prepare the server's response @@ -100,41 +91,34 @@ describe('Single Compression (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(37047, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); - var doc = request.document; - - if (currentStep === 0) { - expect(request.response.documents[0].compression).to.have.members([ - 'snappy', - 'zlib' - ]); - expect(server.isCompressed).to.be.false; - // Acknowledge connection using OP_COMPRESSED with no compression - request.reply(serverResponse, { compression: { compressor: 'no_compression' } }); - } else if (currentStep === 1) { - expect(server.isCompressed).to.be.false; - // Acknowledge insertion using OP_COMPRESSED with no compression - request.reply( - { ok: 1, n: doc.documents.length, lastOp: new Date() }, - { compression: { compressor: 'no_compression' } } - ); - } else if (currentStep === 2 || currentStep === 3) { - expect(server.isCompressed).to.be.false; - // Acknowledge update using OP_COMPRESSED with no compression - request.reply({ ok: 1, n: 1 }, { compression: { compressor: 'no_compression' } }); - } else if (currentStep === 4) { - expect(server.isCompressed).to.be.false; - request.reply({ ok: 1 }, { compression: { compressor: 'no_compression' } }); - } - currentStep++; + server = yield mock.createServer(37047, 'localhost'); + + server.setMessageHandler(request => { + var doc = request.document; + + if (currentStep === 0) { + expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); + expect(server.isCompressed).to.be.false; + // Acknowledge connection using OP_COMPRESSED with no compression + request.reply(serverResponse, { compression: { compressor: 'no_compression' } }); + } else if (currentStep === 1) { + expect(server.isCompressed).to.be.false; + // Acknowledge insertion using OP_COMPRESSED with no compression + request.reply( + { ok: 1, n: doc.documents.length, lastOp: new Date() }, + { compression: { compressor: 'no_compression' } } + ); + } else if (currentStep === 2 || currentStep === 3) { + expect(server.isCompressed).to.be.false; + // Acknowledge update using OP_COMPRESSED with no compression + request.reply({ ok: 1, n: 1 }, { compression: { compressor: 'no_compression' } }); + } else if (currentStep === 4) { + expect(server.isCompressed).to.be.false; + request.reply({ ok: 1 }, { compression: { compressor: 'no_compression' } }); } + currentStep++; }); - }).catch(done); + }); // Attempt to connect var client = new Server({ @@ -166,11 +150,7 @@ describe('Single Compression (mocks)', function() { expect(___err).to.be.null; expect(___r.result.ok).to.equal(1); - client.destroy(); - setTimeout(function() { - running = false; - done(); - }, 500); + mock.cleanup([client, server], () => done()); }); }); }); @@ -197,7 +177,6 @@ describe('Single Compression (mocks)', function() { test: function(done) { // Contain mock server var server = null; - var running = true; var currentStep = 0; // Prepare the server's response @@ -215,41 +194,33 @@ describe('Single Compression (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(37048, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); - var doc = request.document; - - if (currentStep === 0) { - expect(request.response.documents[0].compression).to.have.members([ - 'snappy', - 'zlib' - ]); - expect(server.isCompressed).to.be.false; - // Acknowledge connection using OP_COMPRESSED with snappy - request.reply(serverResponse, { compression: { compressor: 'snappy' } }); - } else if (currentStep === 1) { - expect(server.isCompressed).to.be.true; - // Acknowledge insertion using OP_COMPRESSED with snappy - request.reply( - { ok: 1, n: doc.documents.length, lastOp: new Date() }, - { compression: { compressor: 'snappy' } } - ); - } else if (currentStep === 2 || currentStep === 3) { - expect(server.isCompressed).to.be.true; - // Acknowledge update using OP_COMPRESSED with snappy - request.reply({ ok: 1, n: 1 }, { compression: { compressor: 'snappy' } }); - } else if (currentStep === 4) { - expect(server.isCompressed).to.be.true; - request.reply({ ok: 1 }, { compression: { compressor: 'snappy' } }); - } - currentStep++; + server = yield mock.createServer(37048, 'localhost'); + + server.setMessageHandler(request => { + var doc = request.document; + if (currentStep === 0) { + expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); + expect(server.isCompressed).to.be.false; + // Acknowledge connection using OP_COMPRESSED with snappy + request.reply(serverResponse, { compression: { compressor: 'snappy' } }); + } else if (currentStep === 1) { + expect(server.isCompressed).to.be.true; + // Acknowledge insertion using OP_COMPRESSED with snappy + request.reply( + { ok: 1, n: doc.documents.length, lastOp: new Date() }, + { compression: { compressor: 'snappy' } } + ); + } else if (currentStep === 2 || currentStep === 3) { + expect(server.isCompressed).to.be.true; + // Acknowledge update using OP_COMPRESSED with snappy + request.reply({ ok: 1, n: 1 }, { compression: { compressor: 'snappy' } }); + } else if (currentStep === 4) { + expect(server.isCompressed).to.be.true; + request.reply({ ok: 1 }, { compression: { compressor: 'snappy' } }); } + currentStep++; }); - }).catch(done); + }); // Attempt to connect var client = new Server({ @@ -281,11 +252,7 @@ describe('Single Compression (mocks)', function() { expect(___err).to.be.null; expect(___r.result.ok).to.equal(1); - client.destroy(); - setTimeout(function() { - running = false; - done(); - }, 500); + mock.cleanup([client, server], () => done()); }); }); }); @@ -312,7 +279,6 @@ describe('Single Compression (mocks)', function() { test: function(done) { // Contain mock server var server = null; - var running = true; var currentStep = 0; // Prepare the server's response @@ -330,41 +296,33 @@ describe('Single Compression (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(37049, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); - var doc = request.document; - - if (currentStep === 0) { - expect(request.response.documents[0].compression).to.have.members([ - 'snappy', - 'zlib' - ]); - expect(server.isCompressed).to.be.false; - // Acknowledge connection using OP_COMPRESSED with zlib - request.reply(serverResponse, { compression: { compressor: 'zlib' } }); - } else if (currentStep === 1) { - expect(server.isCompressed).to.be.true; - // Acknowledge insertion using OP_COMPRESSED with zlib - request.reply( - { ok: 1, n: doc.documents.length, lastOp: new Date() }, - { compression: { compressor: 'zlib' } } - ); - } else if (currentStep === 2 || currentStep === 3) { - // Acknowledge update using OP_COMPRESSED with zlib - expect(server.isCompressed).to.be.true; - request.reply({ ok: 1, n: 1 }, { compression: { compressor: 'zlib' } }); - } else if (currentStep === 4) { - expect(server.isCompressed).to.be.true; - request.reply({ ok: 1 }, { compression: { compressor: 'zlib' } }); - } - currentStep++; + server = yield mock.createServer(37049, 'localhost'); + + server.setMessageHandler(request => { + var doc = request.document; + if (currentStep === 0) { + expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); + expect(server.isCompressed).to.be.false; + // Acknowledge connection using OP_COMPRESSED with zlib + request.reply(serverResponse, { compression: { compressor: 'zlib' } }); + } else if (currentStep === 1) { + expect(server.isCompressed).to.be.true; + // Acknowledge insertion using OP_COMPRESSED with zlib + request.reply( + { ok: 1, n: doc.documents.length, lastOp: new Date() }, + { compression: { compressor: 'zlib' } } + ); + } else if (currentStep === 2 || currentStep === 3) { + // Acknowledge update using OP_COMPRESSED with zlib + expect(server.isCompressed).to.be.true; + request.reply({ ok: 1, n: 1 }, { compression: { compressor: 'zlib' } }); + } else if (currentStep === 4) { + expect(server.isCompressed).to.be.true; + request.reply({ ok: 1 }, { compression: { compressor: 'zlib' } }); } + currentStep++; }); - }).catch(done); + }); // Attempt to connect var client = new Server({ @@ -396,11 +354,7 @@ describe('Single Compression (mocks)', function() { expect(___err).to.be.null; expect(___r.result.ok).to.equal(1); - client.destroy(); - setTimeout(function() { - running = false; - done(); - }, 500); + mock.cleanup([client, server], () => done()); }); }); }); @@ -425,7 +379,6 @@ describe('Single Compression (mocks)', function() { test: function(done) { // Contain mock server var server = null; - var running = true; var currentStep = 0; // Prepare the server's response @@ -443,31 +396,26 @@ describe('Single Compression (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(37050, 'localhost'); + server = yield mock.createServer(37050, 'localhost'); - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); - - if (currentStep === 0) { - expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); - expect(server.isCompressed).to.be.false; - // Acknowledge connection using OP_COMPRESSED with snappy - request.reply(serverResponse, { compression: { compressor: 'snappy' } }); - } else if (currentStep === 1) { - expect(server.isCompressed).to.be.true; - // Acknowledge ping using OP_COMPRESSED with snappy - request.reply({ ok: 1 }, { compression: { compressor: 'snappy' } }); - } else if (currentStep >= 2) { - expect(server.isCompressed).to.be.false; - // Acknowledge further uncompressible commands using OP_COMPRESSED with snappy - request.reply({ ok: 1 }, { compression: { compressor: 'snappy' } }); - } - currentStep++; + server.setMessageHandler(request => { + if (currentStep === 0) { + expect(request.response.documents[0].compression).to.have.members(['snappy', 'zlib']); + expect(server.isCompressed).to.be.false; + // Acknowledge connection using OP_COMPRESSED with snappy + request.reply(serverResponse, { compression: { compressor: 'snappy' } }); + } else if (currentStep === 1) { + expect(server.isCompressed).to.be.true; + // Acknowledge ping using OP_COMPRESSED with snappy + request.reply({ ok: 1 }, { compression: { compressor: 'snappy' } }); + } else if (currentStep >= 2) { + expect(server.isCompressed).to.be.false; + // Acknowledge further uncompressible commands using OP_COMPRESSED with snappy + request.reply({ ok: 1 }, { compression: { compressor: 'snappy' } }); } + currentStep++; }); - }).catch(done); + }); // Attempt to connect var client = new Server({ @@ -497,11 +445,7 @@ describe('Single Compression (mocks)', function() { expect(___err).to.be.null; expect(___r.result.ok).to.equal(1); - client.destroy(); - setTimeout(function() { - running = false; - done(); - }, 500); + mock.cleanup([client, server], () => done()); }); }); }); diff --git a/test/tests/functional/single_mocks/timeout_tests.js b/test/tests/functional/single_mocks/timeout_tests.js index 3897ebe5d..19ad47674 100644 --- a/test/tests/functional/single_mocks/timeout_tests.js +++ b/test/tests/functional/single_mocks/timeout_tests.js @@ -2,15 +2,7 @@ var expect = require('chai').expect, assign = require('../../../../lib/utils').assign, co = require('co'), - mockupdb = require('../../../mock'); - -var timeoutPromise = function(timeout) { - return new Promise(function(resolve) { - setTimeout(function() { - resolve(); - }, timeout); - }); -}; + mock = require('../../../mock'); describe('Single Timeout (mocks)', function() { it('Should correctly timeout socket operation and then correctly re-execute', { @@ -26,7 +18,6 @@ describe('Single Timeout (mocks)', function() { // Contain mock server var server = null; - var running = true; // Current index for the ismaster var currentStep = 0; // Primary stop responding @@ -49,31 +40,26 @@ describe('Single Timeout (mocks)', function() { // Boot the mock co(function*() { - server = yield mockupdb.createServer(37019, 'localhost'); - - // Primary state machine - co(function*() { - while (running) { - var request = yield server.receive(); - - // Get the document - var doc = request.document; - if (doc.ismaster && currentStep === 0) { - request.reply(serverIsMaster[0]); - currentStep += 1; - } else if (doc.insert && currentStep === 1) { - // Stop responding to any calls (emulate dropping packets on the floor) - if (stopRespondingPrimary) { - yield timeoutPromise(3000); - continue; - } - - currentStep += 1; - } else if (doc.ismaster && currentStep === 2) { - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 2) { - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); + server = yield mock.createServer(37019, 'localhost'); + + server.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster && currentStep === 0) { + request.reply(serverIsMaster[0]); + currentStep += 1; + } else if (doc.insert && currentStep === 1) { + // Stop responding to any calls (emulate dropping packets on the floor) + if (stopRespondingPrimary) { + // yield timeoutPromise(3000); + // continue; + return; } + + currentStep += 1; + } else if (doc.ismaster && currentStep === 2) { + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 2) { + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); } }); @@ -108,8 +94,7 @@ describe('Single Timeout (mocks)', function() { finished = true; expect(_r.connection.port).to.equal('37019'); replset.destroy({ force: true }); - running = false; - done(); + mock.cleanup([server], () => done()); } else { wait(); } @@ -141,7 +126,6 @@ describe('Single Timeout (mocks)', function() { // Contain mock server var server = null; - var running = true; // Current index for the ismaster var currentStep = 0; // Should fail due to broken pipe @@ -165,7 +149,7 @@ describe('Single Timeout (mocks)', function() { // Boot the mock var __server; co(function*() { - __server = yield mockupdb.createServer(37017, 'localhost', { + __server = yield mock.createServer(37017, 'localhost', { onRead: function(_server, connection) { // Force EPIPE error if (currentStep === 1) { @@ -181,21 +165,16 @@ describe('Single Timeout (mocks)', function() { } }); - // Primary state machine - co(function*() { - while (running) { - var request = yield __server.receive(); - // Get the document - var doc = request.document; - if (doc.ismaster && currentStep === 0) { - currentStep += 1; - request.reply(serverIsMaster[0]); - } else if (doc.insert && currentStep === 2) { - currentStep += 1; - request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); - } else if (doc.ismaster) { - request.reply(serverIsMaster[0]); - } + __server.setMessageHandler(request => { + var doc = request.document; + if (doc.ismaster && currentStep === 0) { + currentStep += 1; + request.reply(serverIsMaster[0]); + } else if (doc.insert && currentStep === 2) { + currentStep += 1; + request.reply({ ok: 1, n: doc.documents, lastOp: new Date() }); + } else if (doc.ismaster) { + request.reply(serverIsMaster[0]); } }); }); @@ -209,7 +188,6 @@ describe('Single Timeout (mocks)', function() { size: 1 }); - // console.log('!!!! server connect') var docs = []; // Create big insert message for (var i = 0; i < 1000; i++) { @@ -290,9 +268,7 @@ describe('Single Timeout (mocks)', function() { expect(r).to.exist; expect(brokenPipe).to.equal(true); _server.destroy(); - running = false; - __server.destroy(); - done(); + mock.cleanup([__server, _server], () => done()); }); }); @@ -303,101 +279,96 @@ describe('Single Timeout (mocks)', function() { } }); - /* change to `it.skip` when it's supported in metamocha - it('Should not start double reconnect timeouts due to socket timeout during attemptReconnect', { - metadata: { - requires: { - generators: true, - topology: 'single' - } - }, - - test: function(done) { - var Server = this.configuration.mongo.Server; - - // Contain mock server - var server = null; - var running = true; - // Current index for the ismaster - var currentStep = 0; - - // Default message fields - var defaultFields = { - 'ismaster': true, - 'maxBsonObjectSize': 16777216, - 'maxMessageSizeBytes': 48000000, - 'maxWriteBatchSize': 1000, - 'localTime': new Date(), - 'maxWireVersion': 3, - 'minWireVersion': 0, - 'ok': 1 - }; - - // Primary server states - var serverIsMaster = [assign({}, defaultFields)]; - - // Boot the mock - co(function*() { - server = yield mockupdb.createServer(37019, 'localhost'); - - // Primary state machine + it.skip( + 'Should not start double reconnect timeouts due to socket timeout during attemptReconnect', + { + metadata: { + requires: { + generators: true, + topology: 'single' + } + }, + + test: function(done) { + var Server = this.configuration.mongo.Server; + + // Contain mock server + var server = null; + // Current index for the ismaster + var currentStep = 0; + + // Default message fields + var defaultFields = { + ismaster: true, + maxBsonObjectSize: 16777216, + maxMessageSizeBytes: 48000000, + maxWriteBatchSize: 1000, + localTime: new Date(), + maxWireVersion: 3, + minWireVersion: 0, + ok: 1 + }; + + // Primary server states + var serverIsMaster = [assign({}, defaultFields)]; + + // Boot the mock co(function*() { - while (running) { + server = yield mock.createServer(37019, 'localhost'); + + server.setMessageHandler(request => { if (currentStep === 1) { - yield timeoutPromise(5000); - continue; + // yield timeoutPromise(5000); + // continue; + return; } - var request = yield server.receive(); - - // Get the document var doc = request.document; if (doc.ismaster && currentStep === 0) { request.reply(serverIsMaster[0]); currentStep += 1; } - } + }); }); - }); - // Attempt to connect - server = new Server({ - host: 'localhost', - port: 37019, - connectionTimeout: 2000, - socketTimeout: 1000, - size: 1 - }); + // Attempt to connect + server = new Server({ + host: 'localhost', + port: 37019, + connectionTimeout: 2000, + socketTimeout: 1000, + size: 1 + }); - // Add event listeners - server.once('connect', function(_server) { - // _server.insert('test.test', [{created:new Date()}], function(err, r) { - // test.ok(err != null); - // // console.dir(err) - // - // function wait() { - // setTimeout(function() { - // _server.insert('test.test', [{created:new Date()}], function(err, r) { - // if (r && !done) { - // done = true; - // test.equal(37019, r.connection.port); - // replset.destroy(); - // running = false; - // test.done(); - // } else { - // wait(); - // } - // }); - // }, 500); - // } - // - // wait(); - // }); - }); + // Add event listeners + server.once('connect', function() { + // _server.insert('test.test', [{created:new Date()}], function(err, r) { + // test.ok(err != null); + // // console.dir(err) + // + // function wait() { + // setTimeout(function() { + // _server.insert('test.test', [{created:new Date()}], function(err, r) { + // if (r && !done) { + // done = true; + // test.equal(37019, r.connection.port); + // replset.destroy(); + // running = false; + // test.done(); + // } else { + // wait(); + // } + // }); + // }, 500); + // } + // + // wait(); + // }); + }); - server.on('error', done); - server.connect(); + server.on('error', done); + server.connect(); + } } - }); - */ + ); });