From 214d414da45336f54bf66e519953a9d493f55b8d Mon Sep 17 00:00:00 2001 From: Xavier Dumesnil Date: Thu, 5 Jul 2018 11:28:46 -0700 Subject: [PATCH 1/3] Add test --- test/index.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/test/index.js b/test/index.js index 26d2fcc..572c88b 100644 --- a/test/index.js +++ b/test/index.js @@ -88,22 +88,41 @@ var Limiter = require('..'), }); describe('when the limit is exceeded', function() { - it('should retain .remaining at 0', function(done) { - var limit = new Limiter({ + var limit; + + beforeEach(function (done) { + limit = new Limiter({ max: 2, id: 'something', db: db }); + + limit.get(function() { + limit.get(function() { + done(); + }); + }); + }); + + it('should retain .remaining at 0', function(done) { limit.get(function(err, res) { - res.remaining.should.equal(2); - limit.get(function(err, res) { - res.remaining.should.equal(1); - limit.get(function(err, res) { - // function caller should reject this call - res.remaining.should.equal(0); + // function caller should reject this call + res.remaining.should.equal(0); + done(); + }); + }); + + it('should return an increasing reset time after each call', function (done) { + var originalResetMs; + limit.get(function(err, res) { + originalResetMs = res.resetMs; + + setTimeout(function() { + limit.get(function (err, res) { + res.resetMs.should.be.greaterThan(originalResetMs); done(); }); - }); + }, 200); }); }); }); From b6a6dae86d93cb6c2995ad200dcd13ced22eae08 Mon Sep 17 00:00:00 2001 From: Xavier Dumesnil Date: Thu, 5 Jul 2018 11:30:06 -0700 Subject: [PATCH 2/3] Return reset time based on the oldest call in range --- index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d85e42c..5663cde 100644 --- a/index.js +++ b/index.js @@ -69,15 +69,21 @@ Limiter.prototype.get = function (fn) { .zcard([key]) .zadd([key, now, now]) .zrange([key, 0, 0]) + .zrange([key, -max, -max]) .pexpire([key, duration]) .exec(function (err, res) { if (err) return fn(err); - var count = parseInt(Array.isArray(res[0]) ? res[1][1] : res[1]); - var oldest = parseInt(Array.isArray(res[0]) ? res[3][1] : res[3]); + + var isIoRedis = Array.isArray(res[0]); + var count = parseInt(isIoRedis ? res[1][1] : res[1]); + var oldest = parseInt(isIoRedis ? res[3][1] : res[3]); + var oldestInRange = parseInt(isIoRedis ? res[4][1] : res[4]); + var resetMicro = (Number.isNaN(oldestInRange) ? oldest : oldestInRange) + duration * 1000; + fn(null, { remaining: count < max ? max - count : 0, - reset: Math.floor((oldest + duration * 1000) / 1000000), - resetMs: Math.floor((oldest + duration * 1000) / 1000), + reset: Math.floor(resetMicro / 1000000), + resetMs: Math.floor(resetMicro / 1000), total: max }); }); From a53a4657bcbbe19ea102e8b22158416c552a272d Mon Sep 17 00:00:00 2001 From: Xavier Dumesnil Date: Thu, 5 Jul 2018 12:30:32 -0700 Subject: [PATCH 3/3] Ensure consistent test results --- test/index.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/index.js b/test/index.js index 572c88b..0f95059 100644 --- a/test/index.js +++ b/test/index.js @@ -113,17 +113,18 @@ var Limiter = require('..'), }); it('should return an increasing reset time after each call', function (done) { - var originalResetMs; - limit.get(function(err, res) { - originalResetMs = res.resetMs; + setTimeout(function () { + limit.get(function(err, res) { + var originalResetMs = res.resetMs; - setTimeout(function() { - limit.get(function (err, res) { - res.resetMs.should.be.greaterThan(originalResetMs); - done(); - }); - }, 200); - }); + setTimeout(function() { + limit.get(function (err, res) { + res.resetMs.should.be.greaterThan(originalResetMs); + done(); + }); + }, 10); + }); + }, 10); }); });