Beispiel #1
0
 def __call__(self, context, callback):
     if test_var.get(
     ) != _EXPECTED_VALUE and not test_common.running_under_gevent():
         # contextvars do not work under gevent, but the rest of this
         # test is still valuable as a test of concurrent runs of the
         # metadata credentials code path.
         raise AssertionError("{} != {}".format(test_var.get(),
                                                _EXPECTED_VALUE))
     callback((), None)
class LocalCredentialsTest(unittest.TestCase):
    def _create_server(self):
        server = grpc.server(ThreadPoolExecutor())
        server.add_generic_rpc_handlers((_GenericHandler(), ))
        return server

    @unittest.skipIf(os.name == 'nt',
                     'TODO(https://github.com/grpc/grpc/issues/20078)')
    def test_local_tcp(self):
        server_addr = 'localhost:{}'
        channel_creds = grpc.local_channel_credentials(
            grpc.LocalConnectionType.LOCAL_TCP)
        server_creds = grpc.local_server_credentials(
            grpc.LocalConnectionType.LOCAL_TCP)

        server = self._create_server()
        port = server.add_secure_port(server_addr.format(0), server_creds)
        server.start()
        with grpc.secure_channel(server_addr.format(port),
                                 channel_creds) as channel:
            self.assertEqual(
                b'abc',
                channel.unary_unary('/test/method')(b'abc',
                                                    wait_for_ready=True))
        server.stop(None)

    @unittest.skipIf(os.name == 'nt',
                     'Unix Domain Socket is not supported on Windows')
    @unittest.skipIf(test_common.running_under_gevent(),
                     'UDS not supported under gevent.')
    def test_uds(self):
        server_addr = 'unix:/tmp/grpc_fullstack_test'
        channel_creds = grpc.local_channel_credentials(
            grpc.LocalConnectionType.UDS)
        server_creds = grpc.local_server_credentials(
            grpc.LocalConnectionType.UDS)

        server = self._create_server()
        server.add_secure_port(server_addr, server_creds)
        server.start()
        with grpc.secure_channel(server_addr, channel_creds) as channel:
            self.assertEqual(
                b'abc',
                channel.unary_unary('/test/method')(b'abc',
                                                    wait_for_ready=True))
        server.stop(None)
Beispiel #3
0
from tests.unit import test_common
from tests.unit._rpc_test_helpers import BaseRPCTest
from tests.unit._rpc_test_helpers import Callback
from tests.unit._rpc_test_helpers import TIMEOUT_SHORT
from tests.unit._rpc_test_helpers import \
    stream_stream_non_blocking_multi_callable
from tests.unit._rpc_test_helpers import \
    unary_stream_non_blocking_multi_callable
from tests.unit._rpc_test_helpers import stream_stream_multi_callable
from tests.unit._rpc_test_helpers import stream_unary_multi_callable
from tests.unit._rpc_test_helpers import unary_stream_multi_callable
from tests.unit._rpc_test_helpers import unary_unary_multi_callable
from tests.unit.framework.common import test_constants


@unittest.skipIf(test_common.running_under_gevent(),
                 "Causes deadlock under gevent.")
class RPCPart2Test(BaseRPCTest, unittest.TestCase):
    def testDefaultThreadPoolIsUsed(self):
        self._consume_one_stream_response_unary_request(
            unary_stream_multi_callable(self._channel))
        self.assertFalse(self._thread_pool.was_used())

    def testExperimentalThreadPoolIsUsed(self):
        self._consume_one_stream_response_unary_request(
            unary_stream_non_blocking_multi_callable(self._channel))
        self.assertTrue(self._thread_pool.was_used())

    def testUnrecognizedMethod(self):
        request = b'abc'
if sys.executable is not None:
    _CLIENT_PATH = os.path.abspath(os.path.realpath(_signal_client.__file__))
else:
    # NOTE(rbellevi): For compatibility with internal testing.
    if len(sys.argv) != 2:
        raise RuntimeError("Must supply path to executable client.")
    client_name = sys.argv[1].split("/")[-1]
    del sys.argv[1]  # For compatibility with test runner.
    _CLIENT_PATH = os.path.realpath(
        os.path.join(os.path.dirname(os.path.abspath(__file__)), client_name))

_HOST = 'localhost'

# The gevent test harness cannot run the monkeypatch code for the child process,
# so we need to instrument it manually.
_GEVENT_ARG = ("--gevent",) if test_common.running_under_gevent() else ()


class _GenericHandler(grpc.GenericRpcHandler):

    def __init__(self):
        self._connected_clients_lock = threading.RLock()
        self._connected_clients_event = threading.Event()
        self._connected_clients = 0

        self._unary_unary_handler = grpc.unary_unary_rpc_method_handler(
            self._handle_unary_unary)
        self._unary_stream_handler = grpc.unary_stream_rpc_method_handler(
            self._handle_unary_stream)

    def _on_client_connect(self):