_reflection_client_test.py
Go to the documentation of this file.
1 # Copyright 2022 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Tests of grpc_reflection.v1alpha.reflection."""
15 
16 import unittest
17 
18 from google.protobuf.descriptor_pool import DescriptorPool
19 import grpc
20 from grpc_reflection.v1alpha import reflection
22  ProtoReflectionDescriptorDatabase
23 
24 from src.proto.grpc.testing import test_pb2
25 # Needed to load the EmptyWithExtensions message
26 from src.proto.grpc.testing.proto2 import empty2_extensions_pb2
27 from tests.unit import test_common
28 
29 _PROTO_PACKAGE_NAME = "grpc.testing"
30 _PROTO_FILE_NAME = "src/proto/grpc/testing/test.proto"
31 _EMPTY_PROTO_FILE_NAME = "src/proto/grpc/testing/empty.proto"
32 _INVALID_FILE_NAME = "i-do-not-exist.proto"
33 _EMPTY_PROTO_SYMBOL_NAME = "grpc.testing.Empty"
34 _INVALID_SYMBOL_NAME = "IDoNotExist"
35 _EMPTY_EXTENSIONS_SYMBOL_NAME = "grpc.testing.proto2.EmptyWithExtensions"
36 
37 
38 class ReflectionClientTest(unittest.TestCase):
39 
40  def setUp(self):
41  self._server = test_common.test_server()
42  self._SERVICE_NAMES = (
43  test_pb2.DESCRIPTOR.services_by_name["TestService"].full_name,
44  reflection.SERVICE_NAME,
45  )
46  reflection.enable_server_reflection(self._SERVICE_NAMES, self._server)
47  port = self._server.add_insecure_port("[::]:0")
48  self._server.start()
49 
50  self._channel = grpc.insecure_channel("localhost:%d" % port)
51 
54 
55  def tearDown(self):
56  self._server.stop(None)
57  self._channel.close()
58 
59  def testListServices(self):
60  services = self._reflection_db.get_services()
61  self.assertCountEqual(self._SERVICE_NAMES, services)
62 
64  self.assertEqual(reflection.SERVICE_NAME,
65  "grpc.reflection.v1alpha.ServerReflection")
66 
67  def testFindFile(self):
68  file_name = _PROTO_FILE_NAME
69  file_desc = self.desc_pool.FindFileByName(file_name)
70  self.assertEqual(file_name, file_desc.name)
71  self.assertEqual(_PROTO_PACKAGE_NAME, file_desc.package)
72  self.assertEqual("proto3", file_desc.syntax)
73  self.assertIn("TestService", file_desc.services_by_name)
74 
75  file_name = _EMPTY_PROTO_FILE_NAME
76  file_desc = self.desc_pool.FindFileByName(file_name)
77  self.assertEqual(file_name, file_desc.name)
78  self.assertEqual(_PROTO_PACKAGE_NAME, file_desc.package)
79  self.assertEqual("proto3", file_desc.syntax)
80  self.assertIn("Empty", file_desc.message_types_by_name)
81 
82  def testFindFileError(self):
83  with self.assertRaises(KeyError):
84  self.desc_pool.FindFileByName(_INVALID_FILE_NAME)
85 
86  def testFindMessage(self):
87  message_name = _EMPTY_PROTO_SYMBOL_NAME
88  message_desc = self.desc_pool.FindMessageTypeByName(message_name)
89  self.assertEqual(message_name, message_desc.full_name)
90  self.assertTrue(message_name.endswith(message_desc.name))
91 
93  with self.assertRaises(KeyError):
94  self.desc_pool.FindMessageTypeByName(_INVALID_SYMBOL_NAME)
95 
97  service_name = self._SERVICE_NAMES[0]
98  service_desc = self.desc_pool.FindServiceByName(service_name)
99  self.assertEqual(service_name, service_desc.full_name)
100  self.assertTrue(service_name.endswith(service_desc.name))
101  file_name = _PROTO_FILE_NAME
102  file_desc = self.desc_pool.FindFileByName(file_name)
103  self.assertIs(file_desc, service_desc.file)
104 
105  method_name = "EmptyCall"
106  self.assertIn(method_name, service_desc.methods_by_name)
107 
108  method_desc = service_desc.FindMethodByName(method_name)
109  self.assertIs(method_desc, service_desc.methods_by_name[method_name])
110  self.assertIs(service_desc, method_desc.containing_service)
111  self.assertEqual(method_name, method_desc.name)
112  self.assertTrue(method_desc.full_name.endswith(method_name))
113 
114  empty_message_desc = self.desc_pool.FindMessageTypeByName(
115  _EMPTY_PROTO_SYMBOL_NAME)
116  self.assertEqual(empty_message_desc, method_desc.input_type)
117  self.assertEqual(empty_message_desc, method_desc.output_type)
118 
120  with self.assertRaises(KeyError):
121  self.desc_pool.FindServiceByName(_INVALID_SYMBOL_NAME)
122 
124  service_name = self._SERVICE_NAMES[0]
125  service_desc = self.desc_pool.FindServiceByName(service_name)
126 
127  # FindMethodByName sometimes raises a KeyError, and sometimes returns None.
128  # See https://github.com/protocolbuffers/protobuf/issues/9592
129  with self.assertRaises(KeyError):
130  res = service_desc.FindMethodByName(_INVALID_SYMBOL_NAME)
131  if res is None:
132  raise KeyError()
133 
135  """
136  Extensions aren't implemented in Protobuf for Python.
137  For now, simply assert that indeed they don't work.
138  """
139  message_name = _EMPTY_EXTENSIONS_SYMBOL_NAME
140  message_desc = self.desc_pool.FindMessageTypeByName(message_name)
141  self.assertEqual(message_name, message_desc.full_name)
142  self.assertTrue(message_name.endswith(message_desc.name))
143  extension_field_descs = self.desc_pool.FindAllExtensions(message_desc)
144 
145  self.assertEqual(0, len(extension_field_descs))
146  with self.assertRaises(KeyError):
147  self.desc_pool.FindExtensionByName(message_name)
148 
149 
150 if __name__ == "__main__":
151  unittest.main(verbosity=2)
tests.reflection._reflection_client_test.ReflectionClientTest.testReflectionServiceName
def testReflectionServiceName(self)
Definition: _reflection_client_test.py:63
grpc.insecure_channel
def insecure_channel(target, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1962
grpc_reflection.v1alpha.proto_reflection_descriptor_database.ProtoReflectionDescriptorDatabase
Definition: proto_reflection_descriptor_database.py:36
tests.reflection._reflection_client_test.ReflectionClientTest.testFindFileError
def testFindFileError(self)
Definition: _reflection_client_test.py:82
tests.reflection._reflection_client_test.ReflectionClientTest
Definition: _reflection_client_test.py:38
tests.reflection._reflection_client_test.ReflectionClientTest.testFindMessage
def testFindMessage(self)
Definition: _reflection_client_test.py:86
tests.reflection._reflection_client_test.ReflectionClientTest._channel
_channel
Definition: _reflection_client_test.py:50
google::protobuf::python::cdescriptor_pool::FindFileByName
static PyObject * FindFileByName(PyObject *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:258
tests.reflection._reflection_client_test.ReflectionClientTest._SERVICE_NAMES
_SERVICE_NAMES
Definition: _reflection_client_test.py:42
google::protobuf::python::cdescriptor_pool::FindServiceByName
static PyObject * FindServiceByName(PyObject *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:360
grpc_reflection.v1alpha
Definition: src/python/grpcio_reflection/grpc_reflection/v1alpha/__init__.py:1
start
static uint64_t start
Definition: benchmark-pound.c:74
tests.reflection._reflection_client_test.ReflectionClientTest.testFindFile
def testFindFile(self)
Definition: _reflection_client_test.py:67
google::protobuf::python::cdescriptor_pool::FindExtensionByName
PyObject * FindExtensionByName(PyDescriptorPool *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:296
close
#define close
Definition: test-fs.c:48
google::protobuf.descriptor_pool
Definition: bloaty/third_party/protobuf/python/google/protobuf/descriptor_pool.py:1
tests.reflection._reflection_client_test.ReflectionClientTest._reflection_db
_reflection_db
Definition: _reflection_client_test.py:52
tests.reflection._reflection_client_test.ReflectionClientTest.desc_pool
desc_pool
Definition: _reflection_client_test.py:53
tests.reflection._reflection_client_test.ReflectionClientTest.testFindExtensionNotImplemented
def testFindExtensionNotImplemented(self)
Definition: _reflection_client_test.py:134
tests.reflection._reflection_client_test.ReflectionClientTest.testFindServiceFindMethod
def testFindServiceFindMethod(self)
Definition: _reflection_client_test.py:96
tests.unit
Definition: src/python/grpcio_tests/tests/unit/__init__.py:1
tests.reflection._reflection_client_test.ReflectionClientTest.testFindMessageError
def testFindMessageError(self)
Definition: _reflection_client_test.py:92
tests.reflection._reflection_client_test.ReflectionClientTest.tearDown
def tearDown(self)
Definition: _reflection_client_test.py:55
tests.reflection._reflection_client_test.ReflectionClientTest.setUp
def setUp(self)
Definition: _reflection_client_test.py:40
tests.reflection._reflection_client_test.ReflectionClientTest._server
_server
Definition: _reflection_client_test.py:41
google::protobuf.descriptor_pool.DescriptorPool
Definition: bloaty/third_party/protobuf/python/google/protobuf/descriptor_pool.py:118
google::protobuf::python::cdescriptor_pool::FindAllExtensions
static PyObject * FindAllExtensions(PyObject *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:453
tests.reflection._reflection_client_test.ReflectionClientTest.testListServices
def testListServices(self)
Definition: _reflection_client_test.py:59
stop
static const char stop[]
Definition: benchmark-async-pummel.c:35
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
tests.reflection._reflection_client_test.ReflectionClientTest.testFindMethodError
def testFindMethodError(self)
Definition: _reflection_client_test.py:123
grpc_reflection.v1alpha.proto_reflection_descriptor_database
Definition: proto_reflection_descriptor_database.py:1
tests.reflection._reflection_client_test.ReflectionClientTest.testFindServiceError
def testFindServiceError(self)
Definition: _reflection_client_test.py:119


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:27