@@ -237,6 +237,36 @@ func distributionDataLatencyCount(vi *viewInformation, countWant int64, wantTags
237237 return nil
238238}
239239
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+
240270// TestAllMetricsOneFunction tests emitted metrics from gRPC. It registers all
241271// the metrics provided by this package. It then configures a system with a gRPC
242272// Client and gRPC server with the OpenCensus Dial and Server Option configured,
@@ -987,10 +1017,13 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
9871017 },
9881018 },
9891019 }
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+ }
9941027 view .Unregister (allViews ... )
9951028 // Assert the expected emissions for each metric match the expected
9961029 // emissions.
0 commit comments