Repository: grpc/grpc-go
File: internal/xds/xdsclient/tests/loadreport_test.go (Go)

Code before:
func (s) TestConcurrentReportLoad(t *testing.T) {
	mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{SupportLoadReportingService: true})
	nodeID := uuid.New().String()
	bc := e2e.DefaultBootstrapContents(t, nodeID, mgmtServer.Address)
	client := createXDSClient(t, bc)

	serverConfig, err := bootstrap.ServerConfigForTesting(bootstrap.ServerConfigTestingOptions{URI: mgmtServer.Address})
	if err != nil {
		t.Fatalf("Failed to create server config for testing: %v", err)
	}

	// Call ReportLoad() concurrently from multiple go routines.
	const numGoroutines = 10
	cancelStores := make([]func(context.Context), numGoroutines)
	var wg sync.WaitGroup
	wg.Add(numGoroutines)
	for i := range numGoroutines {
		go func(idx int) {
			defer wg.Done()
			_, cancelStores[idx] = client.ReportLoad(serverConfig)
		}(i)
	}
	wg.Wait()

	// Cancel all the load reporting streams. The last call to cancel is
	// expectwed to block until a final load report with pending loads is sent.
	// But the stream is currently blocked on a recv() call waiting for the LRS
	// server to send the initial laod reporting response, which it never does.
	// Hence we use a context with short timeout here.
	for i, cancel := range cancelStores {
		sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
		defer sCancel()
		cancel(sCtx)
		t.Logf("Cancelled LRS stream %d", i)
	}
}

// TestConcurrentChannels verifies that we can create multiple gRPC channels
// concurrently with a shared XDSClient, each of which will create a new LRS
// stream without any race.
func (s) TestConcurrentChannels(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true, SupportLoadReportingService: true})

	nodeID := uuid.New().String()
	bc := e2e.DefaultBootstrapContents(t, nodeID, mgmtServer.Address)

	if internal.NewXDSResolverWithPoolForTesting == nil {
		t.Fatalf("internal.NewXDSResolverWithConfigForTesting is nil")

Review comment (suggestion):
```suggestion
	// expected to block until a final load report with pending loads is sent.
```

Code after:
func (s) TestConcurrentReportLoad(t *testing.T) {
	mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{SupportLoadReportingService: true})
	nodeID := uuid.New().String()
	bc := e2e.DefaultBootstrapContents(t, nodeID, mgmtServer.Address)
	client := createXDSClient(t, bc)

	serverConfig, err := bootstrap.ServerConfigForTesting(bootstrap.ServerConfigTestingOptions{URI: mgmtServer.Address})
	if err != nil {
		t.Fatalf("Failed to create server config for testing: %v", err)
	}

	// Call ReportLoad() concurrently from multiple go routines.
	const numGoroutines = 10
	cancelStores := make([]func(context.Context), numGoroutines)
	var wg sync.WaitGroup
	wg.Add(numGoroutines)
	for i := range numGoroutines {
		go func(idx int) {
			defer wg.Done()
			_, cancelStores[idx] = client.ReportLoad(serverConfig)
		}(i)
	}
	wg.Wait()

	// Cancel all the load reporting streams. The last call to cancel is
	// expected to block until a final load report with pending loads is sent.
	// But the stream is currently blocked on a recv() call waiting for the LRS
	// server to send the initial laod reporting response, which it never does.
	// Hence we use a context with short timeout here.
	for i, cancel := range cancelStores {
		sCtx, sCancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
		defer sCancel()
		cancel(sCtx)
		t.Logf("Cancelled LRS stream %d", i)
	}
}

// TestConcurrentChannels verifies that we can create multiple gRPC channels
// concurrently with a shared XDSClient, each of which will create a new LRS
// stream without any race.
func (s) TestConcurrentChannels(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true, SupportLoadReportingService: true})

	nodeID := uuid.New().String()
	bc := e2e.DefaultBootstrapContents(t, nodeID, mgmtServer.Address)

	if internal.NewXDSResolverWithPoolForTesting == nil {
		t.Fatalf("internal.NewXDSResolverWithConfigForTesting is nil")