UNPKG

core

Version:

Core Libs

472 lines (413 loc) 15.8 kB
/* global it, expect, runs, jasmine, waitsFor, describe, spyOn, beforeEach, afterEach */ var rewire = require('rewire'); //var redis = require('redis'); var CredentialsProvider = require('../../lib/credentials/CredentialsProvider.js').CredentialsProvider; var modulePath = './../../lib/client/Redis.js'; describe('Redis', function () { describe('should be truthy', function () { beforeEach(function() { jasmine.Clock.useMock(); jasmine.Clock.tick(1000); }); it('should require', function () { var Redis = require(modulePath).Redis; expect(Redis).toBeTruthy(); }); it('should not throw', function () { expect(function () { require(modulePath); }).not.toThrow(); }); }); describe('redis connection timeout', function() { var RedisMock = null; var Redis = null; var mockClient = null; var redis; beforeEach(function() { jasmine.Clock.useMock(); console.log(); RedisMock = rewire(modulePath); /* module */ Redis = RedisMock.Redis; /* client */ redis = RedisMock.__get__('redis'); //console.log('r', redis); mockClient = jasmine.createSpy(); mockClient.on = jasmine.createSpy('on'); mockClient.on.andCallFake(function (ev, cb) { if (ev === 'ready') { mockClient.ready = true; cb(); } }); mockClient.ping = jasmine.createSpy('ping'); mockClient.quit = jasmine.createSpy('quit'); spyOn(redis, 'createClient'); redis.createClient.andCallFake(function(host, port, options) { mockClient.options = options; return mockClient; }); }); afterEach(function() { jasmine.Clock.reset(); }); it ('should run ping command every defined timeout', function() { var options = { 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }], 'hbInterval': 1000, 'hbTimeout': 1000 }; var r1 = new Redis(new CredentialsProvider(options)); jasmine.Clock.tick(options.hbInterval + 100); expect(mockClient.ping).toHaveBeenCalled(); r1.destroy(); }) it ('should run disconnect client on ping command error', function() { var options = { 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }], 'hbInterval': 1000, 'hbTimeout': 1000 }; var r2 = new Redis(new CredentialsProvider(options)); jasmine.Clock.tick(options.hbInterval); expect(mockClient.ping).toHaveBeenCalled(); jasmine.Clock.tick(options.hbTimeout); expect(mockClient.quit).toHaveBeenCalledWith(jasmine.any(Function)); r2.destroy(); }); }); describe('methods', function () { var RedisMock = null; var Redis = null; var mockClient = null; var redis; beforeEach(function () { jasmine.Clock.useMock(); RedisMock = rewire(modulePath); /* module */ Redis = RedisMock.Redis; /* client */ redis = RedisMock.__get__('redis'); Redis.prototype._heartbeat = jasmine.createSpy('_hb') //console.log('r', redis); mockClient = jasmine.createSpy(); mockClient.on = jasmine.createSpy(); mockClient.on.andCallFake(function (ev, cb) { if (ev === 'ready') { cb(); } }); spyOn(redis, 'createClient'); redis.createClient.andReturn(mockClient); }); it('multiton', function () { var cp = new CredentialsProvider({ 'hosts': [] }); var r1 = new Redis(cp); var r2 = new Redis(cp); expect(r1).toEqual(r2); r1.destroy(); r2.destroy(); }); it('num connections', function () { var cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); var r1 = new Redis(cp); expect(redis.createClient).toHaveBeenCalled(); expect(mockClient.on).toHaveBeenCalled(); r1.destroy(); }); it('set', function () { var called = null, called2 = null, r1, testKey = 'a', testVal = 'b', mockCb; runs(function () { mockClient.set = jasmine.createSpy(); mockClient.set.andCallFake(function (key, val, cb) { called = true; cb(null, null); }); var cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); r1 = new Redis(cp); mockCb = jasmine.createSpy(); mockCb.andCallFake(function () { called2 = true; }); r1.set(testKey, testVal, mockCb); }); waitsFor(function () { return (called != null && called2 != null); }); runs(function () { expect(called).toBeTruthy(); expect(called2).toBeTruthy(); expect(mockClient.set).toHaveBeenCalled(); expect(mockClient.set.mostRecentCall.args[0]).toEqual(testKey); expect(mockClient.set.mostRecentCall.args[1]).toEqual(testVal); expect(mockCb).toHaveBeenCalledWith(null, null); r1.destroy(); }); }); it('get', function () { var called = null, called2 = null, r1, testKey = 'a', testVal = 'b', mockCb; runs(function () { mockClient.get = jasmine.createSpy(); mockClient.get.andCallFake(function (key, cb) { called = true; cb(null, testVal); }); var cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); r1 = new Redis(cp); mockCb = jasmine.createSpy(); mockCb.andCallFake(function () { called2 = true; }); r1.get(testKey, mockCb); }); waitsFor(function () { return (called != null && called2 != null); }); runs(function () { expect(called).toBeTruthy(); expect(called2).toBeTruthy(); expect(mockClient.get).toHaveBeenCalled(); expect(mockClient.get.mostRecentCall.args[0]).toEqual(testKey); expect(mockCb).toHaveBeenCalledWith(null, testVal); r1.destroy(); }); }); it('set', function () { var called = null, called2 = null, mockCb, cp, r1, testKey = 'a', testVal = 'b'; runs(function () { mockClient.set = jasmine.createSpy(); mockClient.set.andCallFake(function (key, value, cb) { called = true; cb(null, null); }); cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); r1 = new Redis(cp); mockCb = jasmine.createSpy(); mockCb.andCallFake(function () { called2 = true; }); r1.set(testKey, testVal, mockCb); }); waitsFor(function () { return (called != null && called2 != null); }); runs(function () { expect(called).toBeTruthy(); expect(called2).toBeTruthy(); expect(mockClient.set).toHaveBeenCalled(); expect(mockClient.set.mostRecentCall.args[0]).toEqual(testKey); expect(mockClient.set.mostRecentCall.args[1]).toEqual(testVal); expect(mockCb).toHaveBeenCalledWith(null, null); r1.destroy(); }); }); it('hmget', function () { var called = null, called2 = null, mockCb, cp, r1, testKey = 'a', testField = 'b', testVal = 'c'; runs(function () { mockClient.hmget = jasmine.createSpy(); mockClient.hmget.andCallFake(function (key, field, cb) { called = true; cb(null, testVal); }); cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); r1 = new Redis(cp); mockCb = jasmine.createSpy(); mockCb.andCallFake(function () { called2 = true; }); r1.hmget(testKey, testField, mockCb); }); waitsFor(function () { return (called != null && called2 != null); }); runs(function () { expect(called).toBeTruthy(); expect(called2).toBeTruthy(); expect(mockClient.hmget).toHaveBeenCalled(); expect(mockClient.hmget.mostRecentCall.args[0]).toEqual(testKey); expect(mockClient.hmget.mostRecentCall.args[1]).toEqual(testField); expect(mockCb).toHaveBeenCalledWith(null, testVal); r1.destroy(); }); }); it('hgetall', function () { var called = null, called2 = null, mockCb, cp, r1, testKey = 'a', testVal = 'c'; runs(function () { mockClient.hgetall = jasmine.createSpy(); mockClient.hgetall.andCallFake(function (key, cb) { called = true; cb(null, testVal); }); cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); r1 = new Redis(cp); mockCb = jasmine.createSpy(); mockCb.andCallFake(function () { called2 = true; }); r1.hgetall(testKey, mockCb); }); waitsFor(function () { return (called != null && called2 != null); }); runs(function () { expect(called).toBeTruthy(); expect(called2).toBeTruthy(); expect(mockClient.hgetall).toHaveBeenCalled(); expect(mockClient.hgetall.mostRecentCall.args[0]).toEqual(testKey); expect(mockCb).toHaveBeenCalledWith(null, testVal); r1.destroy(); }); }); it('del', function () { var called = null, called2 = null, testKey = 'a', cp, r1, mockCb; runs(function () { mockClient.del = jasmine.createSpy(); mockClient.del.andCallFake(function (key, cb) { called = true; cb(null, null); }); cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); r1 = new Redis(cp); mockCb = jasmine.createSpy(); mockCb.andCallFake(function () { called2 = true; }); r1.del(testKey, mockCb); }); waitsFor(function () { return (called != null && called2 != null); }); runs(function () { expect(called).toBeTruthy(); expect(called2).toBeTruthy(); expect(mockClient.del).toHaveBeenCalled(); expect(mockClient.del.mostRecentCall.args[0]).toEqual(testKey); expect(mockCb).toHaveBeenCalledWith(null, null); r1.destroy(); }); }); describe('when command error', function () { beforeEach(function () { var cp = new CredentialsProvider({ 'connections': 2, 'hosts': [{ 'host': 'localhost', 'port': 1234 }] }); this.redis = new Redis(cp); this.callback = jasmine.createSpy('set_callback'); mockClient.set = jasmine.createSpy(); mockClient.set.andCallFake(function (key, data, callback) { callback('error', null); }); }); afterEach(function () { this.redis.destroy(); }); describe('callback', function () { beforeEach(function () { this.redis.set('key', 'data', this.callback); }); it('should be called', function () { expect(this.callback).toHaveBeenCalled(); }); it('should be called with error', function () { expect(this.callback).toHaveBeenCalledWith('error', null); }); }); describe('console.warn', function () { beforeEach(function () { this.consoleWarnSpy = jasmine.createSpy('consoleWarnSpy'); var consoleMock = Object.create(RedisMock.__get__('console')); consoleMock.warn = this.consoleWarnSpy; RedisMock.__set__({ console: consoleMock }); this.redis.set('key', 'data', this.callback); }); it('should be called', function () { expect(this.consoleWarnSpy).toHaveBeenCalled(); }); it('should be called with params', function () { expect(this.consoleWarnSpy).toHaveBeenCalledWith( 'Redis/Error cmd=%s error="%s"', 'set', 'error'); }); }); }); }); });