mirror of https://github.com/grpc/grpc.git
commit
d78563eb43
66 changed files with 358 additions and 362 deletions
@ -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 |
@ -1,92 +0,0 @@ |
||||
# 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 os |
||||
import sys |
||||
import subprocess |
||||
|
||||
import asyncio |
||||
import unittest |
||||
import socket |
||||
|
||||
from grpc.experimental import aio |
||||
from tests_aio.unit import sync_server |
||||
from tests.unit.framework.common import get_socket |
||||
|
||||
|
||||
def _get_free_loopback_tcp_port(): |
||||
host, port, sock = get_socket(listen=False) |
||||
return sock, "{}:{}".format(host, port) |
||||
|
||||
|
||||
class _Server: |
||||
"""_Server is an wrapper for a sync-server subprocess. |
||||
|
||||
The synchronous server is executed in another process which initializes |
||||
implicitly the grpc using the synchronous configuration. Both worlds |
||||
can not coexist within the same process. |
||||
""" |
||||
|
||||
def __init__(self, host_and_port): # pylint: disable=W0621 |
||||
self._host_and_port = host_and_port |
||||
self._handle = None |
||||
|
||||
def start(self): |
||||
assert self._handle is None |
||||
|
||||
try: |
||||
from google3.pyglib import resources |
||||
executable = resources.GetResourceFilename( |
||||
"google3/third_party/py/grpc/sync_server") |
||||
args = [executable, '--host_and_port', self._host_and_port] |
||||
except ImportError: |
||||
executable = sys.executable |
||||
directory, _ = os.path.split(os.path.abspath(__file__)) |
||||
filename = directory + '/sync_server.py' |
||||
args = [ |
||||
executable, filename, '--host_and_port', self._host_and_port |
||||
] |
||||
|
||||
self._handle = subprocess.Popen(args) |
||||
|
||||
def terminate(self): |
||||
if not self._handle: |
||||
return |
||||
|
||||
self._handle.terminate() |
||||
self._handle.wait() |
||||
self._handle = None |
||||
|
||||
|
||||
class AioTestBase(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self._socket, self._target = _get_free_loopback_tcp_port() |
||||
self._server = _Server(self._target) |
||||
self._server.start() |
||||
self._loop = asyncio.new_event_loop() |
||||
asyncio.set_event_loop(self._loop) |
||||
aio.init_grpc_aio() |
||||
|
||||
def tearDown(self): |
||||
self._server.terminate() |
||||
self._socket.close() |
||||
|
||||
@property |
||||
def loop(self): |
||||
return self._loop |
||||
|
||||
@property |
||||
def server_target(self): |
||||
return self._target |
Loading…
Reference in new issue