1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414# pylint:disable=cyclic-import
15+ from time import sleep
1516
1617from unittest import mock
1718
@@ -61,32 +62,32 @@ def __init__(self):
6162 pass
6263
6364 def intercept_unary_unary (
64- self , continuation , client_call_details , request
65+ self , continuation , client_call_details , request
6566 ):
6667 return self ._intercept_call (continuation , client_call_details , request )
6768
6869 def intercept_unary_stream (
69- self , continuation , client_call_details , request
70+ self , continuation , client_call_details , request
7071 ):
7172 return self ._intercept_call (continuation , client_call_details , request )
7273
7374 def intercept_stream_unary (
74- self , continuation , client_call_details , request_iterator
75+ self , continuation , client_call_details , request_iterator
7576 ):
7677 return self ._intercept_call (
7778 continuation , client_call_details , request_iterator
7879 )
7980
8081 def intercept_stream_stream (
81- self , continuation , client_call_details , request_iterator
82+ self , continuation , client_call_details , request_iterator
8283 ):
8384 return self ._intercept_call (
8485 continuation , client_call_details , request_iterator
8586 )
8687
8788 @staticmethod
8889 def _intercept_call (
89- continuation , client_call_details , request_or_iterator
90+ continuation , client_call_details , request_or_iterator
9091 ):
9192 return continuation (client_call_details , request_or_iterator )
9293
@@ -99,7 +100,9 @@ def setUp(self):
99100 self .server .start ()
100101 # use a user defined interceptor along with the opentelemetry client interceptor
101102 interceptors = [Interceptor ()]
102- self .channel = grpc .insecure_channel ("localhost:25565" )
103+ self .channel = grpc .insecure_channel ("localhost:25565" , options = [
104+ # (grpc.experimental.ChannelOptions.SingleThreadedUnaryStream, 1)
105+ ])
103106 self .channel = grpc .intercept_channel (self .channel , * interceptors )
104107 self ._stub = test_server_pb2_grpc .GRPCTestServerStub (self .channel )
105108
@@ -172,11 +175,14 @@ def test_unary_stream(self):
172175 )
173176
174177 def test_unary_stream_can_be_cancel (self ):
175- responses = server_streaming_method (self ._stub , serialize = False )
178+ responses = server_streaming_method (self ._stub )
176179 for i , _ in enumerate (responses ):
177180 if i == 1 :
178181 responses .cancel ()
179182 break
183+ sleep (10 )
184+ self .server .stop (None )
185+ self .channel .close ()
180186 spans = self .memory_exporter .get_finished_spans ()
181187 self .assertEqual (len (spans ), 1 )
182188 span = spans [0 ]
@@ -201,33 +207,6 @@ def test_unary_stream_can_be_cancel(self):
201207 },
202208 )
203209
204- def test_finished_stream_cancel_does_not_change_status_of_span (self ):
205- responses = server_streaming_method (self ._stub , serialize = True )
206- responses .cancel ()
207- spans = self .memory_exporter .get_finished_spans ()
208- self .assertEqual (len (spans ), 1 )
209- span = spans [0 ]
210-
211- self .assertEqual (span .name , "/GRPCTestServer/ServerStreamingMethod" )
212- self .assertIs (span .kind , trace .SpanKind .CLIENT )
213-
214- # Check version and name in span's instrumentation info
215- self .assertEqualSpanInstrumentationInfo (
216- span , opentelemetry .instrumentation .grpc
217- )
218-
219- self .assertSpanHasAttributes (
220- span ,
221- {
222- SpanAttributes .RPC_METHOD : "ServerStreamingMethod" ,
223- SpanAttributes .RPC_SERVICE : "GRPCTestServer" ,
224- SpanAttributes .RPC_SYSTEM : "grpc" ,
225- SpanAttributes .RPC_GRPC_STATUS_CODE : grpc .StatusCode .OK .value [
226- 0
227- ],
228- },
229- )
230-
231210 def test_stream_unary (self ):
232211 client_streaming_method (self ._stub )
233212 spans = self .memory_exporter .get_finished_spans ()
@@ -278,38 +257,6 @@ def test_stream_stream(self):
278257 },
279258 )
280259
281- def test_stream_stream_can_be_cancel (self ):
282- responses = bidirectional_streaming_method (self ._stub , serialize = False )
283- for i , _ in enumerate (responses ):
284- if i == 1 :
285- responses .cancel ()
286- break
287- spans = self .memory_exporter .get_finished_spans ()
288- self .assertEqual (len (spans ), 1 )
289- span = spans [0 ]
290-
291- self .assertEqual (
292- span .name , "/GRPCTestServer/BidirectionalStreamingMethod"
293- )
294- self .assertIs (span .kind , trace .SpanKind .CLIENT )
295-
296- # Check version and name in span's instrumentation info
297- self .assertEqualSpanInstrumentationInfo (
298- span , opentelemetry .instrumentation .grpc
299- )
300-
301- self .assertSpanHasAttributes (
302- span ,
303- {
304- SpanAttributes .RPC_METHOD : "BidirectionalStreamingMethod" ,
305- SpanAttributes .RPC_SERVICE : "GRPCTestServer" ,
306- SpanAttributes .RPC_SYSTEM : "grpc" ,
307- SpanAttributes .RPC_GRPC_STATUS_CODE : grpc .StatusCode .CANCELLED .value [
308- 0
309- ],
310- },
311- )
312-
313260 def test_error_simple (self ):
314261 with self .assertRaises (grpc .RpcError ):
315262 simple_method (self ._stub , error = True )
@@ -385,7 +332,7 @@ def invoker(_request, _metadata):
385332 self .assertEqual (span_end_mock .call_count , 1 )
386333
387334 def test_client_interceptor_trace_context_propagation (
388- self ,
335+ self ,
389336 ): # pylint: disable=no-self-use
390337 """ensure that client interceptor correctly inject trace context into all outgoing requests."""
391338 previous_propagator = get_global_textmap ()
0 commit comments