UNPKG

dl

Version:

DreamLab Libs

271 lines (236 loc) 9.23 kB
var MySQLCredentialPool; describe('MySQLCredentialPool', function () { var CredentialsManager = require('../../lib/credentials/Manager.js').CredentialsManager; it('require', function () { expect(function () { MySQLCredentialPool = require('../../lib/client/MySQLCredentialPool.js').MySQLCredentialPool; }).not.toThrow(); expect(MySQLCredentialPool).toBeTruthy(); }); it('create clients', function () { var mysql = require('mysql'); var loadedCb = []; var connected = 0; var pool; spyOn(mysql, 'createConnection'); var credentialMock = { 'addEventListener': jasmine.createSpy(), 'renewCredential': jasmine.createSpy(), 'getCredential': jasmine.createSpy(), 'removeAllEventListeners': jasmine.createSpy() }; credentialMock.removeAllEventListeners.andCallFake(function (type) { if (type == CredentialsManager.Event.LOADED) { loadedCb = []; } else if (type == CredentialsManager.Event.ERROR) { } }); credentialMock.addEventListener.andCallFake(function() { if (arguments[0] == CredentialsManager.Event.LOADED) { loadedCb.push([arguments[3], arguments[2]]); } }); credentialMock.getCredential.andCallFake(function () { var data = { 'data': { 'hosts': [ { 'host': 'localhost', 'port': 3306 }, { 'host': 'localhost', 'port': 3306 } ], 'user': 'root', 'pass': 'rootpwd', 'db_name': 'test' } }; for (var i = 0; i < loadedCb.length; i++) { var context = loadedCb[i][0]; var cb = loadedCb[i][1]; cb.call(context, data); } }); credentialMock.renewCredential.andCallFake(function () { var data = { 'data': { 'hosts': [ { 'host': 'localhost', 'port': 3306 }, { 'host': 'localhost', 'port': 3306 } ], 'user': 'root', 'pass': 'rootpwd', 'db_name': 'test' } }; for (var i = 0; i < loadedCb.length; i++) { var context = loadedCb[i][0]; var cb = loadedCb[i][1]; cb.call(context, data); } }); mysql.createConnection.andCallFake(function () { connected++; return { 'ping': function (cb) { cb(); }, 'connect': function (cb) { cb(null); }, 'on': function () { }, 'query': function () { } }; }); spyOn(CredentialsManager, 'factory').andReturn(credentialMock); runs(function () { pool = new MySQLCredentialPool(); pool.connect(); }); waitsFor(function () { return connected > 0; }); runs(function () { expect(CredentialsManager.factory).toHaveBeenCalled(); expect(mysql.createConnection).toHaveBeenCalledWith({ user : 'root', password : 'rootpwd', host : 'localhost', port : 3306, database : 'test' }); }); runs(function () { pool.destroy(); }); }); it('reconnections', function () { var mysql = require('mysql'); var connected = 0; var loadedCb = []; var closeCb = []; var id = 0; var connections = 2; var initial_connections = connections; runs(function () { spyOn(mysql, 'createConnection'); mysql.createConnection.andCallFake(function () { return { 'connect': function (cb) { connected++; cb(null); }, 'on': function (e, cb) { if (e == 'close') { closeCb.push(cb); } }, 'query': function () { }, 'destroy': function () { connected--; }, 'id': id++ } }); var credentialMock = { 'addEventListener': jasmine.createSpy(), 'getCredential': jasmine.createSpy(), 'renewCredential': jasmine.createSpy(), 'removeAllEventListeners': jasmine.createSpy() }; credentialMock.removeAllEventListeners.andCallFake(function (type) { if (type == CredentialsManager.Event.LOADED) { loadedCb = []; } else if (type == CredentialsManager.Event.ERROR) { } }); credentialMock.addEventListener.andCallFake(function() { if (arguments[0] == CredentialsManager.Event.LOADED) { loadedCb.push([arguments[3], arguments[2]]); } }); credentialMock.renewCredential.andCallFake(function () { var data = { 'data': { 'hosts': [ { 'host': 'localhost', 'port': 3306 }, { 'host': 'localhost', 'port': 3306 } ], 'user': 'root', 'pass': 'rootpwd', 'db_name': 'test', 'max_connections': connections } }; for (var i = 0; i < loadedCb.length; i++) { var context = loadedCb[i][0]; var cb = loadedCb[i][1]; cb.call(context, data); } }); credentialMock.getCredential.andCallFake(function () { var data = { 'data': { 'hosts': [ { 'host': 'localhost', 'port': 3306 }, { 'host': 'localhost', 'port': 3306 } ], 'user': 'root', 'pass': 'rootpwd', 'db_name': 'test', 'max_connections': connections } }; for (var i = 0; i < loadedCb.length; i++) { var context = loadedCb[i][0]; var cb = loadedCb[i][1]; cb.call(context, data); } }); spyOn(CredentialsManager, 'factory').andReturn(credentialMock); }); runs(function () { pool = new MySQLCredentialPool(); pool.connect(); }); waitsFor(function () { return connected > 0; }); runs(function () { expect(CredentialsManager.factory).toHaveBeenCalled(); expect(mysql.createConnection).toHaveBeenCalledWith({ user : 'root', password : 'rootpwd', host : 'localhost', port : 3306, database : 'test' }); expect(connected).toEqual(2);; }); runs(function () { connections = 1; for (var i=0; i<initial_connections; i++) { closeCb = closeCb.splice(0, 1); closeCb[0](); } }) runs(function () { expect(connected).toEqual(connections); }) runs(function () { pool.destroy(); }); }); });