Browse Source

added tests for verifying request retry behavior

limited retry behavior to errors with status 408 (request timeout).  this also reduces reliance on unit/mock/backend and unit/mock/query.
pull/782/head
Stephen Hess 8 years ago
parent
commit
bf62a1844b
  1. 7
      controller/search.js
  2. 410
      test/unit/controller/search.js
  3. 8
      test/unit/mock/backend.js

7
controller/search.js

@ -8,6 +8,10 @@ var logging = require( '../helper/logging' );
const retry = require('retry'); const retry = require('retry');
function setup( apiConfig, esclient, query ){ function setup( apiConfig, esclient, query ){
function isRequestTimeout(err) {
return _.get(err, 'status') === 408;
}
function controller( req, res, next ){ function controller( req, res, next ){
// do not run controller when a request // do not run controller when a request
// validation error has occurred. // validation error has occurred.
@ -62,7 +66,8 @@ function setup( apiConfig, esclient, query ){
searchService( esclient, cmd, function( err, docs, meta ){ searchService( esclient, cmd, function( err, docs, meta ){
// returns true if the operation should be attempted again // returns true if the operation should be attempted again
// (handles bookkeeping of maxRetries) // (handles bookkeeping of maxRetries)
if (operation.retry(err)) { // only consider for status 408 (request timeout)
if (isRequestTimeout(err) && operation.retry(err)) {
logger.info('request timed out, retrying'); logger.info('request timed out, retrying');
return; return;
} }

410
test/unit/controller/search.js

@ -1,3 +1,5 @@
'use strict';
var setup = require('../../../controller/search'), var setup = require('../../../controller/search'),
mockBackend = require('../mock/backend'), mockBackend = require('../mock/backend'),
mockQuery = require('../mock/query'); mockQuery = require('../mock/query');
@ -19,158 +21,302 @@ var fakeDefaultConfig = {
}; };
// functionally test controller (backend success) // functionally test controller (backend success)
module.exports.tests.functional_success = function(test, common) { // module.exports.tests.functional_success = function(test, common) {
//
// expected geojson features for 'client/suggest/ok/1' fixture // // expected geojson features for 'client/suggest/ok/1' fixture
var expected = [{ // var expected = [{
type: 'Feature', // type: 'Feature',
geometry: { // geometry: {
type: 'Point', // type: 'Point',
coordinates: [-50.5, 100.1] // coordinates: [-50.5, 100.1]
}, // },
properties: { // properties: {
id: 'myid1', // id: 'myid1',
layer: 'mytype1', // layer: 'mytype1',
text: 'test name1, city1, state1' // text: 'test name1, city1, state1'
} // }
}, { // }, {
type: 'Feature', // type: 'Feature',
geometry: { // geometry: {
type: 'Point', // type: 'Point',
coordinates: [-51.5, 100.2] // coordinates: [-51.5, 100.2]
}, // },
properties: { // properties: {
id: 'myid2', // id: 'myid2',
layer: 'mytype2', // layer: 'mytype2',
text: 'test name2, city2, state2' // text: 'test name2, city2, state2'
} // }
}]; // }];
//
var expectedMeta = { // var expectedMeta = {
scores: [10, 20], // scores: [10, 20],
query_type: 'mock' // query_type: 'mock'
}; // };
//
var expectedData = [ // var expectedData = [
{ // {
_id: 'myid1', // _id: 'myid1',
_score: 10, // _score: 10,
_type: 'mytype1', // _type: 'mytype1',
_matched_queries: ['query 1', 'query 2'], // _matched_queries: ['query 1', 'query 2'],
parent: { // parent: {
country: ['country1'], // country: ['country1'],
region: ['state1'], // region: ['state1'],
county: ['city1'] // county: ['city1']
}, // },
center_point: { lat: 100.1, lon: -50.5 }, // center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' }, // name: { default: 'test name1' },
value: 1 // value: 1
}, // },
{ // {
_id: 'myid2', // _id: 'myid2',
_score: 20, // _score: 20,
_type: 'mytype2', // _type: 'mytype2',
_matched_queries: ['query 3'], // _matched_queries: ['query 3'],
parent: { // parent: {
country: ['country2'], // country: ['country2'],
region: ['state2'], // region: ['state2'],
county: ['city2'] // county: ['city2']
}, // },
center_point: { lat: 100.2, lon: -51.5 }, // center_point: { lat: 100.2, lon: -51.5 },
name: { default: 'test name2' }, // name: { default: 'test name2' },
value: 2 // value: 2
} // }
]; // ];
//
test('functional success', function (t) { // test('functional success', function (t) {
var backend = mockBackend('client/search/ok/1', function (cmd) { // var backend = mockBackend('client/search/ok/1', function (cmd) {
t.deepEqual(cmd, { // t.deepEqual(cmd, {
body: {a: 'b'}, // body: {a: 'b'},
index: 'pelias', // index: 'pelias',
searchType: 'dfs_query_then_fetch' // searchType: 'dfs_query_then_fetch'
}, 'correct backend command'); // }, 'correct backend command');
}); // });
var controller = setup(fakeDefaultConfig, backend, mockQuery()); // var controller = setup(fakeDefaultConfig, backend, mockQuery());
var res = { // var res = {
status: function (code) { // status: function (code) {
t.equal(code, 200, 'status set'); // t.equal(code, 200, 'status set');
return res; // return res;
// },
// json: function (json) {
// t.equal(typeof json, 'object', 'returns json');
// t.equal(typeof json.date, 'number', 'date set');
// t.equal(json.type, 'FeatureCollection', 'valid geojson');
// t.true(Array.isArray(json.features), 'features is array');
// t.deepEqual(json.features, expected, 'values correctly mapped');
// }
// };
// var req = { clean: { a: 'b' }, errors: [], warnings: [] };
// var next = function next() {
// t.equal(req.errors.length, 0, 'next was called without error');
// t.deepEqual(res.meta, expectedMeta, 'meta data was set');
// t.deepEqual(res.data, expectedData, 'data was set');
// t.end();
// };
// controller(req, res, next);
// });
//
// test('functional success with alternate index name', function(t) {
// var fakeCustomizedConfig = {
// indexName: 'alternateindexname'
// };
//
// var backend = mockBackend('client/search/ok/1', function (cmd) {
// t.deepEqual(cmd, {
// body: {a: 'b'},
// index: 'alternateindexname',
// searchType: 'dfs_query_then_fetch'
// }, 'correct backend command');
// });
// var controller = setup(fakeCustomizedConfig, backend, mockQuery());
// var res = {
// status: function (code) {
// t.equal(code, 200, 'status set');
// return res;
// }
// };
// var req = { clean: { a: 'b' }, errors: [], warnings: [] };
// var next = function next() {
// t.equal(req.errors.length, 0, 'next was called without error');
// t.end();
// };
// controller(req, res, next);
// });
// };
//
// // functionally test controller (backend failure)
// module.exports.tests.functional_failure = function(test, common) {
// test('functional failure', function(t) {
// var backend = mockBackend( 'client/search/fail/1', function( cmd ){
// t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command');
// });
// var controller = setup( fakeDefaultConfig, backend, mockQuery() );
// var req = { clean: { a: 'b' }, errors: [], warnings: [] };
// var next = function(){
// t.equal(req.errors[0],'an elasticsearch error occurred');
// t.end();
// };
// controller(req, undefined, next );
// });
// };
module.exports.tests.timeout = function(test, common) {
test('default # of request timeout retries should be 3', (t) => {
const config = {
indexName: 'indexName value'
};
const esclient = 'this is the esclient';
const query = () => {
return { body: 'this is the query body' };
};
let searchServiceCallCount = 0;
const timeoutError = {
status: 408,
displayName: 'RequestTimeout',
message: 'Request Timeout after 17ms'
};
// request timeout messages willl be written here
const infoMesssages = [];
// a controller that validates the esclient and cmd that was passed to the search service
const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
t.equal(esclient, 'this is the esclient');
t.deepEqual(cmd, {
index: 'indexName value',
searchType: 'dfs_query_then_fetch',
body: 'this is the query body'
});
// not that the searchService got called
searchServiceCallCount++;
callback(timeoutError);
}, },
json: function (json) { 'pelias-logger': {
t.equal(typeof json, 'object', 'returns json'); get: (service) => {
t.equal(typeof json.date, 'number', 'date set'); t.equal(service, 'api');
t.equal(json.type, 'FeatureCollection', 'valid geojson'); return {
t.true(Array.isArray(json.features), 'features is array'); info: (msg) => {
t.deepEqual(json.features, expected, 'values correctly mapped'); infoMesssages.push(msg);
},
debug: () => {}
};
}
} }
}; })(config, esclient, query);
var req = { clean: { a: 'b' }, errors: [], warnings: [] };
var next = function next() { const req = { clean: { }, errors: [], warnings: [] };
t.equal(req.errors.length, 0, 'next was called without error'); const res = {};
t.deepEqual(res.meta, expectedMeta, 'meta data was set');
t.deepEqual(res.data, expectedData, 'data was set'); var next = function() {
t.equal(searchServiceCallCount, 3+1);
t.deepEqual(
infoMesssages.filter((msg)=> { return msg === 'request timed out, retrying'; } ).length,
3,
'there should be 3 request timed out info messages'
);
t.deepEqual(req, {
clean: {},
errors: [timeoutError.message],
warnings: []
});
t.deepEqual(res, {});
t.end(); t.end();
}; };
controller(req, res, next); controller(req, res, next);
}); });
test('functional success with alternate index name', function(t) { test('explicit apiConfig.requestRetries should retry that many times', (t) => {
var fakeCustomizedConfig = { const config = {
indexName: 'alternateindexname' indexName: 'indexName value',
requestRetries: 17
};
const esclient = 'this is the esclient';
const query = () => {
return { };
}; };
var backend = mockBackend('client/search/ok/1', function (cmd) { let searchServiceCallCount = 0;
t.deepEqual(cmd, {
body: {a: 'b'}, const timeoutError = {
index: 'alternateindexname', status: 408,
searchType: 'dfs_query_then_fetch' displayName: 'RequestTimeout',
}, 'correct backend command'); message: 'Request Timeout after 17ms'
});
var controller = setup(fakeCustomizedConfig, backend, mockQuery());
var res = {
status: function (code) {
t.equal(code, 200, 'status set');
return res;
}
}; };
var req = { clean: { a: 'b' }, errors: [], warnings: [] };
var next = function next() { // a controller that validates the esclient and cmd that was passed to the search service
t.equal(req.errors.length, 0, 'next was called without error'); const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
// not that the searchService got called
searchServiceCallCount++;
callback(timeoutError);
}
})(config, esclient, query);
const req = { clean: { }, errors: [], warnings: [] };
const res = {};
var next = function() {
t.equal(searchServiceCallCount, 17+1);
t.end(); t.end();
}; };
controller(req, res, next); controller(req, res, next);
}); });
};
// functionally test controller (backend failure) test('only status code 408 should be considered a retryable request', (t) => {
module.exports.tests.functional_failure = function(test, common) { const config = {
test('functional failure', function(t) { indexName: 'indexName value',
var backend = mockBackend( 'client/search/fail/1', function( cmd ){ requestRetries: 17
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); };
}); const esclient = 'this is the esclient';
var controller = setup( fakeDefaultConfig, backend, mockQuery() ); const query = () => {
var req = { clean: { a: 'b' }, errors: [], warnings: [] }; return { };
var next = function(){
t.equal(req.errors[0],'an elasticsearch error occurred');
t.end();
}; };
controller(req, undefined, next );
});
};
module.exports.tests.timeout = function(test, common) { let searchServiceCallCount = 0;
test('timeout', function(t) {
var backend = mockBackend( 'client/search/timeout/1', function( cmd ){ const nonTimeoutError = {
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); status: 500,
}); displayName: 'InternalServerError',
var controller = setup( fakeDefaultConfig, backend, mockQuery() ); message: 'an internal server error occurred'
var req = { clean: { a: 'b' }, errors: [], warnings: [] }; };
var next = function(){
t.equal(req.errors[0],'Request Timeout after 5000ms'); // a controller that validates the esclient and cmd that was passed to the search service
const controller = proxyquire('../../../controller/search', {
'../service/search': (esclient, cmd, callback) => {
// not that the searchService got called
searchServiceCallCount++;
callback(nonTimeoutError);
}
})(config, esclient, query);
const req = { clean: { }, errors: [], warnings: [] };
const res = {};
var next = function() {
t.equal(searchServiceCallCount, 1);
t.deepEqual(req, {
clean: {},
errors: [nonTimeoutError.message],
warnings: []
});
t.end(); t.end();
}; };
controller(req, undefined, next );
controller(req, res, next);
}); });
}; };
module.exports.tests.existing_errors = function(test, common) { module.exports.tests.existing_errors = function(test, common) {

8
test/unit/mock/backend.js

@ -35,14 +35,6 @@ responses['client/search/fail/1'] = function( cmd, cb ){
return cb( 'an elasticsearch error occurred' ); return cb( 'an elasticsearch error occurred' );
}; };
responses['client/search/timeout/1'] = function( cmd, cb) {
// timeout errors are objects
return cb({
status: 408,
message: 'Request Timeout after 5000ms'
});
};
responses['client/mget/ok/1'] = function( cmd, cb ){ responses['client/mget/ok/1'] = function( cmd, cb ){
return cb( undefined, mgetEnvelope([{ return cb( undefined, mgetEnvelope([{
_id: 'myid1', _id: 'myid1',

Loading…
Cancel
Save