2014-09-25 12:33:27 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, "./index.coffee"
|
|
|
|
expect = require("chai").expect
|
|
|
|
|
|
|
|
describe "index", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
|
|
|
|
@settings = {}
|
|
|
|
@sentinelClient = set:->
|
|
|
|
@normalRedisClient = get: ->
|
|
|
|
|
|
|
|
@sentinel =
|
|
|
|
createClient: sinon.stub().returns(@sentinelClient)
|
|
|
|
@normalRedis =
|
|
|
|
createClient: sinon.stub().returns(@normalRedisClient)
|
|
|
|
@redis = SandboxedModule.require modulePath, requires:
|
|
|
|
"redis-sentinel":@sentinel
|
|
|
|
"redis":@normalRedis
|
2017-04-12 11:58:59 -04:00
|
|
|
"ioredis": @ioredis =
|
|
|
|
Cluster: class Cluster
|
2017-06-16 10:28:55 -04:00
|
|
|
constructor: (@config, @options) ->
|
2014-09-26 09:46:23 -04:00
|
|
|
@auth_pass = "1234 pass"
|
2014-09-29 06:40:40 -04:00
|
|
|
@endpoints = [
|
2014-09-25 12:33:27 -04:00
|
|
|
{host: '127.0.0.1', port: 26379},
|
|
|
|
{host: '127.0.0.1', port: 26380}
|
|
|
|
]
|
2014-09-29 06:40:40 -04:00
|
|
|
|
2017-04-12 11:58:59 -04:00
|
|
|
describe "sentinel", ->
|
2014-09-29 06:40:40 -04:00
|
|
|
beforeEach ->
|
2014-09-25 12:33:27 -04:00
|
|
|
@masterName = "my master"
|
2014-09-26 09:46:23 -04:00
|
|
|
@sentinelOptions =
|
|
|
|
endpoints:@endpoints
|
|
|
|
masterName:@masterName
|
|
|
|
auth_pass:@auth_pass
|
2014-09-25 12:33:27 -04:00
|
|
|
|
|
|
|
it "should use sentinal if the first argument in an array", ->
|
2014-09-26 09:46:23 -04:00
|
|
|
client = @redis.createClient @sentinelOptions
|
2014-09-25 12:33:27 -04:00
|
|
|
@sentinel.createClient.called.should.equal true
|
|
|
|
@normalRedis.createClient.called.should.equal false
|
|
|
|
client.should.equal @sentinelClient
|
|
|
|
|
|
|
|
it "should pass the options correctly though", ->
|
2014-09-26 09:46:23 -04:00
|
|
|
client = @redis.createClient @sentinelOptions
|
2017-04-12 11:58:59 -04:00
|
|
|
@sentinel.createClient.calledWith(@endpoints, @masterName, {auth_pass:@auth_pass, retry_max_delay: 5000}).should.equal true
|
2014-09-25 12:33:27 -04:00
|
|
|
client.should.equal @sentinelClient
|
|
|
|
|
|
|
|
describe "normal redis", ->
|
|
|
|
beforeEach ->
|
2014-09-26 09:46:23 -04:00
|
|
|
@standardOpts =
|
|
|
|
auth_pass: @auth_pass
|
|
|
|
port: 1234
|
|
|
|
host: "redis.mysite.env"
|
2014-09-25 12:33:27 -04:00
|
|
|
|
|
|
|
it "should use the normal redis driver if a non array is passed", ->
|
2014-09-26 09:46:23 -04:00
|
|
|
client = @redis.createClient @standardOpts
|
2014-09-25 12:33:27 -04:00
|
|
|
@sentinel.createClient.called.should.equal false
|
|
|
|
@normalRedis.createClient.called.should.equal true
|
|
|
|
client.should.equal @normalRedisClient
|
|
|
|
|
|
|
|
it "should use the normal redis driver if a non array is passed", ->
|
2014-09-26 09:46:23 -04:00
|
|
|
client = @redis.createClient @standardOpts
|
2017-04-12 11:58:59 -04:00
|
|
|
@normalRedis.createClient.calledWith(@standardOpts.port, @standardOpts.host, {auth_pass:@auth_pass, retry_max_delay: 5000}).should.equal true
|
2014-09-26 09:46:23 -04:00
|
|
|
|
2017-04-12 11:58:59 -04:00
|
|
|
describe "cluster", ->
|
|
|
|
beforeEach ->
|
|
|
|
@cluster = [{"mock": "cluster"}, { "mock": "cluster2"}]
|
2017-06-16 10:28:55 -04:00
|
|
|
@extraOptions = {keepAlive:100}
|
|
|
|
@settings =
|
|
|
|
cluster: @cluster
|
|
|
|
redisOptions: @extraOptions
|
2017-06-19 10:46:21 -04:00
|
|
|
key_schema: {foo: (x) -> "#{x}"}
|
2014-09-26 09:46:23 -04:00
|
|
|
|
2017-06-16 10:28:55 -04:00
|
|
|
it "should pass the options correctly though with no options", ->
|
2017-04-12 11:58:59 -04:00
|
|
|
client = @redis.createClient cluster: @cluster
|
|
|
|
assert(client instanceof @ioredis.Cluster)
|
|
|
|
client.config.should.deep.equal @cluster
|
2014-09-26 09:46:23 -04:00
|
|
|
|
2017-06-19 10:46:21 -04:00
|
|
|
it "should not pass the key_schema through to the driver", ->
|
|
|
|
client = @redis.createClient cluster: @cluster, key_schema: "foobar"
|
|
|
|
assert(client instanceof @ioredis.Cluster)
|
|
|
|
client.config.should.deep.equal @cluster
|
|
|
|
expect(client.options).to.deep.equal {retry_max_delay: 5000}
|
|
|
|
|
2017-06-16 10:28:55 -04:00
|
|
|
it "should pass the options correctly though with additional options", ->
|
|
|
|
client = @redis.createClient @settings
|
|
|
|
assert(client instanceof @ioredis.Cluster)
|
|
|
|
client.config.should.deep.equal @cluster
|
|
|
|
# need to use expect here because of _.clone in sandbox
|
|
|
|
expect(client.options).to.deep.equal {redisOptions: @extraOptions, retry_max_delay: 5000}
|
|
|
|
|
2017-04-25 10:34:32 -04:00
|
|
|
describe "monkey patch ioredis exec", ->
|
|
|
|
beforeEach ->
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@results = []
|
|
|
|
@multiOrig = { exec: sinon.stub().yields(null, @results)}
|
|
|
|
@client = { multi: sinon.stub().returns(@multiOrig) }
|
|
|
|
@redis._monkeyPatchIoredisExec(@client)
|
|
|
|
@multi = @client.multi()
|
|
|
|
|
|
|
|
it "should return the old redis format for an array", ->
|
|
|
|
@results[0] = [null, 42]
|
|
|
|
@results[1] = [null, "foo"]
|
|
|
|
@multi.exec @callback
|
|
|
|
@callback.calledWith(null, [42, "foo"]).should.equal true
|
|
|
|
|
|
|
|
it "should return the old redis format when there is an error", ->
|
|
|
|
@results[0] = [null, 42]
|
|
|
|
@results[1] = ["error", "foo"]
|
|
|
|
@multi.exec @callback
|
|
|
|
@callback.calledWith("error").should.equal true
|
|
|
|
|
2014-09-29 06:40:40 -04:00
|
|
|
describe "setting the password", ->
|
|
|
|
beforeEach ->
|
|
|
|
@standardOpts =
|
|
|
|
password: @auth_pass
|
|
|
|
port: 1234
|
|
|
|
host: "redis.mysite.env"
|
|
|
|
|
|
|
|
@sentinelOptions =
|
|
|
|
endpoints:@endpoints
|
|
|
|
masterName:@masterName
|
|
|
|
password: @auth_pass
|
|
|
|
|
|
|
|
it "should set the auth_pass from password if password exists for normal redis", ->
|
|
|
|
client = @redis.createClient @standardOpts
|
2017-04-12 11:58:59 -04:00
|
|
|
@normalRedis.createClient.calledWith(@standardOpts.port, @standardOpts.host, {auth_pass:@auth_pass, retry_max_delay: 5000}).should.equal true
|
2014-09-29 06:40:40 -04:00
|
|
|
|
|
|
|
it "should set the auth_pass from password if password exists for sentinal", ->
|
|
|
|
client = @redis.createClient @sentinelOptions
|
2017-04-12 11:58:59 -04:00
|
|
|
@sentinel.createClient.calledWith(@endpoints, @masterName, {auth_pass:@auth_pass, retry_max_delay: 5000}).should.equal true
|
2014-09-26 09:46:23 -04:00
|
|
|
|
2014-09-25 12:33:27 -04:00
|
|
|
|