Supplement TNonblockingServer.py testcase
Client: py
Patch: Zezeng Wang
This closes #2150
diff --git a/lib/py/CMakeLists.txt b/lib/py/CMakeLists.txt
index d56c709..9c4368b 100644
--- a/lib/py/CMakeLists.txt
+++ b/lib/py/CMakeLists.txt
@@ -20,6 +20,7 @@
include_directories(${PYTHON_INCLUDE_DIRS})
add_custom_target(python_build ALL
+ COMMAND ${THRIFT_COMPILER} --gen py test/test_thrift_file/TestServer.thrift
COMMAND ${PYTHON_EXECUTABLE} setup.py build
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Building Python library"
@@ -32,4 +33,5 @@
add_test(PythonThriftTBinaryProtocol ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/thrift_TBinaryProtocol.py)
add_test(PythonThriftTZlibTransport ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/thrift_TZlibTransport.py)
add_test(PythonThriftProtocol ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/thrift_TCompactProtocol.py)
+ add_test(PythonThriftTNonblockingServer ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/thrift_TNonblockingServer.py)
endif()
diff --git a/lib/py/Makefile.am b/lib/py/Makefile.am
index 1d450ee..c6c5ff3 100644
--- a/lib/py/Makefile.am
+++ b/lib/py/Makefile.am
@@ -29,6 +29,7 @@
$(PYTHON3) test/thrift_TBinaryProtocol.py
$(PYTHON3) test/thrift_TZlibTransport.py
$(PYTHON3) test/thrift_TCompactProtocol.py
+ $(PYTHON3) test/thrift_TNonblockingServer.py
else
py3-build:
py3-test:
@@ -36,6 +37,7 @@
all-local: py3-build
$(PYTHON) setup.py build
+ ${THRIFT} --gen py test/test_thrift_file/TestServer.thrift
# We're ignoring prefix here because site-packages seems to be
# the equivalent of /usr/local/lib in Python land.
@@ -51,10 +53,12 @@
$(PYTHON) test/thrift_TBinaryProtocol.py
$(PYTHON) test/thrift_TZlibTransport.py
$(PYTHON) test/thrift_TCompactProtocol.py
+ $(PYTHON) test/thrift_TNonblockingServer.py
clean-local:
$(RM) -r build
+ $(RM) -r gen-py
find . -type f \( -iname "*.pyc" \) | xargs rm -f
find . -type d \( -iname "__pycache__" -or -iname "_trial_temp" \) | xargs rm -rf
diff --git a/lib/py/test/test_thrift_file/TestServer.thrift b/lib/py/test/test_thrift_file/TestServer.thrift
new file mode 100644
index 0000000..0de8856
--- /dev/null
+++ b/lib/py/test/test_thrift_file/TestServer.thrift
@@ -0,0 +1,23 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+
+service TestServer{
+ string add_and_get_msg(1:string msg)
+}
diff --git a/lib/py/test/thrift_TNonblockingServer.py b/lib/py/test/thrift_TNonblockingServer.py
new file mode 100644
index 0000000..7220879
--- /dev/null
+++ b/lib/py/test/thrift_TNonblockingServer.py
@@ -0,0 +1,101 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import os
+import sys
+import threading
+import unittest
+import time
+
+gen_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "gen-py")
+sys.path.append(gen_path)
+import _import_local_thrift # noqa
+from TestServer import TestServer
+from thrift.transport import TSocket, TTransport
+from thrift.protocol import TBinaryProtocol
+from thrift.server import TNonblockingServer
+
+
+class Handler:
+
+ def add_and_get_msg(self, msg):
+ return msg
+
+
+class Server:
+
+ def __init__(self):
+ handler = Handler()
+ processor = TestServer.Processor(handler)
+ transport = TSocket.TServerSocket("127.0.0.1", 30030)
+ self.server = TNonblockingServer.TNonblockingServer(processor, transport)
+
+ def start_server(self):
+ print("-------start server ------\n")
+ self.server.serve()
+ print("------stop server -----\n")
+
+ def close_server(self):
+ self.server.stop()
+ self.server.close()
+
+
+class Client:
+
+ def start_client(self):
+ transport = TSocket.TSocket("127.0.0.1", 30030)
+ trans = TTransport.TFramedTransport(transport)
+ protocol = TBinaryProtocol.TBinaryProtocol(trans)
+ client = TestServer.Client(protocol)
+ trans.open()
+ self.msg = client.add_and_get_msg("hello thrift")
+
+ def get_message(self):
+ try:
+ msg = self.msg
+ return msg
+ except AttributeError as e:
+ raise e
+ print("self.msg not exit\n")
+
+
+class TestNonblockingServer(unittest.TestCase):
+
+ def test_normalconnection(self):
+ serve = Server()
+ client = Client()
+
+ serve_thread = threading.Thread(target=serve.start_server)
+ client_thread = threading.Thread(target=client.start_client)
+ serve_thread.start()
+ time.sleep(10)
+ client_thread.start()
+ client_thread.join(0.5)
+ try:
+ msg = client.get_message()
+ self.assertEqual("hello thrift", msg)
+ except AssertionError as e:
+ raise e
+ print("assert failure")
+ finally:
+ serve.close_server()
+
+
+if __name__ == '__main__':
+ unittest.main()