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

Code before:
	resources := e2e.DefaultClientResources(e2e.ResourceParams{
		DialTarget: serviceName,
		NodeID:     nodeID,
		Host:       "localhost",
		Port:       testutils.ParsePort(t, server.Address),
		SecLevel:   e2e.SecurityLevelNone,
	})
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	if err := mgmtServer.Update(ctx, resources); err != nil {
		t.Fatal(err)
	}

	// Intercept a grpc.NewClient call from the xds client to validate DialOptions.
	xci.GRPCNewClient = func(target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
		actual := map[string]int{}
		for _, opt := range opts {
			if mo, ok := opt.(*mockDialOption); ok {
				actual[mo.name]++
			}
		}
		expected := map[string]int{}
		for _, name := range testDialOptNames {
			expected[name]++
		}
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("grpc.NewClient() was called with unexpected DialOptions: got %v, want %v", actual, expected)
		}
		return grpc.NewClient(target, opts...)
	}
	defer func() { xci.GRPCNewClient = grpc.NewClient }()

	// Create a ClientConn and make a successful RPC. The insecure transport credentials passed into
	// the gRPC.NewClient is the credentials for the data plane communication with the test backend.
	cc, err := grpc.NewClient(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolverBuilder))
	if err != nil {
		t.Fatalf("failed to dial local test server: %v", err)
	}

	client := testgrpc.NewTestServiceClient(cc)
	if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
		t.Fatalf("EmptyCall() failed: %v", err)
	}

	// Close the connection to ensure stats handler calls are made.
	cc.Close()
}


Review comment (suggestion):
Using the `reflect` package for comparison in tests is not recommended. Please see: go/go-style/decisions#types-of-equality.

Code after:
		DialTarget: serviceName,
		NodeID:     nodeID,
		Host:       "localhost",
		Port:       testutils.ParsePort(t, server.Address),
		SecLevel:   e2e.SecurityLevelNone,
	})
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	if err := mgmtServer.Update(ctx, resources); err != nil {
		t.Fatal(err)
	}

	// Intercept a grpc.NewClient call from the xds client to validate DialOptions.
	xci.GRPCNewClient = func(target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
		got := map[string]int{}
		for _, opt := range opts {
			if mo, ok := opt.(*nopDialOption); ok {
				got[mo.name]++
			}
		}
		want := map[string]int{}
		for _, name := range credsBuilder.testDialOptNames {
			want[name]++
		}
		if !cmp.Equal(got, want) {
			t.Errorf("grpc.NewClient() was called with unexpected DialOptions: got %v, want %v", got, want)
		}
		return grpc.NewClient(target, opts...)
	}
	defer func() { xci.GRPCNewClient = grpc.NewClient }()

	// Create a ClientConn and make a successful RPC. The insecure transport
	// credentials passed into the gRPC.NewClient is the credentials for the
	// data plane communication with the test backend.
	cc, err := grpc.NewClient(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolverBuilder))
	if err != nil {
		t.Fatalf("Failed to dial local test server: %v", err)
	}

	client := testgrpc.NewTestServiceClient(cc)
	if _, err := client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
		t.Fatalf("EmptyCall() failed: %v", err)
	}
	cc.Close()
}