Skip to content

Commit

Permalink
Merge pull request #51 from Financial-Times/lambda-friendly
Browse files Browse the repository at this point in the history
Allow option to prevent periodic checking of vanity databases
  • Loading branch information
debugwand authored Feb 6, 2018
2 parents 23a63a4 + 3ee34b0 commit 27966cc
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ Expects:-
- timeout [Number]
- connectTimeout [Number]
- poolConnextions [Boolean]
- raceOnce [Boolean] = don't keep checking up on the health of the dynamodbs. Use for a lambda environment
4 changes: 3 additions & 1 deletion lib/active.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function init (opts) {
metrics = opts.metrics;
dynamoInUse = 'master';
totalFailure = false;
setInterval(raceDynamos, 2*60*1000);
if (!opts.raceOnce) {
setInterval(raceDynamos, 2*60*1000);
}
raceDynamos();
}
5 changes: 2 additions & 3 deletions test/active.single-region-failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const active = proxyquire('../lib/active', {
table: 'urlmgmtapi_master',
instance: {
getItem: (opts, cb) => {
setTimeout(() => cb(new Error('master failure')), 100)
setTimeout(() => cb(new Error('master failure')), 100);
}
}
},
slave: {
table: 'urlmgmtapi_slave',
instance: {
getItem: (opts, cb) => {
setTimeout(() => cb(null, itemFixture), 300)
setTimeout(() => cb(null, itemFixture), 300);
}
}
}
Expand All @@ -45,5 +45,4 @@ describe('#active in a single region failure mode', () => {
done();
}, 500);
});

});
26 changes: 19 additions & 7 deletions test/active.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const proxyquire = require('proxyquire');
const expect = require('chai').expect;
const itemFixture = require('./fixtures/fastft.json');
const metricsMock = require('./utils/metrics-mock');
const sinon = require('sinon')
const sinon = require('sinon');
const dynamosInitStub = sinon.stub();

const active = proxyquire('../lib/active', {
Expand All @@ -17,28 +17,28 @@ const active = proxyquire('../lib/active', {
table: 'urlmgmtapi_master',
instance: {
getItem: (opts, cb) => {
setTimeout(() => cb(null, itemFixture), 300)
setTimeout(() => cb(null, itemFixture), 300);
}
}
},
slave: {
table: 'urlmgmtapi_slave',
instance: {
getItem: (opts, cb) => {
setTimeout(() => cb(null, itemFixture), 100)
setTimeout(() => cb(null, itemFixture), 100);
}
}
}
}
});

describe('#active', () => {
const opts = { metrics: metricsMock }
const opts = { metrics: metricsMock };
before(() => active.init(opts));

it('should pass options to dynamos', () => {
expect(dynamosInitStub.calledWith(opts)).to.be.true
})
expect(dynamosInitStub.calledWith(opts)).to.be.true;
});

it('should start off being ‘master’', () => {
expect(active()).to.eql('master');
Expand All @@ -52,6 +52,18 @@ describe('#active', () => {
}, 500);
});

it('should set up a set interval', () => {
sinon.stub(global, 'setInterval');
active.init(opts);
expect(setInterval.called).to.be.true;
global.setInterval.restore();
});


it('should not run the set interval if raceOnce option is true', () => {
sinon.stub(global, 'setInterval');
const opts = { raceOnce: true, metrics: metricsMock };
active.init(opts);
expect(setInterval.called).to.be.false;
global.setInterval.restore();
});
});
10 changes: 5 additions & 5 deletions test/fixtures/encodedUrl.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Item": {
"FromURL": { "S": "https://www.ft.com/fästft" },
"Code": { "N": "100" },
"ToURL": { "S": "https://www.ft.com/stream/brandId/NTlhNzEyMzMtZjBjZi00Y2U1LTg0ODUtZWVjNmEyYmU1NzQ2-QnJhbmRzä" }
}
"Item": {
"FromURL": { "S": "https://www.ft.com/fästft" },
"Code": { "N": "100" },
"ToURL": { "S": "https://www.ft.com/stream/brandId/NTlhNzEyMzMtZjBjZi00Y2U1LTg0ODUtZWVjNmEyYmU1NzQ2-QnJhbmRzä" }
}
}
10 changes: 5 additions & 5 deletions test/fixtures/fastft.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Item": {
"FromURL": { "S": "https://www.ft.com/fastft" },
"Code": { "N": "100" },
"ToURL": { "S": "https://www.ft.com/stream/brandId/NTlhNzEyMzMtZjBjZi00Y2U1LTg0ODUtZWVjNmEyYmU1NzQ2-QnJhbmRz" }
}
"Item": {
"FromURL": { "S": "https://www.ft.com/fastft" },
"Code": { "N": "100" },
"ToURL": { "S": "https://www.ft.com/stream/brandId/NTlhNzEyMzMtZjBjZi00Y2U1LTg0ODUtZWVjNmEyYmU1NzQ2-QnJhbmRz" }
}
}
10 changes: 5 additions & 5 deletions test/fixtures/justasfastft.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Item": {
"FromURL": { "S": "https://www.ft.com/justasfastft" },
"Code": { "N": "100" },
"ToURL": { "S": "https://www.ft.com/stream/brandId/NTlhNzEyMzMtZjBjZi00Y2U1LTg0ODUtZWVjNmEyYmU1NzQ2-QnJhbmRz" }
}
"Item": {
"FromURL": { "S": "https://www.ft.com/justasfastft" },
"Code": { "N": "100" },
"ToURL": { "S": "https://www.ft.com/stream/brandId/NTlhNzEyMzMtZjBjZi00Y2U1LTg0ODUtZWVjNmEyYmU1NzQ2-QnJhbmRz" }
}
}

0 comments on commit 27966cc

Please sign in to comment.