Skip to content

Commit

Permalink
Fix and improve tests
Browse files Browse the repository at this point in the history
I believe it would be nice to have
tests on CI not only for Python 3.7,
but for all supported Python versions.

These changes:

 - fix test_span_members test

 - fix compatibility with Python 3.5 and 3.4

 - add tests for various Python version on CI

 - allow running tests for any branches
  • Loading branch information
Jamim committed Aug 20, 2019
1 parent 20011c5 commit 402b309
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 34 deletions.
15 changes: 11 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ dist: xenial
language: python

python:
- '3.4'
- '3.5'
- '3.6'
- '3.7'
- 'pypy3.5'
- '3.8-dev'
- 'nightly'

matrix:
allow_failures:
- python: '3.8-dev'
- python: 'nightly'

install:
- pip install tox-travis

script:
- tox

branches:
only:
- master
6 changes: 4 additions & 2 deletions ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def tearDown(self):

def start_response(self, status, response_headers, exc_info=None):
# The span should have started already
self.span_context_manager.__enter__.assert_called()
self.span_context_manager.__enter__.assert_called_with()

self.status = status
self.response_headers = response_headers
Expand All @@ -108,7 +108,9 @@ def validate_response(self, response, error=None):
self.span_context_manager.__exit__.assert_not_called()
self.assertEqual(value, b"*")
except StopIteration:
self.span_context_manager.__exit__.assert_called()
self.span_context_manager.__exit__.assert_called_with(
None, None, None
)
break

self.assertEqual(self.status, "200 OK")
Expand Down
4 changes: 3 additions & 1 deletion opentelemetry-api/src/opentelemetry/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ async def main():
__all__ = ['Context']


Context: typing.Optional[BaseRuntimeContext]
Context = ( # pylint: disable=invalid-name
None
) # type: typing.Optional[BaseRuntimeContext]

try:
from .async_context import AsyncRuntimeContext
Expand Down
9 changes: 5 additions & 4 deletions opentelemetry-api/src/opentelemetry/context/async_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from contextvars import ContextVar
import typing
import typing # pylint: disable=unused-import

from . import base_context

Expand All @@ -23,9 +23,10 @@ class Slot(base_context.BaseRuntimeContext.Slot):
def __init__(self, name: str, default: 'object'):
# pylint: disable=super-init-not-called
self.name = name
self.contextvar: 'ContextVar[object]' = ContextVar(name)
self.default: typing.Callable[..., object]
self.default = base_context.wrap_callable(default)
self.contextvar = ContextVar(name) # type: ContextVar[object]
self.default = base_context.wrap_callable(
default
) # type: typing.Callable[..., object]

def clear(self) -> None:
self.contextvar.set(self.default())
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/context/base_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def set(self, value: 'object') -> None:
raise NotImplementedError

_lock = threading.Lock()
_slots: typing.Dict[str, 'BaseRuntimeContext.Slot'] = {}
_slots = {} # type: typing.Dict[str, 'BaseRuntimeContext.Slot']

