Skip to content

Commit

Permalink
Smart wait in examples smoke test as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos committed Feb 26, 2024
1 parent 7423ab5 commit ebd6f14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
14 changes: 8 additions & 6 deletions examples/indexer.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions examples/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import base64
import time
from dataclasses import dataclass
from typing import List

Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit ebd6f14

Please sign in to comment.