Skip to content

Commit

Permalink
Add test for busy interval caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed Jul 29, 2024
1 parent 46e4af3 commit b37a5fe
Showing 1 changed file with 69 additions and 4 deletions.
73 changes: 69 additions & 4 deletions tests/test_machine.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b37a5fe

Please sign in to comment.