You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_utils.py 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. import platform
  3. import pytest
  4. from megengine.utils.persistent_cache import PersistentCacheOnServer
  5. @pytest.mark.parametrize("with_flag", [True, False])
  6. @pytest.mark.skipif(
  7. platform.system() not in {"Linux", "Darwin"},
  8. reason="redislite not implemented in windows",
  9. )
  10. def test_persistent_cache_redis(monkeypatch, with_flag):
  11. import redislite
  12. server = redislite.Redis()
  13. monkeypatch.delenv("MGE_FASTRUN_CACHE_TYPE", raising=False)
  14. monkeypatch.setenv(
  15. "MGE_FASTRUN_CACHE_URL", "redis+socket://{}".format(server.socket_file)
  16. )
  17. if with_flag:
  18. server.set("mgb-cache-flag", 1)
  19. pc = PersistentCacheOnServer()
  20. pc.put("test", "hello", "world")
  21. if with_flag:
  22. pc = PersistentCacheOnServer()
  23. assert pc.get("test", "hello") == b"world"
  24. assert pc.config.type == "redis"
  25. else:
  26. assert pc.config.type == "in-file"
  27. def test_persistent_cache_file(monkeypatch, tmp_path):
  28. monkeypatch.setenv("MGE_FASTRUN_CACHE_TYPE", "FILE")
  29. monkeypatch.setenv("MGE_FASTRUN_CACHE_DIR", tmp_path)
  30. pc = PersistentCacheOnServer()
  31. pc.put("test", "store", "this")
  32. assert pc.config.type == "in-file"
  33. del pc
  34. pc = PersistentCacheOnServer()
  35. assert pc.get("test", "store") == b"this"
  36. def test_persistent_cache_file_clear(monkeypatch, tmp_path):
  37. monkeypatch.setenv("MGE_FASTRUN_CACHE_TYPE", "FILE")
  38. monkeypatch.setenv("MGE_FASTRUN_CACHE_DIR", tmp_path)
  39. pc = PersistentCacheOnServer()
  40. pc_dummy = PersistentCacheOnServer()
  41. pc.put("test", "drop", "this")
  42. assert pc.config.type == "in-file"
  43. del pc
  44. # this dummy instance shouldn't override cache file
  45. del pc_dummy
  46. os.unlink(os.path.join(tmp_path, "cache.bin"))
  47. pc = PersistentCacheOnServer()
  48. assert pc.get("test", "drop") is None
  49. def test_persistent_cache_memory(monkeypatch):
  50. monkeypatch.setenv("MGE_FASTRUN_CACHE_TYPE", "MEMORY")
  51. pc = PersistentCacheOnServer()
  52. assert pc.config is None
  53. pc.put("test", "drop", "this")
  54. assert pc.config.type == "in-memory"
  55. assert pc.get("test", "drop") == b"this"
  56. del pc
  57. pc = PersistentCacheOnServer()
  58. assert pc.get("test", "drop") is None