Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions openbb_platform/core/openbb_core/provider/utils/lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def wrapped(*args, **kwargs) -> Any:


def _ttl_hash_gen(seconds: int):
start_time = time.time()
start_time = time.monotonic()

while True:
yield floor((time.time() - start_time) / seconds)
yield floor((time.monotonic() - start_time) / seconds)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""TTL expiry must be independent of wall-clock adjustments."""

from openbb_core.provider.utils import lru


def test_clock_correction_cannot_revive_older_cache(monkeypatch):
wall = [1000.0]
elapsed = [100.0]
monkeypatch.setattr(lru.time, "time", lambda: wall[0])
monkeypatch.setattr(lru.time, "monotonic", lambda: elapsed[0])
calls = []

@lru.ttl_cache(ttl=10)
def fetch():
calls.append(len(calls) + 1)
return calls[-1]

assert fetch() == 1
wall[0] += 11
elapsed[0] += 11
assert fetch() == 2
wall[0] -= 11
elapsed[0] += 1
assert fetch() == 2
assert calls == [1, 2]