From 541886750eb1bc32b9aa0323868dd24776baf0eb Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 6 Mar 2024 11:02:13 +0100 Subject: taler-harness: test aborting refreshes --- packages/taler-wallet-core/src/refresh.ts | 85 ++++++++++++++++--------------- 1 file changed, 43 insertions(+), 42 deletions(-) (limited to 'packages/taler-wallet-core/src/refresh.ts') diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts index c6ece1536..9c272ad18 100644 --- a/packages/taler-wallet-core/src/refresh.ts +++ b/packages/taler-wallet-core/src/refresh.ts @@ -107,7 +107,7 @@ import { getCandidateWithdrawalDenomsTx } from "./withdraw.js"; const logger = new Logger("refresh.ts"); export class RefreshTransactionContext implements TransactionContext { - public transactionId: TransactionIdStr; + readonly transactionId: TransactionIdStr; readonly taskId: TaskIdStr; constructor( @@ -126,53 +126,54 @@ export class RefreshTransactionContext implements TransactionContext { async deleteTransaction(): Promise { const refreshGroupId = this.refreshGroupId; - const ws = this.wex; - await ws.db.runReadWriteTx(["refreshGroups", "tombstones"], async (tx) => { - const rg = await tx.refreshGroups.get(refreshGroupId); - if (rg) { - await tx.refreshGroups.delete(refreshGroupId); - await tx.tombstones.put({ - id: TombstoneTag.DeleteRefreshGroup + ":" + refreshGroupId, - }); - } - }); + await this.wex.db.runReadWriteTx( + ["refreshGroups", "tombstones"], + async (tx) => { + const rg = await tx.refreshGroups.get(refreshGroupId); + if (rg) { + await tx.refreshGroups.delete(refreshGroupId); + await tx.tombstones.put({ + id: TombstoneTag.DeleteRefreshGroup + ":" + refreshGroupId, + }); + } + }, + ); } async suspendTransaction(): Promise { const { wex, refreshGroupId, transactionId } = this; - let res = await wex.db.runReadWriteTx(["refreshGroups"], async (tx) => { - const dg = await tx.refreshGroups.get(refreshGroupId); - if (!dg) { - logger.warn( - `can't suspend refresh group, refreshGroupId=${refreshGroupId} not found`, - ); - return undefined; - } - const oldState = computeRefreshTransactionState(dg); - switch (dg.operationStatus) { - case RefreshOperationStatus.Finished: + let transitionInfo = await wex.db.runReadWriteTx( + ["refreshGroups"], + async (tx) => { + const dg = await tx.refreshGroups.get(refreshGroupId); + if (!dg) { + logger.warn( + `can't suspend refresh group, refreshGroupId=${refreshGroupId} not found`, + ); return undefined; - case RefreshOperationStatus.Pending: { - dg.operationStatus = RefreshOperationStatus.Suspended; - await tx.refreshGroups.put(dg); - return { - oldTxState: oldState, - newTxState: computeRefreshTransactionState(dg), - }; } - case RefreshOperationStatus.Suspended: - return undefined; - } - return undefined; - }); - if (res) { - wex.ws.notify({ - type: NotificationType.TransactionStateTransition, - transactionId, - oldTxState: res.oldTxState, - newTxState: res.newTxState, - }); - } + const oldState = computeRefreshTransactionState(dg); + switch (dg.operationStatus) { + case RefreshOperationStatus.Finished: + case RefreshOperationStatus.Suspended: + case RefreshOperationStatus.Failed: + return undefined; + case RefreshOperationStatus.Pending: { + dg.operationStatus = RefreshOperationStatus.Suspended; + await tx.refreshGroups.put(dg); + break; + } + default: + assertUnreachable(dg.operationStatus); + } + return { + oldTxState: oldState, + newTxState: computeRefreshTransactionState(dg), + }; + }, + ); + wex.taskScheduler.stopShepherdTask(this.taskId); + notifyTransition(wex, transactionId, transitionInfo); } async abortTransaction(): Promise { -- cgit v1.2.3