From 8b816e48ede4b6d8635246df0b55bfbaf07a93b7 Mon Sep 17 00:00:00 2001 From: zhengjianbo Date: Wed, 10 Aug 2022 18:55:03 +0800 Subject: [PATCH 1/4] fix the message_type of client Change-Id: Ia36cc82de0489f4d5d3f52c6606cc68b3134c32d Author: zhengjianbo Date: Wed Aug 10 18:55:03 2022 +0800 --- thriftpy2/contrib/aio/client.py | 4 +++- thriftpy2/thrift.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/thriftpy2/contrib/aio/client.py b/thriftpy2/contrib/aio/client.py index 525e8d3c..2e2136dd 100644 --- a/thriftpy2/contrib/aio/client.py +++ b/thriftpy2/contrib/aio/client.py @@ -40,7 +40,9 @@ async def _req(self, _api, *args, **kwargs): return await self._recv(_api) async def _send(self, _api, **kwargs): - self._oprot.write_message_begin(_api, TMessageType.CALL, self._seqid) + oneway = getattr(getattr(self._service, _api + "_result"), "oneway") + msg_type = TMessageType.ONEWAY if oneway else TMessageType.CALL + self._oprot.write_message_begin(_api, msg_type, self._seqid) args = getattr(self._service, _api + "_args")() for k, v in kwargs.items(): setattr(args, k, v) diff --git a/thriftpy2/thrift.py b/thriftpy2/thrift.py index 0c4322b6..30cc5177 100644 --- a/thriftpy2/thrift.py +++ b/thriftpy2/thrift.py @@ -219,7 +219,9 @@ def _req(self, _api, *args, **kwargs): return self._recv(_api) def _send(self, _api, **kwargs): - self._oprot.write_message_begin(_api, TMessageType.CALL, self._seqid) + oneway = getattr(getattr(self._service, _api + "_result"), "oneway") + msg_type = TMessageType.ONEWAY if oneway else TMessageType.CALL + self._oprot.write_message_begin(_api, msg_type, self._seqid) args = getattr(self._service, _api + "_args")() for k, v in kwargs.items(): setattr(args, k, v) From 2e405fb09b390939743be0dbcd0fd899d56c524e Mon Sep 17 00:00:00 2001 From: 1C4nfaN Date: Thu, 12 Oct 2023 22:46:22 +0800 Subject: [PATCH 2/4] chore: add unittest for oneway req --- tests/oneway.thrift | 4 ++++ tests/test_oneway.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/oneway.thrift create mode 100644 tests/test_oneway.py diff --git a/tests/oneway.thrift b/tests/oneway.thrift new file mode 100644 index 00000000..9559431b --- /dev/null +++ b/tests/oneway.thrift @@ -0,0 +1,4 @@ +service echo +{ + oneway void Test(1: string req) +} diff --git a/tests/test_oneway.py b/tests/test_oneway.py new file mode 100644 index 00000000..bcee32ec --- /dev/null +++ b/tests/test_oneway.py @@ -0,0 +1,32 @@ +import multiprocessing +import thriftpy2 +import time +from thriftpy2.rpc import make_client, make_server + + +class Dispatcher(object): + def Test(self, req): + print("Get req msg: %s" % req) + + assert req == "Hello!" + + +oneway_thrift = thriftpy2.load("oneway.thrift", module_name="oneway_thrift") +multiprocessing.set_start_method("fork") + + +class TestOneway(object): + def setup_class(self): + server = make_server(oneway_thrift.echo, Dispatcher(), '127.0.0.1', 6000) + self.p = multiprocessing.Process(target=server.serve) + self.p.start() + time.sleep(1) # Wait a second for server to start. + + def teardown_class(self): + self.p.terminate() + + def test_echo(self): + req = "Hello!" + client = make_client(oneway_thrift.echo, '127.0.0.1', 6000) + + assert client.Test(req) == None From 3d039254353c0f459fa61a3142863f9d62ada359 Mon Sep 17 00:00:00 2001 From: 1Canf4N <40199440+1C4nfaN@users.noreply.github.com> Date: Fri, 13 Oct 2023 10:17:11 +0800 Subject: [PATCH 3/4] Update tests/oneway.thrift Co-authored-by: AN Long --- tests/oneway.thrift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/oneway.thrift b/tests/oneway.thrift index 9559431b..835f53c2 100644 --- a/tests/oneway.thrift +++ b/tests/oneway.thrift @@ -1,4 +1,3 @@ -service echo -{ +service echo { oneway void Test(1: string req) } From 5757c9d822997fb81c1ed1b4cd090957610d07ab Mon Sep 17 00:00:00 2001 From: 1C4nfaN Date: Fri, 13 Oct 2023 10:53:09 +0800 Subject: [PATCH 4/4] fix:avoid global operate --- tests/test_oneway.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_oneway.py b/tests/test_oneway.py index bcee32ec..812cfdae 100644 --- a/tests/test_oneway.py +++ b/tests/test_oneway.py @@ -11,14 +11,14 @@ def Test(self, req): assert req == "Hello!" -oneway_thrift = thriftpy2.load("oneway.thrift", module_name="oneway_thrift") -multiprocessing.set_start_method("fork") +class TestOneway(object): + oneway_thrift = thriftpy2.load("oneway.thrift") -class TestOneway(object): def setup_class(self): - server = make_server(oneway_thrift.echo, Dispatcher(), '127.0.0.1', 6000) - self.p = multiprocessing.Process(target=server.serve) + ctx = multiprocessing.get_context("fork") + server = make_server(self.oneway_thrift.echo, Dispatcher(), '127.0.0.1', 6000) + self.p = ctx.Process(target=server.serve) self.p.start() time.sleep(1) # Wait a second for server to start. @@ -27,6 +27,6 @@ def teardown_class(self): def test_echo(self): req = "Hello!" - client = make_client(oneway_thrift.echo, '127.0.0.1', 6000) + client = make_client(self.oneway_thrift.echo, '127.0.0.1', 6000) assert client.Test(req) == None