From dff825db06b055f215edf3133dcccdbf7f8fbac7 Mon Sep 17 00:00:00 2001 From: codl Date: Fri, 8 Sep 2017 23:19:59 +0200 Subject: [PATCH] test libbrotli --- .coveragerc | 3 ++ lib/brotli.py | 14 ++--- pytest.ini | 2 + test/__init__.py | 0 test/static/bee.txt | 1 + test/static/bee.txt.br | Bin 0 -> 98 bytes test/static/bee.txt.gz | Bin 0 -> 141 bytes test/test_libbrotli.py | 120 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 .coveragerc create mode 100644 pytest.ini create mode 100644 test/__init__.py create mode 100644 test/static/bee.txt create mode 100644 test/static/bee.txt.br create mode 100644 test/static/bee.txt.gz create mode 100644 test/test_libbrotli.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..01cd652 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +omit = + */site-packages/* diff --git a/lib/brotli.py b/lib/brotli.py index 36dd372..b933195 100644 --- a/lib/brotli.py +++ b/lib/brotli.py @@ -8,15 +8,15 @@ import mimetypes class BrotliCache(object): - def __init__(self, redis_uri='redis://', max_wait=0.020, expire=60*60*6): + def __init__(self, redis_uri='redis://', timeout=0.020, expire=60*60*6): self.redis = redis.StrictRedis.from_url(redis_uri) - self.max_wait = max_wait + self.timeout = timeout self.expire = expire self.redis.client_setname('brotlicache') def compress(self, cache_key, lock_key, body, mode=brotli_.MODE_GENERIC): encbody = brotli_.compress(body, mode=mode) - self.redis.set(cache_key, encbody, ex=self.expire) + self.redis.set(cache_key, encbody, px=int(self.expire*1000)) self.redis.delete(lock_key) def wrap_response(self, response): @@ -41,8 +41,8 @@ class BrotliCache(object): target=self.compress, args=(cache_key, lock_key, body, mode)) t.start() - if self.max_wait > 0: - t.join(self.max_wait) + if self.timeout > 0: + t.join(self.timeout) encbody = self.redis.get(cache_key) if not encbody: response.headers.set('x-brotli-cache', 'TIMEOUT') @@ -57,7 +57,7 @@ class BrotliCache(object): return response -def brotli(app, static=True, dynamic=True): +def brotli(app, static=True, dynamic=True, **kwargs): original_static = app.view_functions['static'] def static_maybe_gzip_brotli(filename=None): @@ -79,5 +79,5 @@ def brotli(app, static=True, dynamic=True): if static: app.view_functions['static'] = static_maybe_gzip_brotli if dynamic: - cache = BrotliCache(redis_uri = app.config.get('REDIS_URI')) + cache = BrotliCache(redis_uri=app.config.get('REDIS_URI'), **kwargs) app.after_request(cache.wrap_response) diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..4ad6217 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +redis_port = 15487 diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/static/bee.txt b/test/static/bee.txt new file mode 100644 index 0000000..b84c248 --- /dev/null +++ b/test/static/bee.txt @@ -0,0 +1 @@ +According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground. diff --git a/test/static/bee.txt.br b/test/static/bee.txt.br new file mode 100644 index 0000000000000000000000000000000000000000..14b3d1a9e078147dfee0c67f712d52d4b4b26069 GIT binary patch literal 98 zcmV-o0G(wWrz&TtOnZn~nydVbBcvRr0Fs1;4m6s-Yu*U2!IdRQENDn&x`T`vj%y)6x