freqtrade_origin/tests/util/test_periodiccache.py

33 lines
877 B
Python
Raw Permalink Normal View History

2021-09-13 17:32:51 +00:00
import time_machine
2022-08-10 08:57:19 +00:00
from freqtrade.util import PeriodicCache
2021-09-13 17:32:51 +00:00
def test_ttl_cache():
2024-02-17 15:27:43 +00:00
with time_machine.travel("2021-09-01 05:00:00 +00:00", tick=False) as t:
2021-09-13 17:32:51 +00:00
cache = PeriodicCache(5, ttl=60)
cache1h = PeriodicCache(5, ttl=3600)
assert cache.timer() == 1630472400.0
2024-05-12 13:41:07 +00:00
cache["a"] = 1235
cache1h["a"] = 555123
assert "a" in cache
assert "a" in cache1h
2021-09-13 17:32:51 +00:00
t.move_to("2021-09-01 05:00:59 +00:00")
2024-05-12 13:41:07 +00:00
assert "a" in cache
assert "a" in cache1h
2021-09-13 17:32:51 +00:00
# Cache expired
t.move_to("2021-09-01 05:01:00 +00:00")
2024-05-12 13:41:07 +00:00
assert "a" not in cache
assert "a" in cache1h
2021-09-13 17:32:51 +00:00
t.move_to("2021-09-01 05:59:59 +00:00")
2024-05-12 13:41:07 +00:00
assert "a" not in cache
assert "a" in cache1h
2021-09-13 17:32:51 +00:00
t.move_to("2021-09-01 06:00:00 +00:00")
2024-05-12 13:41:07 +00:00
assert "a" not in cache
assert "a" not in cache1h