Skip to content

Commit

Permalink
feat: proxy cache update (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
oderayi authored Jul 18, 2024
1 parent ceebe58 commit 184e086
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 89 deletions.
23 changes: 18 additions & 5 deletions src/lib/proxyCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ const { createProxyCache } = require('@mojaloop/inter-scheme-proxy-cache-lib')
const Config = require('./config.js')
const ParticipantService = require('../../src/domain/participant')

const proxyCache = createProxyCache(
Config.PROXY_CACHE_CONFIG.type,
Config.PROXY_CACHE_CONFIG.proxyConfig
)
let proxyCache

const connect = async () => {
return getCache().connect()
}

const disconnect = async () => {
return proxyCache?.isConnected && proxyCache.disconnect()
}

const getCache = () => {
if (!proxyCache) {
proxyCache = Object.freeze(createProxyCache(
Config.PROXY_CACHE_CONFIG.type,
Config.PROXY_CACHE_CONFIG.proxyConfig
))
}
return proxyCache
}

Expand All @@ -26,7 +38,8 @@ const checkSameCreditorDebtorProxy = async (debtorDfspId, creditorDfspId) => {
}

module.exports = {
proxyCache,
connect,
disconnect,
getCache,
getFSPProxy,
checkSameCreditorDebtorProxy
Expand Down
4 changes: 2 additions & 2 deletions src/shared/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ const initialize = async function ({ service, port, modules = [], runMigrations
await connectMongoose()
await initializeCache()
if (Config.PROXY_CACHE_CONFIG.enabled) {
await ProxyCache.getCache().connect()
await ProxyCache.connect()
}

let server
Expand Down Expand Up @@ -308,7 +308,7 @@ const initialize = async function ({ service, port, modules = [], runMigrations

await Db.disconnect()
if (Config.PROXY_CACHE_CONFIG.enabled) {
await ProxyCache.getCache().connect()
await ProxyCache.disconnect()
}
process.exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1839,10 +1839,7 @@ Test('Handlers test', async handlersTest => {
await testConsumer.destroy() // this disconnects the consumers

await Producer.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {
}
await ProxyCache.disconnect()

if (debug) {
const elapsedTime = Math.round(((new Date()) - startTime) / 100) / 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ Test('FxFulfil flow Integration Tests -->', async fxFulfilTest => {
producer.disconnect(),
testConsumer.destroy()
])
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {
}
await ProxyCache.disconnect()
await new Promise(resolve => setTimeout(resolve, 5_000))
t.pass('teardown is finished')
t.end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,7 @@ Test('Handlers test', async handlersTest => {
await testConsumer.destroy() // this disconnects the consumers

await Producer.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {
}
await ProxyCache.disconnect()

if (debug) {
const elapsedTime = Math.round(((new Date()) - startTime) / 100) / 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,7 @@ Test('Handlers test', async handlersTest => {
await testConsumer.destroy() // this disconnects the consumers

await Producer.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {
}
await ProxyCache.disconnect()

if (debug) {
const elapsedTime = Math.round(((new Date()) - startTime) / 100) / 10
Expand Down
35 changes: 18 additions & 17 deletions test/integration-override/lib/proxyCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Test('Participant service', async (participantTest) => {
await ParticipantCurrencyCached.initialize()
await ParticipantLimitCached.initialize()
await Cache.initCache()
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
test.pass()
test.end()
} catch (err) {
Expand Down Expand Up @@ -84,11 +84,12 @@ Test('Participant service', async (participantTest) => {

await participantTest.test('getFSPProxy should return proxyId if fsp not in scheme', async (assert) => {
try {
ProxyCache.proxyCache.addDfspIdToProxyMapping('notInSchemeFsp', 'proxyId')
const proxyCache = ProxyCache.getCache()
proxyCache.addDfspIdToProxyMapping('notInSchemeFsp', 'proxyId')
const result = await ProxyCache.getFSPProxy('notInSchemeFsp')
assert.equal(result.inScheme, false, 'not in scheme')
assert.equal(result.proxyId, 'proxyId', 'proxy id matches')
ProxyCache.proxyCache.removeDfspIdFromProxyMapping('notInSchemeFsp')
proxyCache.removeDfspIdFromProxyMapping('notInSchemeFsp')
assert.end()
} catch (err) {
Logger.error(`create participant failed with error - ${err}`)
Expand All @@ -99,11 +100,12 @@ Test('Participant service', async (participantTest) => {

await participantTest.test('getFSPProxy should not return proxyId if fsp is in scheme', async (assert) => {
try {
ProxyCache.proxyCache.addDfspIdToProxyMapping('dfsp1', 'proxyId')
const proxyCache = ProxyCache.getCache()
proxyCache.addDfspIdToProxyMapping('dfsp1', 'proxyId')
const result = await ProxyCache.getFSPProxy('dfsp1')
assert.equal(result.inScheme, true, 'is in scheme')
assert.equal(result.proxyId, null, 'proxy id is null')
ProxyCache.proxyCache.removeDfspIdFromProxyMapping('dfsp1')
proxyCache.removeDfspIdFromProxyMapping('dfsp1')
assert.end()
} catch (err) {
Logger.error(`create participant failed with error - ${err}`)
Expand All @@ -114,12 +116,13 @@ Test('Participant service', async (participantTest) => {

await participantTest.test('checkSameCreditorDebtorProxy should return true if debtor and creditor proxy are the same', async (assert) => {
try {
ProxyCache.proxyCache.addDfspIdToProxyMapping('dfsp1', 'proxyId')
ProxyCache.proxyCache.addDfspIdToProxyMapping('dfsp2', 'proxyId')
const proxyCache = ProxyCache.getCache()
proxyCache.addDfspIdToProxyMapping('dfsp1', 'proxyId')
proxyCache.addDfspIdToProxyMapping('dfsp2', 'proxyId')
const result = await ProxyCache.checkSameCreditorDebtorProxy('dfsp1', 'dfsp2')
assert.equal(result, true, 'returned true')
ProxyCache.proxyCache.removeDfspIdFromProxyMapping('dfsp1')
ProxyCache.proxyCache.removeDfspIdFromProxyMapping('dfsp2')
proxyCache.removeDfspIdFromProxyMapping('dfsp1')
proxyCache.removeDfspIdFromProxyMapping('dfsp2')
assert.end()
} catch (err) {
Logger.error(`create participant failed with error - ${err}`)
Expand All @@ -130,12 +133,13 @@ Test('Participant service', async (participantTest) => {

await participantTest.test('checkSameCreditorDebtorProxy should return false if debtor and creditor proxy are not the same', async (assert) => {
try {
ProxyCache.proxyCache.addDfspIdToProxyMapping('dfsp1', 'proxyId')
ProxyCache.proxyCache.addDfspIdToProxyMapping('dfsp2', 'proxyId2')
const proxyCache = ProxyCache.getCache()
proxyCache.addDfspIdToProxyMapping('dfsp1', 'proxyId')
proxyCache.addDfspIdToProxyMapping('dfsp2', 'proxyId2')
const result = await ProxyCache.checkSameCreditorDebtorProxy('dfsp1', 'dfsp2')
assert.equal(result, false, 'returned false')
ProxyCache.proxyCache.removeDfspIdFromProxyMapping('dfsp1')
ProxyCache.proxyCache.removeDfspIdFromProxyMapping('dfsp2')
proxyCache.removeDfspIdFromProxyMapping('dfsp1')
proxyCache.removeDfspIdFromProxyMapping('dfsp2')
assert.end()
} catch (err) {
Logger.error(`create participant failed with error - ${err}`)
Expand All @@ -159,10 +163,7 @@ Test('Participant service', async (participantTest) => {
}
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {
}
await ProxyCache.disconnect()

assert.pass('database connection closed')
// @ggrg: Having the following 3 lines commented prevents the current test from exiting properly when run individually,
Expand Down
8 changes: 2 additions & 6 deletions test/integration/domain/participant/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Test('Participant service', async (participantTest) => {
try {
sandbox = Sinon.createSandbox()
await Db.connect(Config.DATABASE)
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await ParticipantCached.initialize()
await ParticipantCurrencyCached.initialize()
await ParticipantLimitCached.initialize()
Expand Down Expand Up @@ -467,11 +467,7 @@ Test('Participant service', async (participantTest) => {
}
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()

assert.pass('database connection closed')
// @ggrg: Having the following 3 lines commented prevents the current test from exiting properly when run individually,
Expand Down
8 changes: 2 additions & 6 deletions test/integration/handlers/root.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Test('Root handler test', async handlersTest => {
await handlersTest.test('registerAllHandlers should', async registerAllHandlers => {
await registerAllHandlers.test('setup handlers', async (test) => {
await Db.connect(Config.DATABASE)
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await Handlers.transfers.registerPrepareHandler()
await Handlers.positions.registerPositionHandler()
await Handlers.transfers.registerFulfilHandler()
Expand Down Expand Up @@ -115,11 +115,7 @@ Test('Root handler test', async handlersTest => {
try {
await Db.disconnect()
assert.pass('database connection closed')
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
// TODO: Replace this with KafkaHelper.topics
const topics = [
'topic-transfer-prepare',
Expand Down
8 changes: 2 additions & 6 deletions test/integration/handlers/transfers/handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ const prepareTestData = async (dataObj) => {
Test('Handlers test', async handlersTest => {
const startTime = new Date()
await Db.connect(Config.DATABASE)
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await ParticipantCached.initialize()
await ParticipantCurrencyCached.initialize()
await ParticipantLimitCached.initialize()
Expand Down Expand Up @@ -1348,11 +1348,7 @@ Test('Handlers test', async handlersTest => {
await Handlers.timeouts.stop()
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
assert.pass('database connection closed')
await testConsumer.destroy() // this disconnects the consumers

Expand Down
2 changes: 1 addition & 1 deletion test/integration/helpers/settlementModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const settlementModels = [

exports.prepareData = async () => {
await Db.connect(Config.DATABASE)
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await Enums.initialize()
await ParticipantCached.initialize()
await ParticipantCurrencyCached.initialize()
Expand Down
8 changes: 2 additions & 6 deletions test/integration/models/transfer/facade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Test('Transfer read model test', async (transferReadModelTest) => {
try {
await Db.connect(Config.DATABASE).then(async () => {
await Cache.initCache()
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
transferPrepareResult = await HelperModule.prepareNeededData('transferModel')
assert.pass('setup OK')
assert.end()
Expand Down Expand Up @@ -90,11 +90,7 @@ Test('Transfer read model test', async (transferReadModelTest) => {
try {
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
assert.pass('database connection closed')
assert.end()
} catch (err) {
Expand Down
8 changes: 2 additions & 6 deletions test/integration/models/transfer/ilpPacket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Test('Ilp service tests', async (ilpTest) => {

await ilpTest.test('setup', async (assert) => {
try {
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await Db.connect(Config.DATABASE).then(() => {
assert.pass('setup OK')
assert.end()
Expand Down Expand Up @@ -180,11 +180,7 @@ Test('Ilp service tests', async (ilpTest) => {
try {
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
assert.pass('database connection closed')
assert.end()
} catch (err) {
Expand Down
8 changes: 2 additions & 6 deletions test/integration/models/transfer/transferError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Test('Transfer Error model test', async (transferErrorTest) => {
try {
await Db.connect(Config.DATABASE).then(async () => {
await Cache.initCache()
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
assert.pass('setup OK')
assert.end()
}).catch(err => {
Expand Down Expand Up @@ -92,11 +92,7 @@ Test('Transfer Error model test', async (transferErrorTest) => {
try {
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
assert.pass('database connection closed')
assert.end()
} catch (err) {
Expand Down
8 changes: 2 additions & 6 deletions test/integration/models/transfer/transferExtension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Test('Extension model test', async (extensionTest) => {

await extensionTest.test('setup', async (assert) => {
try {
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await Db.connect(Config.DATABASE).then(() => {
assert.pass('setup OK')
assert.end()
Expand Down Expand Up @@ -198,11 +198,7 @@ Test('Extension model test', async (extensionTest) => {
try {
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
assert.pass('database connection closed')
assert.end()
} catch (err) {
Expand Down
8 changes: 2 additions & 6 deletions test/integration/models/transfer/transferStateChange.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Test('Transfer State Change model test', async (stateChangeTest) => {
await stateChangeTest.test('setup', async (assert) => {
try {
await Db.connect(Config.DATABASE).then(async () => {
await ProxyCache.proxyCache.connect()
await ProxyCache.connect()
await ParticipantCached.initialize()
await ParticipantCurrencyCached.initialize()
await ParticipantLimitCached.initialize()
Expand Down Expand Up @@ -129,11 +129,7 @@ Test('Transfer State Change model test', async (stateChangeTest) => {
try {
await Cache.destroyCache()
await Db.disconnect()
try {
await ProxyCache.proxyCache.disconnect()
} catch (err) {

}
await ProxyCache.disconnect()
assert.pass('database connection closed')
assert.end()
} catch (err) {
Expand Down
Loading

0 comments on commit 184e086

Please sign in to comment.