RedisBackendTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RedisBackend.js"
  5. SandboxedModule = require('sandboxed-module')
  6. RedisKeyBuilder = require "../../../../app/js/RedisKeyBuilder"
  7. describe "RedisBackend", ->
  8. beforeEach ->
  9. @Settings =
  10. redis:
  11. documentupdater: [{
  12. primary: true
  13. port: "6379"
  14. host: "localhost"
  15. password: "single-password"
  16. key_schema:
  17. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  18. docLines: ({doc_id}) -> "doclines:#{doc_id}"
  19. docOps: ({doc_id}) -> "DocOps:#{doc_id}"
  20. docVersion: ({doc_id}) -> "DocVersion:#{doc_id}"
  21. projectKey: ({doc_id}) -> "ProjectId:#{doc_id}"
  22. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  23. docsInProject: ({project_id}) -> "DocsIn:#{project_id}"
  24. }, {
  25. cluster: [{
  26. port: "7000"
  27. host: "localhost"
  28. }]
  29. password: "cluster-password"
  30. key_schema:
  31. blockingKey: ({doc_id}) -> "Blocking:{#{doc_id}}"
  32. docLines: ({doc_id}) -> "doclines:{#{doc_id}}"
  33. docOps: ({doc_id}) -> "DocOps:{#{doc_id}}"
  34. docVersion: ({doc_id}) -> "DocVersion:{#{doc_id}}"
  35. projectKey: ({doc_id}) -> "ProjectId:{#{doc_id}}"
  36. pendingUpdates: ({doc_id}) -> "PendingUpdates:{#{doc_id}}"
  37. docsInProject: ({project_id}) -> "DocsIn:{#{project_id}}"
  38. }]
  39. test_context = @
  40. class Cluster
  41. constructor: (@config) ->
  42. test_context.rclient_ioredis = @
  43. @RedisBackend = SandboxedModule.require modulePath, requires:
  44. "settings-sharelatex": @Settings
  45. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
  46. "redis-sharelatex": @redis =
  47. createClient: sinon.stub().returns @rclient_redis = {}
  48. "ioredis": @ioredis =
  49. Cluster: Cluster
  50. @client = @RedisBackend.createClient()
  51. @doc_id = "mock-doc-id"
  52. it "should create a redis client", ->
  53. @redis.createClient
  54. .calledWith({
  55. port: "6379"
  56. host: "localhost"
  57. password: "single-password"
  58. })
  59. .should.equal true
  60. it "should create an ioredis cluster client", ->
  61. @rclient_ioredis.config.should.deep.equal [{
  62. port: "7000"
  63. host: "localhost"
  64. }]
  65. describe "individual commands", ->
  66. describe "with the same results", ->
  67. beforeEach (done) ->
  68. @content = "bar"
  69. @rclient_redis.get = sinon.stub()
  70. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(null, @content)
  71. @rclient_ioredis.get = sinon.stub()
  72. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, @content)
  73. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  74. setTimeout () -> # Let all background requests complete
  75. done(error)
  76. it "should return the result", ->
  77. @result.should.equal @content
  78. it "should have called the redis client with the appropriate key", ->
  79. @rclient_redis.get
  80. .calledWith("doclines:#{@doc_id}")
  81. .should.equal true
  82. it "should have called the ioredis cluster client with the appropriate key", ->
  83. @rclient_ioredis.get
  84. .calledWith("doclines:{#{@doc_id}}")
  85. .should.equal true
  86. describe "with different results", ->
  87. beforeEach (done) ->
  88. @rclient_redis.get = sinon.stub()
  89. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(null, "primary-result")
  90. @rclient_ioredis.get = sinon.stub()
  91. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, "secondary-result")
  92. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  93. setTimeout () -> # Let all background requests complete
  94. done(error)
  95. it "should return the primary result", ->
  96. @result.should.equal "primary-result"
  97. it "should log out the difference", ->
  98. @logger.warn
  99. .calledWith({
  100. results: [
  101. "primary-result",
  102. "secondary-result"
  103. ]
  104. }, "redis return values do not match")
  105. .should.equal true
  106. describe "when the secondary errors", ->
  107. beforeEach (done) ->
  108. @rclient_redis.get = sinon.stub()
  109. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(null, "primary-result")
  110. @rclient_ioredis.get = sinon.stub()
  111. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(@error = new Error("oops"))
  112. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  113. setTimeout () -> # Let all background requests complete
  114. done(error)
  115. it "should return the primary result", ->
  116. @result.should.equal "primary-result"
  117. it "should log out the secondary error", ->
  118. @logger.error
  119. .calledWith({
  120. err: @error
  121. }, "error in redis backend")
  122. .should.equal true
  123. describe "when the primary errors", ->
  124. beforeEach (done) ->
  125. @rclient_redis.get = sinon.stub()
  126. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(@error = new Error("oops"))
  127. @rclient_ioredis.get = sinon.stub()
  128. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, "secondary-result")
  129. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (@returned_error, @result) =>
  130. setTimeout () -> # Let all background requests complete
  131. done()
  132. it "should return the error", ->
  133. @returned_error.should.equal @error
  134. it "should log out the error", ->
  135. @logger.error
  136. .calledWith({
  137. err: @error
  138. }, "error in redis backend")
  139. .should.equal true
  140. describe "when the command has the key in a non-zero argument index", ->
  141. beforeEach (done) ->
  142. @script = "mock-script"
  143. @key_count = 1
  144. @value = "mock-value"
  145. @rclient_redis.eval = sinon.stub()
  146. @rclient_redis.eval.withArgs(@script, @key_count, "Blocking:#{@doc_id}", @value).yields(null)
  147. @rclient_ioredis.eval = sinon.stub()
  148. @rclient_ioredis.eval.withArgs(@script, @key_count, "Blocking:{#{@doc_id}}", @value).yields(null, @content)
  149. @client.eval @script, @key_count, RedisKeyBuilder.blockingKey({doc_id: @doc_id}), @value, (error) =>
  150. setTimeout () -> # Let all background requests complete
  151. done(error)
  152. it "should have called the redis client with the appropriate key", ->
  153. @rclient_redis.eval
  154. .calledWith(@script, @key_count, "Blocking:#{@doc_id}", @value)
  155. .should.equal true
  156. it "should have called the ioredis cluster client with the appropriate key", ->
  157. @rclient_ioredis.eval
  158. .calledWith(@script, @key_count, "Blocking:{#{@doc_id}}", @value)
  159. .should.equal true
  160. describe "multi commands", ->
  161. beforeEach ->
  162. # We will test with:
  163. # rclient.multi()
  164. # .get("doclines:foo")
  165. # .get("DocVersion:foo")
  166. # .exec (...) ->
  167. @doclines = "mock-doclines"
  168. @version = "42"
  169. @rclient_redis.multi = sinon.stub().returns @rclient_redis
  170. @rclient_ioredis.multi = sinon.stub().returns @rclient_ioredis
  171. describe "with the same results", ->
  172. beforeEach (done) ->
  173. @rclient_redis.get = sinon.stub()
  174. @rclient_redis.exec = sinon.stub().yields(null, [@doclines, @version])
  175. @rclient_ioredis.get = sinon.stub()
  176. @rclient_ioredis.exec = sinon.stub().yields(null, [ [null, @doclines], [null, @version] ])
  177. multi = @client.multi()
  178. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  179. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  180. multi.exec (error, @result) =>
  181. setTimeout () ->
  182. done(error)
  183. it "should return the result", ->
  184. @result.should.deep.equal [@doclines, @version]
  185. it "should have called the redis client with the appropriate keys", ->
  186. @rclient_redis.get
  187. .calledWith("doclines:#{@doc_id}")
  188. .should.equal true
  189. @rclient_redis.get
  190. .calledWith("DocVersion:#{@doc_id}")
  191. .should.equal true
  192. @rclient_ioredis.exec
  193. .called
  194. .should.equal true
  195. it "should have called the ioredis cluster client with the appropriate keys", ->
  196. @rclient_ioredis.get
  197. .calledWith("doclines:{#{@doc_id}}")
  198. .should.equal true
  199. @rclient_ioredis.get
  200. .calledWith("DocVersion:{#{@doc_id}}")
  201. .should.equal true
  202. @rclient_ioredis.exec
  203. .called
  204. .should.equal true
  205. describe "with different results", ->
  206. beforeEach (done) ->
  207. @rclient_redis.get = sinon.stub()
  208. @rclient_redis.exec = sinon.stub().yields(null, [@doclines, @version])
  209. @rclient_ioredis.get = sinon.stub()
  210. @rclient_ioredis.exec = sinon.stub().yields(null, [ [null, "different-doc-lines"], [null, @version] ])
  211. multi = @client.multi()
  212. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  213. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  214. multi.exec (error, @result) =>
  215. setTimeout () ->
  216. done(error)
  217. it "should return the primary result", ->
  218. @result.should.deep.equal [@doclines, @version]
  219. it "should log out the difference", ->
  220. @logger.warn
  221. .calledWith({
  222. results: [
  223. [@doclines, @version],
  224. ["different-doc-lines", @version]
  225. ]
  226. }, "redis return values do not match")
  227. .should.equal true
  228. describe "when the secondary errors", ->
  229. beforeEach (done) ->
  230. @rclient_redis.get = sinon.stub()
  231. @rclient_redis.exec = sinon.stub().yields(null, [@doclines, @version])
  232. @rclient_ioredis.get = sinon.stub()
  233. @rclient_ioredis.exec = sinon.stub().yields(@error = new Error("oops"))
  234. multi = @client.multi()
  235. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  236. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  237. multi.exec (error, @result) =>
  238. setTimeout () ->
  239. done(error)
  240. it "should return the primary result", ->
  241. @result.should.deep.equal [@doclines, @version]
  242. it "should log out the secondary error", ->
  243. @logger.error
  244. .calledWith({
  245. err: @error
  246. }, "error in redis backend")
  247. .should.equal true
  248. describe "when the secondary errors", ->
  249. beforeEach (done) ->
  250. @rclient_redis.get = sinon.stub()
  251. @rclient_redis.exec = sinon.stub().yields(@error = new Error("oops"))
  252. @rclient_ioredis.get = sinon.stub()
  253. @rclient_ioredis.exec = sinon.stub().yields([ [null, @doclines], [null, @version] ])
  254. multi = @client.multi()
  255. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  256. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  257. multi.exec (@returned_error) =>
  258. setTimeout () -> done()
  259. it "should return the error", ->
  260. @returned_error.should.equal @error
  261. it "should log out the error", ->
  262. @logger.error
  263. .calledWith({
  264. err: @error
  265. }, "error in redis backend")
  266. .should.equal true