RedisWrapperTests.coffee 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/infrastructure/RedisWrapper.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe 'RedisWrapper', ->
  9. beforeEach ->
  10. @featureName = 'somefeature'
  11. @settings =
  12. redis:
  13. web:
  14. port:"1234"
  15. host:"somewhere"
  16. password: "password"
  17. somefeature: {}
  18. @normalRedisInstance =
  19. thisIsANormalRedisInstance: true
  20. n: 1
  21. @clusterRedisInstance =
  22. thisIsAClusterRedisInstance: true
  23. n: 2
  24. @redis =
  25. createClient: sinon.stub().returns(@normalRedisInstance)
  26. @ioredis =
  27. Cluster: sinon.stub().returns(@clusterRedisInstance)
  28. @logger = {log: sinon.stub()}
  29. @RedisWrapper = SandboxedModule.require modulePath, requires:
  30. 'logger-sharelatex': @logger
  31. 'settings-sharelatex': @settings
  32. 'redis-sharelatex': @redis
  33. 'ioredis': @ioredis
  34. describe 'client', ->
  35. beforeEach ->
  36. @call = () =>
  37. @RedisWrapper.client(@featureName)
  38. describe 'when feature uses cluster', ->
  39. beforeEach ->
  40. @settings.redis.somefeature =
  41. cluster: [1, 2, 3]
  42. it 'should return a cluster client', ->
  43. client = @call()
  44. expect(client).to.equal @clusterRedisInstance
  45. expect(client.__is_redis_cluster).to.equal true
  46. describe 'when feature uses normal redis', ->
  47. beforeEach ->
  48. @settings.redis.somefeature =
  49. port:"1234"
  50. host:"somewhere"
  51. password: "password"
  52. it 'should return a regular redis client', ->
  53. client = @call()
  54. expect(client).to.equal @normalRedisInstance
  55. expect(client.__is_redis_cluster).to.equal undefined