fix integration tests

Instead of using the deprecated runUntilDone, we now wait
for specific notifications.

The old way doesn't work, since p2p push transactions are not considered
done until the counterparty has accepted the payment.
This commit is contained in:
Florian Dold 2023-08-03 19:24:04 +02:00
parent 57e86b759e
commit ee47aa4837
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
3 changed files with 68 additions and 23 deletions

View File

@ -17,13 +17,20 @@
/** /**
* Imports. * Imports.
*/ */
import { AbsoluteTime, Duration } from "@gnu-taler/taler-util"; import {
AbsoluteTime,
Duration,
NotificationType,
TransactionMajorState,
TransactionMinorState,
} from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { defaultCoinConfig } from "../harness/denomStructures.js"; import { defaultCoinConfig } from "../harness/denomStructures.js";
import { GlobalTestState, WalletCli } from "../harness/harness.js"; import { GlobalTestState, WalletCli } from "../harness/harness.js";
import { import {
createSimpleTestkudosEnvironment, createSimpleTestkudosEnvironmentV2,
withdrawViaBank, createWalletDaemonWithClient,
withdrawViaBankV2,
} from "../harness/helpers.js"; } from "../harness/helpers.js";
/** /**
@ -32,12 +39,7 @@ import {
export async function runAgeRestrictionsPeerTest(t: GlobalTestState) { export async function runAgeRestrictionsPeerTest(t: GlobalTestState) {
// Set up test environment // Set up test environment
const { const { bank, exchange } = await createSimpleTestkudosEnvironmentV2(
wallet: walletOne,
bank,
exchange,
merchant,
} = await createSimpleTestkudosEnvironment(
t, t,
defaultCoinConfig.map((x) => x("TESTKUDOS")), defaultCoinConfig.map((x) => x("TESTKUDOS")),
{ {
@ -45,20 +47,29 @@ export async function runAgeRestrictionsPeerTest(t: GlobalTestState) {
}, },
); );
const walletTwo = new WalletCli(t, "walletTwo"); const w1 = await createWalletDaemonWithClient(t, {
const walletThree = new WalletCli(t, "walletThree"); name: "w1",
persistent: true,
});
const w2 = await createWalletDaemonWithClient(t, {
name: "w2",
persistent: true,
});
const wallet1 = w1.walletClient;
const wallet2 = w2.walletClient;
{ {
const wallet = walletOne; const withdrawalRes = await withdrawViaBankV2(t, {
walletClient: wallet1,
await withdrawViaBank(t, {
wallet,
bank, bank,
exchange, exchange,
amount: "TESTKUDOS:20", amount: "TESTKUDOS:20",
restrictAge: 13, restrictAge: 13,
}); });
await withdrawalRes.withdrawalFinishedCond;
const purse_expiration = AbsoluteTime.toProtocolTimestamp( const purse_expiration = AbsoluteTime.toProtocolTimestamp(
AbsoluteTime.addDuration( AbsoluteTime.addDuration(
AbsoluteTime.now(), AbsoluteTime.now(),
@ -66,7 +77,7 @@ export async function runAgeRestrictionsPeerTest(t: GlobalTestState) {
), ),
); );
const initResp = await wallet.client.call( const initResp = await wallet1.client.call(
WalletApiOperation.InitiatePeerPushDebit, WalletApiOperation.InitiatePeerPushDebit,
{ {
partialContractTerms: { partialContractTerms: {
@ -77,20 +88,35 @@ export async function runAgeRestrictionsPeerTest(t: GlobalTestState) {
}, },
); );
await wallet.runUntilDone(); const peerPushReadyCond = wallet1.waitForNotificationCond(
(x) =>
x.type === NotificationType.TransactionStateTransition &&
x.newTxState.major === TransactionMajorState.Pending &&
x.newTxState.minor === TransactionMinorState.Ready &&
x.transactionId === initResp.transactionId,
);
const checkResp = await walletTwo.client.call( await peerPushReadyCond;
const checkResp = await wallet2.call(
WalletApiOperation.PreparePeerPushCredit, WalletApiOperation.PreparePeerPushCredit,
{ {
talerUri: initResp.talerUri, talerUri: initResp.talerUri,
}, },
); );
await walletTwo.client.call(WalletApiOperation.ConfirmPeerPushCredit, { await wallet2.call(WalletApiOperation.ConfirmPeerPushCredit, {
peerPushPaymentIncomingId: checkResp.peerPushPaymentIncomingId, peerPushPaymentIncomingId: checkResp.peerPushPaymentIncomingId,
}); });
await walletTwo.runUntilDone(); const peerPullCreditDoneCond = wallet2.waitForNotificationCond(
(x) =>
x.type === NotificationType.TransactionStateTransition &&
x.newTxState.major === TransactionMajorState.Done &&
x.transactionId === checkResp.transactionId,
);
await peerPullCreditDoneCond;
} }
} }

View File

@ -23,6 +23,7 @@ import {
j2s, j2s,
NotificationType, NotificationType,
TransactionMajorState, TransactionMajorState,
TransactionMinorState,
WalletNotification, WalletNotification,
} from "@gnu-taler/taler-util"; } from "@gnu-taler/taler-util";
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
@ -46,12 +47,14 @@ export async function runPeerToPeerPullTest(t: GlobalTestState) {
const w1 = await createWalletDaemonWithClient(t, { const w1 = await createWalletDaemonWithClient(t, {
name: "w1", name: "w1",
persistent: true,
handleNotification(wn) { handleNotification(wn) {
allW1Notifications.push(wn); allW1Notifications.push(wn);
}, },
}); });
const w2 = await createWalletDaemonWithClient(t, { const w2 = await createWalletDaemonWithClient(t, {
name: "w2", name: "w2",
persistent: true,
handleNotification(wn) { handleNotification(wn) {
allW2Notifications.push(wn); allW2Notifications.push(wn);
}, },
@ -89,6 +92,15 @@ export async function runPeerToPeerPullTest(t: GlobalTestState) {
}, },
); );
const peerPullCreditReadyCond = wallet1.waitForNotificationCond(
(x) => x.type === NotificationType.TransactionStateTransition &&
x.transactionId === resp.transactionId &&
x.newTxState.major === TransactionMajorState.Pending &&
x.newTxState.minor === TransactionMinorState.Ready,
);
await peerPullCreditReadyCond;
const checkResp = await wallet2.client.call( const checkResp = await wallet2.client.call(
WalletApiOperation.PreparePeerPullDebit, WalletApiOperation.PreparePeerPullDebit,
{ {
@ -98,8 +110,6 @@ export async function runPeerToPeerPullTest(t: GlobalTestState) {
console.log(`checkResp: ${j2s(checkResp)}`); console.log(`checkResp: ${j2s(checkResp)}`);
// FIXME: The wallet should emit a more appropriate notification here.
// Yes, it's technically a withdrawal.
const peerPullCreditDoneCond = wallet1.waitForNotificationCond( const peerPullCreditDoneCond = wallet1.waitForNotificationCond(
(x) => x.type === NotificationType.TransactionStateTransition && (x) => x.type === NotificationType.TransactionStateTransition &&
x.transactionId === resp.transactionId && x.transactionId === resp.transactionId &&

View File

@ -436,16 +436,25 @@ async function handlePeerPullCreditCreatePurse(
logger.info(`reserve merge response: ${j2s(resp)}`); logger.info(`reserve merge response: ${j2s(resp)}`);
await ws.db const transactionId = constructTransactionIdentifier({
tag: TransactionType.PeerPullCredit,
pursePub: pullIni.pursePub,
});
const transitionInfo = await ws.db
.mktx((x) => [x.peerPullPaymentInitiations]) .mktx((x) => [x.peerPullPaymentInitiations])
.runReadWrite(async (tx) => { .runReadWrite(async (tx) => {
const pi2 = await tx.peerPullPaymentInitiations.get(pursePub); const pi2 = await tx.peerPullPaymentInitiations.get(pursePub);
if (!pi2) { if (!pi2) {
return; return;
} }
const oldTxState = computePeerPullCreditTransactionState(pi2);
pi2.status = PeerPullPaymentInitiationStatus.PendingReady; pi2.status = PeerPullPaymentInitiationStatus.PendingReady;
await tx.peerPullPaymentInitiations.put(pi2); await tx.peerPullPaymentInitiations.put(pi2);
const newTxState = computePeerPullCreditTransactionState(pi2);
return { oldTxState, newTxState };
}); });
notifyTransition(ws, transactionId, transitionInfo);
return TaskRunResult.finished(); return TaskRunResult.finished();
} }