-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from agilecontent/release-sprint73
Release sprint73
- Loading branch information
Showing
7 changed files
with
333 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
const request = require('request'); | ||
|
||
class CacheRemoteObject { | ||
|
||
static create(url, refreshTimeSeconds) { | ||
return new CacheRemoteObject(url, refreshTimeSeconds); | ||
} | ||
|
||
constructor(url, refreshTimeSeconds) { | ||
this.url = url; | ||
this.refreshTimeSeconds = (refreshTimeSeconds) ? refreshTimeSeconds : 0; | ||
this.isRefreshing = false; | ||
this.lastRefresh = null; | ||
this.remoteObject = null; | ||
} | ||
|
||
nextRefresh(context) { | ||
return this.lastRefresh + (1000 * this.refreshTimeSeconds); | ||
} | ||
|
||
isCacheStale(context) { | ||
return this.lastRefresh === null || | ||
this.nextRefresh(context) < new Date().getTime(); | ||
} | ||
|
||
getRemoteObject(context) { | ||
return new Promise((resolve, reject) => { | ||
if (!this.url) { | ||
return reject(new Error('Missing Remote Object URL.')); | ||
} | ||
return request(this.url, (error, response, body) => { | ||
if (error) { | ||
context.logger.error({ err: error }, 'Error calling the remote object service'); | ||
return reject(`Error calling url ${this.url}.`); | ||
} | ||
|
||
if (response.statusCode !== 200) { | ||
context.logger.error(`Error calling the remote object service. HTTP status code: ${response.statusCode}`); | ||
context.logger.info(`HTTP Body: ${body}`); | ||
|
||
return reject(new Error(`Could not get a valid response from ${this.url}.`)); | ||
} | ||
|
||
return resolve(JSON.parse(body)); | ||
}); | ||
}); | ||
} | ||
|
||
refreshCache(context, forceRefresh) { | ||
if (forceRefresh == undefined) { | ||
forceRefresh = false; | ||
} | ||
|
||
// Already refreshing | ||
if (!forceRefresh && this.isRefreshing) { | ||
return Promise.resolve(); | ||
} | ||
|
||
// It is not the time | ||
if (!forceRefresh && !this.isCacheStale(context)) { | ||
return Promise.resolve(); | ||
} | ||
|
||
// Ok, lets refresh | ||
this.isRefreshing = true; | ||
|
||
return this.getRemoteObject(context) | ||
.then((result) => { | ||
this.remoteObject = result; | ||
this.lastRefresh = new Date().getTime(); | ||
|
||
this.isRefreshing = false; | ||
return Promise.resolve(result); | ||
}) | ||
.catch((err) => { | ||
this.isRefreshing = false; | ||
context.logger.error({ err: err }, 'Failed to refresh the remote object'); | ||
return Promise.reject(err); | ||
}); | ||
} | ||
|
||
getCached(context) { | ||
this.refreshCache(context); //async call | ||
|
||
return this.remoteObject; | ||
} | ||
|
||
getFresh(context) { | ||
return this.refreshCache(context, true); | ||
} | ||
|
||
} | ||
|
||
module.exports = CacheRemoteObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
'use strict'; | ||
/* global describe, before, it, beforeEach*/ | ||
|
||
const nock = require('nock'); | ||
const should = require('should'); | ||
const CacheRemoteObject = require('../../lib/cache-remote-object'); | ||
const uuid = require('uuid').v4; | ||
const tools = require('../../lib/index'); | ||
const sleep = require('sleep'); | ||
|
||
const objectServerUrl = 'http://remote.object'; | ||
const objectUrl = 'http://remote.object/remoteobject.json'; | ||
const notFoundObjectUrl = 'http://remote.object/notfound.json'; | ||
const invalidObjectUrl = 'http://object.invalid/invalid.json'; | ||
const objectRefreshTime = 2; | ||
|
||
let callId = uuid(); | ||
let config = { key: 'value' }; | ||
let logger = tools.createLogger(); | ||
let serviceLocator = tools.createServiceLocator(); | ||
let context = tools.createCallContext(callId, config, logger, serviceLocator); | ||
|
||
const remoteObjectValue = { | ||
result: { | ||
key1: 'value1', | ||
key2: 'value2', | ||
key3: 3, | ||
key4: true | ||
} | ||
}; | ||
|
||
describe('Cache Remote Object', function () { | ||
describe('.create', function () { | ||
it('return an CacheRemoteObject object', () => { | ||
let remoteObject = CacheRemoteObject.create(objectUrl, objectRefreshTime); | ||
remoteObject.should.be.an.instanceOf(CacheRemoteObject); | ||
}); | ||
}); | ||
describe('.nextRefresh', function () { | ||
it('return correct time for next refresh', () => { | ||
let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime); | ||
|
||
remoteObject.lastRefresh = new Date().getTime(); | ||
should.equal(remoteObject.nextRefresh(context), remoteObject.lastRefresh + (1000 * objectRefreshTime)); | ||
}); | ||
}); | ||
|
||
describe('.isCacheStale', function () { | ||
beforeEach(function () { | ||
nock(objectServerUrl) | ||
.get('/remoteobject.json') | ||
.reply(200, remoteObjectValue); | ||
}); | ||
|
||
it('should return true on first call', () => { | ||
let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime); | ||
|
||
should.equal(remoteObject.isCacheStale(context), true); | ||
}); | ||
|
||
it('should return false if cache previously refreshed', (done) => { | ||
let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime); | ||
|
||
remoteObject.getFresh(context) | ||
.then((result) => { | ||
should.deepEqual(result, remoteObjectValue); | ||
should.equal(remoteObject.isCacheStale(context), false); | ||
done(); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('should return true if cache previously outdated', (done) => { | ||
let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime); | ||
|
||
remoteObject.getFresh(context) | ||
.then((result) => { | ||
should.deepEqual(result, remoteObjectValue); | ||
sleep.sleep(objectRefreshTime + 1); | ||
should.ok(remoteObject.isCacheStale(context)); | ||
done(); | ||
}).catch((err) => { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('.getCached', function () { | ||
before(function () { | ||
nock(objectServerUrl) | ||
.get('/remoteobject.json') | ||
.reply(200, remoteObjectValue); | ||
}); | ||
|
||
it('First call should return null.', function () { | ||
let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime); | ||
|
||
let cached = remoteObject.getCached(context); | ||
should.equal(cached, null); | ||
sleep.sleep(objectRefreshTime + 2); | ||
}); | ||
}); | ||
|
||
describe('.getFresh', function () { | ||
before(function () { | ||
nock(objectServerUrl) | ||
.get('/remoteobject.json') | ||
.times(1) | ||
.reply(200, remoteObjectValue); | ||
|
||
nock(objectServerUrl) | ||
.get('/notfound.json') | ||
.reply(404, 'Not Found'); | ||
}); | ||
|
||
it('should get error for empty url', function (done) { | ||
let remoteObject = new CacheRemoteObject(null, objectRefreshTime); | ||
|
||
remoteObject.getFresh(context) | ||
.then((result) => { | ||
done(new Error('Missing Remote Object URL.')); | ||
}) | ||
.catch((err) => { | ||
should.equal(err.message, 'Missing Remote Object URL.'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
it('should get error for not found url', function (done) { | ||
let remoteObject = new CacheRemoteObject(notFoundObjectUrl, objectRefreshTime); | ||
|
||
remoteObject.getFresh(context) | ||
.then((result) => { | ||
done(new Error('Should not resolve when url not found.')); | ||
}) | ||
.catch((err) => { | ||
should.equal(err.message, `Could not get a valid response from ${notFoundObjectUrl}.`); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
it('should get error for invalid url', function (done) { | ||
let remoteObject = new CacheRemoteObject(invalidObjectUrl, objectRefreshTime); | ||
|
||
remoteObject.getFresh(context) | ||
.then((result) => { | ||
done(new Error('Should not resolve when url is invalid.')); | ||
}) | ||
.catch((err) => { | ||
should.equal(err, `Error calling url ${invalidObjectUrl}.`); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
it('should return object : cached and not cached.', function (done) { | ||
let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime); | ||
|
||
remoteObject.getFresh(context) | ||
.then((result) => { | ||
should.deepEqual(result, remoteObjectValue); | ||
remoteObjectValue.result.key5 = 'newvalue'; | ||
|
||
let cached = () => { | ||
return remoteObject.getCached(context); | ||
}; | ||
|
||
let notCached = () => { | ||
nock.cleanAll(); | ||
sleep.sleep((objectRefreshTime + 1)); | ||
nock(objectServerUrl) | ||
.get('/remoteobject.json') | ||
.reply(200, remoteObjectValue); | ||
|
||
return remoteObject.getFresh(context); | ||
}; | ||
|
||
return Promise.all([cached(), notCached()]); | ||
}) | ||
.then((results) => { | ||
should.equal(remoteObjectValue.result.key5, 'newvalue'); | ||
should.notDeepEqual(results[0], remoteObjectValue); | ||
should.deepEqual(results[1], remoteObjectValue); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.