Repository: grpc/grpc-go
File: internal/resolver/delegatingresolver/delegatingresolver_ext_test.go (Go)

Code before:
	}{
		{
			name:               "no port in target",
			target:             "test.com",
			wantConnectAddress: "test.com:443",
		},
		{
			name:               "port specified in target",
			target:             "test.com:8080",
			wantConnectAddress: "test.com:8080",
		},
		{
			name:               "colon after host in target but no post",
			target:             "test.com:",
			wantErrorSubstring: "missing port after port-separator colon",
		},
		{
			name:                     "add default port env variable diabled, no port in target",
			target:                   "test.com",
			wantConnectAddress:       "test.com",
			disableDefaultPortEnvVar: true,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if test.disableDefaultPortEnvVar {
				testutils.SetEnvConfig(t, &envconfig.EnableDefaultPortForProxyTarget, false)
			}
			overrideTestHTTPSProxy(t, envProxyAddr)

			targetResolver := manual.NewBuilderWithScheme("dns")
			target := targetResolver.Scheme() + ":///" + test.target
			// Set up a manual DNS resolver to control the proxy address resolution.
			proxyResolver, proxyResolverBuilt := setupDNS(t)

			tcc, stateCh, _ := createTestResolverClientConn(t)
			_, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false)
			if test.wantErrorSubstring != "" {
				// Case 1: We expected an error.
				if err == nil {
					t.Fatalf("Delegating resolver created, want error containing %q", test.wantErrorSubstring)
				}
				if !strings.Contains(err.Error(), test.wantErrorSubstring) {
					t.Fatalf("Delegating resolver failed with error %v, want error containing %v", err.Error(), test.wantErrorSubstring)
				}
				return
			}

			// Case 2: We did NOT expect an error.
			if err != nil {

Review comment (refactor):
Oh I see that this helper does re-register the original `dns` resolver. But maybe this section can still be a little more explicit/readable etc. I'm not able to think of the exact way to make this better right now. 

Code after:
			wantConnectAddress: "test.com:443",
			defaultPortEnvVar:  true,
		},
		{
			name:               "complete_target",
			target:             "test.com:8080",
			wantConnectAddress: "test.com:8080",
			defaultPortEnvVar:  true,
		},
		{
			name:               "no_port_with_default_port_env_variable_disabled",
			target:             "test.com",
			wantConnectAddress: "test.com",
			defaultPortEnvVar:  false,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			testutils.SetEnvConfig(t, &envconfig.EnableDefaultPortForProxyTarget, test.defaultPortEnvVar)
			overrideTestHTTPSProxy(t, envProxyAddr)

			targetResolver := manual.NewBuilderWithScheme("dns")
			target := targetResolver.Scheme() + ":///" + test.target
			// Set up a manual DNS resolver to control the proxy address resolution.
			proxyResolver, proxyResolverBuilt := setupDNS(t)

			tcc, stateCh, _ := createTestResolverClientConn(t)
			if _, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false); err != nil {
				t.Fatalf("Delegating resolver creation failed unexpectedly with error: %v", err)
			}
			ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
			defer cancel()

			// Wait for the proxy resolver to be built before calling UpdateState.
			mustBuildResolver(ctx, t, proxyResolverBuilt)
			proxyResolver.UpdateState(resolver.State{
				Addresses: []resolver.Address{
					{Addr: resolvedProxyTestAddr1},
				},
			})

			wantState := resolver.State{
				Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, test.wantConnectAddress)},
				Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, test.wantConnectAddress)}}},
			}

			var gotState resolver.State
			select {
			case gotState = <-stateCh:
			case <-ctx.Done():