Repository: grpc/grpc-go
File: internal/resolver/dns/dns_resolver_test.go (Go)

Code before:
		hostLookupTable: map[string][]string{
			"foo.bar.com": {"1.2.3.4", "5.6.7.8"},
		},
		txtLookupTable: map[string][]string{
			"_grpc_config.foo.bar.com": txtRecordServiceConfig(txtRecordGood),
		},
	}
	overrideNetResolver(t, tr)

	r, stateCh, _ := buildResolverWithTestClientConn(t, target)

	// Verify that the first update pushed by the resolver matches expectations.
	wantAddrs := []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}, {Addr: "5.6.7.8" + colonDefaultPort}}
	wantSC := scJSON
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	verifyUpdateFromResolver(ctx, t, stateCh, expectedUpdate{
		addrs: wantAddrs,
		endpoints: []resolver.Endpoint{
			{Addresses: []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}}},
			{Addresses: []resolver.Address{{Addr: "5.6.7.8" + colonDefaultPort}}},
		},
		sc: wantSC,
	})

	// Update state in the fake net.Resolver to return only one address and a
	// new service config.
	tr.UpdateHostLookupTable(map[string][]string{target: {"1.2.3.4"}})
	tr.UpdateTXTLookupTable(map[string][]string{
		"_grpc_config.foo.bar.com": txtRecordServiceConfig(`[{"serviceConfig":{"loadBalancingPolicy": "grpclb"}}]`),
	})

	// Ask the resolver to re-resolve and verify that the new update matches
	// expectations.
	r.ResolveNow(resolver.ResolveNowOptions{})
	wantAddrs = []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}}
	wantSC = `{"loadBalancingPolicy": "grpclb"}`
	verifyUpdateFromResolver(ctx, t, stateCh, expectedUpdate{
		addrs:     wantAddrs,
		endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}}}},
		sc:        wantSC,
	})

	// Update state in the fake resolver to return no addresses and the same
	// service config as before.
	tr.UpdateHostLookupTable(map[string][]string{target: nil})

	// Ask the resolver to re-resolve and verify that the new update matches
	// expectations.
	r.ResolveNow(resolver.ResolveNowOptions{})
	verifyUpdateFromResolver(ctx, t, stateCh, expectedUpdate{sc: wantSC})

Review comment (suggestion):
Can we reuse `wantAddrs` to create the `wantEndpoints` so that if test changes later, we only have to modify in one place.

Code after:
// ResolveNow method.
func (s) TestDNSResolver_ResolveNow(t *testing.T) {
	const target = "foo.bar.com"

	overrideResolutionInterval(t, 0)
	overrideTimeAfterFunc(t, 0)
	tr := &testNetResolver{
		hostLookupTable: map[string][]string{
			"foo.bar.com": {"1.2.3.4", "5.6.7.8"},
		},
		txtLookupTable: map[string][]string{
			"_grpc_config.foo.bar.com": txtRecordServiceConfig(txtRecordGood),
		},
	}
	overrideNetResolver(t, tr)

	r, stateCh, _ := buildResolverWithTestClientConn(t, target)

	// Verify that the first update pushed by the resolver matches expectations.
	wantAddrs := []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}, {Addr: "5.6.7.8" + colonDefaultPort}}
	wantSC := mustParseServiceConfig(t, scJSON)
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	verifyUpdateFromResolver(ctx, t, stateCh, resolverUpdate{
		addrs: wantAddrs,
		endpoints: []resolver.Endpoint{
			{Addresses: []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}}},
			{Addresses: []resolver.Address{{Addr: "5.6.7.8" + colonDefaultPort}}},
		},
		serviceConfig: wantSC,
	})

	// Update state in the fake net.Resolver to return only one address and a
	// new service config.
	tr.UpdateHostLookupTable(map[string][]string{target: {"1.2.3.4"}})
	tr.UpdateTXTLookupTable(map[string][]string{
		"_grpc_config.foo.bar.com": txtRecordServiceConfig(`[{"serviceConfig":{"loadBalancingPolicy": "grpclb"}}]`),
	})

	// Ask the resolver to re-resolve and verify that the new update matches
	// expectations.
	r.ResolveNow(resolver.ResolveNowOptions{})
	wantAddrs = []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}}
	wantSC = mustParseServiceConfig(t, `{"loadBalancingPolicy": "grpclb"}`)
	verifyUpdateFromResolver(ctx, t, stateCh, resolverUpdate{
		addrs:         wantAddrs,
		endpoints:     []resolver.Endpoint{{Addresses: []resolver.Address{{Addr: "1.2.3.4" + colonDefaultPort}}}},
		serviceConfig: wantSC,
	})

	// Update state in the fake resolver to return no addresses and the same