Skip to content

Commit d85ac93

Browse files
author
Emanuele Palazzetti
committed
[django] add tests for all backends
1 parent 68c4c10 commit d85ac93

File tree

3 files changed

+264
-2
lines changed

3 files changed

+264
-2
lines changed

tests/contrib/django/app/settings.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,28 @@
1919
'default': {
2020
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
2121
'LOCATION': 'unique-snowflake',
22-
}
22+
},
23+
'redis': {
24+
'BACKEND': 'django_redis.cache.RedisCache',
25+
'LOCATION': 'redis://127.0.0.1:56379/1',
26+
},
27+
'pylibmc': {
28+
'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
29+
'LOCATION': '127.0.0.1:51211',
30+
},
31+
'python_memcached': {
32+
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
33+
'LOCATION': '127.0.0.1:51211',
34+
},
35+
'django_pylibmc': {
36+
'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
37+
'LOCATION': '127.0.0.1:51211',
38+
'BINARY': True,
39+
'OPTIONS': {
40+
'tcp_nodelay': True,
41+
'ketama': True
42+
}
43+
},
2344
}
2445

2546
SITE_ID = 1
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
import time
2+
3+
# 3rd party
4+
from nose.tools import eq_, ok_
5+
from django.core.cache import caches
6+
7+
# testing
8+
from .utils import DjangoTraceTestCase
9+
10+
11+
class DjangoCacheRedisTest(DjangoTraceTestCase):
12+
"""
13+
Ensures that the cache system is properly traced in
14+
different cache backend
15+
"""
16+
def test_cache_redis_get(self):
17+
# get the redis cache
18+
cache = caches['redis']
19+
20+
# (trace) the cache miss
21+
start = time.time()
22+
hit = cache.get('missing_key')
23+
end = time.time()
24+
25+
# tests
26+
spans = self.tracer.writer.pop()
27+
eq_(len(spans), 1)
28+
29+
span = spans[0]
30+
eq_(span.service, 'django')
31+
eq_(span.resource, 'get')
32+
eq_(span.name, 'django.cache')
33+
eq_(span.span_type, 'cache')
34+
eq_(span.error, 0)
35+
36+
expected_meta = {
37+
'django.cache.backend': 'django_redis.cache.RedisCache',
38+
'django.cache.key': 'missing_key',
39+
}
40+
41+
eq_(span.meta, expected_meta)
42+
assert start < span.start < span.start + span.duration < end
43+
44+
def test_cache_redis_get_many(self):
45+
# get the redis cache
46+
cache = caches['redis']
47+
48+
# (trace) the cache miss
49+
start = time.time()
50+
hit = cache.get_many(['missing_key', 'another_key'])
51+
end = time.time()
52+
53+
# tests
54+
spans = self.tracer.writer.pop()
55+
eq_(len(spans), 1)
56+
57+
span = spans[0]
58+
eq_(span.service, 'django')
59+
eq_(span.resource, 'get_many')
60+
eq_(span.name, 'django.cache')
61+
eq_(span.span_type, 'cache')
62+
eq_(span.error, 0)
63+
64+
expected_meta = {
65+
'django.cache.backend': 'django_redis.cache.RedisCache',
66+
'django.cache.key': str(['missing_key', 'another_key']),
67+
}
68+
69+
eq_(span.meta, expected_meta)
70+
assert start < span.start < span.start + span.duration < end
71+
72+
def test_cache_pylibmc_get(self):
73+
# get the redis cache
74+
cache = caches['pylibmc']
75+
76+
# (trace) the cache miss
77+
start = time.time()
78+
hit = cache.get('missing_key')
79+
end = time.time()
80+
81+
# tests
82+
spans = self.tracer.writer.pop()
83+
eq_(len(spans), 1)
84+
85+
span = spans[0]
86+
eq_(span.service, 'django')
87+
eq_(span.resource, 'get')
88+
eq_(span.name, 'django.cache')
89+
eq_(span.span_type, 'cache')
90+
eq_(span.error, 0)
91+
92+
expected_meta = {
93+
'django.cache.backend': 'django.core.cache.backends.memcached.PyLibMCCache',
94+
'django.cache.key': 'missing_key',
95+
}
96+
97+
eq_(span.meta, expected_meta)
98+
assert start < span.start < span.start + span.duration < end
99+
100+
def test_cache_pylibmc_get_many(self):
101+
# get the redis cache
102+
cache = caches['pylibmc']
103+
104+
# (trace) the cache miss
105+
start = time.time()
106+
hit = cache.get_many(['missing_key', 'another_key'])
107+
end = time.time()
108+
109+
# tests
110+
spans = self.tracer.writer.pop()
111+
eq_(len(spans), 1)
112+
113+
span = spans[0]
114+
eq_(span.service, 'django')
115+
eq_(span.resource, 'get_many')
116+
eq_(span.name, 'django.cache')
117+
eq_(span.span_type, 'cache')
118+
eq_(span.error, 0)
119+
120+
expected_meta = {
121+
'django.cache.backend': 'django.core.cache.backends.memcached.PyLibMCCache',
122+
'django.cache.key': str(['missing_key', 'another_key']),
123+
}
124+
125+
eq_(span.meta, expected_meta)
126+
assert start < span.start < span.start + span.duration < end
127+
128+
def test_cache_memcached_get(self):
129+
# get the redis cache
130+
cache = caches['python_memcached']
131+
132+
# (trace) the cache miss
133+
start = time.time()
134+
hit = cache.get('missing_key')
135+
end = time.time()
136+
137+
# tests
138+
spans = self.tracer.writer.pop()
139+
eq_(len(spans), 1)
140+
141+
span = spans[0]
142+
eq_(span.service, 'django')
143+
eq_(span.resource, 'get')
144+
eq_(span.name, 'django.cache')
145+
eq_(span.span_type, 'cache')
146+
eq_(span.error, 0)
147+
148+
expected_meta = {
149+
'django.cache.backend': 'django.core.cache.backends.memcached.MemcachedCache',
150+
'django.cache.key': 'missing_key',
151+
}
152+
153+
eq_(span.meta, expected_meta)
154+
assert start < span.start < span.start + span.duration < end
155+
156+
def test_cache_memcached_get_many(self):
157+
# get the redis cache
158+
cache = caches['python_memcached']
159+
160+
# (trace) the cache miss
161+
start = time.time()
162+
hit = cache.get_many(['missing_key', 'another_key'])
163+
end = time.time()
164+
165+
# tests
166+
spans = self.tracer.writer.pop()
167+
eq_(len(spans), 1)
168+
169+
span = spans[0]
170+
eq_(span.service, 'django')
171+
eq_(span.resource, 'get_many')
172+
eq_(span.name, 'django.cache')
173+
eq_(span.span_type, 'cache')
174+
eq_(span.error, 0)
175+
176+
expected_meta = {
177+
'django.cache.backend': 'django.core.cache.backends.memcached.MemcachedCache',
178+
'django.cache.key': str(['missing_key', 'another_key']),
179+
}
180+
181+
eq_(span.meta, expected_meta)
182+
assert start < span.start < span.start + span.duration < end
183+
184+
def test_cache_django_pylibmc_get(self):
185+
# get the redis cache
186+
cache = caches['django_pylibmc']
187+
188+
# (trace) the cache miss
189+
start = time.time()
190+
hit = cache.get('missing_key')
191+
end = time.time()
192+
193+
# tests
194+
spans = self.tracer.writer.pop()
195+
eq_(len(spans), 1)
196+
197+
span = spans[0]
198+
eq_(span.service, 'django')
199+
eq_(span.resource, 'get')
200+
eq_(span.name, 'django.cache')
201+
eq_(span.span_type, 'cache')
202+
eq_(span.error, 0)
203+
204+
expected_meta = {
205+
'django.cache.backend': 'django_pylibmc.memcached.PyLibMCCache',
206+
'django.cache.key': 'missing_key',
207+
}
208+
209+
eq_(span.meta, expected_meta)
210+
assert start < span.start < span.start + span.duration < end
211+
212+
def test_cache_django_pylibmc_get_many(self):
213+
# get the redis cache
214+
cache = caches['django_pylibmc']
215+
216+
# (trace) the cache miss
217+
start = time.time()
218+
hit = cache.get_many(['missing_key', 'another_key'])
219+
end = time.time()
220+
221+
# tests
222+
spans = self.tracer.writer.pop()
223+
eq_(len(spans), 1)
224+
225+
span = spans[0]
226+
eq_(span.service, 'django')
227+
eq_(span.resource, 'get_many')
228+
eq_(span.name, 'django.cache')
229+
eq_(span.span_type, 'cache')
230+
eq_(span.error, 0)
231+
232+
expected_meta = {
233+
'django.cache.backend': 'django_pylibmc.memcached.PyLibMCCache',
234+
'django.cache.key': str(['missing_key', 'another_key']),
235+
}
236+
237+
eq_(span.meta, expected_meta)
238+
assert start < span.start < span.start + span.duration < end

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ envlist =
1111
flake8
1212
{py27,py34}-elasticsearch{23}
1313
{py27,py34}-falcon{10}
14-
{py27,py34}-django{18,19,110}
14+
{py27,py34}-django{18,19,110}-djangopylibmc06-djangoredis45-pylibmc-redis-memcached
1515
{py27,py34}-flask{010,011}-blinker
1616
{py27,py34}-flask{010,011}-flaskcache{013}-memcached-redis-blinker
1717
{py27}-flask{010,011}-flaskcache{012}-memcached-redis-blinker
@@ -51,13 +51,16 @@ deps =
5151
django18: django>=1.8,<1.9
5252
django19: django>=1.9,<1.10
5353
django110: django>=1.10,<1.11
54+
djangopylibmc06: django-pylibmc>=0.6,<0.7
55+
djangoredis45: django-redis>=4.5,<4.6
5456
flask010: flask>=0.10,<0.11
5557
flask011: flask>=0.11
5658
flaskcache012: flask_cache>=0.12,<0.13
5759
flaskcache013: flask_cache>=0.13,<0.14
5860
memcached: python-memcached
5961
mongoengine: mongoengine
6062
mysqlconnector21: mysql-connector>=2.1,<2.2
63+
pylibmc: pylibmc
6164
pylibmc140: pylibmc>=1.4.0,<1.5.0
6265
pylibmc150: pylibmc>=1.5.0
6366
pymongo30: pymongo>=3.0,<3.1

0 commit comments

Comments
 (0)