Two new views, picked from mockups. `sky`: a star chart of the sky over Zlín as seen lying on your back, with the planets, the moon in its phase, tonight's darkness, cloud for the next 12 hours and the next visible ISS pass. `iss`: a dotted world map with day and night, the ISS and its ground track, and the next pass over Zlín with the cloud cover for that hour. Positions are calculated locally with ephem; only the ISS orbit (CelesTrak, every 6 h, retried gently) and cloud cover (Open-Meteo) are fetched. The poller gained a per-source retry delay for that. Co-Authored-By: Claude Opus 5.5 <[email protected]>
99 lines
2.5 KiB
Python
99 lines
2.5 KiB
Python
import threading
|
|
import time
|
|
|
|
from poller import Poller
|
|
|
|
|
|
class FakeClock:
|
|
def __init__(self):
|
|
self.now = 1000.0
|
|
|
|
def __call__(self):
|
|
return self.now
|
|
|
|
|
|
def flaky(results):
|
|
"""fetch() returning each item of `results` in turn; an Exception item is raised."""
|
|
items = iter(results)
|
|
|
|
def fetch():
|
|
item = next(items)
|
|
if isinstance(item, Exception):
|
|
raise item
|
|
return item
|
|
|
|
return fetch
|
|
|
|
|
|
def test_success_stores_value_and_bumps_version():
|
|
p = Poller(flaky(["a"]), interval=10)
|
|
assert p.poll_once() is True
|
|
assert (p.value, p.version, p.failures) == ("a", 1, 0)
|
|
|
|
|
|
def test_failure_keeps_last_good_value_and_counts():
|
|
p = Poller(flaky(["a", RuntimeError("boom")]), interval=10)
|
|
p.poll_once()
|
|
assert p.poll_once() is False
|
|
assert (p.value, p.version, p.failures) == ("a", 1, 1)
|
|
|
|
|
|
def test_success_resets_failures():
|
|
p = Poller(flaky([RuntimeError("x"), RuntimeError("y"), "b"]), interval=10)
|
|
p.poll_once()
|
|
p.poll_once()
|
|
assert p.failures == 2
|
|
p.poll_once()
|
|
assert (p.value, p.failures) == ("b", 0)
|
|
|
|
|
|
def test_age_uses_injected_clock():
|
|
clock = FakeClock()
|
|
p = Poller(flaky(["a"]), interval=10, clock=clock)
|
|
p.poll_once()
|
|
clock.now += 90
|
|
assert p.age() == 90
|
|
|
|
|
|
def test_age_none_before_first_success():
|
|
p = Poller(flaky([RuntimeError("x")]), interval=10)
|
|
assert p.age() is None
|
|
p.poll_once()
|
|
assert p.age() is None
|
|
|
|
|
|
def test_slow_fetch_does_not_block_reader():
|
|
release = threading.Event()
|
|
|
|
def slow():
|
|
release.wait(5)
|
|
return "late"
|
|
|
|
p = Poller(slow, interval=60)
|
|
p.start()
|
|
try:
|
|
assert p.value is None # returns immediately although fetch is blocked
|
|
release.set()
|
|
deadline = time.monotonic() + 5
|
|
while p.version == 0 and time.monotonic() < deadline:
|
|
time.sleep(0.01)
|
|
assert (p.value, p.version) == ("late", 1)
|
|
finally:
|
|
release.set()
|
|
p.stop()
|
|
|
|
|
|
def test_failed_fetch_retries_within_a_minute_by_default():
|
|
p = Poller(lambda: 1, interval=6 * 3600)
|
|
assert p.wait_after(ok=True) == 6 * 3600
|
|
assert p.wait_after(ok=False) == 60
|
|
|
|
|
|
def test_retry_can_be_slower_for_sources_that_ban_eager_clients():
|
|
p = Poller(lambda: 1, interval=6 * 3600, retry=15 * 60)
|
|
assert p.wait_after(ok=False) == 15 * 60
|
|
|
|
|
|
def test_retry_never_waits_longer_than_the_interval():
|
|
assert Poller(lambda: 1, interval=30, retry=15 * 60).wait_after(ok=False) == 30
|