-
Notifications
You must be signed in to change notification settings - Fork 4.6k
balancer/randomsubsetting: Extend the unit tests in the randomsubsetting package. #8781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
04b9193
a2f51bc
7638565
b79c675
26c52b6
7a18d55
ae6f169
345055c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * | ||
| * Copyright 2025 gRPC authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package randomsubsetting | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| xxhash "github.com/cespare/xxhash/v2" | ||
| "google.golang.org/grpc/resolver" | ||
|
|
||
| _ "google.golang.org/grpc/balancer/roundrobin" // For round_robin LB policy in tests | ||
| ) | ||
|
|
||
| func (s) TestSubsettingEndpointsDomain(t *testing.T) { | ||
|
|
||
| testCases := []struct { | ||
| endpoints []resolver.Endpoint | ||
| subsetSize uint32 | ||
| want uint32 | ||
| }{ | ||
| { | ||
| endpoints: makeEndpoints(4), | ||
| subsetSize: 0, | ||
| want: 0, | ||
| }, | ||
| { | ||
| endpoints: makeEndpoints(3), | ||
| subsetSize: 4, | ||
| want: 3, | ||
| }, | ||
| { | ||
| endpoints: makeEndpoints(5), | ||
| subsetSize: 3, | ||
| want: 3, | ||
| }, | ||
| { | ||
| endpoints: []resolver.Endpoint{}, | ||
| subsetSize: 1, | ||
| want: 0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(fmt.Sprint(tc.subsetSize), func(t *testing.T) { | ||
| b := &subsettingBalancer{ | ||
| cfg: &lbConfig{SubsetSize: tc.subsetSize}, | ||
| hashSeed: 0, | ||
| hashDigest: xxhash.New(), | ||
| } | ||
| subsetOfEndpoints := b.calculateSubset(tc.endpoints) | ||
| got := len(subsetOfEndpoints) | ||
| if fmt.Sprint(got) != fmt.Sprint(tc.want) { | ||
| t.Fatalf("subset size=%v; endpoints(%v) = %v; want %v", tc.want, tc.endpoints, got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (s) TestUniformDistributionOfEndpoints(t *testing.T) { | ||
|
||
|
|
||
| var ( | ||
| subsetSize = 4 | ||
| iteration = 1600 | ||
| diff int | ||
| ) | ||
|
|
||
| endpoints := makeEndpoints(16) | ||
| expected := iteration / len(endpoints) * subsetSize | ||
| diff = expected / 7 // allow ~14% difference | ||
|
||
| EndpointCount := make(map[string]int, len(endpoints)) | ||
|
|
||
| for i := 0; i < iteration; i++ { | ||
| t.Run(fmt.Sprint(subsetSize), func(t *testing.T) { | ||
| t.Helper() | ||
| lb := &subsettingBalancer{ | ||
| cfg: &lbConfig{SubsetSize: uint32(subsetSize)}, | ||
| hashSeed: uint64(i ^ 3 + iteration*i + subsetSize), | ||
| hashDigest: xxhash.New(), | ||
| } | ||
| subsetOfEndpoints := lb.calculateSubset(endpoints) | ||
|
|
||
| for _, ep := range subsetOfEndpoints { | ||
| EndpointCount[ep.Addresses[0].Addr]++ | ||
| } | ||
| }) | ||
| } | ||
| // Verify the distribution is uniform within a small diff range. | ||
| // The expected count for each endpoint is: iteration / total_endpoints * subset_size | ||
| // e.g. 1600 / 16 * 4 = 400 +/- diff | ||
| for epAddr, count := range EndpointCount { | ||
| if count < expected-diff || count > expected+diff { | ||
| t.Fatalf("endpoint %v selected %v times; expected <=> %v", epAddr, count, expected) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is similar to the existing test
TestCalculateSubset_Simple.You can remove this and add more test cases in
TestCalculateSubset_Simpleif you think of any.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Yes, the only possible case that could be added to
TestCalculateSubset_Simpleis the case where the number of endpoints is strictly greater than the subset size.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it is only one test case missing, i had renamed function and added to test set.