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
1615
1716import grpc
1817from tests .protobuf import ( # pylint: disable=no-name-in-module
@@ -56,32 +55,32 @@ def __init__(self):
5655 pass
5756
5857 def intercept_unary_unary (
59- self , continuation , client_call_details , request
58+ self , continuation , client_call_details , request
6059 ):
6160 return self ._intercept_call (continuation , client_call_details , request )
6261
6362 def intercept_unary_stream (
64- self , continuation , client_call_details , request
63+ self , continuation , client_call_details , request
6564 ):
6665 return self ._intercept_call (continuation , client_call_details , request )
6766
6867 def intercept_stream_unary (
69- self , continuation , client_call_details , request_iterator
68+ self , continuation , client_call_details , request_iterator
7069 ):
7170 return self ._intercept_call (
7271 continuation , client_call_details , request_iterator
7372 )
7473
7574 def intercept_stream_stream (
76- self , continuation , client_call_details , request_iterator
75+ self , continuation , client_call_details , request_iterator
7776 ):
7877 return self ._intercept_call (
7978 continuation , client_call_details , request_iterator
8079 )
8180
8281 @staticmethod
8382 def _intercept_call (
84- continuation , client_call_details , request_or_iterator
83+ continuation , client_call_details , request_or_iterator
8584 ):
8685 return continuation (client_call_details , request_or_iterator )
8786
@@ -94,9 +93,7 @@ def setUp(self):
9493 self .server .start ()
9594 # use a user defined interceptor along with the opentelemetry client interceptor
9695 interceptors = [Interceptor ()]
97- self .channel = grpc .insecure_channel ("localhost:25565" , options = [
98- # (grpc.experimental.ChannelOptions.SingleThreadedUnaryStream, 1)
99- ])
96+ self .channel = grpc .insecure_channel ("localhost:25565" )
10097 self .channel = grpc .intercept_channel (self .channel , * interceptors )
10198 self ._stub = test_server_pb2_grpc .GRPCTestServerStub (self .channel )
10299
@@ -173,14 +170,11 @@ def test_unary_stream(self):
173170 )
174171
175172 def test_unary_stream_can_be_cancel (self ):
176- responses = server_streaming_method (self ._stub )
173+ responses = server_streaming_method (self ._stub , serialize = False )
177174 for i , _ in enumerate (responses ):
178175 if i == 1 :
179176 responses .cancel ()
180177 break
181- sleep (10 )
182- self .server .stop (None )
183- self .channel .close ()
184178 spans = self .memory_exporter .get_finished_spans ()
185179 self .assertEqual (len (spans ), 1 )
186180 span = spans [0 ]
@@ -205,6 +199,33 @@ def test_unary_stream_can_be_cancel(self):
205199 },
206200 )
207201
202+ def test_finished_stream_cancel_does_not_change_status_of_span (self ):
203+ responses = server_streaming_method (self ._stub , serialize = True )
204+ responses .cancel ()
205+ spans = self .memory_exporter .get_finished_spans ()
206+ self .assertEqual (len (spans ), 1 )
207+ span = spans [0 ]
208+
209+ self .assertEqual (span .name , "/GRPCTestServer/ServerStreamingMethod" )
210+ self .assertIs (span .kind , trace .SpanKind .CLIENT )
211+
212+ # Check version and name in span's instrumentation info
213+ self .assertEqualSpanInstrumentationInfo (
214+ span , opentelemetry .instrumentation .grpc
215+ )
216+
217+ self .assertSpanHasAttributes (
218+ span ,
219+ {
220+ SpanAttributes .RPC_METHOD : "ServerStreamingMethod" ,
221+ SpanAttributes .RPC_SERVICE : "GRPCTestServer" ,
222+ SpanAttributes .RPC_SYSTEM : "grpc" ,
223+ SpanAttributes .RPC_GRPC_STATUS_CODE : grpc .StatusCode .OK .value [
224+ 0
225+ ],
226+ },
227+ )
228+
208229 def test_stream_unary (self ):
209230 client_streaming_method (self ._stub )
210231 spans = self .memory_exporter .get_finished_spans ()
@@ -259,6 +280,38 @@ def test_stream_stream(self):
259280 },
260281 )
261282
283+ def test_stream_stream_can_be_cancel (self ):
284+ responses = bidirectional_streaming_method (self ._stub , serialize = False )
285+ for i , _ in enumerate (responses ):
286+ if i == 1 :
287+ responses .cancel ()
288+ break
289+ spans = self .memory_exporter .get_finished_spans ()
290+ self .assertEqual (len (spans ), 1 )
291+ span = spans [0 ]
292+
293+ self .assertEqual (
294+ span .name , "/GRPCTestServer/BidirectionalStreamingMethod"
295+ )
296+ self .assertIs (span .kind , trace .SpanKind .CLIENT )
297+
298+ # Check version and name in span's instrumentation info
299+ self .assertEqualSpanInstrumentationInfo (
300+ span , opentelemetry .instrumentation .grpc
301+ )
302+
303+ self .assertSpanHasAttributes (
304+ span ,
305+ {
306+ SpanAttributes .RPC_METHOD : "BidirectionalStreamingMethod" ,
307+ SpanAttributes .RPC_SERVICE : "GRPCTestServer" ,
308+ SpanAttributes .RPC_SYSTEM : "grpc" ,
309+ SpanAttributes .RPC_GRPC_STATUS_CODE : grpc .StatusCode .CANCELLED .value [
310+ 0
311+ ],
312+ },
313+ )
314+
262315 def test_error_simple (self ):
263316 with self .assertRaises (grpc .RpcError ):
264317 simple_method (self ._stub , error = True )
@@ -308,7 +361,7 @@ def test_error_stream_stream(self):
308361 )
309362
310363 def test_client_interceptor_trace_context_propagation (
311- self ,
364+ self ,
312365 ): # pylint: disable=no-self-use
313366 """ensure that client interceptor correctly inject trace context into all outgoing requests."""
314367 previous_propagator = get_global_textmap ()
0 commit comments