Repository: grpc/grpc-go
File: xds/internal/clients/grpctransport/grpc_transport_test.go (Go)

Code before:
			if tr != nil {
				defer tr.Close()
			}
			if !test.wantErr && tr == nil {
				t.Fatalf("got non-nil transport from Build(), want nil")
			}
			if test.wantErr && tr != nil {
				t.Fatalf("got nil transport from Build(), want non-nil")
			}
		})
	}
}

// TestNewStream_Success verifies that grpcTransport.NewStream() successfully
// creates a new client stream for the server when provided a valid server URI.
func (s) TestNewStream_Success(t *testing.T) {
	ts := setupTestServer(t, &v3discoverypb.DiscoveryResponse{VersionInfo: "1"})

	serverCfg := clients.ServerConfig{
		ServerURI:  ts.address,
		Extensions: ServerConfigExtension{Credentials: insecure.NewBundle()},
	}
	builder := Builder{}
	transport, err := builder.Build(serverCfg)
	if err != nil {
		t.Fatalf("Failed to build transport: %v", err)
	}
	defer transport.Close()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	_, err = transport.NewStream(ctx, "/envoy.service.discovery.v3.AggregatedDiscoveryService/StreamAggregatedResources")
	if err != nil {
		t.Fatalf("transport.NewStream() failed: %v", err)
	}
}

// TestNewStream_Error verifies that grpcTransport.NewStream() returns an error
// when attempting to create a stream with an invalid server URI.
func (s) TestNewStream_Error(t *testing.T) {
	serverCfg := clients.ServerConfig{
		ServerURI:  "invalid-server-uri",
		Extensions: ServerConfigExtension{Credentials: insecure.NewBundle()},
	}
	builder := Builder{}
	transport, err := builder.Build(serverCfg)
	if err != nil {
		t.Fatalf("Failed to build transport: %v", err)
	}
	defer transport.Close()


Review comment (bug):
Do you think we can simplify all this complex logic by splitting the success and failure cases into separate tests? See: https://g3doc.corp.google.com/company/teams/go-community/gotip/episodes/50.md?cl=head

Code after:
			name:      "Extensions is nil",
			serverCfg: clients.ServerConfig{ServerURI: "server-address"},
		},
		{
			name: "Extensions is not a ServerConfigExtension",
			serverCfg: clients.ServerConfig{
				ServerURI:  "server-address",
				Extensions: 1,
			},
		},
		{
			name: "ServerConfigExtension Credentials is nil",
			serverCfg: clients.ServerConfig{
				ServerURI:  "server-address",
				Extensions: ServerConfigExtension{},
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			b := &Builder{}
			tr, err := b.Build(test.serverCfg)
			if err == nil {
				t.Fatalf("Build() succeeded, want error")
			}
			if tr != nil {
				t.Fatalf("Got non-nil transport from Build(), want nil")
			}
		})
	}
}

// TestNewStream_Success verifies that NewStream() successfully creates a new
// client stream for the server when provided a valid server URI.
func (s) TestNewStream_Success(t *testing.T) {
	ts := setupTestServer(t, &v3discoverypb.DiscoveryResponse{VersionInfo: "1"})

	serverCfg := clients.ServerConfig{
		ServerURI:  ts.address,
		Extensions: ServerConfigExtension{Credentials: insecure.NewBundle()},
	}
	builder := Builder{}
	transport, err := builder.Build(serverCfg)
	if err != nil {
		t.Fatalf("Failed to build transport: %v", err)
	}
	defer transport.Close()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	if _, err = transport.NewStream(ctx, "/envoy.service.discovery.v3.AggregatedDiscoveryService/StreamAggregatedResources"); err != nil {