Skip to content

Commit 1ea21a5

Browse files
author
Sam Park
committed
Tests
1 parent 6e58a10 commit 1ea21a5

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

tests/contrib/dogpile_cache/__init__.py

Whitespace-only changes.

tests/contrib/dogpile_cache/common.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from dogpile.cache import make_region
2+
3+
4+
# Setup a simple dogpile cache region for testing.
5+
# The backend is trivial so we can use memory to simplify test setup.
6+
test_region = make_region(name='TestRegion')
7+
# This lets us 'flush' the region between tests.
8+
cache_dict = {}
9+
test_region.configure('dogpile.cache.memory', arguments={'cache_dict': cache_dict})
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import dogpile
2+
import pytest
3+
import wrapt
4+
5+
from ddtrace import Pin
6+
from ddtrace.contrib.dogpile_cache.patch import patch, unpatch
7+
from ddtrace.contrib.dogpile_cache.region import _wrap_get_create, _wrap_get_create_multi
8+
9+
from tests.test_tracer import get_dummy_tracer
10+
11+
12+
@pytest.fixture
13+
def tracer():
14+
return get_dummy_tracer()
15+
16+
17+
@pytest.fixture
18+
def region(tracer):
19+
patch()
20+
# Setup a simple dogpile cache region for testing.
21+
# The backend is trivial so we can use memory to simplify test setup.
22+
test_region = dogpile.cache.make_region(name='TestRegion')
23+
test_region.configure('dogpile.cache.memory')
24+
Pin.override(dogpile.cache, tracer=tracer)
25+
return test_region
26+
27+
28+
@pytest.fixture(autouse=True)
29+
def cleanup():
30+
yield
31+
unpatch()
32+
33+
34+
@pytest.fixture
35+
def single_cache(region):
36+
@region.cache_on_arguments()
37+
def fn(x):
38+
return x * 2
39+
return fn
40+
41+
42+
@pytest.fixture
43+
def multi_cache(region):
44+
@region.cache_multi_on_arguments()
45+
def fn(*x):
46+
print(x)
47+
return [i * 2 for i in x]
48+
49+
return fn
50+
51+
52+
def test_doesnt_trace_with_no_pin(tracer, single_cache, multi_cache):
53+
# No pin is set
54+
unpatch()
55+
56+
assert single_cache(1) == 2
57+
assert tracer.writer.pop_traces() == []
58+
59+
assert multi_cache(2, 3) == [4, 6]
60+
assert tracer.writer.pop_traces() == []
61+
62+
63+
def test_doesnt_trace_with_disabled_pin(tracer, single_cache, multi_cache):
64+
tracer.enabled = False
65+
66+
assert single_cache(1) == 2
67+
assert tracer.writer.pop_traces() == []
68+
69+
assert multi_cache(2, 3) == [4, 6]
70+
assert tracer.writer.pop_traces() == []
71+
72+
73+
def test_traces(tracer, single_cache, multi_cache):
74+
assert single_cache(1) == 2
75+
traces = tracer.writer.pop_traces()
76+
assert len(traces) == 1
77+
spans = traces[0]
78+
assert len(spans) == 3
79+
span = spans[0]
80+
assert span.name == 'dogpile.cache'
81+
assert span.resource == 'get_or_create'
82+
assert span.meta['key'] == 'tests.contrib.dogpile_cache.test_tracing:fn|1'
83+
assert span.meta['hit'] == 'False'
84+
assert span.meta['expired'] == 'True'
85+
assert span.meta['backend'] == 'MemoryBackend'
86+
assert span.meta['region'] == 'TestRegion'
87+
span = spans[1]
88+
assert span.name == 'dogpile.cache'
89+
assert span.resource == 'acquire_lock'
90+
# Normally users will probably also enable tracing for their specific cache system,
91+
# in which case a span in the middle would be here showing the actual lookup. But
92+
# that's not the job of this tracing. Just FYI.
93+
span = spans[2]
94+
assert span.name == 'dogpile.cache'
95+
assert span.resource == 'release_lock'
96+
97+
assert multi_cache(2, 3) == [4, 6]
98+
traces = tracer.writer.pop_traces()
99+
assert len(traces) == 1
100+
spans = traces[0]
101+
print([(s.name, s.resource, s.meta) for s in spans])
102+
assert len(spans) == 5
103+
span = spans[0]
104+
assert span.meta['keys'] == (
105+
"['tests.contrib.dogpile_cache.test_tracing:fn|2', "
106+
+ "'tests.contrib.dogpile_cache.test_tracing:fn|3']"
107+
)
108+
assert span.meta['hit'] == 'False'
109+
assert span.meta['expired'] == 'True'
110+
assert span.meta['backend'] == 'MemoryBackend'
111+
assert span.meta['region'] == 'TestRegion'
112+
span = spans[1]
113+
assert span.name == 'dogpile.cache'
114+
assert span.resource == 'acquire_lock'
115+
span = spans[2]
116+
assert span.name == 'dogpile.cache'
117+
assert span.resource == 'acquire_lock'
118+
span = spans[3]
119+
assert span.name == 'dogpile.cache'
120+
assert span.resource == 'release_lock'
121+
span = spans[4]
122+
assert span.name == 'dogpile.cache'
123+
assert span.resource == 'release_lock'

tox.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ envlist =
6161
django_contrib{,_autopatch}-{py34,py35,py36}-django{200}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached
6262
django_drf_contrib-{py27,py34,py35,py36}-django{111}-djangorestframework{34,37,38}
6363
django_drf_contrib-{py34,py35,py36}-django{200}-djangorestframework{37,38}
64+
dogpile_contrib-{py27,35,36,37}-dogpilecache{07,08,latest}
6465
elasticsearch_contrib-{py27,py34,py35,py36}-elasticsearch{16,17,18,23,24,51,52,53,54,63,64}
6566
elasticsearch_contrib-{py27,py34,py35,py36}-elasticsearch1{100}
6667
elasticsearch_contrib-{py27,py34,py35,py36}-elasticsearch2{50}
@@ -210,6 +211,9 @@ deps =
210211
djangorestframework34: djangorestframework>=3.4,<3.5
211212
djangorestframework37: djangorestframework>=3.7,<3.8
212213
djangorestframework38: djangorestframework>=3.8,<3.9
214+
dogpilecache07: dogpile.cache==0.7.*
215+
dogpilecache08: dogpile.cache==0.8.*
216+
dogpilecachelatest: dogpile.cache
213217
elasticsearch16: elasticsearch>=1.6,<1.7
214218
elasticsearch17: elasticsearch>=1.7,<1.8
215219
elasticsearch18: elasticsearch>=1.8,<1.9
@@ -395,6 +399,7 @@ commands =
395399
django_contrib: pytest {posargs} tests/contrib/django
396400
django_contrib_autopatch: python tests/ddtrace_run.py pytest {posargs} tests/contrib/django
397401
django_drf_contrib: pytest {posargs} tests/contrib/djangorestframework
402+
dogpile_contrib: pytest {posargs} tests/contrib/dogpile_cache
398403
elasticsearch_contrib: pytest {posargs} tests/contrib/elasticsearch
399404
falcon_contrib: pytest {posargs} tests/contrib/falcon/test_middleware.py tests/contrib/falcon/test_distributed_tracing.py
400405
falcon_contrib_autopatch: python tests/ddtrace_run.py pytest {posargs} tests/contrib/falcon/test_autopatch.py

0 commit comments

Comments
 (0)