test.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "./index.coffee"
  7. expect = require("chai").expect
  8. describe "index", ->
  9. beforeEach ->
  10. @settings = {}
  11. @sentinelClient =
  12. set: ->
  13. on: ->
  14. @normalRedisClient =
  15. get: ->
  16. on: ->
  17. @ioredisConstructor = ioredisConstructor = sinon.stub()
  18. @sentinel =
  19. createClient: sinon.stub().returns(@sentinelClient)
  20. @normalRedis =
  21. createClient: sinon.stub().returns(@normalRedisClient)
  22. @ioredis = class IoRedis
  23. constructor: ioredisConstructor
  24. on: sinon.stub()
  25. @ioredis.Cluster = class Cluster
  26. constructor: (@config, @options) ->
  27. on: sinon.stub()
  28. @redis = SandboxedModule.require modulePath, requires:
  29. "redis-sentinel":@sentinel
  30. "redis":@normalRedis
  31. "ioredis": @ioredis
  32. @auth_pass = "1234 pass"
  33. @endpoints = [
  34. {host: '127.0.0.1', port: 26379},
  35. {host: '127.0.0.1', port: 26380}
  36. ]
  37. describe "sentinel", ->
  38. beforeEach ->
  39. @masterName = "my master"
  40. @sentinelOptions =
  41. endpoints:@endpoints
  42. masterName:@masterName
  43. auth_pass:@auth_pass
  44. it "should use sentinal if the first argument in an array", ->
  45. client = @redis.createClient @sentinelOptions
  46. @sentinel.createClient.called.should.equal true
  47. @normalRedis.createClient.called.should.equal false
  48. client.should.equal @sentinelClient
  49. it "should pass the options correctly though", ->
  50. client = @redis.createClient @sentinelOptions
  51. @sentinel.createClient.calledWith(@endpoints, @masterName, {auth_pass:@auth_pass, retry_max_delay: 5000}).should.equal true
  52. client.should.equal @sentinelClient
  53. describe "single node redis", ->
  54. beforeEach ->
  55. @standardOpts =
  56. auth_pass: @auth_pass
  57. port: 1234
  58. host: "redis.mysite.env"
  59. it "should use the ioredis driver in single-instance mode if a non array is passed", ->
  60. client = @redis.createClient @standardOpts
  61. @sentinel.createClient.called.should.equal false
  62. @normalRedis.createClient.called.should.equal false
  63. assert.equal(client.constructor, @ioredis)
  64. it "should call createClient for the ioredis driver in single-instance mode if a non array is passed", ->
  65. client = @redis.createClient @standardOpts
  66. @ioredisConstructor.calledWith(@standardOpts).should.equal true
  67. describe "cluster", ->
  68. beforeEach ->
  69. @cluster = [{"mock": "cluster"}, { "mock": "cluster2"}]
  70. @extraOptions = {keepAlive:100}
  71. @settings =
  72. cluster: @cluster
  73. redisOptions: @extraOptions
  74. key_schema: {foo: (x) -> "#{x}"}
  75. it "should pass the options correctly though with no options", ->
  76. client = @redis.createClient cluster: @cluster
  77. assert(client instanceof @ioredis.Cluster)
  78. client.config.should.deep.equal @cluster
  79. it "should not pass the key_schema through to the driver", ->
  80. client = @redis.createClient cluster: @cluster, key_schema: "foobar"
  81. assert(client instanceof @ioredis.Cluster)
  82. client.config.should.deep.equal @cluster
  83. expect(client.options).to.deep.equal {retry_max_delay: 5000}
  84. it "should pass the options correctly though with additional options", ->
  85. client = @redis.createClient @settings
  86. assert(client instanceof @ioredis.Cluster)
  87. client.config.should.deep.equal @cluster
  88. # need to use expect here because of _.clone in sandbox
  89. expect(client.options).to.deep.equal {redisOptions: @extraOptions, retry_max_delay: 5000}
  90. describe "monkey patch ioredis exec", ->
  91. beforeEach ->
  92. @callback = sinon.stub()
  93. @results = []
  94. @multiOrig = { exec: sinon.stub().yields(null, @results)}
  95. @client = { multi: sinon.stub().returns(@multiOrig) }
  96. @redis._monkeyPatchIoredisExec(@client)
  97. @multi = @client.multi()
  98. it "should return the old redis format for an array", ->
  99. @results[0] = [null, 42]
  100. @results[1] = [null, "foo"]
  101. @multi.exec @callback
  102. @callback.calledWith(null, [42, "foo"]).should.equal true
  103. it "should return the old redis format when there is an error", ->
  104. @results[0] = [null, 42]
  105. @results[1] = ["error", "foo"]
  106. @multi.exec @callback
  107. @callback.calledWith("error").should.equal true