Bläddra i källkod

add tests for CachedLoadingPage

Daniel Sheffield 1 år sedan
förälder
incheckning
c3dc4ac7c2
1 ändrade filer med 65 tillägg och 0 borttagningar
  1. 65 0
      test/rest/test_CachedLoadingPage.py

+ 65 - 0
test/rest/test_CachedLoadingPage.py

@@ -0,0 +1,65 @@
+#
+# Copyright (c) Daniel Sheffield 2023
+#
+# All rights reserved
+#
+# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
+from queue import Queue
+from typing import Any, Generator
+from pytest import mark, fixture
+from time import sleep, time
+from app.rest.CachedLoadingPage import (
+    CachedLoadingPage,
+)
+
+
+@fixture
+def cache():
+    return CachedLoadingPage("start")
+
+
+def test_get_age(cache: CachedLoadingPage):
+    sleep(0.1)
+    assert cache.age > 0.1
+
+def test_get_queue(cache: CachedLoadingPage):
+    assert type(cache.queue) == Queue
+    cache.queue.put("next", timeout=0.1)
+    assert cache.update() == "next"
+
+def test_get_loaded(cache: CachedLoadingPage):
+    assert cache.loaded is False
+    cache.queue.put("not None", timeout=0.1)
+    assert cache.update() == "not None"
+    assert cache.loaded is False
+    cache.queue.put(None, timeout=0.1)
+    assert cache.update() == "not None"
+    assert cache.loaded is True
+
+def test_update(cache: CachedLoadingPage):
+    cache.queue.put("not None", timeout=0.1)
+    assert cache.update() == "not None"
+    cache.queue.put("next value", timeout=0.1)
+    assert cache.update() == "next value"
+    assert cache.update() == "next value"
+    cache.queue.put("another value", timeout=0.1)
+    cache.queue.put("final value", timeout=0.1)
+    assert cache.update() == "another value"
+    assert cache.update() == "final value"
+    cache.queue.put(None, timeout=0.1)
+    assert cache.update() == "final value"
+    assert cache.loaded is True
+    assert cache.update() == "final value"
+
+def test_stale(cache: CachedLoadingPage):
+    cache._created = time() - 10*60
+    assert cache.stale
+
+def test_lock(cache: CachedLoadingPage):
+    cache.queue.put("not None", timeout=0.1)
+    assert cache.update() == "not None"
+    assert cache._lock.acquire()
+    cache.queue.put("next value", timeout=0.1)
+    assert cache.update() == "not None"
+    cache._lock.release()
+    assert cache.update() == "next value"