From 822f76a88327d4a05c7afe1f14e72280fd0c4ba9 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Mon, 19 Dec 2016 15:12:22 +0000 Subject: [PATCH] Add unit tests for RedisWrapper --- .../coffee/infrastructure/RedisWrapper.coffee | 2 +- .../infrastructure/RedisWrapperTests.coffee | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 services/web/test/UnitTests/coffee/infrastructure/RedisWrapperTests.coffee diff --git a/services/web/app/coffee/infrastructure/RedisWrapper.coffee b/services/web/app/coffee/infrastructure/RedisWrapper.coffee index ae77918d2f..5d8b5836b5 100644 --- a/services/web/app/coffee/infrastructure/RedisWrapper.coffee +++ b/services/web/app/coffee/infrastructure/RedisWrapper.coffee @@ -21,7 +21,7 @@ module.exports = Redis = if redisFeatureSettings?.cluster? logger.log {feature}, "creating redis-cluster client" rclient = new ioredis.Cluster(redisFeatureSettings.cluster) - rclient._is_redis_cluster = true + rclient.__is_redis_cluster = true else logger.log {feature}, "creating redis client" rclient = redis.createClient(redisFeatureSettings) diff --git a/services/web/test/UnitTests/coffee/infrastructure/RedisWrapperTests.coffee b/services/web/test/UnitTests/coffee/infrastructure/RedisWrapperTests.coffee new file mode 100644 index 0000000000..83ea202dcd --- /dev/null +++ b/services/web/test/UnitTests/coffee/infrastructure/RedisWrapperTests.coffee @@ -0,0 +1,66 @@ +assert = require("chai").assert +sinon = require('sinon') +chai = require('chai') +should = chai.should() +expect = chai.expect +modulePath = "../../../../app/js/infrastructure/RedisWrapper.js" +SandboxedModule = require('sandboxed-module') + +describe 'RedisWrapper', -> + + beforeEach -> + @featureName = 'somefeature' + @settings = + redis: + web: + port:"1234" + host:"somewhere" + password: "password" + somefeature: {} + @normalRedisInstance = + thisIsANormalRedisInstance: true + n: 1 + @clusterRedisInstance = + thisIsAClusterRedisInstance: true + n: 2 + @redis = + createClient: sinon.stub().returns(@normalRedisInstance) + @ioredis = + Cluster: sinon.stub().returns(@clusterRedisInstance) + @logger = {log: sinon.stub()} + + @RedisWrapper = SandboxedModule.require modulePath, requires: + 'logger-sharelatex': @logger + 'settings-sharelatex': @settings + 'redis-sharelatex': @redis + 'ioredis': @ioredis + + describe 'client', -> + + beforeEach -> + @call = () => + @RedisWrapper.client(@featureName) + + describe 'when feature uses cluster', -> + + beforeEach -> + @settings.redis.somefeature = + cluster: [1, 2, 3] + + it 'should return a cluster client', -> + client = @call() + expect(client).to.equal @clusterRedisInstance + expect(client.__is_redis_cluster).to.equal true + + describe 'when feature uses normal redis', -> + + beforeEach -> + @settings.redis.somefeature = + port:"1234" + host:"somewhere" + password: "password" + + it 'should return a regular redis client', -> + client = @call() + expect(client).to.equal @normalRedisInstance + expect(client.__is_redis_cluster).to.equal undefined