context_peer_test.py
Go to the documentation of this file.
1 # Copyright 2020 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 """Testing the server context ability to access peer info."""
15 
16 import asyncio
17 import logging
18 import os
19 from typing import Callable, Iterable, Sequence, Tuple
20 import unittest
21 
22 import grpc
23 from grpc.experimental import aio
24 
25 from src.proto.grpc.testing import messages_pb2
26 from src.proto.grpc.testing import test_pb2_grpc
27 from tests.unit.framework.common import test_constants
28 from tests_aio.unit import _common
29 from tests_aio.unit._test_base import AioTestBase
30 from tests_aio.unit._test_server import TestServiceServicer
31 from tests_aio.unit._test_server import start_test_server
32 
33 _REQUEST = b'\x03\x07'
34 _TEST_METHOD = '/test/UnaryUnary'
35 
36 
38 
39  async def test_peer(self):
40 
41  @grpc.unary_unary_rpc_method_handler
42  async def check_peer_unary_unary(request: bytes,
43  context: aio.ServicerContext):
44  self.assertEqual(_REQUEST, request)
45  # The peer address could be ipv4 or ipv6
46  self.assertIn('ip', context.peer())
47  return request
48 
49  # Creates a server
50  server = aio.server()
52  'test', {'UnaryUnary': check_peer_unary_unary})
53  server.add_generic_rpc_handlers((handlers,))
54  port = server.add_insecure_port('[::]:0')
55  await server.start()
56 
57  # Creates a channel
58  async with aio.insecure_channel('localhost:%d' % port) as channel:
59  response = await channel.unary_unary(_TEST_METHOD)(_REQUEST)
60  self.assertEqual(_REQUEST, response)
61 
62  await server.stop(None)
63 
64 
65 if __name__ == '__main__':
66  logging.basicConfig(level=logging.DEBUG)
67  unittest.main(verbosity=2)
tests_aio.unit
Definition: src/python/grpcio_tests/tests_aio/unit/__init__.py:1
tests_aio.unit._test_server
Definition: tests_aio/unit/_test_server.py:1
tests_aio.unit._test_base
Definition: _test_base.py:1
tests_aio.unit.context_peer_test.TestContextPeer.test_peer
def test_peer(self)
Definition: context_peer_test.py:39
grpc::experimental
Definition: include/grpcpp/channel.h:46
tests_aio.unit.context_peer_test.TestContextPeer
Definition: context_peer_test.py:37
grpc.method_handlers_generic_handler
def method_handlers_generic_handler(service, method_handlers)
Definition: src/python/grpcio/grpc/__init__.py:1590
tests.unit.framework.common
Definition: src/python/grpcio_tests/tests/unit/framework/common/__init__.py:1
tests_aio.unit._test_base.AioTestBase
Definition: _test_base.py:49


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