From 1ebaace7f1cf18965c42ddd5ee3e915a47a3f9b4 Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Wed, 23 Oct 2019 19:02:26 -0700 Subject: [PATCH] Bring back the file that I accidentally rm -f --- .../grpcio_tests/tests_aio/unit/_test_base.py | 29 +++++++++++++++++++ .../grpcio_tests/tests_aio/unit/init_test.py | 5 ++-- .../tests_aio/unit/server_test.py | 6 ++-- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/python/grpcio_tests/tests_aio/unit/_test_base.py diff --git a/src/python/grpcio_tests/tests_aio/unit/_test_base.py b/src/python/grpcio_tests/tests_aio/unit/_test_base.py new file mode 100644 index 00000000000..09b0ef7abf8 --- /dev/null +++ b/src/python/grpcio_tests/tests_aio/unit/_test_base.py @@ -0,0 +1,29 @@ +# Copyright 2019 The gRPC Authors +# +# Licensed 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 asyncio +import unittest +from grpc.experimental import aio + +class AioTestBase(unittest.TestCase): + + def setUp(self): + self._loop = asyncio.new_event_loop() + asyncio.set_event_loop(self._loop) + aio.init_grpc_aio() + + @property + def loop(self): + return self._loop + diff --git a/src/python/grpcio_tests/tests_aio/unit/init_test.py b/src/python/grpcio_tests/tests_aio/unit/init_test.py index d575cfeb780..b2a07633028 100644 --- a/src/python/grpcio_tests/tests_aio/unit/init_test.py +++ b/src/python/grpcio_tests/tests_aio/unit/init_test.py @@ -19,6 +19,7 @@ import unittest import grpc from grpc.experimental import aio from tests_aio.unit._test_server import start_test_server +from tests_aio.unit._test_base import AioTestBase class TestAioRpcError(unittest.TestCase): @@ -60,7 +61,7 @@ class TestAioRpcError(unittest.TestCase): second_aio_rpc_error.__class__) -class TestInsecureChannel(unittest.TestCase): +class TestInsecureChannel(AioTestBase): def test_insecure_channel(self): @@ -70,7 +71,7 @@ class TestInsecureChannel(unittest.TestCase): channel = aio.insecure_channel(server_target) self.assertIsInstance(channel, aio.Channel) - asyncio.get_event_loop().run_until_complete(coro()) + self.loop.run_until_complete(coro()) if __name__ == '__main__': diff --git a/src/python/grpcio_tests/tests_aio/unit/server_test.py b/src/python/grpcio_tests/tests_aio/unit/server_test.py index 2d543893176..ba1c53472e3 100644 --- a/src/python/grpcio_tests/tests_aio/unit/server_test.py +++ b/src/python/grpcio_tests/tests_aio/unit/server_test.py @@ -20,6 +20,7 @@ import grpc from grpc.experimental import aio from src.proto.grpc.testing import messages_pb2 from src.proto.grpc.testing import benchmark_service_pb2_grpc +from tests_aio.unit._test_base import AioTestBase _TEST_METHOD_PATH = '' @@ -37,10 +38,9 @@ class GenericHandler(grpc.GenericRpcHandler): return grpc.unary_unary_rpc_method_handler(unary_unary) -class TestServer(unittest.TestCase): +class TestServer(AioTestBase): def test_unary_unary(self): - loop = asyncio.get_event_loop() async def test_unary_unary_body(): server = aio.server() @@ -53,7 +53,7 @@ class TestServer(unittest.TestCase): response = await unary_call(_REQUEST) self.assertEqual(response, _RESPONSE) - loop.run_until_complete(test_unary_unary_body()) + self.loop.run_until_complete(test_unary_unary_body()) if __name__ == '__main__':