From 219a731eed49141430890bd87495a29903b11fd4 Mon Sep 17 00:00:00 2001 From: Isac Souza Date: Wed, 10 Aug 2022 09:10:56 -0300 Subject: [PATCH] Fix kwargs when internal opts are present --- tests/test_kwargs.py | 8 ++++---- tox.ini | 2 +- zerorpc/core.py | 31 +++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/tests/test_kwargs.py b/tests/test_kwargs.py index e3d7009..4aa56b3 100644 --- a/tests/test_kwargs.py +++ b/tests/test_kwargs.py @@ -19,12 +19,12 @@ def test_client_connect(): client = zerorpc.Client() client.connect(endpoint) - args = 1, 2, 3 + args = [1, 2, 3] kwargs = {'a': 7, 'b': 8} res = client.echo(*args, **kwargs) assert len(res) == 2 assert res[0] == args - assert len(res[1]) == 3 + assert len(res[1]) == 2 assert 'a' in res[1] and 'b' in res[1] def test_client_quick_connect(): @@ -41,10 +41,10 @@ def test_client_quick_connect(): client = zerorpc.Client(endpoint) - args = 1, 2, 3 + args = [1, 2, 3] kwargs = {'a': 7, 'b': 8} res = client.echo(*args, **kwargs) assert len(res) == 2 assert res[0] == args - assert len(res[1]) == 3 + assert len(res[1]) == 2 assert 'a' in res[1] and 'b' in res[1] diff --git a/tox.ini b/tox.ini index f2727d8..2f1a989 100644 --- a/tox.ini +++ b/tox.ini @@ -7,7 +7,7 @@ deps = pytest commands = #flake8 zerorpc bin - pytest -v + pytest -v {posargs} passenv = ZPC_TEST_TIME_FACTOR [flake8] diff --git a/zerorpc/core.py b/zerorpc/core.py index 3bee937..f9f0b38 100644 --- a/zerorpc/core.py +++ b/zerorpc/core.py @@ -45,6 +45,23 @@ from logging import getLogger logger = getLogger(__name__) +class MessageOptions: + def __init__(self, timeout, slots, async_response): + self.timeout = timeout + self.slots = slots + self.async_response = async_response + + @staticmethod + def from_kwargs(kwargs_dict, default_timeout): + timeout = kwargs_dict.pop('timeout_', default_timeout) + slots = kwargs_dict.pop('slots_', 100) + # In python 3.7, "async" is a reserved keyword, clients should now use + # "async_": support both for the time being + async_ = kwargs_dict.pop('async_', False) + + return MessageOptions(timeout, slots, async_) + + class ServerBase(object): def __init__(self, channel, methods=None, name=None, context=None, @@ -255,25 +272,23 @@ class ClientBase(object): if isinstance(method, bytes): method = method.decode('utf-8') - timeout = kwargs.pop('timeout_', self._timeout) + opts = MessageOptions.from_kwargs(kwargs, self._timeout) + channel = self._multiplexer.channel() hbchan = HeartBeatOnChannel(channel, freq=self._heartbeat_freq, passive=self._passive_heartbeat) - bufchan = BufferedChannel(hbchan, inqueue_size=kwargs.pop('slots_', 100)) + bufchan = BufferedChannel(hbchan, inqueue_size=opts.slots) xheader = self._context.hook_get_task_context() request_event = bufchan.new_event(method, args, kwargs, xheader) self._context.hook_client_before_request(request_event) bufchan.emit_event(request_event) - # In python 3.7, "async" is a reserved keyword, clients should now use - # "async_": support both for the time being - async_ = kwargs.pop('async_', False) - if not async_: - return self._process_response(request_event, bufchan, timeout) + if not opts.async_response: + return self._process_response(request_event, bufchan, opts.timeout) return eventlet.spawn(self._process_response, request_event, bufchan, - timeout) + opts.timeout) def __getattr__(self, method): return lambda *args, **kwargs: self(method, *args, **kwargs) -- 2.25.1