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

Commit

Permalink
make client_for compatible with new TChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
blampe committed Aug 26, 2015
1 parent 99804e7 commit 4e5b2fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -7,6 +7,7 @@ Changes by Version

- Fixed a bug where the 'not found' handler would incorrectly return
serialization mismatch errors..
- Made ``client_for`` compatible with ``tchannel.TChannel``.


0.16.0 (2015-08-25)
Expand Down
38 changes: 28 additions & 10 deletions tchannel/thrift/client.py
Expand Up @@ -26,6 +26,7 @@
from thrift import Thrift
from tornado import gen

from tchannel import schemes
from tchannel.errors import OneWayNotSupportedError

from ..serializer.thrift import ThriftSerializer
Expand Down Expand Up @@ -166,17 +167,34 @@ def send(self, *args, **kwargs):

body = serializer.serialize_body(call_args)
header = serializer.serialize_header({})
response = yield self.tchannel.request(
hostport=self.hostport, service=self.service
).send(
arg1=endpoint,
arg2=header,
arg3=body, # body
headers=self.protocol_headers,
traceflag=self.trace
)
body = yield response.get_body()

# Glue for old API.
if hasattr(self.tchannel, 'request'):
response = yield self.tchannel.request(
hostport=self.hostport, service=self.service
).send(
arg1=endpoint,
arg2=header,
arg3=body, # body
headers=self.protocol_headers,
traceflag=self.trace
)
body = yield response.get_body()
else:
response = yield self.tchannel.call(
scheme=schemes.THRIFT,
service=self.service,
arg1=endpoint,
arg2=header,
arg3=body,
hostport=self.hostport,
#headers=self.protocol_headers,
#traceflag=self.trace,
)
body = response.body

call_result = serializer.deserialize_body(body)

if not result_spec:
# void return type and no exceptions allowed
raise gen.Return(None)
Expand Down
25 changes: 25 additions & 0 deletions tests/schemes/test_thrift.py
Expand Up @@ -35,8 +35,10 @@
from tchannel.errors import OneWayNotSupportedError
from tchannel.errors import UnexpectedError
from tchannel.errors import ValueExpectedError
from tchannel.thrift import client_for
from tchannel.testing.data.generated.ThriftTest import SecondService
from tchannel.testing.data.generated.ThriftTest import ThriftTest
from tchannel.tornado import TChannel as DeprecatedTChannel


# TODO - where possible, in req/res style test, create parameterized tests,
Expand Down Expand Up @@ -1183,3 +1185,26 @@ def test_headers_should_be_a_map_of_strings(headers):
request=mock.MagicMock(),
headers=headers,
)


@pytest.mark.gen_test
@pytest.mark.call
@pytest.mark.parametrize('ClientTChannel', [TChannel, DeprecatedTChannel])
def test_client_for(ClientTChannel):
server = TChannel(name='server')

@server.thrift.register(ThriftTest)
def testString(request):
return request.body.thing.encode('rot13')

server.listen()

tchannel = ClientTChannel(name='client')

client = client_for('server', ThriftTest)(
tchannel=tchannel,
hostport=server.hostport,
)

resp = yield client.testString(thing='foo')
assert resp == 'sbb'

0 comments on commit 4e5b2fc

Please sign in to comment.