@@ -237,6 +237,36 @@ func distributionDataLatencyCount(vi *viewInformation, countWant int64, wantTags
237
237
return nil
238
238
}
239
239
240
+ // waitForServerCompletedRPCs waits until both Unary and Streaming metric rows
241
+ // appear, in two separate rows, for server completed RPC's view. Returns an
242
+ // error if the Unary and Streaming metric are not found within the passed
243
+ // context's timeout.
244
+ func waitForServerCompletedRPCs (ctx context.Context ) error {
245
+ for ; ctx .Err () == nil ; <- time .After (time .Millisecond ) {
246
+ rows , err := view .RetrieveData ("grpc.io/server/completed_rpcs" )
247
+ if err != nil {
248
+ continue
249
+ }
250
+ unaryFound := false
251
+ streamingFound := false
252
+ for _ , row := range rows {
253
+ for _ , tag := range row .Tags {
254
+ if tag .Value == "grpc.testing.TestService/UnaryCall" {
255
+ unaryFound = true
256
+ break
257
+ } else if tag .Value == "grpc.testing.TestService/FullDuplexCall" {
258
+ streamingFound = true
259
+ break
260
+ }
261
+ }
262
+ if unaryFound && streamingFound {
263
+ return nil
264
+ }
265
+ }
266
+ }
267
+ return fmt .Errorf ("timeout when waiting for Unary and Streaming rows to be present for \" grpc.io/server/completed_rpcs\" " )
268
+ }
269
+
240
270
// TestAllMetricsOneFunction tests emitted metrics from gRPC. It registers all
241
271
// the metrics provided by this package. It then configures a system with a gRPC
242
272
// Client and gRPC server with the OpenCensus Dial and Server Option configured,
@@ -987,10 +1017,13 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
987
1017
},
988
1018
},
989
1019
}
990
- // Unregister all the views. Unregistering a view causes a synchronous
991
- // upload of any collected data for the view to any registered exporters.
992
- // Thus, after this unregister call, the exporter has the data to make
993
- // assertions on immediately.
1020
+ // Server Side stats.End call happens asynchronously for both Unary and
1021
+ // Streaming calls with respect to the RPC returning client side. Thus, add
1022
+ // a sync point at the global view package level for these two rows to be
1023
+ // recorded, which will be synchronously uploaded to exporters right after.
1024
+ if err := waitForServerCompletedRPCs (ctx ); err != nil {
1025
+ t .Fatal (err )
1026
+ }
994
1027
view .Unregister (allViews ... )
995
1028
// Assert the expected emissions for each metric match the expected
996
1029
// emissions.
0 commit comments