Debian Codegraph @

src/python/grpcio_testing/grpc_testing/_common.py

raw
1 # Copyright 2017 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 """Common interfaces and implementation."""
15
16 import abc
17 import collections
18
19
20 def _fuss(tuplified_metadata):
21 return tuplified_metadata + ((
22 'grpc.metadata_added_by_runtime',
23 'gRPC is allowed to add metadata in transmission and does so.',
24 ),)
25
26
27 FUSSED_EMPTY_METADATA = _fuss(())
28
29
30 def fuss_with_metadata(metadata):
31 if metadata is None:
32 return FUSSED_EMPTY_METADATA
33 else:
34 return _fuss(tuple(metadata))
35
36
37 def rpc_names(service_descriptors):
38 rpc_names_to_descriptors = {}
39 for service_descriptor in service_descriptors:
40 for method_descriptor in service_descriptor.methods_by_name.values():
41 rpc_name = '/{}/{}'.format(service_descriptor.full_name,
42 method_descriptor.name)
43 rpc_names_to_descriptors[rpc_name] = method_descriptor
44 return rpc_names_to_descriptors
45
46
47 class ChannelRpcRead(
48 collections.namedtuple('ChannelRpcRead', (
49 'response',
50 'trailing_metadata',
51 'code',
52 'details',
53 ))):
54 pass
55
56
57 class ChannelRpcHandler(abc.ABC):
58
59 @abc.abstractmethod
60 def initial_metadata(self):
61 raise NotImplementedError()
62
63 @abc.abstractmethod
64 def add_request(self, request):
65 raise NotImplementedError()
66
67 @abc.abstractmethod
68 def close_requests(self):
69 raise NotImplementedError()
70
71 @abc.abstractmethod
72 def take_response(self):
73 raise NotImplementedError()
74
75 @abc.abstractmethod
76 def cancel(self, code, details):
77 raise NotImplementedError()
78
79 @abc.abstractmethod
80 def termination(self):
81 raise NotImplementedError()
82
83 @abc.abstractmethod
84 def is_active(self):
85 raise NotImplementedError()
86
87 @abc.abstractmethod
88 def time_remaining(self):
89 raise NotImplementedError()
90
91 @abc.abstractmethod
92 def add_callback(self, callback):
93 raise NotImplementedError()
94
95
96 class ChannelHandler(abc.ABC):
97
98 @abc.abstractmethod
99 def invoke_rpc(self, method_full_rpc_name, invocation_metadata, requests,
100 requests_closed, timeout):
101 raise NotImplementedError()
102
103
104 class ServerRpcRead(
105 collections.namedtuple('ServerRpcRead', (
106 'request',
107 'requests_closed',
108 'terminated',
109 ))):
110 pass
111
112
113 REQUESTS_CLOSED = ServerRpcRead(None, True, False)
114 TERMINATED = ServerRpcRead(None, False, True)
115
116
117 class ServerRpcHandler(abc.ABC):
118
119 @abc.abstractmethod
120 def send_initial_metadata(self, initial_metadata):
121 raise NotImplementedError()
122
123 @abc.abstractmethod
124 def take_request(self):
125 raise NotImplementedError()
126
127 @abc.abstractmethod
128 def add_response(self, response):
129 raise NotImplementedError()
130
131 @abc.abstractmethod
132 def send_termination(self, trailing_metadata, code, details):
133 raise NotImplementedError()
134
135 @abc.abstractmethod
136 def add_termination_callback(self, callback):
137 raise NotImplementedError()
138
139
140 class Serverish(abc.ABC):
141
142 @abc.abstractmethod
143 def invoke_unary_unary(self, method_descriptor, handler,
144 invocation_metadata, request, deadline):
145 raise NotImplementedError()
146
147 @abc.abstractmethod
148 def invoke_unary_stream(self, method_descriptor, handler,
149 invocation_metadata, request, deadline):
150 raise NotImplementedError()
151
152 @abc.abstractmethod
153 def invoke_stream_unary(self, method_descriptor, handler,
154 invocation_metadata, deadline):
155 raise NotImplementedError()
156
157 @abc.abstractmethod
158 def invoke_stream_stream(self, method_descriptor, handler,
159 invocation_metadata, deadline):
160 raise NotImplementedError()