test.coffee 4.7 KB

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