From 6dd6db3318af3437a7e87b1999edf981168b9363 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Fri, 15 Sep 2017 14:55:50 -0400 Subject: [PATCH] feat(connection-spy): add class for monitoring active connections This gives us more control over the process of monitoring active and removed connections in a given topology. This also allows for an event-driven approach to cleanup, whereas in the past we have depended upon timeouts. NODE-1132 --- test/tests/functional/shared.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/tests/functional/shared.js b/test/tests/functional/shared.js index c2805dde3..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'), @@ -104,6 +105,38 @@ const delay = function(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;