Skip to content

Introduce TestPhaseManager construct to tag tests' elements #2137

@marioevz

Description

@marioevz

Introduce a generalized TestPhaseManager which would be similar to this:

class BenchmarkManager:
"""Context manager for managing benchmark test phases."""
def __init__(self):
"""Initialize the BenchmarkManager with empty transaction and block lists."""
self.setup_transactions: List[Transaction] = []
self.setup_blocks: List[Block] = []
self.execution_transactions: List[Transaction] = []
self.execution_blocks: List[Block] = []
@contextmanager
def setup(self):
"""Context manager for the setup phase of a benchmark test."""
token = _current_phase.set(BenchmarkPhase.SETUP)
try:
yield self
finally:
_current_phase.reset(token)
@contextmanager
def execution(self):
"""Context manager for the execution phase of a benchmark test."""
token = _current_phase.set(BenchmarkPhase.EXECUTION)
try:
yield self
finally:
_current_phase.reset(token)
def add_transaction(self, tx: Transaction):
"""Add a transaction to the current phase."""
current_phase = _current_phase.get()
if current_phase == BenchmarkPhase.SETUP:
self.setup_transactions.append(tx)
elif current_phase == BenchmarkPhase.EXECUTION:
self.execution_transactions.append(tx)
else:
self.setup_transactions.append(tx)
def add_block(self, block: Block):
"""Add a block to the current phase."""
current_phase = _current_phase.get()
if current_phase == BenchmarkPhase.SETUP:
self.setup_blocks.append(block)
elif current_phase == BenchmarkPhase.EXECUTION:
self.execution_blocks.append(block)
else:
self.setup_blocks.append(block)
def get_current_phase(self) -> Optional[BenchmarkPhase]:
"""Get the current benchmark phase."""
return _current_phase.get()

But it has an instance defined at the ethereum_test_types level so it can be accessed by Transaction, Block and perhaps other classes to tag themselves as part of an explicit phase of a given test.

This would be particularly important in benchmark tests where we need some transactions and accounts to be part of a "setup" phase so execute can pick these up and treat them accordingly when running tests in BloatNet for example.

test_phase_manager = TestPhaseManager()

...

class Transaction(
    TransactionGeneric[HexNumber], TransactionTransitionToolConverter, SignableRLPSerializable
):
    """Generic object that can represent all Ethereum transaction types."""
    ...
    test_phase: str | None = Field(default_factory=lambda _: test_phase_manager.get())

We could then create a generic pytest fixture in ./src/pytest_plugins/shared/execute_fill.py that returns the test_phase_manager in every case.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions