@@ -88,8 +88,11 @@ async def run_with_test_server(
88
88
channel = grpc .aio .insecure_channel (f"localhost:{ port :d} " )
89
89
90
90
await server .start ()
91
- resp = await runnable (channel )
92
- await server .stop (1000 )
91
+
92
+ try :
93
+ resp = await runnable (channel )
94
+ finally :
95
+ await server .stop (1000 )
93
96
94
97
return resp
95
98
@@ -514,9 +517,79 @@ async def request(channel):
514
517
request = Request (client_id = 1 , request_data = failure_message )
515
518
msg = request .SerializeToString ()
516
519
517
- with testcase .assertRaises (Exception ):
520
+ with testcase .assertRaises (grpc .RpcError ) as cm :
521
+ await channel .unary_unary (rpc_call )(msg )
522
+
523
+ self .assertEqual (
524
+ cm .exception .code (), grpc .StatusCode .FAILED_PRECONDITION
525
+ )
526
+ self .assertEqual (cm .exception .details (), failure_message )
527
+
528
+ await run_with_test_server (request , servicer = AbortServicer ())
529
+
530
+ spans_list = self .memory_exporter .get_finished_spans ()
531
+ self .assertEqual (len (spans_list ), 1 )
532
+ span = spans_list [0 ]
533
+
534
+ self .assertEqual (span .name , rpc_call )
535
+ self .assertIs (span .kind , trace .SpanKind .SERVER )
536
+
537
+ # Check version and name in span's instrumentation info
538
+ self .assertEqualSpanInstrumentationInfo (
539
+ span , opentelemetry .instrumentation .grpc
540
+ )
541
+
542
+ # make sure this span errored, with the right status and detail
543
+ self .assertEqual (span .status .status_code , StatusCode .ERROR )
544
+ self .assertEqual (
545
+ span .status .description ,
546
+ f"{ grpc .StatusCode .FAILED_PRECONDITION } :{ failure_message } " ,
547
+ )
548
+
549
+ # Check attributes
550
+ self .assertSpanHasAttributes (
551
+ span ,
552
+ {
553
+ SpanAttributes .NET_PEER_IP : "[::1]" ,
554
+ SpanAttributes .NET_PEER_NAME : "localhost" ,
555
+ SpanAttributes .RPC_METHOD : "SimpleMethod" ,
556
+ SpanAttributes .RPC_SERVICE : "GRPCTestServer" ,
557
+ SpanAttributes .RPC_SYSTEM : "grpc" ,
558
+ SpanAttributes .RPC_GRPC_STATUS_CODE : grpc .StatusCode .FAILED_PRECONDITION .value [
559
+ 0
560
+ ],
561
+ },
562
+ )
563
+
564
+ async def test_abort_with_trailing_metadata (self ):
565
+ """Check that we can catch an abort properly when trailing_metadata provided"""
566
+ rpc_call = "/GRPCTestServer/SimpleMethod"
567
+ failure_message = "failure message"
568
+
569
+ class AbortServicer (GRPCTestServerServicer ):
570
+ # pylint:disable=C0103
571
+ async def SimpleMethod (self , request , context ):
572
+ metadata = (("meta" , "data" ),)
573
+ await context .abort (
574
+ grpc .StatusCode .FAILED_PRECONDITION ,
575
+ failure_message ,
576
+ trailing_metadata = metadata ,
577
+ )
578
+
579
+ testcase = self
580
+
581
+ async def request (channel ):
582
+ request = Request (client_id = 1 , request_data = failure_message )
583
+ msg = request .SerializeToString ()
584
+
585
+ with testcase .assertRaises (grpc .RpcError ) as cm :
518
586
await channel .unary_unary (rpc_call )(msg )
519
587
588
+ self .assertEqual (
589
+ cm .exception .code (), grpc .StatusCode .FAILED_PRECONDITION
590
+ )
591
+ self .assertEqual (cm .exception .details (), failure_message )
592
+
520
593
await run_with_test_server (request , servicer = AbortServicer ())
521
594
522
595
spans_list = self .memory_exporter .get_finished_spans ()
0 commit comments