Skip to content

Commit

Permalink
feat(workweek): fix caching issues on test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Sep 5, 2024
1 parent 68a5e97 commit d5693eb
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 30 deletions.
9 changes: 5 additions & 4 deletions packages/backend/src/asset/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ async function createAsset(
LiquidityAccountType.ASSET,
trx
)
await deps.cacheDataStore.set(asset.id, asset)
return asset
})
} catch (err) {
Expand All @@ -139,7 +138,7 @@ async function updateAsset(
.patchAndFetchById(id, { withdrawalThreshold, liquidityThreshold })
.throwIfNotFound()

await deps.cacheDataStore.set(id, returnVal)
await deps.cacheDataStore.delete(id)
return returnVal
} catch (err) {
if (err instanceof NotFoundError) {
Expand All @@ -157,6 +156,8 @@ async function deleteAsset(
if (!deps.knex) {
throw new Error('Knex undefined')
}

await deps.cacheDataStore.delete(id)
try {
// return error in case there is a peer or wallet address using the asset
const peer = await Peer.query(deps.knex).where('assetId', id).first()
Expand All @@ -170,7 +171,6 @@ async function deleteAsset(
if (walletAddress) {
return AssetError.CannotDeleteInUseAsset
}
await deps.cacheDataStore.delete(id)
return await Asset.query(deps.knex)
.patchAndFetchById(id, { deletedAt: deletedAt.toISOString() })
.throwIfNotFound()
Expand Down Expand Up @@ -212,8 +212,9 @@ async function getAll(deps: ServiceDependencies): Promise<Asset[]> {
async function setAssetOn(
deps: ServiceDependencies,
obj: ToSetOn
): Promise<void> {
): Promise<void | Asset> {
if (!obj) return
const asset = await getAsset(deps, obj.assetId)
if (asset) obj.asset = asset
return asset
}
6 changes: 5 additions & 1 deletion packages/backend/src/graphql/resolvers/liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ describe('Liquidity Resolvers', (): void => {
const timeoutTwoPhase = 10

beforeAll(async (): Promise<void> => {
deps = await initIocContainer(Config)
deps = initIocContainer({
...Config,
enableTelemetry: true,
localCacheDuration: 0
})
appContainer = await createTestApp(deps)
knex = appContainer.knex
accountingService = await deps.use('accountingService')
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export function initIocContainer(
logger,
knex,
accountingService: await deps.use('accountingService'),
cacheDataStore: createInMemoryDataStore(30000)
cacheDataStore: createInMemoryDataStore(config.localCacheDuration)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ describe('Incoming Payment Service', (): void => {
let config: IAppConfig

beforeAll(async (): Promise<void> => {
deps = await initIocContainer(Config)
deps = initIocContainer({
...Config,
localCacheDuration: 0
})
appContainer = await createTestApp(deps)
accountingService = await deps.use('accountingService')
knex = appContainer.knex
Expand Down
12 changes: 7 additions & 5 deletions packages/backend/src/open_payments/payment/incoming/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,16 @@ async function getIncomingPaymentPage(
options: ListOptions
): Promise<IncomingPayment[]> {
const page = await IncomingPayment.query(deps.knex).list(options)
for (const incomingPayment of page) {
await deps.assetService.setOn(incomingPayment)
await deps.walletAddressService.setOn(incomingPayment)
}

const amounts = await deps.accountingService.getAccountsTotalReceived(
page.map((payment: IncomingPayment) => payment.id)
)

for (const payment of page) {
await deps.walletAddressService.setOn(payment)
await deps.assetService.setOn(payment.walletAddress)
await deps.assetService.setOn(payment)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
return page.map((payment: IncomingPayment, i: number) => {
try {
Expand Down
25 changes: 18 additions & 7 deletions packages/backend/src/open_payments/payment/outgoing/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ async function getOutgoingPaymentsPage(
): Promise<OutgoingPayment[]> {
const { filter, pagination, sortOrder } = options ?? {}

const query = OutgoingPayment.query(deps.knex).withGraphFetched(
'[quote.asset, walletAddress]'
)
const query = OutgoingPayment.query(deps.knex)

if (filter?.receiver?.in && filter.receiver.in.length) {
query
Expand All @@ -131,6 +129,13 @@ async function getOutgoingPaymentsPage(
const amounts = await deps.accountingService.getAccountsTotalSent(
page.map((payment: OutgoingPayment) => payment.id)
)
for (const payment of page) {
await deps.walletAddressService.setOn(payment)
await deps.assetService.setOn(payment.walletAddress)
await deps.quoteService.setOn(payment)
await deps.assetService.setOn(payment.quote)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
return page.map((payment: OutgoingPayment, i: number) => {
payment.sentAmount = {
Expand All @@ -148,9 +153,9 @@ async function getOutgoingPayment(
): Promise<OutgoingPayment | undefined> {
const outgoingPayment = await OutgoingPayment.query(deps.knex).get(options)
if (outgoingPayment) {
await deps.walletAddressService.setOn(outgoingPayment)
await deps.quoteService.setOn(outgoingPayment)
await deps.assetService.setOn(outgoingPayment.quote)
await deps.walletAddressService.setOn(outgoingPayment)
return addSentAmount(deps, outgoingPayment)
}
}
Expand Down Expand Up @@ -677,12 +682,18 @@ async function getOutgoingPaymentPage(
deps: ServiceDependencies,
options: ListOptions
): Promise<OutgoingPayment[]> {
const page = await OutgoingPayment.query(deps.knex)
.list(options)
.withGraphFetched('[quote.asset, walletAddress]')
const page = await OutgoingPayment.query(deps.knex).list(options)
const amounts = await deps.accountingService.getAccountsTotalSent(
page.map((payment: OutgoingPayment) => payment.id)
)

for (const payment of page) {
await deps.walletAddressService.setOn(payment)
await deps.assetService.setOn(payment.walletAddress)
await deps.quoteService.setOn(payment)
await deps.assetService.setOn(payment.quote)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
return page.map((payment: OutgoingPayment, i: number) => {
payment.sentAmount = {
Expand Down
15 changes: 6 additions & 9 deletions packages/backend/src/open_payments/quote/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async function createQuote(
)
stopTimerFee && stopTimerFee()

const q = await Quote.transaction(deps.knex, async (trx) => {
return await Quote.transaction(deps.knex, async (trx) => {
const createdQuote = await Quote.query(trx)
.insertAndFetch({
walletAddressId: options.walletAddressId,
Expand All @@ -189,10 +189,8 @@ async function createQuote(
estimatedExchangeRate: quote.estimatedExchangeRate
})
.withGraphFetched('[asset, fee, walletAddress]')
await deps.walletAddressService.setOn(createdQuote)
await deps.assetService.setOn(createdQuote)

return await finalizeQuote(
const quoteFin = await finalizeQuote(
{
...deps,
knex: trx
Expand All @@ -201,12 +199,11 @@ async function createQuote(
createdQuote,
receiver
)
})

stopTimer && stopTimer()
await deps.cacheDataStore.set(quoteFin.id, quoteFin)
stopTimer && stopTimer()

await deps.cacheDataStore.set(q.id, q)
return q
return quoteFin
})
} catch (err) {
if (isQuoteError(err)) {
stopTimer && stopTimer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const baseGetTests = <M extends WalletAddressSubresource>({
})
test(`${
match ? '' : 'cannot '
}list model`, async (): Promise<void> => {
}list model (get list)`, async (): Promise<void> => {
if (testList && model.walletAddressId) {
await testList(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async function updateWalletAddress(
}
await trx.commit()

await deps.cacheDataStore.delete(updatedWalletAddress.id)
await deps.cacheDataStore.set(updatedWalletAddress.id, updatedWalletAddress)
return updatedWalletAddress
} catch (err) {
await trx.rollback()
Expand Down

0 comments on commit d5693eb

Please sign in to comment.