Skip to content

Commit b8ee37d

Browse files
authored
pickfirst: Move var for mocking the shuffle func from internal/internal to pickfirst/internal (#7698)
1 parent d9d8f34 commit b8ee37d

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
// Package internal contains code internal to the pickfirst package.
19+
package internal
20+
21+
import "math/rand"
22+
23+
// RandShuffle pseudo-randomizes the order of addresses.
24+
var RandShuffle = rand.Shuffle

balancer/pickfirst/pickfirst.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import (
2626
"math/rand"
2727

2828
"google.golang.org/grpc/balancer"
29+
"google.golang.org/grpc/balancer/pickfirst/internal"
2930
"google.golang.org/grpc/connectivity"
3031
"google.golang.org/grpc/grpclog"
31-
"google.golang.org/grpc/internal"
3232
internalgrpclog "google.golang.org/grpc/internal/grpclog"
3333
"google.golang.org/grpc/internal/pretty"
3434
"google.golang.org/grpc/resolver"
@@ -37,7 +37,6 @@ import (
3737

3838
func init() {
3939
balancer.Register(pickfirstBuilder{})
40-
internal.ShuffleAddressListForTesting = func(n int, swap func(i, j int)) { rand.Shuffle(n, swap) }
4140
}
4241

4342
var logger = grpclog.Component("pick-first-lb")
@@ -143,7 +142,7 @@ func (b *pickfirstBalancer) UpdateClientConnState(state balancer.ClientConnState
143142
// within each endpoint. - A61
144143
if cfg.ShuffleAddressList {
145144
endpoints = append([]resolver.Endpoint{}, endpoints...)
146-
internal.ShuffleAddressListForTesting.(func(int, func(int, int)))(len(endpoints), func(i, j int) { endpoints[i], endpoints[j] = endpoints[j], endpoints[i] })
145+
internal.RandShuffle(len(endpoints), func(i, j int) { endpoints[i], endpoints[j] = endpoints[j], endpoints[i] })
147146
}
148147

149148
// "Flatten the list by concatenating the ordered list of addresses for each

test/pickfirst_test.go renamed to balancer/pickfirst/pickfirst_ext_test.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
package test
19+
package pickfirst_test
2020

2121
import (
2222
"context"
@@ -28,11 +28,13 @@ import (
2828

2929
"google.golang.org/grpc"
3030
"google.golang.org/grpc/backoff"
31+
pfinternal "google.golang.org/grpc/balancer/pickfirst/internal"
3132
"google.golang.org/grpc/codes"
3233
"google.golang.org/grpc/connectivity"
3334
"google.golang.org/grpc/credentials/insecure"
3435
"google.golang.org/grpc/internal"
3536
"google.golang.org/grpc/internal/channelz"
37+
"google.golang.org/grpc/internal/grpctest"
3638
"google.golang.org/grpc/internal/stubserver"
3739
"google.golang.org/grpc/internal/testutils"
3840
"google.golang.org/grpc/internal/testutils/pickfirst"
@@ -45,7 +47,38 @@ import (
4547
testpb "google.golang.org/grpc/interop/grpc_testing"
4648
)
4749

48-
const pickFirstServiceConfig = `{"loadBalancingConfig": [{"pick_first":{}}]}`
50+
const (
51+
pickFirstServiceConfig = `{"loadBalancingConfig": [{"pick_first":{}}]}`
52+
// Default timeout for tests in this package.
53+
defaultTestTimeout = 10 * time.Second
54+
// Default short timeout, to be used when waiting for events which are not
55+
// expected to happen.
56+
defaultTestShortTimeout = 100 * time.Millisecond
57+
)
58+
59+
func init() {
60+
channelz.TurnOn()
61+
}
62+
63+
type s struct {
64+
grpctest.Tester
65+
}
66+
67+
func Test(t *testing.T) {
68+
grpctest.RunSubTests(t, s{})
69+
}
70+
71+
// parseServiceConfig is a test helper which uses the manual resolver to parse
72+
// the given service config. It calls t.Fatal() if service config parsing fails.
73+
func parseServiceConfig(t *testing.T, r *manual.Resolver, sc string) *serviceconfig.ParseResult {
74+
t.Helper()
75+
76+
scpr := r.CC.ParseServiceConfig(sc)
77+
if scpr.Err != nil {
78+
t.Fatalf("Failed to parse service config %q: %v", sc, scpr.Err)
79+
}
80+
return scpr
81+
}
4982

5083
// setupPickFirst performs steps required for pick_first tests. It starts a
5184
// bunch of backends exporting the TestService, creates a ClientConn to them
@@ -377,16 +410,15 @@ func (s) TestPickFirst_ShuffleAddressList(t *testing.T) {
377410
const serviceConfig = `{"loadBalancingConfig": [{"pick_first":{ "shuffleAddressList": true }}]}`
378411

379412
// Install a shuffler that always reverses two entries.
380-
origShuf := internal.ShuffleAddressListForTesting
381-
defer func() { internal.ShuffleAddressListForTesting = origShuf }()
382-
internal.ShuffleAddressListForTesting = func(n int, f func(int, int)) {
413+
origShuf := pfinternal.RandShuffle
414+
defer func() { pfinternal.RandShuffle = origShuf }()
415+
pfinternal.RandShuffle = func(n int, f func(int, int)) {
383416
if n != 2 {
384417
t.Errorf("Shuffle called with n=%v; want 2", n)
385418
return
386419
}
387420
f(0, 1) // reverse the two addresses
388421
}
389-
390422
// Set up our backends.
391423
cc, r, backends := setupPickFirst(t, 2)
392424
addrs := stubBackendsToResolverAddrs(backends)
@@ -434,9 +466,9 @@ func (s) TestPickFirst_ShuffleAddressList(t *testing.T) {
434466
// Test config parsing with the env var turned on and off for various scenarios.
435467
func (s) TestPickFirst_ParseConfig_Success(t *testing.T) {
436468
// Install a shuffler that always reverses two entries.
437-
origShuf := internal.ShuffleAddressListForTesting
438-
defer func() { internal.ShuffleAddressListForTesting = origShuf }()
439-
internal.ShuffleAddressListForTesting = func(n int, f func(int, int)) {
469+
origShuf := pfinternal.RandShuffle
470+
defer func() { pfinternal.RandShuffle = origShuf }()
471+
pfinternal.RandShuffle = func(n int, f func(int, int)) {
440472
if n != 2 {
441473
t.Errorf("Shuffle called with n=%v; want 2", n)
442474
return

internal/internal.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ var (
207207
// default resolver scheme.
208208
UserSetDefaultScheme = false
209209

210-
// ShuffleAddressListForTesting pseudo-randomizes the order of addresses. n
211-
// is the number of elements. swap swaps the elements with indexes i and j.
212-
ShuffleAddressListForTesting any // func(n int, swap func(i, j int))
213-
214210
// ConnectedAddress returns the connected address for a SubConnState. The
215211
// address is only valid if the state is READY.
216212
ConnectedAddress any // func (scs SubConnState) resolver.Address

test/balancer_switching_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ const (
4646
loadBalancedServicePort = 443
4747
wantGRPCLBTraceDesc = `Channel switches to new LB policy "grpclb"`
4848
wantRoundRobinTraceDesc = `Channel switches to new LB policy "round_robin"`
49+
pickFirstServiceConfig = `{"loadBalancingConfig": [{"pick_first":{}}]}`
4950

5051
// This is the number of stub backends set up at the start of each test. The
5152
// first backend is used for the "grpclb" policy and the rest are used for
5253
// other LB policies to test balancer switching.
5354
backendCount = 3
5455
)
5556

57+
// stubBackendsToResolverAddrs converts from a set of stub server backends to
58+
// resolver addresses. Useful when pushing addresses to the manual resolver.
59+
func stubBackendsToResolverAddrs(backends []*stubserver.StubServer) []resolver.Address {
60+
addrs := make([]resolver.Address, len(backends))
61+
for i, backend := range backends {
62+
addrs[i] = resolver.Address{Addr: backend.Address}
63+
}
64+
return addrs
65+
}
66+
5667
// setupBackendsAndFakeGRPCLB sets up backendCount number of stub server
5768
// backends and a fake grpclb server for tests which exercise balancer switch
5869
// scenarios involving grpclb.

0 commit comments

Comments
 (0)