Skip to content

Commit

Permalink
error handler for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Evheniy Bystrov committed Feb 28, 2017
1 parent d28921a commit 4e26e6d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 14 deletions.
33 changes: 20 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = class {
},
async getCacheDate() {},
async updateCacheDate() {},
timeoutErrorHandler() {},
ttl: 86400, // 24 hours
},
config
Expand Down Expand Up @@ -48,25 +49,31 @@ module.exports = class {
const timeNow = new Date().valueOf();
debug('Time now: %s', timeNow);

const time = await this.config.getCacheDate();
debug('Cached time: %s', time);
try {
const time = await this.config.getCacheDate();
debug('Cached time: %s', time);

const timeWithTTL = ((parseInt(time, 10) || new Date().valueOf()) + parseInt(this.config.ttl, 10));
debug('Time with TTL: %s', timeWithTTL);
const timeWithTTL = ((parseInt(time, 10) || new Date().valueOf()) + parseInt(this.config.ttl, 10));
debug('Time with TTL: %s', timeWithTTL);

const isDateActual = timeWithTTL > new Date().valueOf();
debug(isDateActual);
const isDateActual = timeWithTTL > new Date().valueOf();
debug(isDateActual);

if (!isDateActual) {
if (!isDateActual) {

debug('Updating data');
await this.config.setDataToCache(
await this.config.getDataFromSource()
);
debug('Updating data');
await this.config.setDataToCache(
await this.config.getDataFromSource()
);

debug('Updating date');
await this.config.updateCacheDate();
debug('Updating date');
await this.config.updateCacheDate();
}

} catch (error) {
this.config.timeoutErrorHandler(error);
}

},
0
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "offline-first-storage",
"version": "0.0.3",
"version": "0.0.4",
"description": "Offline first storage",
"main": "index.js",
"scripts": {
Expand Down
111 changes: 111 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,115 @@ describe('Offline first storage', () => {
expect(data).to.be.equal('test');
});

it('should test flow with timeoutErrorHandler', async () => {
let isGetDataFromCache = false;
let isGetDataFromSource = false;
let isSetDataToCache = false;
let isGetCacheDate = false;
let isUpdateCacheDate = false;
let isTimeoutErrorHandler = false;

const timeNow = new Date().valueOf().toString(10);

await redis.set(key, 'test');
await redis.set(`date_${key}`, timeNow);

const ofs = new OFS({
async getDataFromSource() {
isGetDataFromSource = true;
return 'test';
},
async getDataFromCache() {
isGetDataFromCache = true;
return redis.get(key);
},

async setDataToCache(data) {
isSetDataToCache = true;
await redis.set(key, data);
},
async getCacheDate() {
isGetCacheDate = true;
return redis.get(`date_${key}`);
},
async updateCacheDate() {
isUpdateCacheDate = true;
await redis.set(`date_${key}`, timeNow);
throw new Error('test');
},
timeoutErrorHandler() {
isTimeoutErrorHandler = true;
},
ttl: 0,
});

const data = await ofs.getData();

await pause(100);

expect(isGetDataFromCache).is.true;
expect(isGetDataFromSource).is.true;
expect(isSetDataToCache).is.true;
expect(isGetCacheDate).is.true;
expect(isUpdateCacheDate).is.true;
expect(isTimeoutErrorHandler).is.true;

expect(data).to.be.equal('test');
});

it('should test flow with timeoutErrorHandler without time record', async () => {
let isGetDataFromCache = false;
let isGetDataFromSource = false;
let isSetDataToCache = false;
let isGetCacheDate = false;
let isUpdateCacheDate = false;
let isTimeoutErrorHandler = false;

const timeNow = new Date().valueOf().toString(10);

await redis.set(key, 'test');

const ofs = new OFS({
async getDataFromSource() {
isGetDataFromSource = true;
return 'test';
},
async getDataFromCache() {
isGetDataFromCache = true;
return redis.get(key);
},

async setDataToCache(data) {
isSetDataToCache = true;
await redis.set(key, data);
},
async getCacheDate() {
isGetCacheDate = true;
return redis.get(`date_${key}`);
},
async updateCacheDate() {
isUpdateCacheDate = true;
await redis.set(`date_${key}`, timeNow);
throw new Error('test');
},
timeoutErrorHandler() {
isTimeoutErrorHandler = true;
},
ttl: 0,
});

const data = await ofs.getData();

await pause(100);

expect(isGetDataFromCache).is.true;
expect(isGetDataFromSource).is.true;
expect(isSetDataToCache).is.true;
expect(isGetCacheDate).is.true;
expect(isUpdateCacheDate).is.true;
expect(isTimeoutErrorHandler).is.true;

expect(data).to.be.equal('test');
});

});

0 comments on commit 4e26e6d

Please sign in to comment.