Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:uber/tchannel-python into bryce_m…
Browse files Browse the repository at this point in the history
…ock_server
  • Loading branch information
blampe committed Aug 24, 2015
2 parents 775c277 + 2054865 commit e9f6f98
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -14,6 +14,7 @@ Changes by Version
before. Note the breaking change to the top-level TChannel on the next line.
- Fixed a crash that would occur when forking with an unitialized ``TChannel``
instance.
- Add ``hooks`` property in the ``tchannel.TChannel`` class.
- **BREAKING** - ``tchannel.TChannel.register`` no longer has the same
functionality as ``tchannel.tornado.TChannel.register``, instead it exposes
the new server API. See the upgrade guide for details.
Expand Down
12 changes: 10 additions & 2 deletions tchannel/request.py
Expand Up @@ -54,13 +54,21 @@ class Request(object):
__slots__ = (
'body',
'headers',
'transport'
'transport',
'endpoint',
)

def __init__(self, body=None, headers=None, transport=None):
def __init__(
self,
body=None,
headers=None,
transport=None,
endpoint=None,
):
self.body = body
self.headers = headers
self.transport = transport
self.endpoint = endpoint


class TransportHeaders(object):
Expand Down
4 changes: 3 additions & 1 deletion tchannel/tchannel.py
Expand Up @@ -105,7 +105,9 @@ def __init__(self, name, hostport=None, process_name=None,
self.json = schemes.JsonArgScheme(self)
self.thrift = schemes.ThriftArgScheme(self)

self.hooks = self._dep_tchannel.hooks
@property
def hooks(self):
return self._dep_tchannel.hooks

@gen.coroutine
def call(
Expand Down
13 changes: 5 additions & 8 deletions tchannel/tornado/dispatch.py
Expand Up @@ -65,11 +65,8 @@ def my_method(request, response):
FALLBACK = object()

def __init__(self, _handler_returns_response=False):
self.handlers = {
self.FALLBACK: Handler(
self.not_found, RawSerializer(), RawSerializer()
)
}
self.handlers = {}
self.register(self.FALLBACK, self.not_found)
self._handler_returns_response = _handler_returns_response

_HANDLER_NAMES = {
Expand Down Expand Up @@ -181,6 +178,7 @@ def handle_call(self, request, connection):
body=b,
headers=he,
transport=t,
endpoint=request.endpoint,
)

# Not safe to have coroutine yields statement within
Expand Down Expand Up @@ -271,11 +269,10 @@ def check_health(request, response):
self.handlers[rule] = Handler(handler, req_serializer, resp_serializer)

@staticmethod
def not_found(request, response):
def not_found(request, response=None):
"""Default behavior for requests to unrecognized endpoints."""
raise BadRequestError(
description="Endpoint '%s' for service '%s' is not defined" % (
description="Endpoint '%s' is not defined" % (
request.endpoint,
request.service,
),
)
33 changes: 32 additions & 1 deletion tests/test_tchannel.py
Expand Up @@ -25,11 +25,13 @@

import subprocess
import textwrap
from mock import MagicMock, patch

import psutil
import pytest

from tchannel import TChannel, Request, Response, schemes
from tchannel import TChannel, Request, Response, schemes, errors
from tchannel.event import EventHook
from tchannel.response import TransportHeaders

# TODO - need integration tests for timeout and retries, use testing.vcr
Expand Down Expand Up @@ -227,3 +229,32 @@ def endpoint(request):
assert isinstance(resp, Response)
assert resp.headers == 'resp headers'
assert resp.body == 'resp body'


@pytest.mark.gen_test
@pytest.mark.call
def test_endpoint_not_found():
server = TChannel(name='server')
server.listen()

tchannel = TChannel(name='client')

with pytest.raises(errors.BadRequestError):
yield tchannel.raw(
service='server',
hostport=server.hostport,
endpoint='foo',
)


def test_event_hook_register():
server = TChannel(name='server')
mock_hook = MagicMock(spec=EventHook)
with (
patch(
'tchannel.event.EventRegistrar.register',
autospec=True,
)
) as mock_register:
server.hooks.register(mock_hook)
mock_register.called

0 comments on commit e9f6f98

Please sign in to comment.