test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS206: Consider reworking classes to avoid initClass
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. const should = require('chai').should()
  8. const SandboxedModule = require('sandboxed-module')
  9. const assert = require('assert')
  10. const path = require('path')
  11. const sinon = require('sinon')
  12. const modulePath = path.join(__dirname, './../../../index.js')
  13. const { expect } = require('chai')
  14. describe('index', function () {
  15. beforeEach(function () {
  16. let Cluster, IoRedis, ioredisConstructor
  17. this.settings = {}
  18. this.ioredisConstructor = ioredisConstructor = sinon.stub()
  19. this.ioredis = IoRedis = (function () {
  20. let createIoRedis
  21. IoRedis = class IoRedis {
  22. static initClass() {
  23. this.prototype.on = sinon.stub()
  24. createIoRedis = ioredisConstructor
  25. }
  26. constructor() {
  27. return createIoRedis.apply(this, arguments)
  28. }
  29. }
  30. IoRedis.initClass()
  31. return IoRedis
  32. })()
  33. this.ioredis.Cluster = Cluster = (function () {
  34. Cluster = class Cluster {
  35. static initClass() {
  36. this.prototype.on = sinon.stub()
  37. }
  38. constructor(config, options) {
  39. this.config = config
  40. this.options = options
  41. }
  42. }
  43. Cluster.initClass()
  44. return Cluster
  45. })()
  46. this.redis = SandboxedModule.require(modulePath, {
  47. requires: {
  48. ioredis: this.ioredis,
  49. },
  50. })
  51. return (this.auth_pass = '1234 pass')
  52. })
  53. describe('single node redis', function () {
  54. beforeEach(function () {
  55. return (this.standardOpts = {
  56. auth_pass: this.auth_pass,
  57. port: 1234,
  58. host: 'redis.mysite.env',
  59. })
  60. })
  61. it('should use the ioredis driver in single-instance mode if a non array is passed', function () {
  62. const client = this.redis.createClient(this.standardOpts)
  63. return assert.equal(client.constructor, this.ioredis)
  64. })
  65. return it('should call createClient for the ioredis driver in single-instance mode if a non array is passed', function () {
  66. const client = this.redis.createClient(this.standardOpts)
  67. return this.ioredisConstructor
  68. .calledWith(this.standardOpts)
  69. .should.equal(true)
  70. })
  71. })
  72. describe('cluster', function () {
  73. beforeEach(function () {
  74. this.cluster = [{ mock: 'cluster' }, { mock: 'cluster2' }]
  75. this.extraOptions = { keepAlive: 100 }
  76. return (this.settings = {
  77. cluster: this.cluster,
  78. redisOptions: this.extraOptions,
  79. key_schema: {
  80. foo(x) {
  81. return `${x}`
  82. },
  83. },
  84. })
  85. })
  86. it('should pass the options correctly though with no options', function () {
  87. const client = this.redis.createClient({ cluster: this.cluster })
  88. assert(client instanceof this.ioredis.Cluster)
  89. return client.config.should.deep.equal(this.cluster)
  90. })
  91. it('should not pass the key_schema through to the driver', function () {
  92. const client = this.redis.createClient({
  93. cluster: this.cluster,
  94. key_schema: 'foobar',
  95. })
  96. assert(client instanceof this.ioredis.Cluster)
  97. client.config.should.deep.equal(this.cluster)
  98. return expect(client.options).to.deep.equal({ retry_max_delay: 5000 })
  99. })
  100. return it('should pass the options correctly though with additional options', function () {
  101. const client = this.redis.createClient(this.settings)
  102. assert(client instanceof this.ioredis.Cluster)
  103. client.config.should.deep.equal(this.cluster)
  104. // need to use expect here because of _.clone in sandbox
  105. return expect(client.options).to.deep.equal({
  106. redisOptions: this.extraOptions,
  107. retry_max_delay: 5000,
  108. })
  109. })
  110. })
  111. return describe('monkey patch ioredis exec', function () {
  112. beforeEach(function () {
  113. this.callback = sinon.stub()
  114. this.results = []
  115. this.multiOrig = { exec: sinon.stub().yields(null, this.results) }
  116. this.client = { multi: sinon.stub().returns(this.multiOrig) }
  117. this.redis._monkeyPatchIoredisExec(this.client)
  118. return (this.multi = this.client.multi())
  119. })
  120. it('should return the old redis format for an array', function () {
  121. this.results[0] = [null, 42]
  122. this.results[1] = [null, 'foo']
  123. this.multi.exec(this.callback)
  124. return this.callback.calledWith(null, [42, 'foo']).should.equal(true)
  125. })
  126. return it('should return the old redis format when there is an error', function () {
  127. this.results[0] = [null, 42]
  128. this.results[1] = ['error', 'foo']
  129. this.multi.exec(this.callback)
  130. return this.callback.calledWith('error').should.equal(true)
  131. })
  132. })
  133. })