Repository: grpc/grpc-go
File: balancer/randomsubsetting/randomsubsetting_test.go (Go)

Code before:
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			gotCfg, gotErr := parser.ParseConfig(json.RawMessage(test.input))
			// Substring match makes this very tightly coupled to the
			// internalserviceconfig.BalancerConfig error strings. However, it
			// is important to distinguish the different types of error messages
			// possible as the parser has a few defined buckets of ways it can
			// error out.
			if (gotErr != nil) != (test.wantErr != "") {
				t.Fatalf("ParseConfig(%v) = %v, wantErr %v", test.input, gotErr, test.wantErr)
			}
			if gotErr != nil && !strings.Contains(gotErr.Error(), test.wantErr) {
				t.Fatalf("ParseConfig(%v) = %v, wantErr %v", test.input, gotErr, test.wantErr)
			}
			if test.wantErr != "" {
				return
			}
			if diff := cmp.Diff(gotCfg, test.wantCfg); diff != "" {
				t.Fatalf("ParseConfig(%v) got unexpected output, diff (-got +want): %v", test.input, diff)
			}
		})
	}
}

func setupBackends(t *testing.T, backendsCount int) ([]resolver.Address, func()) {
	t.Helper()

	backends := make([]*stubserver.StubServer, backendsCount)
	addresses := make([]resolver.Address, backendsCount)
	for i := 0; i < backendsCount; i++ {
		backend := &stubserver.StubServer{
			EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
				return &testpb.Empty{}, nil
			},
		}
		if err := backend.StartServer(); err != nil {
			t.Fatalf("Failed to start backend: %v", err)
		}
		t.Logf("Started good TestService backend at: %q", backend.Address)
		backends[i] = backend
		addresses[i] = resolver.Address{
			Addr: backend.Address,
		}
	}

	cancel := func() {
		for _, backend := range backends {
			backend.Stop()
		}
	}

Review comment (refactor):
This could be simplified a good deal with something like this:

```golang
func startTestServiceBackends(t *testing.T, num int) []resolver.Address {
	t.Helper()

	addresses := make([]resolver.Address, num)
	for i := 0; i < num; i++ {
		server := stubserver.StartTestService(t, nil)
		t.Cleanup(server.Stop)
		addresses = append(addresses, resolver.Address{Addr: backend.Address})
	}
	return addresses
}
```

Code after:
			// internalserviceconfig.BalancerConfig error strings. However, it
			// is important to distinguish the different types of error messages
			// possible as the parser has a few defined buckets of ways it can
			// error out.
			if (gotErr != nil) != (test.wantErr != "") {
				t.Fatalf("ParseConfig(%v) = %v, wantErr %v", test.input, gotErr, test.wantErr)
			}
			if gotErr != nil && !strings.Contains(gotErr.Error(), test.wantErr) {
				t.Fatalf("ParseConfig(%v) = %v, wantErr %v", test.input, gotErr, test.wantErr)
			}
			if test.wantErr != "" {
				return
			}
			if diff := cmp.Diff(test.wantCfg, gotCfg); diff != "" {
				t.Fatalf("ParseConfig(%s) got unexpected output, diff (-want +got):\n%s", test.input, diff)
			}
		})
	}
}

func makeEndpoints(n int) []resolver.Endpoint {
	endpoints := make([]resolver.Endpoint, n)
	for i := 0; i < n; i++ {
		endpoints[i] = resolver.Endpoint{
			Addresses: []resolver.Address{{Addr: fmt.Sprintf("endpoint-%d", i)}},
		}
	}
	return endpoints
}

func (s) TestCalculateSubset_Simple(t *testing.T) {
	tests := []struct {
		name       string
		endpoints  []resolver.Endpoint
		subsetSize uint32
		want       []resolver.Endpoint
	}{
		{
			name:       "NoEndpoints",
			endpoints:  []resolver.Endpoint{},
			subsetSize: 3,
			want:       []resolver.Endpoint{},
		},
		{
			name:       "SubsetSizeLargerThanNumberOfEndpoints",
			endpoints:  makeEndpoints(5),
			subsetSize: 10,
			want:       makeEndpoints(5),
		},
		{
			name:       "SubsetSizeEqualToNumberOfEndpoints",