Create a RedisWrapper, and use it for rate limiting.

This commit is contained in:
Shane Kilkelly
2016-12-19 12:17:02 +00:00
parent 637fcb5784
commit ef0a5801d5
5 changed files with 98 additions and 4366 deletions
@@ -6,7 +6,7 @@ expect = chai.expect
modulePath = "../../../../app/js/infrastructure/RateLimiter.js"
SandboxedModule = require('sandboxed-module')
describe "FileStoreHandler", ->
describe "RateLimiter", ->
beforeEach ->
@settings =
@@ -15,23 +15,22 @@ describe "FileStoreHandler", ->
port:"1234"
host:"somewhere"
password: "password"
@redbackInstance =
addCount: sinon.stub()
@redback =
createRateLimit: sinon.stub().returns(@redbackInstance)
@redis =
createClient: ->
return auth:->
@rclient =
incr: sinon.stub()
get: sinon.stub()
expire: sinon.stub()
exec: sinon.stub()
@rclient.multi = sinon.stub().returns(@rclient)
@RedisWrapper =
client: sinon.stub().returns(@rclient)
@limiter = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
"logger-sharelatex" : @logger = {log:sinon.stub(), err:sinon.stub()}
"redis-sharelatex": @redis
"redback": use: => @redback
"./RedisWrapper": @RedisWrapper
@endpointName = "compiles"
@subject = "some project id"
@subject = "some-project-id"
@timeInterval = 20
@throttleLimit = 5
@@ -40,43 +39,47 @@ describe "FileStoreHandler", ->
subjectName: @subject
throttle: @throttleLimit
timeInterval: @timeInterval
@key = "RateLimiter:#{@endpointName}:{#{@subject}}"
for redisType, resultSet of {
normal:[10, '10', 10],
cluster:[[null,10], [null,'10'], [null,10]]
}
do (redisType, resultSet) ->
describe "addCount", ->
describe "addCount with #{redisType} redis", ->
beforeEach ->
@redbackInstance.addCount.callsArgWith(2, null, 10)
beforeEach ->
@results = resultSet
@rclient.incr = sinon.stub()
@rclient.get = sinon.stub()
@rclient.expire = sinon.stub()
@rclient.exec = sinon.stub().callsArgWith(0, null, @results)
it "should use correct namespace", (done)->
@limiter.addCount @details, =>
@redback.createRateLimit.calledWith(@endpointName).should.equal true
done()
it "should use correct key", (done)->
@limiter.addCount @details, =>
@rclient.incr.calledWith(@key).should.equal true
done()
it "should only call it once", (done)->
@limiter.addCount @details, =>
@redbackInstance.addCount.callCount.should.equal 1
done()
it "should only call it once", (done)->
@limiter.addCount @details, =>
@rclient.exec.callCount.should.equal 1
done()
it "should use the subjectName", (done)->
@limiter.addCount @details, =>
@redbackInstance.addCount.calledWith(@details.subjectName, @details.timeInterval).should.equal true
done()
it "should return true if the count is less than throttle", (done)->
@details.throttle = 100
@limiter.addCount @details, (err, canProcess)=>
canProcess.should.equal true
done()
it "should return true if the count is less than throttle", (done)->
@details.throttle = 100
@limiter.addCount @details, (err, canProcess)=>
canProcess.should.equal true
done()
it "should return true if the count is less than throttle", (done)->
@details.throttle = 1
@limiter.addCount @details, (err, canProcess)=>
canProcess.should.equal false
done()
it "should return false if the limit is matched", (done)->
@details.throttle = 10
@limiter.addCount @details, (err, canProcess)=>
canProcess.should.equal false
done()
it "should return true if the count is less than throttle", (done)->
@details.throttle = 1
@limiter.addCount @details, (err, canProcess)=>
canProcess.should.equal false
done()
it "should return false if the limit is matched", (done)->
@details.throttle = 10
@limiter.addCount @details, (err, canProcess)=>
canProcess.should.equal false
done()