A test clock supplies a chosen time value to code that would otherwise read wall time.
Python testing: inject a clock to test expiry at the exact boundary
Operation contract
The lease is valid strictly before its expiry. The fixture passes the current instant as an integer rather than patching a global clock. This makes the equal-to-expiry case explicit, repeatable and independent of machine timezone or test execution speed. The same function rejects Boolean times and malformed lease fields before checking expiry. A test need not sleep to reach a boundary it can supply as data.
Failure and ownership boundary
An injected clock does not solve clock trust across hosts. Stored leases still need a deployed authority for timestamps, monotonic versus wall-clock policy and transaction ordering. The fixture checks a pure predicate only. Python SQLite lease renewal: reject stale tokens and exact-expiry ownership and Python expiry exercise: separate pending, active and expired records at exact boundaries connect the predicate to state transitions.
Working program
def active_lease(now_ms, expiry_ms):
if type(now_ms) is not int or type(expiry_ms) is not int or not 0 <= now_ms <= 100000 or not 0 <= expiry_ms <= 100000:
raise ValueError("bounded millisecond times")
return now_ms < expiry_ms
for controlled_now in (499, 500, 501):
print(controlled_now, active_lease(controlled_now, 500))
try:
active_lease(True, 500)
except ValueError:
print("boolean clock rejected")Output
499 True
500 False
501 False
boolean clock rejectedCosts and limits
The predicate uses bounded integer comparisons and no sleeping. A production storage check has database and contention costs that are absent here. Time units and clock authority must remain part of the caller contract.
Common Mistakes
- A sleeping test can miss or delay the exact equality boundary.
- A local injected clock does not establish distributed clock agreement.
Connected lessons
Python expiry exercise: separate pending, active and expired records at exact boundaries, Python SQLite lease renewal: reject stale tokens and exact-expiry ownership, Pytest fixtures: isolated files and parameterized rejection tests.
