Repository: grpc/grpc-go
File: xds/internal/xdsclient/transport/transport_test.go (Go)

Code before:
	// dialer is no longer called.
	internal.GRPCNewClient = grpc.NewClient
	c, err = transport.New(opts)
	defer func() {
		if c != nil {
			c.Close()
		}
	}()
	if err != nil {
		t.Fatalf("transport.New(%v) failed: %v", opts, err)
	}

	if customDialerCalled {
		t.Fatalf("transport.New(%+v) custom dialer called = true, want false", opts)
	}
}

const testDialerCredsBuilderName = "test_dialer_creds"

// testDialerCredsBuilder implements the `Credentials` interface defined in
// package `xds/bootstrap` and encapsulates an insecure credential with a
// custom Dialer that specifies how to dial the xDS server.
type testDialerCredsBuilder struct {
	dialCalled chan struct{}
}

func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
	return &testDialerCredsBundle{
		Bundle:     insecure.NewBundle(),
		dialCalled: t.dialCalled,
	}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
	return testDialerCredsBuilderName
}

// testDialerCredsBundle implements the `Bundle` interface defined in package
// `credentials` and encapsulates an insecure credential with a custom Dialer
// that specifies how to dial the xDS server.
type testDialerCredsBundle struct {
	credentials.Bundle
	dialCalled chan struct{}
}

func (t *testDialerCredsBundle) Dialer(_ context.Context, address string) (net.Conn, error) {
	close(t.dialCalled)
	return net.Dial("tcp", address)
}

func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {

Review comment (suggestion):
Add a comment?

```golang
	// Closed with the custom Dialer is invoked. 
	// Needs to be passed in by the test.
	dialCalled chan struct{}
```

Code after:
	// dialer is no longer called.
	internal.GRPCNewClient = grpc.NewClient
	c, err = transport.New(opts)
	defer func() {
		if c != nil {
			c.Close()
		}
	}()
	if err != nil {
		t.Fatalf("transport.New(%v) failed: %v", opts, err)
	}

	if customDialerCalled {
		t.Fatalf("transport.New(%+v) custom dialer called = true, want false", opts)
	}
}

const testDialerCredsBuilderName = "test_dialer_creds"

// testDialerCredsBuilder implements the `Credentials` interface defined in
// package `xds/bootstrap` and encapsulates an insecure credential with a
// custom Dialer that specifies how to dial the xDS server.
type testDialerCredsBuilder struct {
	// Closed with the custom Dialer is invoked.
	// Needs to be passed in by the test.
	dialCalled chan struct{}
}

func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
	return &testDialerCredsBundle{
		Bundle:     insecure.NewBundle(),
		dialCalled: t.dialCalled,
	}, func() {}, nil
}

func (t *testDialerCredsBuilder) Name() string {
	return testDialerCredsBuilderName
}

// testDialerCredsBundle implements the `Bundle` interface defined in package
// `credentials` and encapsulates an insecure credential with a custom Dialer
// that specifies how to dial the xDS server.
type testDialerCredsBundle struct {
	credentials.Bundle
	dialCalled chan struct{}
}

func (t *testDialerCredsBundle) Dialer(_ context.Context, address string) (net.Conn, error) {
	close(t.dialCalled)
	return net.Dial("tcp", address)
}