[Git][NTPsec/ntpsec][master] Added tests for Cache(), Cache() now deletes expired entries.
Ian Bruene
gitlab at mg.gitlab.com
Sun Aug 27 19:39:20 UTC 2017
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
f2158f28 by Ian Bruene at 2017-08-27T14:37:06-05:00
Added tests for Cache(), Cache() now deletes expired entries.
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -489,6 +489,8 @@ class Cache:
value, settime = self._cache[key]
if settime >= monoclock() - self.ttl:
return value
+ else: # key expired, delete it
+ del self._cache[key]
def set(self, key, value):
self._cache[key] = (value, monoclock())
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -371,5 +371,38 @@ class TestPylibUtilMethods(unittest.TestCase):
# Test -xxx.xxx
self.assertEqual(f(-123.456789), "-123.457")
+ def test_Cache(self):
+ c = ntp.util.Cache
+
+ monodata = []
+ def monoclock_jig():
+ return monodata.pop(0)
+
+ # Test init
+ cls = c()
+ self.assertEqual(cls._cache, {})
+ try:
+ monotemp = ntp.util.monoclock
+ ntp.util.monoclock = monoclock_jig
+ # Test set
+ monodata = [5, 10, 315, 20]
+ cls.set("foo", 42)
+ cls.set("bar", 23)
+ self.assertEqual(cls._cache, {"foo": (42, 5),
+ "bar": (23, 10)})
+ self.assertEqual(monodata, [315, 20])
+ # Test get, expired
+ result = cls.get("foo")
+ self.assertEqual(result, None)
+ self.assertEqual(monodata, [20])
+ self.assertEqual(cls._cache, {"bar": (23, 10)})
+ # Test get, valid
+ result = cls.get("bar")
+ self.assertEqual(result, 23)
+ self.assertEqual(monodata, [])
+ self.assertEqual(cls._cache, {"bar": (23, 10)})
+ finally:
+ ntp.util.monoclock = monotemp
+
if __name__ == '__main__':
unittest.main()
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/f2158f28c3d3903454779625963f28452a252e26
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/f2158f28c3d3903454779625963f28452a252e26
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170827/347e1f0f/attachment.html>
More information about the vc
mailing list