@classmethod
def clear(cls) -> None:
Expand Down Expand Up @@ -112,7 +112,7 @@ def with_current_context(

def call_with_current_context(
*args: 'object',
**kwargs: 'object',
**kwargs: 'object'
) -> 'object':
try:
backup_context = self.snapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import threading
import typing
import typing # pylint: disable=unused-import

from . import base_context

Expand All @@ -25,15 +25,16 @@ class Slot(base_context.BaseRuntimeContext.Slot):
def __init__(self, name: str, default: 'object'):
# pylint: disable=super-init-not-called
self.name = name
self.default: typing.Callable[..., object]
self.default = base_context.wrap_callable(default)
self.default = base_context.wrap_callable(
default
) # type: typing.Callable[..., object]

def clear(self) -> None:
setattr(self._thread_local, self.name, self.default())

def get(self) -> 'object':
try:
got: object = getattr(self._thread_local, self.name)
got = getattr(self._thread_local, self.name) # type: object
return got
except AttributeError:
value = self.default()
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/src/opentelemetry/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def my_factory_for_t(api_type: typing.Type[T]) -> typing.Optional[T]:
# code.
# ImplementationFactory = Callable[[Type[_T]], Optional[_T]]

_DEFAULT_FACTORY: Optional[_UntrustedImplFactory[object]] = None
_DEFAULT_FACTORY = None # type: Optional[_UntrustedImplFactory[object]]


def _try_load_impl_from_modname(
Expand Down
27 changes: 15 additions & 12 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@
"""

from contextlib import contextmanager
import typing
from typing import Callable
from typing import Dict
from typing import Iterator
from typing import Optional
from typing import Type
from typing import Union

from opentelemetry import loader
from opentelemetry import types

# TODO: quarantine
ParentSpan = typing.Optional[typing.Union['Span', 'SpanContext']]
ParentSpan = Optional[Union['Span', 'SpanContext']]


class Span:
Expand Down Expand Up @@ -155,7 +160,7 @@ def get_default(cls) -> 'TraceOptions':
DEFAULT_TRACE_OPTIONS = TraceOptions.get_default()


class TraceState(typing.Dict[str, str]):
class TraceState(Dict[str, str]):
"""A list of key-value pairs representing vendor-specific trace info.
Keys and values are strings of up to 256 printable US-ASCII characters.
Expand Down Expand Up @@ -278,7 +283,7 @@ def get_current_span(self) -> 'Span':
def start_span(self,
name: str,
parent: ParentSpan = CURRENT_SPAN
) -> typing.Iterator['Span']:
) -> Iterator['Span']:
"""Context manager for span creation.
Create a new span. Start the span and set it as the current span in
Expand Down Expand Up @@ -362,7 +367,7 @@ def create_span(self,
return INVALID_SPAN

@contextmanager # type: ignore
def use_span(self, span: 'Span') -> typing.Iterator[None]:
def use_span(self, span: 'Span') -> Iterator[None]:
"""Context manager for controlling a span's lifetime.
Start the given span and set it as the current span in this tracer's
Expand All @@ -378,9 +383,10 @@ def use_span(self, span: 'Span') -> typing.Iterator[None]:
yield


_TRACER: typing.Optional[Tracer] = None
_TRACER_FACTORY: typing.Optional[
typing.Callable[[typing.Type[Tracer]], typing.Optional[Tracer]]] = None
FactoryType = Callable[[Type[Tracer]], Optional[Tracer]]

_TRACER = None # type: Optional[Tracer]
_TRACER_FACTORY = None # type: Optional[FactoryType]


def tracer() -> Tracer:
Expand All @@ -398,10 +404,7 @@ def tracer() -> Tracer:
return _TRACER


def set_preferred_tracer_implementation(
factory: typing.Callable[
[typing.Type[Tracer]], typing.Optional[Tracer]]
) -> None:
def set_preferred_tracer_implementation(factory: FactoryType) -> None:
"""Set the factory to be used to create the tracer.
See :mod:`opentelemetry.loader` for details.
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def test_start_span_explicit(self):
self.assertIsNotNone(child.end_time)

def test_span_members(self):
context = contextvars.ContextVar('test_span_members')
tracer = trace.Tracer(context)
tracer = trace.Tracer('test_span_members')

other_context1 = trace_api.SpanContext(
trace_id=trace.generate_trace_id(),
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[tox]
skipsdist = True
skip_missing_interpreters = True
envlist =
py{34,35,36,37}-test-{api,sdk}
py{34,35,36,37}-test-ext-wsgi
py3{4,5,6,7,8}-test-{api,sdk,ext-wsgi}
pypy35-test-{api,sdk,ext-wsgi}
lint
py37-mypy
docs
Expand All @@ -24,6 +25,7 @@ changedir =
test-ext-wsgi: ext/opentelemetry-ext-wsgi/tests

commands_pre =
pip install -U pip setuptools
test: pip install -e {toxinidir}/opentelemetry-api
test-sdk: pip install -e {toxinidir}/opentelemetry-sdk
ext: pip install -e {toxinidir}/opentelemetry-api
Expand Down

0 comments on commit 402b309

Please sign in to comment.