Skip to content

Commit

Permalink
Merge pull request #191 from telefonicaid/maxRetryTime
Browse files Browse the repository at this point in the history
Max retry time
  • Loading branch information
xval committed Oct 3, 2013
2 parents 7dd4d22 + 311f576 commit 1e0103d
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/configBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ exports.longFailingTimeout = 10000;
*/
exports.minQuorum = 2;

/*
Max retry time (milliseconds)
the sum of the different retry intervals must be less than this value
*/
exports.maxRetryTime = 20 * 60 * 1000; //20 mins


/* LISTENER AND CONSUMER CONFIGURATION
======================================
Expand Down
14 changes: 14 additions & 0 deletions lib/eventWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ function validateHeaders(simpleRequest) {

errorsHeaders.push(error);
}

var sum = 0;
for (var i = 0; i < retrySplit.length; i++) {
sum += Number(retrySplit[i]);
}

if (sum > configGlobal.maxRetryTime) {
var error = {}
error.type = MG.INVALID_PARAMETER;
error.parameter = MG.HEAD_RELAYER_RETRY;
error.userMessage = 'The sum of the different intervals must be less than ' + configGlobal.maxRetryTime +' ms';

errorsHeaders.push(error);
}
}

//check Persistence Header
Expand Down
5 changes: 2 additions & 3 deletions test/acceptance/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
exports.rushServer = {hostname: '54.229.149.119', port: 443};
exports.externalEndpoint = 'www.google.com';
exports.user = 'rush:RuSh1';
exports.rushServer = {hostname: 'ec2-54-229-212-149.eu-west-1.compute.amazonaws.com', port: 443};
exports.externalEndpoint = 'www.google.es';
exports.token = '';
182 changes: 182 additions & 0 deletions test/e2e/maxRetryTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
var should = require('should');
var superagent = require('superagent');
var config = require('./config');
var globalConfig = require('../../lib/configBase.js');
var _ = require('underscore');
var chai = require('chai');
var expect = chai.expect;
var redisModule = require('redis');

var listener = require('../../lib/listener.js');

//RUSH ENDPOINT
var HOST = config.rushServer.hostname;
var PORT = config.rushServer.port;
var RUSHENDPOINT = 'http://' + HOST + ':' + PORT;

//Final host endpoint
var fhHOST = config.simpleServerHostname;
var fhPORT = config.simpleServerPort;

ENDPOINT = fhHOST + ':' + fhPORT;

// Time to wait to check the status of the task
var TIMEOUT = 600;
var CREATED = 201;
var describeTimeout = 5000;

function _validScenario(data){

var values = [];
var counter = 0, value;

while((counter + (value = _.random(0, globalConfig.maxRetryTime))) <= globalConfig.maxRetryTime) {
counter += value;
values.push(value)
}

values.push(globalConfig.maxRetryTime - counter);

var times = values.toString();


it(data.name +' /' +data.method +' #FRT', function(done){
var agent = superagent.agent();
var id;

var method;
switch(data.method){
case "DELETE":
method = 'del';
break;
default:
method = data.method.toLowerCase()
}
var req = agent
[method](RUSHENDPOINT + data.path )
.set('x-relayer-host', ENDPOINT) //Always the same endpoint
.set('x-relayer-persistence','BODY')
.set('content-type','application/json')
.set('x-relayer-retry', times)
.set(data.headers)
if(data.method === 'POST' || data.method === 'PUT'){
req = req.send(data.body);
}
req.end(function(err, res) {
expect(err).to.not.exist;
expect(res.statusCode).to.eql(CREATED);
expect(res.body).to.exist;
expect(res.body.id).to.exist;
id=res.body.id;
res.text.should.not.include('exception');
done();
});
});
}

function _invalidScenario(data){

var values = [];
var counter = 0, value;
var maxRetry = globalConfig.maxRetryTime;

while((counter + (value = _.random(0, maxRetry))) <= maxRetry) {
counter += value;
values.push(value)
}

values.push(maxRetry - counter + _.random(0, maxRetry));

var times = values.toString();

//it(data.name, function(done){
//it(data.name + data.protocol.toUpperCase() + ' /' +data.method + ' #FRT', function(done){
it(data.name + ' /' +data.method + ' #FRT', function(done){
var agent = superagent.agent();
var id;

var method;
switch(data.method){
case "DELETE":
method = 'del';
break;
default:
method = data.method.toLowerCase()
}
var req = agent
[method](RUSHENDPOINT + data.path )
.set('x-relayer-host', ENDPOINT) //Always the same endpoint
.set('x-relayer-persistence','BODY')
.set('content-type','application/json')
.set('x-relayer-retry', times)
.set(data.headers)
if(data.method === 'POST' || data.method === 'PUT'){
req = req.send(data.body);
}
req.end(function(err, res) {
expect(err).to.not.exist;
expect(res.statusCode).to.eql(400);
expect(res.body).to.exist;
expect(res.body.exceptionId).to.eql('SVC0002');
expect(res.body.exceptionText).to.eql('Invalid parameter value: x-relayer-retry');
expect(res.body.userMessage).to.eql('The sum of the different intervals must be less than ' + maxRetry + ' ms');
done();
});
});
}

describe('Multiple Feature: Maximum Retry value', function() {
this.timeout(TIMEOUT);

before(function (done) {
listener.start(done);
});

after(function (done) {
var rc = redisModule.createClient(globalConfig.queue.redisPort, globalConfig.queue.redisHost);
rc.flushall(function(){
listener.stop(done);
});
rc.quit();
});

describe('Retrieve requests with a valid RETRY headers policy ', function () {

var dataSet = [
{method: 'GET', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 1 Should accept the request using HTTP "},
{method: 'POST', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 2 Should accept the request using HTTP "},
{method: 'PUT', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 3 Should accept the request using HTTP "},
{method: 'DELETE', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 4 Should accept the request using HTTP "},
{method: 'GET', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 5 Should accept the request using HTTPS "},
{method: 'POST', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 6 Should accept the request using HTTPS "},
{method: 'PUT', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 7 Should accept the request using HTTPS "},
{method: 'DELETE', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 8 Should accept the request using HTTPS "}
];

for(i=0; i < dataSet.length; i++){
_validScenario(dataSet[i]); //Launch every test in data set
}
});

describe('Retrieve requests with a valid RETRY headers policy over the MAXIMUM set', function () {

var dataSetPOST = [
{method: 'GET', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 1 Should NOT accept the request using HTTP "},
{method: 'POST', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 2 Should NOT accept the request using HTTP "},
{method: 'PUT', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 3 Should NOT accept the request using HTTP "},
{method: 'DELETE', path: '/', headers: {'X-Relayer-Protocol':'http'}, body: {}, name : "Case 4 Should NOT accept the request using HTTP "},
{method: 'GET', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 5 Should NOT accept the request using HTTPS "},
{method: 'POST', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 6 Should NOT accept the request using HTTPS "},
{method: 'PUT', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 7 Should NOT accept the request using HTTPS "},
{method: 'DELETE', path: '/', headers: {'X-Relayer-Protocol':'https'}, body: {}, name : "Case 8 Should NOT accept the request using HTTPS "}
];

for(i=0; i < dataSetPOST.length; i++){
_invalidScenario(dataSetPOST[i]); //Launch every test in data set
}
});


});

//TODO: path different to empty

0 comments on commit 1e0103d

Please sign in to comment.