Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('caches concurrent requests to the appropriate file', function(done) {
// Fire off 20 concurrent requests and ensure that all the cached responses
// end up in the appropriate place.
async.times(20, function(index, next) {
var randomInput = Math.random().toString();
var url = '/echo_chunked?input=' + randomInput;
var cacheKey = ['GET', 'localhost:9333', url].join('');
var cacheBase = crypto.createHash('sha256').update(cacheKey).digest('hex');
request.get('http://localhost:9333' + url, function(error, response, body) {
next(null, {
url: url,
input: randomInput,
output: body,
cacheBase: cacheBase,
});
});
}, function(error, requests) {
setTimeout(function() {
it('returns 429 over rate limit error when the requests per second exceeds the rate limit plus burst', function(done) {
this.timeout(7000);
async.times(40, function(index, callback) {
request.get('http://localhost:9080/info/', this.options, function(error, response) {
callback(error, response.statusCode);
});
}.bind(this), function(error, responseCodes) {
should.not.exist(error);
var successes = _.filter(responseCodes, function(code) { return code === 200; });
var overLimits = _.filter(responseCodes, function(code) { return code === 429; });
responseCodes.length.should.eql(40);
// The rate limiting and burst handling is a bit fuzzy since we
// don't know exactly when the initial rate limit has been exceeded
// (since nginx limits aren't based on hard counts, but instead the
// average rate of requests, and we also don't know how fast the
// nodejs tests are actually making requests). Since we don't know
// when the burst kicks in, just make sure we generally start
setTimeout(function() {
// Perform a number of requests to make sure any local worker
// cache is cleared across all possible worker processes.
async.times(20, function(index, timesCallback) {
request.get(url, function(error, response) {
response.headers['x-ratelimit-limit'].should.eql('90');
timesCallback();
}.bind(this));
}.bind(this), done);
}.bind(this), 2100);
}.bind(this));
it('returns 429 over rate limit error when concurrent connections from a single IP exceeds the configured number', function(done) {
this.timeout(20000);
async.times(21, function(index, callback) {
request.get('http://localhost:9080/delay/2000', this.options, function(error, response) {
callback(error, response.statusCode);
});
}.bind(this), function(error, responseCodes) {
should.not.exist(error);
var successes = _.filter(responseCodes, function(code) { return code === 200; });
var overLimits = _.filter(responseCodes, function(code) { return code === 429; });
responseCodes.length.should.eql(21);
successes.length.should.eql(20);
overLimits.length.should.eql(1);
done();
});
});
});
it('counts api keys differently', function(done) {
var options = {
headers: headers({
'X-Forwarded-For': this.ipAddress,
'X-Api-Key': this.apiKey,
}, headerOverrides),
};
async.times(limit, function(index, asyncCallback) {
request.get('http://localhost:9080' + path, options, function(error, response) {
response.statusCode.should.eql(200);
asyncCallback(null);
});
}.bind(this), function() {
Factory.create('api_user', function(user) {
options.headers['X-Api-Key'] = user.api_key;
request.get('http://localhost:9080' + path, options, function(error, response) {
response.statusCode.should.eql(200);
done();
});
});
});
});
it('does not trip the circuit breaker for 404s', function (done) {
client = Client.createClient({
retries: 0,
circuitBreakerMaxFailures: 1,
stats: stats
});
nock.cleanAll();
api.get(path).times(3).reply(404);
async.times(2, function (i, cb) {
client.get(url, function () {
cb();
});
}, function () {
client.get(url, function (err) {
assert(err);
assert.equal(err.statusCode, 404);
done();
});
});
});
function masterMain (options, cb) {
var module = options.module;
var clientProcessCount = options.clientProcessCount;
var serverProcessCount = options.serverProcessCount;
_.times(serverProcessCount, function (idx) {
var server = cluster.fork();
server.send({
module: module
});
});
async.times(clientProcessCount, function (idx, cb) {
var client = cp.fork(__dirname + '/client');
client.on('message', function (result) {
cb(null, result);
});
client.on('exit', function () {
//console.log('Client exited');
});
}, finish);
function finish (err, results) {
cluster.disconnect(function () {
if (err) {
return cb && cb(err);
}
(cb) => {
Async.times(
limit,
(n, next) => instance.send(message, queueName, next),
cb
);
},
(result, cb) => instance.getAll(queueName, handler, options, cb)
beforeEach(function (done) {
var self = this;
async.times(2, function (n, next) {
var pub = publisher = zmq.socket('pub');
pub.connect(pub_listener);
var sub = subscriber = zmq.socket('sub');
sub.connect(sub_listener);
var srv = http();
var sio = io(srv, { adapter: Adapter({
pubClient: pub,
subClient: sub
}) });
srv.listen(function () {
['/', '/nsp'].forEach(function (name) {
sio.of(name).on('connection', function (socket) {
socket.on('join', function (fn) {
socket.join('room', fn);
});
Factory.create('api_user', function(user) {
async.times(51, function(index, timesCallback) {
request.get('http://localhost:9080/hello?api_key=' + user.api_key + '&index=' + index + '&rand=' + Math.random(), this.options, function(error, response) {
should.not.exist(error);
timesCallback(null, response.statusCode);
});
}.bind(this), function(error, statusCodes) {
should.not.exist(error);
statusCodes.length.should.eql(51);
var byCode = _.groupBy(statusCodes);
var successCount = (byCode[200] || []).length;
var overRateLimitCount = (byCode[429] || []).length;
successCount.should.be.gte(49);
successCount.should.be.lte(50);
overRateLimitCount.should.gte(1);
overRateLimitCount.should.lte(2);
if(overRateLimitCount === 1) {