From b37a5fe09149877c927c52a6d558a07d2047ba7f Mon Sep 17 00:00:00 2001 From: derekpierre Date: Mon, 29 Jul 2024 12:28:59 -0400 Subject: [PATCH] Add test for busy interval caching. --- tests/test_machine.py | 73 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/tests/test_machine.py b/tests/test_machine.py index 2e9e7fd..59b2f8f 100644 --- a/tests/test_machine.py +++ b/tests/test_machine.py @@ -1,11 +1,9 @@ -import time -from unittest.mock import ANY - import math +import time from typing import List +from unittest.mock import ANY import pytest - import pytest_twisted from eth_account import Account from eth_utils import ValidationError @@ -18,6 +16,7 @@ Web3Exception, ) +import atxm from atxm import AutomaticTxMachine from atxm.strategies import AsyncTxStrategy, TimeoutStrategy from atxm.tx import FaultedTx, FinalizedTx, FutureTx, PendingTx @@ -1538,6 +1537,72 @@ def test_pause_when_busy(clock, machine, eip1559_transaction, account, mocker): machine.stop() +@pytest.mark.usefixtures("disable_auto_mining") +def test_busy_interval_caching( + ethereum_tester, + machine, + eip1559_transaction, + account, + mock_wake_sleep, + mocker, +): + average_blocktime_spy = mocker.spy(atxm.machine, "_get_average_blocktime") + + assert machine.current_state == machine._IDLE + assert not machine.paused + assert not machine.busy + assert machine._busy_interval is None + assert average_blocktime_spy.call_count == 0 + + atx = machine.queue_transaction( + params=eip1559_transaction, + signer=account, + on_broadcast_failure=mocker.Mock(), + on_fault=mocker.Mock(), + on_finalized=mocker.Mock(), + on_insufficient_funds=mocker.Mock(), + ) + + # broadcast tx + machine._cycle() + assert machine.current_state == machine._BUSY + assert machine.busy + assert machine._busy_interval is not None + busy_interval_value = machine._busy_interval + assert machine._task.interval == busy_interval_value + assert average_blocktime_spy.call_count == 1 + + ethereum_tester.mine_block() + + # busy -> pause + machine.pause() + assert machine.current_state == machine._PAUSED + assert machine.paused + + # resume after pausing + machine.resume() + machine._cycle() # wake doesn't do anything because mocked + assert machine.current_state == machine._BUSY + assert machine._busy_interval is not None + assert machine._busy_interval == busy_interval_value + assert machine._task.interval == busy_interval_value + assert average_blocktime_spy.call_count == 1 + assert not machine.paused + + # finalize tx + while machine.busy: + ethereum_tester.mine_block() + machine._cycle() + + ethereum_tester.mine_block() + assert atx.final is True + + # transition to idle + machine._cycle() + assert machine.current_state == machine._IDLE + assert machine._task.interval != busy_interval_value + + @pytest.mark.usefixtures("disable_auto_mining") def test_simple_state_transitions( ethereum_tester,