16
16
*
17
17
*/
18
18
19
- package test
19
+ package pickfirst_test
20
20
21
21
import (
22
22
"context"
@@ -28,11 +28,13 @@ import (
28
28
29
29
"google.golang.org/grpc"
30
30
"google.golang.org/grpc/backoff"
31
+ pfinternal "google.golang.org/grpc/balancer/pickfirst/internal"
31
32
"google.golang.org/grpc/codes"
32
33
"google.golang.org/grpc/connectivity"
33
34
"google.golang.org/grpc/credentials/insecure"
34
35
"google.golang.org/grpc/internal"
35
36
"google.golang.org/grpc/internal/channelz"
37
+ "google.golang.org/grpc/internal/grpctest"
36
38
"google.golang.org/grpc/internal/stubserver"
37
39
"google.golang.org/grpc/internal/testutils"
38
40
"google.golang.org/grpc/internal/testutils/pickfirst"
@@ -45,7 +47,38 @@ import (
45
47
testpb "google.golang.org/grpc/interop/grpc_testing"
46
48
)
47
49
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
+ }
49
82
50
83
// setupPickFirst performs steps required for pick_first tests. It starts a
51
84
// bunch of backends exporting the TestService, creates a ClientConn to them
@@ -377,16 +410,15 @@ func (s) TestPickFirst_ShuffleAddressList(t *testing.T) {
377
410
const serviceConfig = `{"loadBalancingConfig": [{"pick_first":{ "shuffleAddressList": true }}]}`
378
411
379
412
// 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 )) {
383
416
if n != 2 {
384
417
t .Errorf ("Shuffle called with n=%v; want 2" , n )
385
418
return
386
419
}
387
420
f (0 , 1 ) // reverse the two addresses
388
421
}
389
-
390
422
// Set up our backends.
391
423
cc , r , backends := setupPickFirst (t , 2 )
392
424
addrs := stubBackendsToResolverAddrs (backends )
@@ -434,9 +466,9 @@ func (s) TestPickFirst_ShuffleAddressList(t *testing.T) {
434
466
// Test config parsing with the env var turned on and off for various scenarios.
435
467
func (s ) TestPickFirst_ParseConfig_Success (t * testing.T ) {
436
468
// 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 )) {
440
472
if n != 2 {
441
473
t .Errorf ("Shuffle called with n=%v; want 2" , n )
442
474
return
0 commit comments