Skip to content

Commit

Permalink
Fix Utils.until
Browse files Browse the repository at this point in the history
  • Loading branch information
Lezek123 committed Oct 21, 2024
1 parent 1beb710 commit 349606f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
10 changes: 5 additions & 5 deletions tests/network-tests/src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ export class Api {
public async untilProposalsCanBeCreated(
numberOfProposals = 1,
intervalMs = BLOCKTIME,
timeoutMs = 180000
maxRetries = 180
): Promise<void> {
await Utils.until(
`${numberOfProposals} proposals can be created`,
Expand All @@ -663,17 +663,17 @@ export class Api {
return maxActiveProposalLimit.sub(activeProposalsN).toNumber() >= numberOfProposals
},
intervalMs,
timeoutMs
maxRetries
)
}

public async untilCouncilStage(
targetStage: 'Announcing' | 'Voting' | 'Revealing' | 'Idle',
announcementPeriodNr: number | null = null,
blocksReserve = 4,
intervalMs = BLOCKTIME
intervalMs = BLOCKTIME,
maxRetries = 600
): Promise<void> {
const stageTimeoutMs = 100 * 6 * 1000
await Utils.until(
`council stage ${targetStage} (+${blocksReserve} blocks reserve)`,
async ({ debug }) => {
Expand Down Expand Up @@ -710,7 +710,7 @@ export class Api {
)
},
intervalMs,
stageTimeoutMs
maxRetries
)
}

Expand Down
35 changes: 15 additions & 20 deletions tests/network-tests/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,27 @@ export class Utils {
name: string,
conditionFunc: (props: { debug: Debugger.Debugger }) => Promise<boolean>,
intervalMs = BLOCKTIME,
timeoutMs = 10 * 60 * 1000
maxRetries = 600
): Promise<void> {
const debug = extendDebug(`awaiting:${name}`)
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error(`Awaiting ${name} - timeout reached`)), timeoutMs)
const check = async () => {
let result = false
try {
result = await conditionFunc({ debug })
} catch (e: any) {
debug(`conditionFunc error: ${e.toString()}`)
return
}
for (let i = 0; i < maxRetries; ++i) {
try {
const result = await conditionFunc({ debug })
if (result) {
clearInterval(interval)
clearTimeout(timeout)
debug('Condition satisfied!')
resolve()
return
}
debug('Condition not satisfied, waiting...')
} catch (e: any) {
debug(`conditionFunc error: ${e.toString()}`)
// Continue the loop...
}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const interval = setInterval(check, intervalMs)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
check()
})

if (i !== maxRetries - 1) {
debug(`Attempt ${i + 1}/${maxRetries}: Condition not satisfied, waiting...`)
await Utils.wait(intervalMs)
}
}

throw new Error(`Awaiting ${name} - max retries reached!`)
}
}

0 comments on commit 349606f

Please sign in to comment.