Skip to content

Commit c761c4c

Browse files
committed
update test case
1 parent ebfcc38 commit c761c4c

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

lib/index.js

+42-7
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,69 @@
3636
}
3737

3838
/**
39-
* get token
40-
* @param callback {function(Object, Object=)} - callback
39+
* get 1 token
40+
* callback:
41+
* err: errors
42+
* result: {
43+
* limit: rate limit within duration,
44+
* remaining: number of token remaining,
45+
* reset: seconds left to reset the rate
46+
* }
47+
* @param callback {function(Object, Object=)} - finish callback
4148
*/
4249
R3Limiter.prototype.get = function(callback) {
4350

4451
var _this = this;
4552

4653
if (this._lua_sha1) {
47-
this.executeFunction(callback);
54+
this._executeFunction(callback);
4855
} else {
4956
this.redis_client.script('load', this._lua_string, function(err, sha1) {
5057
_this._lua_sha1 = sha1;
51-
_this.executeFunction(callback);
58+
_this._executeFunction(callback);
5259
});
5360
}
5461
};
5562

63+
5664
/**
57-
*
65+
* A helper for getting token and fire the callback
66+
* if rate limit reached, it will try to get token again until token available.
67+
* callback:
68+
* err: error
69+
* @param callback {function(Object=)} - finish callback
70+
*/
71+
R3Limiter.prototype.run = function(callback) {
72+
var _this = this;
73+
this.get(function(err, result) {
74+
if (err || !result) {
75+
callback(err);
76+
} else {
77+
if (result.remaining === -1) {
78+
setTimeout(function(){
79+
_this.run(callback);
80+
}, 1000 * (result.reset + 1));
81+
} else {
82+
callback();
83+
}
84+
}
85+
});
86+
};
87+
88+
/**
89+
* execute lua function
5890
* @param callback
91+
* @private
5992
*/
60-
R3Limiter.prototype.executeFunction = function(callback) {
93+
R3Limiter.prototype._executeFunction = function(callback) {
6194

6295
var _this = this;
6396

6497
this.redis_client.evalsha(this._lua_sha1, 3, this.key, this.limit, this.duration, function(err, result) {
6598
if (err) {
6699
callback(err);
67100
} else {
101+
var error = null;
68102
try {
69103
result = JSON.parse(result);
70104
result = {
@@ -73,10 +107,11 @@
73107
reset: parseInt(result[1])
74108
};
75109
} catch (e) {
110+
error = 'limiter_json_error';
76111
result = null;
77112
}
78113

79-
callback(null, result);
114+
callback(error, result);
80115
}
81116
});
82117
};

test/index.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/*global describe,beforeEach,it,should */
2+
13
require('should');
24
var Limiter = require('../lib/');
35
var redis = require('redis');
@@ -7,8 +9,12 @@ var redis_client = redis.createClient();
79
describe('Limiter', function() {
810
beforeEach(function(done) {
911
redis_client.keys('limit:*', function(err, keys) {
10-
if (err) return done(err);
11-
if (!keys.length) return done();
12+
if (err) {
13+
return done(err);
14+
}
15+
if (!keys.length) {
16+
return done();
17+
}
1218
var args = keys.concat(done);
1319
redis_client.del.apply(redis_client, args);
1420
});
@@ -105,7 +111,7 @@ describe('Limiter', function() {
105111
res.remaining.should.equal(0);
106112
setTimeout(function() {
107113
limiter.get(function(err, res) {
108-
console.log(res.reset);
114+
//console.log(res.reset);
109115
res.reset.should.be.below(2000);
110116
res.remaining.should.equal(-1);
111117
done();
@@ -224,4 +230,34 @@ describe('Limiter', function() {
224230
});
225231
});
226232
});
233+
234+
235+
describe('test run helper', function() {
236+
it('should return no errors', function(done) {
237+
this.timeout(7000);
238+
239+
var limiter = new Limiter({
240+
limit: 2,
241+
duration: 1,
242+
key: 'something',
243+
redis_client: redis_client
244+
});
245+
246+
limiter.run(function(err) {
247+
(!err).should.equal(true);
248+
});
249+
limiter.run(function(err) {
250+
(!err).should.equal(true);
251+
});
252+
limiter.run(function(err) {
253+
(!err).should.equal(true);
254+
});
255+
limiter.run(function(err) {
256+
(!err).should.equal(true);
257+
done();
258+
});
259+
260+
});
261+
});
262+
227263
});

0 commit comments

Comments
 (0)