Debian Codegraph @

tests/conftest.py

raw
1 import os
2 import shutil
3 import socket
4 import subprocess
5
6 import cachelib.file
7 import cachelib.simple
8 import flask
9 import pytest
10 from xprocess import ProcessStarter
11
12 import flask_caching as fsc
13
14
15 @pytest.fixture
16 def app(request):
17 app = flask.Flask(
18 request.module.__name__, template_folder=os.path.dirname(__file__)
19 )
20 app.testing = True
21 app.config["CACHE_TYPE"] = "SimpleCache"
22 return app
23
24
25 @pytest.fixture
26 def cache(app):
27 return fsc.Cache(app)
28
29
30 class _Clock:
31 def __init__(self, now):
32 self.now = now
33
34 def __call__(self):
35 return self.now
36
37 def advance(self, seconds):
38 self.now += seconds
39
40
41 @pytest.fixture
42 def clock(monkeypatch):
43 # cachelib does ``from time import time``, so the module attribute has to
44 # be replaced
45 fake = _Clock(1_700_000_000.0)
46 monkeypatch.setattr(cachelib.simple, "time", fake)
47 monkeypatch.setattr(cachelib.file, "time", fake)
48 return fake
49
50
51 @pytest.fixture(
52 params=[method for method in fsc.SUPPORTED_HASH_FUNCTIONS],
53 ids=[method.__name__ for method in fsc.SUPPORTED_HASH_FUNCTIONS],
54 )
55 def hash_method(request):
56 return request.param
57
58
59 def _server_is_running(port):
60 with socket.socket() as sock:
61 sock.settimeout(1)
62 return sock.connect_ex(("127.0.0.1", port)) == 0
63
64
65 def _ensure_server(xprocess, name, executable, port, starter):
66 if _server_is_running(port):
67 yield
68 return
69
70 if os.environ.get("CI", "false") == "true":
71 pytest.fail(f"no {executable} server listening on port {port}")
72
73 if shutil.which(executable) is None:
74 pytest.skip(f"could not find {executable} executable")
75
76 try:
77 xprocess.ensure(name, starter)
78 except (OSError, RuntimeError) as exc:
79 pytest.skip(f"could not start {executable}: {exc}")
80
81 yield
82 xprocess.getinfo(name).terminate()
83
84
85 @pytest.fixture(scope="class")
86 def redis_server(xprocess):
87 package_name = "redis"
88 pytest.importorskip(
89 modname=package_name, reason=f"could not find python package {package_name}"
90 )
91
92 class Starter(ProcessStarter):
93 timeout = 20
94 pattern = "[Rr]eady to accept connections"
95 args = ["redis-server", "--port 6360"]
96
97 def startup_check(self):
98 out = subprocess.run(
99 ["redis-cli", "-p", "6360", "ping"], stdout=subprocess.PIPE
100 )
101 return out.stdout == b"PONG\n"
102
103 yield from _ensure_server(xprocess, package_name, "redis-server", 6360, Starter)
104
105
106 @pytest.fixture(scope="class")
107 def memcache_server(xprocess):
108 package_name = "pylibmc"
109 pytest.importorskip(
110 modname=package_name, reason=f"could not find python package {package_name}"
111 )
112
113 class Starter(ProcessStarter):
114 timeout = 20
115 pattern = "server listening"
116 args = ["memcached", "-vv", "-p", "11212"]
117
118 def startup_check(self):
119 out = subprocess.run(["memcached", "-p", "11212"], stderr=subprocess.PIPE)
120 return b"Address already" in out.stderr
121
122 yield from _ensure_server(xprocess, package_name, "memcached", 11212, Starter)