From ebd6f1421031dbbae9d2143b651c3ae84688cee7 Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Mon, 26 Feb 2024 15:13:01 -0500 Subject: [PATCH] Smart wait in examples smoke test as well --- examples/indexer.py | 14 ++++++++------ examples/utils.py | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/examples/indexer.py b/examples/indexer.py index d786607a..45e30e8c 100644 --- a/examples/indexer.py +++ b/examples/indexer.py @@ -1,7 +1,12 @@ import json from algosdk import transaction from algosdk.v2client import indexer -from utils import get_accounts, get_algod_client, get_indexer_client +from utils import ( + get_accounts, + get_algod_client, + get_indexer_client, + indexer_wait_for_round, +) # example: INDEXER_CREATE_CLIENT @@ -41,11 +46,8 @@ algod_client, algod_client.send_transaction(ptxn.sign(acct.private_key)), 4 ) -# sleep for a couple seconds to allow indexer to catch up -import time - -time.sleep(2) - +# allow indexer to catch up +indexer_wait_for_round(indexer_client, res["confirmed-round"], 30) # example: INDEXER_LOOKUP_ASSET # lookup a single asset diff --git a/examples/utils.py b/examples/utils.py index 64c3958d..d5c3a8bc 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -1,5 +1,6 @@ import os import base64 +import time from dataclasses import dataclass from typing import List @@ -55,6 +56,31 @@ def get_sandbox_default_wallet() -> Wallet: ) +def indexer_wait_for_round( + client: indexer.IndexerClient, round: int, max_attempts: int +) -> None: + """waits for the indexer to catch up to the given round""" + indexer_round = 0 + attempts = 0 + + while True: + indexer_status = client.health() + indexer_round = indexer_status["round"] + if indexer_round >= round: + # Success + break + + # Sleep for 1 second and try again + time.sleep(1) + attempts += 1 + + if attempts >= max_attempts: + # Failsafe to prevent infinite loop + raise RuntimeError( + f"Timeout waiting for indexer to catch up to round {round}. It is currently on {indexer_round}" + ) + + @dataclass class SandboxAccount: """SandboxAccount is a simple dataclass to hold a sandbox account details"""