Debian Codegraph @

src/python/grpcio_tests/tests/unit/_dns_resolver_test.py

raw
1 # Copyright 2019 The 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 for an actual dns resolution."""
15
16 import logging
17 import unittest
18
19 import grpc
20
21 from tests.unit import test_common
22 from tests.unit.framework.common import test_constants
23
24 _METHOD = '/ANY/METHOD'
25 _REQUEST = b'\x00\x00\x00'
26 _RESPONSE = _REQUEST
27
28
29 class GenericHandler(grpc.GenericRpcHandler):
30
31 def service(self, unused_handler_details):
32 return grpc.unary_unary_rpc_method_handler(
33 lambda request, unused_context: request,
34 )
35
36
37 class DNSResolverTest(unittest.TestCase):
38
39 def setUp(self):
40 self._server = test_common.test_server()
41 self._server.add_generic_rpc_handlers((GenericHandler(),))
42 self._port = self._server.add_insecure_port('[::]:0')
43 self._server.start()
44
45 def tearDown(self):
46 self._server.stop(None)
47
48 def test_connect_loopback(self):
49 # NOTE(https://github.com/grpc/grpc/issues/18422)
50 # In short, Gevent + C-Ares = Segfault. The C-Ares driver is not
51 # supported by custom io manager like "gevent"
52 # NOTE(b/201064791): use loopback46.unittest.grpc.io since
53 # it returns the expected responses even when DNS64 dns servers
54 # are used on the test worker (and for purposes of this
55 # test the use of loopback4 vs loopback46 makes no difference).
56 with grpc.insecure_channel('loopback46.unittest.grpc.io:%d' %
57 self._port) as channel:
58 self.assertEqual(
59 channel.unary_unary(_METHOD)(
60 _REQUEST,
61 timeout=10,
62 ), _RESPONSE)
63
64
65 if __name__ == '__main__':
66 logging.basicConfig()
67 unittest.main(verbosity=2)