Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Park committed Nov 1, 2019
1 parent 6e58a10 commit 1ea21a5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
Empty file.
9 changes: 9 additions & 0 deletions tests/contrib/dogpile_cache/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dogpile.cache import make_region


# Setup a simple dogpile cache region for testing.
# The backend is trivial so we can use memory to simplify test setup.
test_region = make_region(name='TestRegion')
# This lets us 'flush' the region between tests.
cache_dict = {}
test_region.configure('dogpile.cache.memory', arguments={'cache_dict': cache_dict})
123 changes: 123 additions & 0 deletions tests/contrib/dogpile_cache/test_tracing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import dogpile
import pytest
import wrapt

from ddtrace import Pin
from ddtrace.contrib.dogpile_cache.patch import patch, unpatch
from ddtrace.contrib.dogpile_cache.region import _wrap_get_create, _wrap_get_create_multi

from tests.test_tracer import get_dummy_tracer


@pytest.fixture
def tracer():
return get_dummy_tracer()


@pytest.fixture
def region(tracer):
patch()
# Setup a simple dogpile cache region for testing.
# The backend is trivial so we can use memory to simplify test setup.
test_region = dogpile.cache.make_region(name='TestRegion')
test_region.configure('dogpile.cache.memory')
Pin.override(dogpile.cache, tracer=tracer)
return test_region


@pytest.fixture(autouse=True)
def cleanup():
yield
unpatch()


@pytest.fixture
def single_cache(region):
@region.cache_on_arguments()
def fn(x):
return x * 2
return fn


@pytest.fixture
def multi_cache(region):
@region.cache_multi_on_arguments()
def fn(*x):
print(x)
return [i * 2 for i in x]

return fn


def test_doesnt_trace_with_no_pin(tracer, single_cache, multi_cache):
# No pin is set
unpatch()

assert single_cache(1) == 2
assert tracer.writer.pop_traces() == []

assert multi_cache(2, 3) == [4, 6]
assert tracer.writer.pop_traces() == []


def test_doesnt_trace_with_disabled_pin(tracer, single_cache, multi_cache):
tracer.enabled = False

assert single_cache(1) == 2
assert tracer.writer.pop_traces() == []

assert multi_cache(2, 3) == [4, 6]
assert tracer.writer.pop_traces() == []


def test_traces(tracer, single_cache, multi_cache):
assert single_cache(1) == 2
traces = tracer.writer.pop_traces()
assert len(traces) == 1
spans = traces[0]
assert len(spans) == 3
span = spans[0]
assert span.name == 'dogpile.cache'
assert span.resource == 'get_or_create'
assert span.meta['key'] == 'tests.contrib.dogpile_cache.test_tracing:fn|1'
assert span.meta['hit'] == 'False'
assert span.meta['expired'] == 'True'
assert span.meta['backend'] == 'MemoryBackend'
assert span.meta['region'] == 'TestRegion'
span = spans[1]
assert span.name == 'dogpile.cache'
assert span.resource == 'acquire_lock'
# Normally users will probably also enable tracing for their specific cache system,
# in which case a span in the middle would be here showing the actual lookup. But
# that's not the job of this tracing. Just FYI.
span = spans[2]
assert span.name == 'dogpile.cache'
assert span.resource == 'release_lock'

assert multi_cache(2, 3) == [4, 6]
traces = tracer.writer.pop_traces()
assert len(traces) == 1
spans = traces[0]
print([(s.name, s.resource, s.meta) for s in spans])
assert len(spans) == 5
span = spans[0]
assert span.meta['keys'] == (
"['tests.contrib.dogpile_cache.test_tracing:fn|2', "
+ "'tests.contrib.dogpile_cache.test_tracing:fn|3']"
)
assert span.meta['hit'] == 'False'
assert span.meta['expired'] == 'True'
assert span.meta['backend'] == 'MemoryBackend'
assert span.meta['region'] == 'TestRegion'
span = spans[1]
assert span.name == 'dogpile.cache'
assert span.resource == 'acquire_lock'
span = spans[2]
assert span.name == 'dogpile.cache'
assert span.resource == 'acquire_lock'
span = spans[3]
assert span.name == 'dogpile.cache'
assert span.resource == 'release_lock'
span = spans[4]
assert span.name == 'dogpile.cache'
assert span.resource == 'release_lock'
5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ envlist =
django_contrib{,_autopatch}-{py34,py35,py36}-django{200}-djangopylibmc06-djangoredis45-pylibmc-redis{210}-memcached
django_drf_contrib-{py27,py34,py35,py36}-django{111}-djangorestframework{34,37,38}
django_drf_contrib-{py34,py35,py36}-django{200}-djangorestframework{37,38}
dogpile_contrib-{py27,35,36,37}-dogpilecache{07,08,latest}
elasticsearch_contrib-{py27,py34,py35,py36}-elasticsearch{16,17,18,23,24,51,52,53,54,63,64}
elasticsearch_contrib-{py27,py34,py35,py36}-elasticsearch1{100}
elasticsearch_contrib-{py27,py34,py35,py36}-elasticsearch2{50}
Expand Down Expand Up @@ -210,6 +211,9 @@ deps =
djangorestframework34: djangorestframework>=3.4,<3.5
djangorestframework37: djangorestframework>=3.7,<3.8
djangorestframework38: djangorestframework>=3.8,<3.9
dogpilecache07: dogpile.cache==0.7.*
dogpilecache08: dogpile.cache==0.8.*
dogpilecachelatest: dogpile.cache
elasticsearch16: elasticsearch>=1.6,<1.7
elasticsearch17: elasticsearch>=1.7,<1.8
elasticsearch18: elasticsearch>=1.8,<1.9
Expand Down Expand Up @@ -395,6 +399,7 @@ commands =
django_contrib: pytest {posargs} tests/contrib/django
django_contrib_autopatch: python tests/ddtrace_run.py pytest {posargs} tests/contrib/django
django_drf_contrib: pytest {posargs} tests/contrib/djangorestframework
dogpile_contrib: pytest {posargs} tests/contrib/dogpile_cache
elasticsearch_contrib: pytest {posargs} tests/contrib/elasticsearch
falcon_contrib: pytest {posargs} tests/contrib/falcon/test_middleware.py tests/contrib/falcon/test_distributed_tracing.py
falcon_contrib_autopatch: python tests/ddtrace_run.py pytest {posargs} tests/contrib/falcon/test_autopatch.py
Expand Down

0 comments on commit 1ea21a5

Please sign in to comment.