• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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"""Test of gRPC Python's application-layer API."""
15
16import logging
17import threading
18import unittest
19
20import grpc
21
22from tests.unit import _from_grpc_import_star
23from tests.unit import test_common
24
25
26class AllTest(unittest.TestCase):
27    def testAll(self):
28        expected_grpc_code_elements = (
29            "FutureTimeoutError",
30            "FutureCancelledError",
31            "Future",
32            "ChannelConnectivity",
33            "Compression",
34            "StatusCode",
35            "Status",
36            "RpcError",
37            "RpcContext",
38            "Call",
39            "ChannelCredentials",
40            "CallCredentials",
41            "AuthMetadataContext",
42            "AuthMetadataPluginCallback",
43            "AuthMetadataPlugin",
44            "ServerCertificateConfiguration",
45            "ServerCredentials",
46            "UnaryUnaryMultiCallable",
47            "UnaryStreamMultiCallable",
48            "StreamUnaryMultiCallable",
49            "StreamStreamMultiCallable",
50            "UnaryUnaryClientInterceptor",
51            "UnaryStreamClientInterceptor",
52            "StreamUnaryClientInterceptor",
53            "StreamStreamClientInterceptor",
54            "Channel",
55            "ServicerContext",
56            "RpcMethodHandler",
57            "HandlerCallDetails",
58            "GenericRpcHandler",
59            "ServiceRpcHandler",
60            "Server",
61            "ServerInterceptor",
62            "LocalConnectionType",
63            "local_channel_credentials",
64            "local_server_credentials",
65            "alts_channel_credentials",
66            "alts_server_credentials",
67            "unary_unary_rpc_method_handler",
68            "unary_stream_rpc_method_handler",
69            "stream_unary_rpc_method_handler",
70            "ClientCallDetails",
71            "stream_stream_rpc_method_handler",
72            "method_handlers_generic_handler",
73            "ssl_channel_credentials",
74            "metadata_call_credentials",
75            "access_token_call_credentials",
76            "composite_call_credentials",
77            "composite_channel_credentials",
78            "compute_engine_channel_credentials",
79            "ssl_server_credentials",
80            "ssl_server_certificate_configuration",
81            "dynamic_ssl_server_credentials",
82            "channel_ready_future",
83            "insecure_channel",
84            "secure_channel",
85            "intercept_channel",
86            "server",
87            "protos",
88            "services",
89            "protos_and_services",
90            "xds_channel_credentials",
91            "xds_server_credentials",
92            "insecure_server_credentials",
93        )
94
95        self.assertCountEqual(
96            expected_grpc_code_elements, _from_grpc_import_star.GRPC_ELEMENTS
97        )
98
99
100class ChannelConnectivityTest(unittest.TestCase):
101    def testChannelConnectivity(self):
102        self.assertSequenceEqual(
103            (
104                grpc.ChannelConnectivity.IDLE,
105                grpc.ChannelConnectivity.CONNECTING,
106                grpc.ChannelConnectivity.READY,
107                grpc.ChannelConnectivity.TRANSIENT_FAILURE,
108                grpc.ChannelConnectivity.SHUTDOWN,
109            ),
110            tuple(grpc.ChannelConnectivity),
111        )
112
113
114class ChannelTest(unittest.TestCase):
115    def compute_engine_channel_credentials(self):
116        class TestCallCredentials(grpc.AuthMetadataPlugin):
117            def __call__(self, context, callback):
118                callback((), None)
119
120        test_call_credentials = TestCallCredentials()
121        call_credentials = grpc.metadata_call_credentials(
122            test_call_credentials, "test call credentials"
123        )
124        return grpc.compute_engine_channel_credentials(call_credentials)
125
126    def test_ssl_secure_channel(self):
127        channel = grpc.secure_channel(
128            "google.com:443", grpc.ssl_channel_credentials()
129        )
130        channel.close()
131
132    def test_compute_engine_secure_channel(self):
133        channel = grpc.secure_channel(
134            "google.com:443", self.compute_engine_channel_credentials()
135        )
136        channel.close()
137
138    def test_multiple_ssl_secure_channel(self):
139        _THREAD_COUNT = 10
140        wait_group = test_common.WaitGroup(_THREAD_COUNT)
141
142        def create_secure_channel():
143            wait_group.done()
144            wait_group.wait()
145            channel = grpc.secure_channel(
146                "google.com:443", grpc.ssl_channel_credentials()
147            )
148            channel.close()
149
150        threads = []
151        for _ in range(_THREAD_COUNT):
152            thread = threading.Thread(target=create_secure_channel)
153            thread.setDaemon(True)
154            thread.start()
155            threads.append(thread)
156
157        for thread in threads:
158            thread.join()
159
160    def test_multiple_compute_engine_secure_channel(self):
161        _THREAD_COUNT = 10
162        wait_group = test_common.WaitGroup(_THREAD_COUNT)
163
164        def create_secure_channel():
165            wait_group.done()
166            wait_group.wait()
167            channel = grpc.secure_channel(
168                "google.com:443", self.compute_engine_channel_credentials()
169            )
170            channel.close()
171
172        threads = []
173        for _ in range(_THREAD_COUNT):
174            thread = threading.Thread(target=create_secure_channel)
175            thread.setDaemon(True)
176            thread.start()
177            threads.append(thread)
178
179        for thread in threads:
180            thread.join()
181
182
183if __name__ == "__main__":
184    logging.basicConfig()
185    unittest.main(verbosity=2)
186