From 801ff3518431815ba9420f6699b505c36e649ad4 Mon Sep 17 00:00:00 2001 From: kylezs Date: Fri, 31 May 2024 10:31:38 +0200 Subject: [PATCH] pick: fix: more robust governance bouncer test (#4912) (#4922) fix: more robust governance bouncer test (#4912) * fix: more robust governance test * chore: lint * fix: simpler tryUntilSuccess * chore: lint --- bouncer/shared/multiple_members_governance.ts | 4 ++-- bouncer/shared/utils.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/bouncer/shared/multiple_members_governance.ts b/bouncer/shared/multiple_members_governance.ts index 622a9cfa80..0cef968d2b 100644 --- a/bouncer/shared/multiple_members_governance.ts +++ b/bouncer/shared/multiple_members_governance.ts @@ -1,6 +1,6 @@ import assert from 'assert'; import Keyring from '../polkadot/keyring'; -import { getChainflipApi, observeEvent } from '../shared/utils'; +import { getChainflipApi, observeEvent, tryUntilSuccess } from '../shared/utils'; import { snowWhite, submitGovernanceExtrinsic } from '../shared/cf_governance'; async function getGovernanceMembers(): Promise { @@ -30,7 +30,7 @@ async function addAliceToGovernance() { await using chainflip = await getChainflipApi(); await observeEvent('governance:Executed', chainflip); - assert((await getGovernanceMembers()).length === 2, 'Governance should now have 2 members'); + await tryUntilSuccess(async () => (await getGovernanceMembers()).length === 2, 3000, 10); console.log('Added Alice to governance!'); } diff --git a/bouncer/shared/utils.ts b/bouncer/shared/utils.ts index 3f8e21dba6..a104fa055b 100644 --- a/bouncer/shared/utils.ts +++ b/bouncer/shared/utils.ts @@ -977,3 +977,19 @@ export function calculateFeeWithBps(fineAmount: bigint, bps: number): bigint { const divisor = BigInt(10000 / bps); return fineAmount / divisor + (fineAmount % divisor > divisor / 2n ? 1n : 0n); } + +// Throws error if unsuccessful. +export async function tryUntilSuccess( + closure: () => Promise, + pollTime: number, + maxAttempts: number, + logTag?: string, +) { + for (let i = 0; i < maxAttempts; i++) { + if (await closure()) { + return; + } + await sleep(pollTime); + } + throw new Error('tryUntilSuccess failed: ' + logTag); +}