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