UNPKG

dl

Version:

DreamLab Libs

207 lines (177 loc) 7.77 kB
var rewire = require('rewire'); var modulePath = '../../lib/client/PGCredentialPool.js'; var EventEmitter = require('events').EventEmitter; describe('PGCredentialPool', function () { describe('require', function () { it ('should be truthy', function () { var PGCredentialPool = require(modulePath).PGCredentialPool; expect(PGCredentialPool).toBeTruthy(); }); it('should not throw exception ',function () { expect(function () { var PGCredentialPool = require(modulePath).PGCredentialPool; }).not.toThrow(); }); }); describe('methods', function () { var PGCredentialPoolMock; var PGCredentialPool; var credentialProviderMock = { addEventListener: function () {} }; beforeEach(function () { PGCredentialPoolMock = rewire(modulePath); PGCredentialPool = PGCredentialPoolMock.PGCredentialPool; }); describe('_dropConnections', function () { var ClientMock = function (config) { EventEmitter.call(this); }; ClientMock.prototype = Object.create(EventEmitter.prototype); ClientMock.prototype.connect = function () {}; ClientMock.prototype.end = function () {}; var staleClients; var stalePort; var staleHost; var staleHosts; beforeEach(function () { this.instance = new PGCredentialPool(credentialProviderMock); spyOn(this.instance, 'scheduleRenew'); spyOn(this.instance, '_removeClient').andCallThrough(); }); describe('when clients to drop', function () { beforeEach(function () { staleClients = []; stalePort = 0; staleHost = 'localhost' staleHosts = {}; staleHosts[[staleHost, stalePort].join(':')] = { host: staleHost, port: stalePort }; var client; for (var i = 0; i < 10; i++) { client = new ClientMock(); client.host = 'localhost'; client.port = i % 3; if (client.host == staleHost && client.port == stalePort) staleClients.push(client); this.instance.add(client); } this.instance._dropConnections(staleHosts); }); it('should remove stale clients', function () { expect(this.instance._removeClient).toHaveBeenCalled(); expect(this.instance._removeClient.callCount).toEqual(staleClients.length); }); it('should leave good clients', function () { expect(this.instance.getKnownLength()).toEqual(6); }); it('should remove clients connected to localhost:0', function () { var client; for (var i = 0, len = this.instance._removeClient.callCount; i < len; i++) { client = this.instance._removeClient.argsForCall[i][0]; expect(staleClients).toContain(client); expect(client.host).toEqual(staleHost); expect(client.port).toEqual(stalePort); } }); }); describe('when no clients to drop', function () { beforeEach(function () { staleClients = []; stalePort = 666; staleHost = 'localhost' staleHosts = {}; staleHosts[[staleHost, stalePort].join(':')] = { host: staleHost, port: stalePort }; var client; for (var i = 0; i < 10; i++) { client = new ClientMock(); client.host = 'localhost'; client.port = i % 3; if (client.host == staleHost && client.port == stalePort) staleClients.push(client); this.instance.add(client); } this.instance._dropConnections(staleHosts); }); it('should not remove clients', function () { expect(this.instance._removeClient).not.toHaveBeenCalled(); }); it('should leave all clients', function () { expect(this.instance.getKnownLength()).toEqual(10); }); }); }); describe('mergeHosts', function () { beforeEach(function () { this.instance = new PGCredentialPool(credentialProviderMock); this.instance._hosts = { 'localhost:0': { host: 'localhost', port: 0 }, 'localhost:1': { host: 'localhost', port: 1 } } }); describe('when host added', function () { beforeEach(function () { this.newHosts = [{ host: 'localhost', port: 0 }, { host: 'localhost', port: 1 }, { host: 'localhost', port: 2 }]; }); it('should return object', function () { expect(this.instance.mergeHosts(this.newHosts)).toBeDefined(); }); it('should return empty object', function () { expect(Object.keys(this.instance.mergeHosts(this.newHosts)).length).toEqual(0); }); }); describe('when no changes', function () { beforeEach(function () { this.newHosts = [{ host: 'localhost', port: 0 }, { host: 'localhost', port: 1 }]; }); it('should return object', function () { expect(this.instance.mergeHosts(this.newHosts)).toBeDefined(); }); it('should return empty object', function () { expect(Object.keys(this.instance.mergeHosts(this.newHosts)).length).toEqual(0); }); }); describe('when host removed', function () { beforeEach(function () { this.newHosts = [{ host: 'localhost', port: 0 }]; }); it('should return object', function () { expect(this.instance.mergeHosts(this.newHosts)).toBeDefined(); }); it('should return object with 1 value', function () { expect(Object.keys(this.instance.mergeHosts(this.newHosts)).length).toEqual(1); }); it('should return removed hosts object', function () { expect(this.instance.mergeHosts(this.newHosts).hasOwnProperty('localhost:1')).toBeTruthy(); }); }); }); }); });