UXInteract / task_015 /tests /validate_enhancements.py
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
4.34 kB
import importlib
import pathlib
import socket
import sys
import time
import threading
import math
def deny_network(*_args, **_kwargs):
raise AssertionError("network access is forbidden during enhanced validation")
socket.create_connection = deny_network
socket.socket.connect = deny_network
root = pathlib.Path.cwd()
sys.path.insert(0, str(root))
pycron = importlib.import_module("pycron")
def new_scheduler():
return pycron.CronScheduler()
def wait_until(predicate, timeout=1.5):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
if predicate():
return True
time.sleep(0.01)
return predicate()
# ENHANCED_CASE_1: adding an interval job must not run it until start() is called.
dormant_count = {"value": 0}
dormant = new_scheduler()
dormant.add_interval_job(
0.04, lambda: dormant_count.__setitem__("value", dormant_count["value"] + 1)
)
time.sleep(0.10)
assert dormant_count["value"] == 0, "an added job ran before the scheduler was started"
dormant.start()
assert wait_until(lambda: dormant_count["value"] >= 2), (
"a started short-interval job did not repeat"
)
dormant.stop()
# ENHANCED_CASE_2: separately added interval jobs must both execute repeatedly.
counts = {"fast": 0, "slow": 0}
multiple = new_scheduler()
multiple.add_interval_job(0.03, lambda: counts.__setitem__("fast", counts["fast"] + 1))
multiple.add_interval_job(0.06, lambda: counts.__setitem__("slow", counts["slow"] + 1))
multiple.start()
assert wait_until(lambda: counts["fast"] >= 3 and counts["slow"] >= 2), (
f"separately added interval jobs did not both repeat: {counts}"
)
multiple.stop()
# ENHANCED_CASE_3: stop() must cancel a pending interval, including its first callback.
late_count = {"value": 0}
stopped_early = new_scheduler()
stopped_early.add_interval_job(
0.25, lambda: late_count.__setitem__("value", late_count["value"] + 1)
)
stopped_early.start()
stopped_early.stop()
time.sleep(0.28)
assert late_count["value"] == 0, "a callback ran after stop() cancelled its pending interval"
# ENHANCED_CASE_4: a stopped scheduler can start again without losing jobs.
restart_count = {"value": 0}
restarted = new_scheduler()
restarted.add_interval_job(0.04, lambda: restart_count.__setitem__("value", restart_count["value"] + 1))
restarted.start()
assert wait_until(lambda: restart_count["value"] >= 2), "job did not run before restart"
restarted.stop()
before_restart = restart_count["value"]
restarted.start()
assert wait_until(lambda: restart_count["value"] >= before_restart + 2), "job did not resume after restart"
restarted.stop()
# ENHANCED_CASE_5: invalid intervals must not create busy loops or delayed failures.
for invalid in (0, -0.01, float("nan"), float("inf")):
constructors = {
"IntervalSchedule": pycron.IntervalSchedule,
"add_interval_job": lambda seconds: new_scheduler().add_interval_job(seconds, lambda: None),
}
for name, construct in constructors.items():
try:
construct(invalid)
except (ValueError, TypeError):
continue
raise AssertionError(f"{name} accepted invalid interval {invalid!r}")
pycron.IntervalSchedule(0.125)
# ENHANCED_CASE_6: calling start twice must not duplicate each interval tick.
idempotent_count = {"value": 0}
idempotent = new_scheduler()
idempotent.add_interval_job(0.10, lambda: idempotent_count.__setitem__("value", idempotent_count["value"] + 1))
started_at = time.monotonic()
idempotent.start()
idempotent.start()
assert wait_until(lambda: idempotent_count["value"] >= 4), "repeated start stopped execution"
idempotent.stop()
elapsed = time.monotonic() - started_at
assert idempotent_count["value"] <= math.floor(elapsed / 0.10) + 1, (
f"repeated start duplicated interval callbacks: {idempotent_count['value']}"
)
# The first callback must wait for its interval; late scheduling is permissible.
first_tick = threading.Event()
fired_at = []
not_early = new_scheduler()
not_early.add_interval_job(0.20, lambda: (fired_at.append(time.monotonic()), first_tick.set()))
started_at = time.monotonic()
not_early.start()
assert first_tick.wait(2), "first callback never fired"
not_early.stop()
assert fired_at[0] - started_at >= 0.195, "callback ran before its initial interval"
print("enhanced cases passed for task 20")