Skip to content

Commit

Permalink
fix(mojaloop/#3474): correct caching TTL and upgrade library (#966)
Browse files Browse the repository at this point in the history
* fix: correct caching TTL and upgrade library

* fix: update tests for catbox-memory lib upgrade

* fix: upgrade package-lock.json file

* fix: upgrade package-lock.json file

* Update package.json

---------

Co-authored-by: Kevin Leyow <[email protected]>
  • Loading branch information
aaronreynoza and kleyow authored Sep 4, 2023
1 parent bdf54ef commit b24497e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
18 changes: 4 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"Sam Kummary <[email protected]>",
"Shashikant Hirugade <[email protected]>",
"Steven Oderayi <[email protected]>",
"Valentin Genev <[email protected]>"
"Valentin Genev <[email protected]>",
"Aaron Reynoza <[email protected]>"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -80,6 +81,7 @@
"@hapi/inert": "7.1.0",
"@hapi/joi": "17.1.1",
"@hapi/vision": "7.0.3",
"@hapi/catbox-memory": "6.0.1",
"@mojaloop/central-services-database": "10.7.0",
"@mojaloop/central-services-error-handling": "12.0.7",
"@mojaloop/central-services-health": "14.0.2",
Expand All @@ -95,7 +97,6 @@
"ajv-keywords": "5.1.0",
"base64url": "3.0.1",
"blipp": "4.0.2",
"catbox-memory": "4.0.1",
"commander": "11.0.0",
"cron": "2.4.3",
"decimal.js": "10.4.3",
Expand Down
8 changes: 3 additions & 5 deletions src/lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const CatboxMemory = require('catbox-memory')
const CatboxMemory = require('@hapi/catbox-memory')
const Config = require('../lib/config')

let enabled = true
Expand Down Expand Up @@ -31,7 +31,7 @@ class CacheClient {
}

set (key, value) {
catboxMemoryClient.set(key, value, ttl)
catboxMemoryClient.set(key, value, parseInt(ttl))
}

drop (key) {
Expand Down Expand Up @@ -62,9 +62,7 @@ const initCache = async function () {
enabled = Config.CACHE_CONFIG.CACHE_ENABLED

// Init catbox.
// Note: The strange looking "module.exports.CatboxMemory" reference
// simplifies the setup of tests.
catboxMemoryClient = new module.exports.CatboxMemory({
catboxMemoryClient = new CatboxMemory.Engine({
maxByteSize: Config.CACHE_CONFIG.MAX_BYTE_SIZE
})
catboxMemoryClient.start()
Expand Down
30 changes: 15 additions & 15 deletions test/unit/lib/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ Test('Cache test', async (cacheTest) => {

await cacheTest.test('Cache should', async (initTest) => {
await initTest.test('call constructor of CatboxMemory', async (test) => {
sandbox.stub(Cache.CatboxMemory)
const catboxMemoryConstructorSpy = sandbox.spy(Cache, 'CatboxMemory')
sandbox.stub(Cache.CatboxMemory.Engine)
const catboxMemoryConstructorSpy = sandbox.spy(Cache.CatboxMemory, 'Engine')
await Cache.initCache()
test.ok(catboxMemoryConstructorSpy.calledOnce)
await Cache.destroyCache()
test.end()
})

await initTest.test('init+start and then stop CatboxMemory', async (test) => {
sandbox.spy(Cache.CatboxMemory.prototype, 'start')
sandbox.spy(Cache.CatboxMemory.prototype, 'stop')
sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'start')
sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'stop')
await Cache.initCache()
test.ok(Cache.CatboxMemory.prototype.start.calledOnce)
test.notOk(Cache.CatboxMemory.prototype.stop.calledOnce)
test.ok(Cache.CatboxMemory.Engine.prototype.start.calledOnce)
test.notOk(Cache.CatboxMemory.Engine.prototype.stop.calledOnce)
await Cache.destroyCache()
test.ok(Cache.CatboxMemory.prototype.start.calledOnce)
test.ok(Cache.CatboxMemory.prototype.stop.calledOnce)
test.ok(Cache.CatboxMemory.Engine.prototype.start.calledOnce)
test.ok(Cache.CatboxMemory.Engine.prototype.stop.calledOnce)
test.end()
})
initTest.end()
Expand Down Expand Up @@ -72,7 +72,7 @@ Test('Cache test', async (cacheTest) => {

await cacheClientTest.test('get() should call Catbox Memory get()', async (test) => {
Config.CACHE_CONFIG.CACHE_ENABLED = true
const getSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'get')
const getSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'get')

const cacheClient = Cache.registerCacheClient({
id: 'testCacheClient',
Expand All @@ -93,7 +93,7 @@ Test('Cache test', async (cacheTest) => {

await cacheClientTest.test('get() should NOT call Catbox Memory get() when cache is disabled', async (test) => {
Config.CACHE_CONFIG.CACHE_ENABLED = false
const getSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'get')
const getSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'get')

const cacheClient = Cache.registerCacheClient({
id: 'testCacheClient',
Expand All @@ -114,8 +114,8 @@ Test('Cache test', async (cacheTest) => {

await cacheClientTest.test('set() should call Catbox Memory set() and should work', async (test) => {
Config.CACHE_CONFIG.CACHE_ENABLED = true
const getSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'get')
const setSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'set')
const getSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'get')
const setSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'set')
const cacheClient = Cache.registerCacheClient({
id: 'testCacheClient',
preloadCache: async () => {}
Expand Down Expand Up @@ -145,9 +145,9 @@ Test('Cache test', async (cacheTest) => {

await cacheClientTest.test('drop() works', async (test) => {
Config.CACHE_CONFIG.CACHE_ENABLED = true
const getSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'get')
const setSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'set')
const dropSpy = sandbox.spy(Cache.CatboxMemory.prototype, 'drop')
const getSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'get')
const setSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'set')
const dropSpy = sandbox.spy(Cache.CatboxMemory.Engine.prototype, 'drop')
const cacheClient = Cache.registerCacheClient({
id: 'testCacheClient',
preloadCache: async () => {}
Expand Down

0 comments on commit b24497e

Please sign in to comment.