mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
"""Time admission follows caller estimates and measured operation costs."""
|
|
import math
|
|
|
|
import pytest
|
|
|
|
from obliteratus.execution_budget import ExecutionBudget
|
|
|
|
|
|
@pytest.mark.parametrize("option", ["execution_budget", "export_progress"])
|
|
def test_pipeline_rejects_invalid_controls_before_execution(option):
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
with pytest.raises(TypeError, match=option):
|
|
AbliterationPipeline("offline", method="basic", **{option: object()})
|
|
valid = (
|
|
ExecutionBudget(lambda: 10, 1, {"bayesian_trial": 1})
|
|
if option == "execution_budget" else lambda _event: None
|
|
)
|
|
pipeline = AbliterationPipeline("offline", method="basic", **{option: valid})
|
|
with pytest.raises(TypeError, match=option):
|
|
setattr(pipeline, option, object())
|
|
assert getattr(pipeline, option) is valid
|
|
assert option not in vars(pipeline)
|
|
|
|
|
|
def test_given_dynamic_reserve_when_context_changes_then_decision_changes():
|
|
reserve = [10.0]
|
|
budget = ExecutionBudget(lambda: 40.0, lambda: reserve[0], {"trial": 20.0})
|
|
assert budget.decide("trial").allowed
|
|
reserve[0] = 21.0
|
|
assert not budget.decide("trial").allowed
|
|
|
|
|
|
def test_given_observed_duration_when_slower_then_estimate_increases_with_margin():
|
|
budget = ExecutionBudget(lambda: 100, 10, {"trial": 20}, duration_margin=1.25)
|
|
budget.observe("trial", 40)
|
|
budget.observe("trial", 10)
|
|
assert budget.decide("trial").operation_seconds == 50
|
|
assert budget.decide("trial").allowed
|
|
|
|
|
|
@pytest.mark.parametrize("value", [True, -1, math.nan, math.inf])
|
|
def test_given_invalid_duration_when_observed_then_rejected(value):
|
|
budget = ExecutionBudget(lambda: 100, 10, {"trial": 20})
|
|
with pytest.raises(ValueError):
|
|
budget.observe("trial", value)
|
|
|
|
|
|
@pytest.mark.parametrize("value", [True, math.nan, math.inf])
|
|
def test_given_invalid_remaining_when_deciding_then_rejected(value):
|
|
budget = ExecutionBudget(lambda: value, 10, {"trial": 20})
|
|
with pytest.raises(ValueError, match="remaining_seconds"):
|
|
budget.decide("trial")
|
|
|
|
|
|
def test_given_expired_deadline_when_deciding_then_remaining_is_zero():
|
|
budget = ExecutionBudget(lambda: -1, 10, {"trial": 20})
|
|
assert budget.decide("trial").remaining_seconds == 0
|
|
assert not budget.decide("trial").allowed
|
|
|
|
|
|
@pytest.mark.parametrize("kwargs", [
|
|
{"remaining_seconds": 10}, {"clock": 10}, {"finalization_seconds": -1},
|
|
{"operation_estimates": {}}, {"operation_estimates": {"": 1}},
|
|
{"operation_estimates": {"trial": 0}}, {"operation_estimates": {"trial": True}},
|
|
{"duration_margin": .5}, {"duration_margin": math.inf},
|
|
])
|
|
def test_given_invalid_budget_when_constructed_then_rejected(kwargs):
|
|
values = dict(remaining_seconds=lambda: 100, finalization_seconds=10, operation_estimates={"trial": 20})
|
|
with pytest.raises((TypeError, ValueError)):
|
|
ExecutionBudget(**(values | kwargs))
|
|
|
|
|
|
def test_given_missing_estimate_when_admitting_then_requires_context():
|
|
budget = ExecutionBudget(lambda: 100, 10, {"trial": 20})
|
|
with pytest.raises(ValueError, match="Missing time estimate for verify"):
|
|
budget.decide("verify")
|