UNPKG

fmem-mapcache-redis

Version:
241 lines (196 loc) 6.95 kB
"use strict"; var chai = require('chai'); var expect = chai.expect; var should = chai.should; var FMem = require('fmem'); var RedisMapCache = require('../lib/mapcache-redis'); var redisMapCacheConfig = { redis:{ host: "127.0.0.1", port: "6379", db: 1 } }; describe('FMem', function() { beforeEach(function(done) { var mc = new RedisMapCache(redisMapCacheConfig); mc.clear(function(err){ done(); }) }); describe('Basic Cache Test', function() { it('memorized function call should return the identical result with non-wrapped', function(done) { this.timeout(1000); var fmem = new FMem({ cache: new RedisMapCache(redisMapCacheConfig) }) var count = 0; var func = function(val, cb){ count ++; setTimeout( () => cb(null, val*val), 100 ); } var funcWrapped = fmem.memorizeWrapCB(func); var result, resultWrapped; func(2, function(err, val){ result = val; funcWrapped(2, function(err, val){ resultWrapped = val; expect(result).to.equal(resultWrapped); fmem.s.mapcache.clear(()=>{}); funcWrapped(2, function(err, val){ resultWrapped = val; expect(result).to.equal(resultWrapped); funcWrapped(2, function(err, val){ resultWrapped = val; expect(result).to.equal(resultWrapped); expect(count).to.equal(3); done(); // this should take bit over 300ms (100ms x 3times) }) }) }) }); }) }) describe('Basic Cache Test Async', function() { it('memorized function call should return the identical result with non-wrapped', function(done) { this.timeout(200); var fmem = new FMem({ cache: new RedisMapCache(redisMapCacheConfig) }) var count = 0; var func = function(val){ return new Promise(function(resolve, reject){ count++; setTimeout( () => resolve(val*val), 100 ); }) } var funcWrapped = fmem.memorizeWrapAsync(func); var result, resultWrapped; funcWrapped(2); // 1 funcWrapped(2); funcWrapped(2); funcWrapped(2); funcWrapped(2); funcWrapped(2); funcWrapped(2); Promise.all([ func(2) // 2 .then( (v) => result=v ) , funcWrapped(2) .then( (v) => resultWrapped=v ) ]) .then( (v) => {expect(result).to.equal(resultWrapped); expect(count).to.equal(2); } ) .then( () => done() ) .catch( done ) // this test should take bit over 100ms (2 function calls are parallet and multiple async wrapped function should make only one actuall call.) }) }) describe('PurgeFunction Test', function() { it('memorized function call should return the identical result with non-wrapped', function(done) { var fmem = new FMem({ cache: new RedisMapCache(redisMapCacheConfig) }) var count = 0; var data = 'dataFirst'; var dataUpdated = 'dataSecond'; var funcRead = function(val, cb){ count ++; setTimeout( () => cb(null, data+val.toString()), 100 ); } var funcUpdate = function(val, cb){ data = dataUpdated; cb(null, data); } var funcWrapped = fmem.memorizeWrapCB(funcRead); var purgeWrapped = fmem.purgeWrapCB(funcUpdate, [ function (inputArgs, resultArgs) { var args; fmem.clearMemorize(funcRead, inputArgs, function(e){ }); } ] ); var result, resultWrapped; funcRead(2, function(err, val){ result = val; funcWrapped(2, function(err, val){ resultWrapped = val; expect(result).to.equal(resultWrapped); purgeWrapped(2, function(err, val){ funcWrapped(2, function(err, val){ resultWrapped = val; expect(result).to.not.equal(resultWrapped); result = resultWrapped; funcWrapped(2, function(err, val){ resultWrapped = val; expect(result).to.equal(resultWrapped); expect(count).to.equal(3); done(); // this should take bit over 300ms (100ms x 3times) }) }) }) }) }); }) }) describe('PurgeFunction Test Async', function() { it('memorized function call should return the identical result with non-wrapped', function(done) { this.timeout(1000); var fmem = new FMem({ cache: new RedisMapCache(redisMapCacheConfig) }) var count = 0; var data = 'dataFirst'; var dataUpdated = 'dataSecond'; var funcRead = function(val){ return new Promise(function(resolve, reject){ count++; setTimeout( () => resolve(data+val.toString()), 100 ); }) } var funcUpdate = function(val){ return new Promise(function(resolve, rejecT){ data = dataUpdated; resolve(data); }) } var funcWrapped = fmem.memorizeWrapAsync(funcRead); var purgeWrapped = fmem.purgeWrapAsync(funcUpdate, (v) => { return new Promise(function(resolve, reject){ data = dataUpdated; fmem.clearMemorize(funcRead, v.i, function(e){ }); resolve(v); }) }); funcRead(2) //1 .then( (v) => {expect(v).to.equal('dataFirst2')} ) .then( () => funcWrapped(2) ) //2 .then( (v) => {expect(v).to.equal('dataFirst2')} ) .then( () => funcWrapped(2) ) .then( (v) => {expect(v).to.equal('dataFirst2')} ) .then( () => funcWrapped(2) ) .then( (v) => {expect(v).to.equal('dataFirst2')} ) .then( () => funcWrapped(2) ) .then( (v) => {expect(v).to.equal('dataFirst2')} ) .then( () => Promise.all([ funcRead(2), //3 funcWrapped(2), funcWrapped(2), funcWrapped(2), funcWrapped(2), funcWrapped(2) ]) ) .then( () => purgeWrapped(2) ) // data updated and the cache is cleared .then( () => funcWrapped(2) ) // 4 cache recreated with udpated data .then( (v) => {expect(v).to.equal('dataSecond2')} ) .then( () => funcWrapped(2) ) .then( (v) => {expect(v).to.equal('dataSecond2')} ) .then( () => funcWrapped(2) ) .then( (v) => {expect(v).to.equal('dataSecond2')} ) .then( (v) => {expect(count).to.equal(4);} ) .then( () => done() ) .catch( done ) // the test time should be little bit over 400ms (100ms x 4times + overhead) }) }) })