Repository: grpc/grpc-go
File: orca/service_test.go (Go)

Code before:
//
// TODO: Change this test to use the client API, when ready, to read the
// out-of-band metrics pushed by the server.
func (s) TestE2E_CustomBackendMetrics_OutOfBand(t *testing.T) {
	lis, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatal(err)
	}

	// Override the min reporting interval in the internal package.
	const shortReportingInterval = 10 * time.Millisecond
	smr := orca.NewServerMetricsRecorder()
	opts := orca.ServiceOptions{MinReportingInterval: shortReportingInterval, ServerMetricsProvider: smr}
	internal.AllowAnyMinReportingInterval.(func(*orca.ServiceOptions))(&opts)

	var requests int64
	var mu sync.Mutex

	const numRequests = 20

	stub := &stubserver.StubServer{
		UnaryCallF: func(ctx context.Context, req *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
			mu.Lock()
			requests++

			smr.SetNamedUtilization(requestsMetricKey, float64(requests)*0.01)
			mu.Unlock()

			smr.SetCPUUtilization(50.0)
			smr.SetMemoryUtilization(0.9)
			smr.SetApplicationUtilization(1.2)
			return &testpb.SimpleResponse{}, nil
		},
		EmptyCallF: func(ctx context.Context, req *testpb.Empty) (*testpb.Empty, error) {
			smr.DeleteNamedUtilization(requestsMetricKey)
			smr.SetCPUUtilization(0)
			smr.SetMemoryUtilization(0)
			smr.DeleteApplicationUtilization()
			return &testpb.Empty{}, nil
		},
	}

	// Assign the gRPC server to the stub server and start serving.
	stub.Listener = lis
	stub.S = grpc.NewServer()
	// Register the OpenRCAService with a very short metrics reporting interval.
	if err := orca.Register(stub.S, opts); err != nil {
		t.Fatalf("orca.EnableOutOfBandMetricsReportingForTesting() failed: %v", err)
	}
	stubserver.StartTestService(t, stub)
	defer stub.S.Stop()

Review comment (suggestion):
seems like we have changed how the lock is handled here, but this seems to be the right way of doing it. 

But this would go away if we use an atominc.Int64 here. 

Code after:
//
// TODO: Change this test to use the client API, when ready, to read the
// out-of-band metrics pushed by the server.
func (s) TestE2E_CustomBackendMetrics_OutOfBand(t *testing.T) {
	lis, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatal(err)
	}

	// Override the min reporting interval in the internal package.
	const shortReportingInterval = 10 * time.Millisecond
	smr := orca.NewServerMetricsRecorder()
	opts := orca.ServiceOptions{MinReportingInterval: shortReportingInterval, ServerMetricsProvider: smr}
	internal.AllowAnyMinReportingInterval.(func(*orca.ServiceOptions))(&opts)

	var requests atomic.Int64

	stub := &stubserver.StubServer{
		Listener: lis,
		UnaryCallF: func(ctx context.Context, req *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
			newRequests := requests.Add(1)

			smr.SetNamedUtilization(requestsMetricKey, float64(newRequests)*0.01)
			smr.SetCPUUtilization(50.0)
			smr.SetMemoryUtilization(0.9)
			smr.SetApplicationUtilization(1.2)
			return &testpb.SimpleResponse{}, nil
		},
		EmptyCallF: func(ctx context.Context, req *testpb.Empty) (*testpb.Empty, error) {
			smr.DeleteNamedUtilization(requestsMetricKey)
			smr.SetCPUUtilization(0)
			smr.SetMemoryUtilization(0)
			smr.DeleteApplicationUtilization()
			return &testpb.Empty{}, nil
		},
	}

	// Assign the gRPC server to the stub server and start serving.
	stub.S = grpc.NewServer()
	// Register the OpenRCAService with a very short metrics reporting interval.
	if err := orca.Register(stub.S, opts); err != nil {
		t.Fatalf("orca.EnableOutOfBandMetricsReportingForTesting() failed: %v", err)
	}
	stubserver.StartTestService(t, stub)
	defer stub.S.Stop()
	t.Logf("Started gRPC server at %s...", lis.Addr().String())

	// Dial the test server.
	cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		t.Fatalf("grpc.NewClient(%s) failed: %v", lis.Addr().String(), err)