Skip to content

Commit

Permalink
Merge pull request #122 from DataDog/backwards
Browse files Browse the repository at this point in the history
Backwards compatibility tests
  • Loading branch information
clutchski authored Nov 26, 2016
2 parents db3ac40 + 88b46a1 commit 112c1b4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
6 changes: 4 additions & 2 deletions ddtrace/contrib/mongoengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

with require_modules(required_modules) as missing_modules:
if not missing_modules:
from .patch import patch
from .patch import patch, trace_mongoengine

__all__ = ['patch', 'trace_mongoengine']


__all__ = ['patch']
5 changes: 5 additions & 0 deletions ddtrace/contrib/mongoengine/patch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoengine

from .trace import WrappedConnect
from ddtrace.util import deprecated

# Original connect function
_connect = mongoengine.connect
Expand All @@ -12,3 +13,7 @@ def patch():
def unpatch():
setattr(mongoengine, 'connect', _connect)

@deprecated(message='Use patching instead (see the docs).', version='0.6.0')
def trace_mongoengine(*args, **kwargs):
return _connect

2 changes: 1 addition & 1 deletion ddtrace/contrib/pylibmc/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def patch():
setattr(pylibmc, 'Client', TracedClient)

def unpatch():
setattr(pylibmc, 'Elasticsearch', _Client)
setattr(pylibmc, 'Client', _Client)

27 changes: 27 additions & 0 deletions tests/contrib/mongoengine/test_backwards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
ensure old interfaces exist and won't break things.
"""


import mongoengine

from tests.test_tracer import get_dummy_tracer
from tests.contrib import config

class Singer(mongoengine.Document):
first_name = mongoengine.StringField(max_length=50)
last_name = mongoengine.StringField(max_length=50)


def test_less_than_v04():
# interface from < v0.4
from ddtrace.contrib.mongoengine import trace_mongoengine
tracer = get_dummy_tracer()

connect = trace_mongoengine(tracer, service="my-mongo-db", patch=False)
connect(port=config.MONGO_CONFIG['port'])

lc = Singer()
lc.first_name = 'leonard'
lc.last_name = 'cohen'
lc.save()
13 changes: 13 additions & 0 deletions tests/contrib/mysql/test_backwards_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from ddtrace.contrib.mysql import get_traced_mysql_connection
from tests.test_tracer import get_dummy_tracer
from tests.contrib import config


def test_pre_v4():
tracer = get_dummy_tracer()
MySQL = get_traced_mysql_connection(tracer, service="my-mysql-server")
conn = MySQL(**config.MYSQL_CONFIG)
cursor = conn.cursor()
cursor.execute("SELECT 1")
assert cursor.fetchone()[0] == 1
7 changes: 7 additions & 0 deletions tests/contrib/psycopg/test_psycopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,10 @@ def test_patch_unpatch(self):
spans = writer.pop()
assert spans, spans
eq_(len(spans), 1)

def test_backwards_compatibilty_v3():
tracer = get_dummy_tracer()
factory = connection_factory(tracer, service="my-postgres-db")
conn = psycopg2.connect(connection_factory=factory, **POSTGRES_CONFIG)
conn.cursor().execute("select 'blah'")

2 changes: 1 addition & 1 deletion tests/contrib/redis/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nose.tools import eq_, ok_

from ddtrace import Pin, compat
from ddtrace.contrib.redis import get_traced_redis
from ddtrace.contrib.redis import get_traced_redis, get_traced_redis_from
from ddtrace.contrib.redis.patch import patch, unpatch
from ..config import REDIS_CONFIG
from ...test_tracer import get_dummy_tracer
Expand Down
18 changes: 17 additions & 1 deletion tests/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
import sys

# 3p
import pylibmc
import pympler.tracker
import psycopg2
import redis


# project
import ddtrace
from tests.contrib import config


ddtrace.patch(redis=True)
# verbosity
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
ddtrace.tracer.debug_logging = False

ddtrace.patch_all()
ddtrace.tracer.writer = None


Expand All @@ -30,9 +36,15 @@ def __init__(self):
self._redis = redis.Redis(**config.REDIS_CONFIG)
self._pg = psycopg2.connect(**config.POSTGRES_CONFIG)

url = "%s:%s" % (
config.MEMCACHED_CONFIG["host"],
config.MEMCACHED_CONFIG["port"])
self._pylibmc = pylibmc.Client([url])

def ping(self, i):
self._ping_redis(i)
self._ping_pg(i)
self._ping_pylibmc(i)

def _ping_redis(self, i):
with self._redis.pipeline() as p:
Expand All @@ -48,6 +60,10 @@ def _ping_pg(self, i):
finally:
cur.close()

def _ping_pylibmc(self, i):
self._pylibmc.set("a", 1)
self._pylibmc.incr("a", 2)
self._pylibmc.decr("a", 1)

if __name__ == '__main__':
k = KitchenSink()
Expand Down

0 comments on commit 112c1b4

Please sign in to comment.