Skip to content

Commit

Permalink
closes mbr#54
Browse files Browse the repository at this point in the history
  • Loading branch information
lnoor committed Apr 29, 2018
1 parent 324a6b5 commit 39b709d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 14 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import pytest
import _compat
from six.moves.mock import Mock
import six

from tinyrpc.exc import RPCError
from tinyrpc.client import RPCClient, RPCProxy
from tinyrpc.protocols import RPCProtocol, RPCResponse, RPCErrorResponse
from tinyrpc.protocols import RPCProtocol, RPCResponse, RPCErrorResponse, RPCRequest
from tinyrpc.transports import ClientTransport

@pytest.fixture(params=['test_method1', 'method2', 'CamelCasedMethod'])
Expand Down Expand Up @@ -95,6 +96,7 @@ def test_client_uses_correct_transport(client, mock_protocol, method_name,
assert mock_transport.send_message.called



def test_client_passes_correct_reply(client, mock_protocol, method_name,
method_args, method_kwargs,
one_way_setting, mock_transport):
Expand All @@ -117,3 +119,14 @@ def test_client_raises_error_replies(client, mock_protocol, method_name,
if not one_way_setting:
with pytest.raises(RPCError):
client.call(method_name, method_args, method_kwargs, one_way_setting)


def test_client_send_binary_message(client, mock_protocol, method_name,
method_args, method_kwargs,
one_way_setting, mock_transport):
req = Mock(RPCRequest)
req.serialize.return_value = u'unicode not acceptable'
mock_protocol.create_request.return_value = req
client.call(method_name, method_args, method_kwargs, one_way_setting)
assert mock_transport.send_message.called
assert isinstance(mock_transport.send_message.call_args[0][0], six.binary_type)
12 changes: 6 additions & 6 deletions tinyrpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""Defines the elements of a RPC call.
RPCCall is used with :py:meth:`~tinyrpc.client.RPCClient.call_all`
to provide the list of requests to be processed. Each request contains the
to provide the list of requests to be processed. Each request contains the
elements defined in this tuple.
"""

Expand Down Expand Up @@ -38,7 +38,7 @@ def _send_and_handle_reply(self, req, one_way, transport=None, no_exception=Fals
tport = self.transport if transport is None else transport

# sends ...
reply = tport.send_message(req.serialize())
reply = tport.send_message(req.serialize().encode('utf-8'))

if one_way:
# ... and be done
Expand Down Expand Up @@ -75,20 +75,20 @@ def call(self, method, args, kwargs, one_way=False):

def call_all(self, requests):
"""Calls the methods in the request in parallel.
When the :py:mod:`gevent` module is already loaded it is assumed to be
correctly initialized, including monkey patching if necessary.
In that case the RPC calls defined by ``requests`` is performed in
parallel otherwise the methods are called sequentially.
:param requests: A listof either RPCCall or RPCCallTo elements.
When RPCCallTo is used each element defines a transport.
Otherwise the default transport set when RPCClient is
created is used.
:return: A list with replies matching the order of the requests.
"""
threads = []

if 'gevent' in sys.modules:
# assume that gevent is available and functional, make calls in parallel
import gevent
Expand All @@ -105,7 +105,7 @@ def call_all(self, requests):
tr = r.transport.transport if len(r) == 4 else None
threads.append(self._send_and_handle_reply(req, False, tr, True))
return threads

def get_proxy(self, prefix='', one_way=False):
"""Convenience method for creating a proxy.
Expand Down

0 comments on commit 39b709d

Please sign in to comment.