test: replace manual.GenerateAndRegisterManualResolver with grpc.WithResolvers (#3700)

This commit is contained in:
Zou Nengren 2020-06-24 00:49:44 +08:00 коммит произвёл GitHub
Родитель 9a46550357
Коммит 4e63bcab52
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 152 добавлений и 215 удалений

Просмотреть файл

@ -396,8 +396,7 @@ func newLoadBalancer(numberOfBackends int, statsChan chan *lbpb.ClientStats) (ts
var grpclbConfig = `{"loadBalancingConfig": [{"grpclb": {}}]}`
func (s) TestGRPCLB(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(1, nil)
if err != nil {
@ -419,7 +418,7 @@ func (s) TestGRPCLB(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -444,8 +443,7 @@ func (s) TestGRPCLB(t *testing.T) {
// The remote balancer sends response with duplicates to grpclb client.
func (s) TestGRPCLBWeighted(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(2, nil)
if err != nil {
@ -470,7 +468,7 @@ func (s) TestGRPCLBWeighted(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -510,8 +508,7 @@ func (s) TestGRPCLBWeighted(t *testing.T) {
}
func (s) TestDropRequest(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(2, nil)
if err != nil {
@ -536,7 +533,7 @@ func (s) TestDropRequest(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -661,8 +658,7 @@ func (s) TestDropRequest(t *testing.T) {
// When the balancer in use disconnects, grpclb should connect to the next address from resolved balancer address list.
func (s) TestBalancerDisconnects(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
var (
tests []*testServers
@ -694,7 +690,7 @@ func (s) TestBalancerDisconnects(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -739,8 +735,7 @@ func (s) TestFallback(t *testing.T) {
balancer.Register(newLBBuilderWithFallbackTimeout(100 * time.Millisecond))
defer balancer.Register(newLBBuilder())
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(1, nil)
if err != nil {
@ -771,7 +766,7 @@ func (s) TestFallback(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -867,8 +862,7 @@ func (s) TestFallback(t *testing.T) {
}
func (s) TestExplicitFallback(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(1, nil)
if err != nil {
@ -899,7 +893,7 @@ func (s) TestExplicitFallback(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -971,7 +965,7 @@ func (s) TestExplicitFallback(t *testing.T) {
func (s) TestFallBackWithNoServerAddress(t *testing.T) {
resolveNowCh := make(chan struct{}, 1)
r, cleanup := manual.GenerateAndRegisterManualResolver()
r := manual.NewBuilderWithScheme("whatever")
r.ResolveNowCallback = func(resolver.ResolveNowOptions) {
select {
case <-resolveNowCh:
@ -979,7 +973,6 @@ func (s) TestFallBackWithNoServerAddress(t *testing.T) {
}
resolveNowCh <- struct{}{}
}
defer cleanup()
tss, cleanup, err := newLoadBalancer(1, nil)
if err != nil {
@ -1009,7 +1002,7 @@ func (s) TestFallBackWithNoServerAddress(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -1090,8 +1083,7 @@ func (s) TestFallBackWithNoServerAddress(t *testing.T) {
}
func (s) TestGRPCLBPickFirst(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(3, nil)
if err != nil {
@ -1120,7 +1112,7 @@ func (s) TestGRPCLBPickFirst(t *testing.T) {
creds := serverNameCheckCreds{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds), grpc.WithContextDialer(fakeNameDialer))
if err != nil {
t.Fatalf("Failed to dial to the backend %v", err)
@ -1245,8 +1237,7 @@ func checkStats(stats, expected *rpcStats) error {
}
func runAndCheckStats(t *testing.T, drop bool, statsChan chan *lbpb.ClientStats, runRPCs func(*grpc.ClientConn), statsWant *rpcStats) error {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
tss, cleanup, err := newLoadBalancer(1, statsChan)
if err != nil {
@ -1270,7 +1261,7 @@ func runAndCheckStats(t *testing.T, drop bool, statsChan chan *lbpb.ClientStats,
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName,
cc, err := grpc.DialContext(ctx, r.Scheme()+":///"+beServerName, grpc.WithResolvers(r),
grpc.WithTransportCredentials(&creds),
grpc.WithPerRPCCredentials(failPreRPCCred{}),
grpc.WithContextDialer(fakeNameDialer))

Просмотреть файл

@ -98,8 +98,7 @@ func startTestServers(count int) (_ *test, err error) {
}
func (s) TestOneBackend(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
test, err := startTestServers(1)
if err != nil {
@ -107,7 +106,7 @@ func (s) TestOneBackend(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -128,8 +127,7 @@ func (s) TestOneBackend(t *testing.T) {
}
func (s) TestBackendsRoundRobin(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
backendCount := 5
test, err := startTestServers(backendCount)
@ -138,7 +136,7 @@ func (s) TestBackendsRoundRobin(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -187,8 +185,7 @@ func (s) TestBackendsRoundRobin(t *testing.T) {
}
func (s) TestAddressesRemoved(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
test, err := startTestServers(1)
if err != nil {
@ -196,7 +193,7 @@ func (s) TestAddressesRemoved(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -233,8 +230,7 @@ func (s) TestAddressesRemoved(t *testing.T) {
}
func (s) TestCloseWithPendingRPC(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
test, err := startTestServers(1)
if err != nil {
@ -242,7 +238,7 @@ func (s) TestCloseWithPendingRPC(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -266,8 +262,7 @@ func (s) TestCloseWithPendingRPC(t *testing.T) {
}
func (s) TestNewAddressWhileBlocking(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
test, err := startTestServers(1)
if err != nil {
@ -275,7 +270,7 @@ func (s) TestNewAddressWhileBlocking(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -313,8 +308,7 @@ func (s) TestNewAddressWhileBlocking(t *testing.T) {
}
func (s) TestOneServerDown(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
backendCount := 3
test, err := startTestServers(backendCount)
@ -323,7 +317,7 @@ func (s) TestOneServerDown(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -410,8 +404,7 @@ func (s) TestOneServerDown(t *testing.T) {
}
func (s) TestAllServersDown(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
backendCount := 3
test, err := startTestServers(backendCount)
@ -420,7 +413,7 @@ func (s) TestAllServersDown(t *testing.T) {
}
defer test.cleanup()
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}

Просмотреть файл

@ -147,14 +147,13 @@ func checkRoundRobin(cc *ClientConn, servers []*server) error {
}
func (s) TestSwitchBalancer(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
const numServers = 2
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -179,14 +178,13 @@ func (s) TestSwitchBalancer(t *testing.T) {
// Test that balancer specified by dial option will not be overridden.
func (s) TestBalancerDialOption(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
const numServers = 2
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}), WithBalancerName(roundrobin.Name))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}), WithBalancerName(roundrobin.Name))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -207,10 +205,9 @@ func (s) TestBalancerDialOption(t *testing.T) {
// First addr update contains grpclb.
func (s) TestSwitchBalancerGRPCLBFirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -268,10 +265,9 @@ func (s) TestSwitchBalancerGRPCLBFirst(t *testing.T) {
// First addr update does not contain grpclb.
func (s) TestSwitchBalancerGRPCLBSecond(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -345,10 +341,9 @@ func (s) TestSwitchBalancerGRPCLBSecond(t *testing.T) {
// when the resolved address doesn't contain grpclb addresses, balancer will be
// switched back to roundrobin.
func (s) TestSwitchBalancerGRPCLBRoundRobin(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -408,10 +403,9 @@ func (s) TestSwitchBalancerGRPCLBRoundRobin(t *testing.T) {
// service config won't take effect. But when there's no grpclb address in a new
// resolved address list, balancer will be switched to the new one.
func (s) TestSwitchBalancerGRPCLBServiceConfig(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -495,14 +489,13 @@ func (s) TestSwitchBalancerGRPCLBWithGRPCLBNotRegistered(t *testing.T) {
internal.BalancerUnregister("grpclb")
defer balancer.Register(&magicalLB{})
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
const numServers = 3
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}

Просмотреть файл

@ -66,10 +66,9 @@ func (s) TestDialWithTimeout(t *testing.T) {
<-dialDone // Close conn only after dial returns.
}()
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{lisAddr}})
client, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithTimeout(5*time.Second))
client, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithTimeout(5*time.Second))
close(dialDone)
if err != nil {
t.Fatalf("Dial failed. Err: %v", err)
@ -119,10 +118,9 @@ func (s) TestDialWithMultipleBackendsNotSendingServerPreface(t *testing.T) {
}
}()
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{lis1Addr, lis2Addr}})
client, err := Dial(r.Scheme()+":///test.server", WithInsecure())
client, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
if err != nil {
t.Fatalf("Dial failed. Err: %v", err)
}
@ -642,10 +640,9 @@ func (s) TestConnectParamsWithMinConnectTimeout(t *testing.T) {
}
func (s) TestResolverServiceConfigBeforeAddressNotPanic(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -660,10 +657,9 @@ func (s) TestResolverServiceConfigBeforeAddressNotPanic(t *testing.T) {
func (s) TestResolverServiceConfigWhileClosingNotPanic(t *testing.T) {
for i := 0; i < 10; i++ { // Run this multiple times to make sure it doesn't panic.
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme(fmt.Sprintf("whatever-%d", i))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -674,10 +670,9 @@ func (s) TestResolverServiceConfigWhileClosingNotPanic(t *testing.T) {
}
func (s) TestResolverEmptyUpdateNotPanic(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -754,10 +749,9 @@ func (s) TestClientUpdatesParamsAfterGoAway(t *testing.T) {
}
func (s) TestDisableServiceConfigOption(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
addr := r.Scheme() + ":///non.existent"
cc, err := Dial(addr, WithInsecure(), WithDisableServiceConfig())
cc, err := Dial(addr, WithInsecure(), WithResolvers(r), WithDisableServiceConfig())
if err != nil {
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
}
@ -1013,8 +1007,7 @@ func (s) TestUpdateAddresses_RetryFromFirstAddr(t *testing.T) {
}
func (s) TestDefaultServiceConfig(t *testing.T) {
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
addr := r.Scheme() + ":///non.existent"
js := `{
"methodConfig": [
@ -1055,7 +1048,7 @@ func testInvalidDefaultServiceConfig(t *testing.T) {
}
func testDefaultServiceConfigWhenResolverServiceConfigDisabled(t *testing.T, r *manual.Resolver, addr string, js string) {
cc, err := Dial(addr, WithInsecure(), WithDisableServiceConfig(), WithDefaultServiceConfig(js))
cc, err := Dial(addr, WithInsecure(), WithDisableServiceConfig(), WithResolvers(r), WithDefaultServiceConfig(js))
if err != nil {
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
}
@ -1071,7 +1064,7 @@ func testDefaultServiceConfigWhenResolverServiceConfigDisabled(t *testing.T, r *
}
func testDefaultServiceConfigWhenResolverDoesNotReturnServiceConfig(t *testing.T, r *manual.Resolver, addr string, js string) {
cc, err := Dial(addr, WithInsecure(), WithDefaultServiceConfig(js))
cc, err := Dial(addr, WithInsecure(), WithResolvers(r), WithDefaultServiceConfig(js))
if err != nil {
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
}
@ -1085,7 +1078,7 @@ func testDefaultServiceConfigWhenResolverDoesNotReturnServiceConfig(t *testing.T
}
func testDefaultServiceConfigWhenResolverReturnInvalidServiceConfig(t *testing.T, r *manual.Resolver, addr string, js string) {
cc, err := Dial(addr, WithInsecure(), WithDefaultServiceConfig(js))
cc, err := Dial(addr, WithInsecure(), WithResolvers(r), WithDefaultServiceConfig(js))
if err != nil {
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
}

Просмотреть файл

@ -51,10 +51,9 @@ func main() {
defer s.Stop()
/***** Initialize manual resolver and Dial *****/
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
// Set up a connection to the server.
conn, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithBalancerName("round_robin"))
conn, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName("round_robin"))
if err != nil {
log.Fatalf("did not connect: %v", err)
}

Просмотреть файл

@ -54,8 +54,7 @@ func callUnaryEcho(c pb.EchoClient) {
func main() {
flag.Parse()
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{
Addresses: []resolver.Address{
{Addr: "localhost:50051"},
@ -68,6 +67,7 @@ func main() {
options := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithResolvers(r),
grpc.WithDefaultServiceConfig(serviceConfig),
}

Просмотреть файл

@ -39,14 +39,16 @@ func errorDesc(err error) string {
}
func (s) TestOneBackendPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 1
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server",
WithInsecure(),
WithResolvers(r),
WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -72,14 +74,13 @@ func (s) TestOneBackendPickfirst(t *testing.T) {
}
func (s) TestBackendsPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 2
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -105,14 +106,13 @@ func (s) TestBackendsPickfirst(t *testing.T) {
}
func (s) TestNewAddressWhileBlockingPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 1
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -141,14 +141,13 @@ func (s) TestNewAddressWhileBlockingPickfirst(t *testing.T) {
}
func (s) TestCloseWithPendingRPCPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 1
_, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -177,14 +176,13 @@ func (s) TestCloseWithPendingRPCPickfirst(t *testing.T) {
}
func (s) TestOneServerDownPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 2
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -218,14 +216,13 @@ func (s) TestOneServerDownPickfirst(t *testing.T) {
}
func (s) TestAllServersDownPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 2
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
@ -261,14 +258,13 @@ func (s) TestAllServersDownPickfirst(t *testing.T) {
}
func (s) TestAddressesRemovedPickfirst(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
numServers := 3
servers, scleanup := startServers(t, numServers, math.MaxInt32)
defer scleanup()
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithCodec(testCodec{}))
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r), WithCodec(testCodec{}))
if err != nil {
t.Fatalf("failed to dial: %v", err)
}

Просмотреть файл

@ -77,14 +77,14 @@ func testResolverErrorPolling(t *testing.T, badUpdate func(*manual.Resolver), go
return 0
}
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
rn := make(chan struct{})
defer func() { close(rn) }()
r.ResolveNowCallback = func(resolver.ResolveNowOptions) { rn <- struct{}{} }
defaultDialOptions := []DialOption{
WithInsecure(),
WithResolvers(r),
withResolveNowBackoff(resolverBackoff),
}
cc, err := Dial(r.Scheme()+":///test.server", append(defaultDialOptions, dopts...)...)
@ -173,11 +173,10 @@ func (s) TestServiceConfigErrorPolling(t *testing.T) {
// sure there is no data race in this code path, and also that there is no
// deadlock.
func (s) TestResolverErrorInBuild(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{ServiceConfig: &serviceconfig.ParseResult{Err: errors.New("resolver build err")}})
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
if err != nil {
t.Fatalf("Dial(_, _) = _, %v; want _, nil", err)
}
@ -194,10 +193,9 @@ func (s) TestResolverErrorInBuild(t *testing.T) {
}
func (s) TestServiceConfigErrorRPC(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure())
cc, err := Dial(r.Scheme()+":///test.server", WithInsecure(), WithResolvers(r))
if err != nil {
t.Fatalf("Dial(_, _) = _, %v; want _, nil", err)
}

Просмотреть файл

@ -321,13 +321,12 @@ func (testBalancerKeepAddresses) Close() {
// Make sure that non-grpclb balancers don't get grpclb addresses even if name
// resolver sends them
func (s) TestNonGRPCLBBalancerGetsNoGRPCLBAddress(t *testing.T) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
b := newTestBalancerKeepAddresses()
balancer.Register(b)
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(),
cc, err := grpc.Dial(r.Scheme()+":///test.server", grpc.WithInsecure(), grpc.WithResolvers(r),
grpc.WithBalancerName(b.Name()))
if err != nil {
t.Fatalf("failed to dial: %v", err)
@ -433,8 +432,7 @@ func (s) TestAddressAttributesInNewSubConn(t *testing.T) {
stub.Register(attrBalancerName, bf)
t.Logf("Registered balancer %s...", attrBalancerName)
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
t.Logf("Registered manual resolver with scheme %s...", r.Scheme())
lis, err := net.Listen("tcp", "localhost:0")
@ -451,6 +449,7 @@ func (s) TestAddressAttributesInNewSubConn(t *testing.T) {
creds := &attrTransportCreds{}
dopts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
grpc.WithResolvers(r),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{ "loadBalancingConfig": [{"%v": {}}] }`, attrBalancerName)),
}
cc, err := grpc.Dial(r.Scheme()+":///test.server", dopts...)
@ -517,10 +516,9 @@ func (s) TestServersSwap(t *testing.T) {
defer cleanup()
// Initialize client
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: addr1}}})
cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithInsecure())
cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithInsecure(), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("Error creating client: %v", err)
}
@ -570,15 +568,14 @@ func (s) TestEmptyAddrs(t *testing.T) {
go s.Serve(lis)
// Initialize pickfirst client
pfr, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
pfr := manual.NewBuilderWithScheme("whatever")
pfrnCalled := grpcsync.NewEvent()
pfr.ResolveNowCallback = func(resolver.ResolveNowOptions) {
pfrnCalled.Fire()
}
pfr.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})
pfcc, err := grpc.DialContext(ctx, pfr.Scheme()+":///", grpc.WithInsecure())
pfcc, err := grpc.DialContext(ctx, pfr.Scheme()+":///", grpc.WithInsecure(), grpc.WithResolvers(pfr))
if err != nil {
t.Fatalf("Error creating client: %v", err)
}
@ -596,15 +593,15 @@ func (s) TestEmptyAddrs(t *testing.T) {
<-pfrnCalled.Done()
// Initialize roundrobin client
rrr, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
rrr := manual.NewBuilderWithScheme("whatever")
rrrnCalled := grpcsync.NewEvent()
rrr.ResolveNowCallback = func(resolver.ResolveNowOptions) {
rrrnCalled.Fire()
}
rrr.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: lis.Addr().String()}}})
rrcc, err := grpc.DialContext(ctx, rrr.Scheme()+":///", grpc.WithInsecure(),
rrcc, err := grpc.DialContext(ctx, rrr.Scheme()+":///", grpc.WithInsecure(), grpc.WithResolvers(rrr),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{ "loadBalancingConfig": [{"%v": {}}] }`, roundrobin.Name)))
if err != nil {
t.Fatalf("Error creating client: %v", err)
@ -660,10 +657,9 @@ func (s) TestWaitForReady(t *testing.T) {
go s.Serve(lis)
// Initialize client
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithInsecure())
cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithInsecure(), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("Error creating client: %v", err)
}

Просмотреть файл

@ -209,12 +209,11 @@ func (s) TestCZNestedChannelRegistrationAndDeletion(t *testing.T) {
// avoid calling API to set balancer type, which will void service config's change of balancer.
e.balancer = ""
te := newTest(t, e)
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
resolvedAddrs := []resolver.Address{{Addr: "127.0.0.1:0", Type: resolver.GRPCLB, ServerName: "grpclb.server"}}
r.InitialState(resolver.State{Addresses: resolvedAddrs})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
if err := verifyResultWithDelay(func() (bool, error) {
@ -255,14 +254,13 @@ func (s) TestCZClientSubChannelSocketRegistrationAndDeletion(t *testing.T) {
te := newTest(t, e)
var svrAddrs []resolver.Address
te.startServers(&testServer{security: e.security}, num)
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
for _, a := range te.srvAddrs {
svrAddrs = append(svrAddrs, resolver.Address{Addr: a})
}
r.InitialState(resolver.State{Addresses: svrAddrs})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
// Here, we just wait for all sockets to be up. In the future, if we implement
// IDLE, we may need to make several rpc calls to create the sockets.
@ -507,14 +505,13 @@ func (s) TestCZChannelMetrics(t *testing.T) {
te.maxClientSendMsgSize = newInt(8)
var svrAddrs []resolver.Address
te.startServers(&testServer{security: e.security}, num)
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
for _, a := range te.srvAddrs {
svrAddrs = append(svrAddrs, resolver.Address{Addr: a})
}
r.InitialState(resolver.State{Addresses: svrAddrs})
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
tc := testpb.NewTestServiceClient(cc)
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
@ -1397,12 +1394,11 @@ func (s) TestCZChannelTraceCreationDeletion(t *testing.T) {
// avoid calling API to set balancer type, which will void service config's change of balancer.
e.balancer = ""
te := newTest(t, e)
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
resolvedAddrs := []resolver.Address{{Addr: "127.0.0.1:0", Type: resolver.GRPCLB, ServerName: "grpclb.server"}}
r.InitialState(resolver.State{Addresses: resolvedAddrs})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
var nestedConn int64
if err := verifyResultWithDelay(func() (bool, error) {
@ -1472,11 +1468,10 @@ func (s) TestCZSubChannelTraceCreationDeletion(t *testing.T) {
e := tcpClearRREnv
te := newTest(t, e)
te.startServer(&testServer{security: e.security})
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: te.srvAddr}}})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
var subConn int64
// Here, we just wait for all sockets to be up. In the future, if we implement
@ -1566,12 +1561,11 @@ func (s) TestCZChannelAddressResolutionChange(t *testing.T) {
e.balancer = ""
te := newTest(t, e)
te.startServer(&testServer{security: e.security})
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
addrs := []resolver.Address{{Addr: te.srvAddr}}
r.InitialState(resolver.State{Addresses: addrs})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
var cid int64
// Here, we just wait for all sockets to be up. In the future, if we implement
@ -1670,15 +1664,14 @@ func (s) TestCZSubChannelPickedNewAddress(t *testing.T) {
e.balancer = ""
te := newTest(t, e)
te.startServers(&testServer{security: e.security}, 3)
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
var svrAddrs []resolver.Address
for _, a := range te.srvAddrs {
svrAddrs = append(svrAddrs, resolver.Address{Addr: a})
}
r.InitialState(resolver.State{Addresses: svrAddrs})
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
tc := testpb.NewTestServiceClient(cc)
// make sure the connection is up
@ -1730,11 +1723,10 @@ func (s) TestCZSubChannelConnectivityState(t *testing.T) {
e := tcpClearRREnv
te := newTest(t, e)
te.startServer(&testServer{security: e.security})
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: te.srvAddr}}})
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
tc := testpb.NewTestServiceClient(cc)
// make sure the connection is up
@ -1825,11 +1817,10 @@ func (s) TestCZChannelConnectivityState(t *testing.T) {
e := tcpClearRREnv
te := newTest(t, e)
te.startServer(&testServer{security: e.security})
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: te.srvAddr}}})
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
tc := testpb.NewTestServiceClient(cc)
// make sure the connection is up
@ -1886,12 +1877,11 @@ func (s) TestCZTraceOverwriteChannelDeletion(t *testing.T) {
te := newTest(t, e)
channelz.SetMaxTraceEntry(1)
defer channelz.ResetMaxTraceEntryToDefault()
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
resolvedAddrs := []resolver.Address{{Addr: "127.0.0.1:0", Type: resolver.GRPCLB, ServerName: "grpclb.server"}}
r.InitialState(resolver.State{Addresses: resolvedAddrs})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
var nestedConn int64
if err := verifyResultWithDelay(func() (bool, error) {
@ -1950,11 +1940,10 @@ func (s) TestCZTraceOverwriteSubChannelDeletion(t *testing.T) {
channelz.SetMaxTraceEntry(1)
defer channelz.ResetMaxTraceEntryToDefault()
te.startServer(&testServer{security: e.security})
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: te.srvAddr}}})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
defer te.tearDown()
var subConn int64
// Here, we just wait for all sockets to be up. In the future, if we implement
@ -2009,11 +1998,10 @@ func (s) TestCZTraceTopChannelDeletionTraceClear(t *testing.T) {
e := tcpClearRREnv
te := newTest(t, e)
te.startServer(&testServer{security: e.security})
r, cleanup := manual.GenerateAndRegisterManualResolver()
defer cleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{{Addr: te.srvAddr}}})
te.resolverScheme = r.Scheme()
te.clientConn()
te.clientConn(grpc.WithResolvers(r))
var subConn int64
// Here, we just wait for all sockets to be up. In the future, if we implement
// IDLE, we may need to make several rpc calls to create the sockets.

Просмотреть файл

@ -411,10 +411,9 @@ func (s) TestCredsHandshakeAuthority(t *testing.T) {
go s.Serve(lis)
defer s.Stop()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred))
cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
}
@ -452,10 +451,9 @@ func (s) TestCredsHandshakeServerNameAuthority(t *testing.T) {
go s.Serve(lis)
defer s.Stop()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred))
cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred), grpc.WithResolvers(r))
if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
}

Просмотреть файл

@ -1435,11 +1435,10 @@ func newDuration(b time.Duration) (a *time.Duration) {
func (s) TestGetMethodConfig(t *testing.T) {
te := testServiceConfigSetup(t, tcpClearRREnv)
defer te.tearDown()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
addrs := []resolver.Address{{Addr: te.srvAddr}}
r.UpdateState(resolver.State{
Addresses: addrs,
@ -1521,12 +1520,11 @@ func (s) TestGetMethodConfig(t *testing.T) {
func (s) TestServiceConfigWaitForReady(t *testing.T) {
te := testServiceConfigSetup(t, tcpClearRREnv)
defer te.tearDown()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
// Case1: Client API set failfast to be false, and service config set wait_for_ready to be false, Client API should win, and the rpc will wait until deadline exceeds.
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
addrs := []resolver.Address{{Addr: te.srvAddr}}
r.UpdateState(resolver.State{
Addresses: addrs,
@ -1610,12 +1608,11 @@ func (s) TestServiceConfigWaitForReady(t *testing.T) {
func (s) TestServiceConfigTimeout(t *testing.T) {
te := testServiceConfigSetup(t, tcpClearRREnv)
defer te.tearDown()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
// Case1: Client API sets timeout to be 1ns and ServiceConfig sets timeout to be 1hr. Timeout should be 1ns (min of 1ns and 1hr) and the rpc will wait until deadline exceeds.
te.resolverScheme = r.Scheme()
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
addrs := []resolver.Address{{Addr: te.srvAddr}}
r.UpdateState(resolver.State{
Addresses: addrs,
@ -1708,8 +1705,7 @@ func (s) TestServiceConfigTimeout(t *testing.T) {
func (s) TestServiceConfigMaxMsgSize(t *testing.T) {
e := tcpClearRREnv
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
// Setting up values and objects shared across all test cases.
const smallSize = 1
@ -1736,7 +1732,7 @@ func (s) TestServiceConfigMaxMsgSize(t *testing.T) {
te1.resolverScheme = r.Scheme()
te1.nonBlockingDial = true
te1.startServer(&testServer{security: e.security})
cc1 := te1.clientConn()
cc1 := te1.clientConn(grpc.WithResolvers(r))
addrs := []resolver.Address{{Addr: te1.srvAddr}}
sc := parseCfg(r, `{
@ -1827,7 +1823,7 @@ func (s) TestServiceConfigMaxMsgSize(t *testing.T) {
te2.startServer(&testServer{security: e.security})
defer te2.tearDown()
cc2 := te2.clientConn()
cc2 := te2.clientConn(grpc.WithResolvers(r))
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: te2.srvAddr}}, ServiceConfig: sc})
tc = testpb.NewTestServiceClient(cc2)
@ -1888,7 +1884,7 @@ func (s) TestServiceConfigMaxMsgSize(t *testing.T) {
te3.startServer(&testServer{security: e.security})
defer te3.tearDown()
cc3 := te3.clientConn()
cc3 := te3.clientConn(grpc.WithResolvers(r))
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: te3.srvAddr}}, ServiceConfig: sc})
tc = testpb.NewTestServiceClient(cc3)
@ -1971,12 +1967,11 @@ func (s) TestStreamingRPCWithTimeoutInServiceConfigRecv(t *testing.T) {
te := testServiceConfigSetup(t, tcpClearRREnv)
te.startServer(&testServer{security: tcpClearRREnv.security})
defer te.tearDown()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
te.resolverScheme = r.Scheme()
te.nonBlockingDial = true
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
tc := testpb.NewTestServiceClient(cc)
r.UpdateState(resolver.State{
@ -5017,9 +5012,8 @@ func (ss *stubServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallSer
// Start starts the server and creates a client connected to it.
func (ss *stubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption) error {
r, cleanup := manual.GenerateAndRegisterManualResolver()
r := manual.NewBuilderWithScheme("whatever")
ss.r = r
ss.cleanups = append(ss.cleanups, cleanup)
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
@ -5036,7 +5030,7 @@ func (ss *stubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption)
target := ss.r.Scheme() + ":///" + ss.addr
opts := append([]grpc.DialOption{grpc.WithInsecure()}, dopts...)
opts := append([]grpc.DialOption{grpc.WithInsecure(), grpc.WithResolvers(r)}, dopts...)
cc, err := grpc.Dial(target, opts...)
if err != nil {
return fmt.Errorf("grpc.Dial(%q) = %v", target, err)
@ -6693,12 +6687,11 @@ func (s) TestGoAwayThenClose(t *testing.T) {
testpb.RegisterTestServiceServer(s2, ts)
go s2.Serve(lis2)
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
r.InitialState(resolver.State{Addresses: []resolver.Address{
{Addr: lis1.Addr().String()},
}})
cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithInsecure())
cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithInsecure())
if err != nil {
t.Fatalf("Error creating client: %v", err)
}
@ -6763,12 +6756,11 @@ func (s) TestRPCWaitsForResolver(t *testing.T) {
te := testServiceConfigSetup(t, tcpClearRREnv)
te.startServer(&testServer{security: tcpClearRREnv.security})
defer te.tearDown()
r, rcleanup := manual.GenerateAndRegisterManualResolver()
defer rcleanup()
r := manual.NewBuilderWithScheme("whatever")
te.resolverScheme = r.Scheme()
te.nonBlockingDial = true
cc := te.clientConn()
cc := te.clientConn(grpc.WithResolvers(r))
tc := testpb.NewTestServiceClient(cc)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)

Просмотреть файл

@ -152,19 +152,19 @@ type clientConfig struct {
}
func setupClient(c *clientConfig) (cc *grpc.ClientConn, r *manual.Resolver, deferFunc func(), err error) {
r, rcleanup := manual.GenerateAndRegisterManualResolver()
r = manual.NewBuilderWithScheme("whatever")
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure(), grpc.WithBalancerName(c.balancerName))
opts = append(opts, grpc.WithInsecure(), grpc.WithResolvers(r), grpc.WithBalancerName(c.balancerName))
if c.testHealthCheckFuncWrapper != nil {
opts = append(opts, internal.WithHealthCheckFunc.(func(internal.HealthChecker) grpc.DialOption)(c.testHealthCheckFuncWrapper))
}
opts = append(opts, c.extraDialOption...)
cc, err = grpc.Dial(r.Scheme()+":///test.server", opts...)
if err != nil {
rcleanup()
return nil, nil, nil, fmt.Errorf("dial failed due to err: %v", err)
}
return cc, r, func() { cc.Close(); rcleanup() }, nil
return cc, r, func() { cc.Close() }, nil
}
func (s) TestHealthCheckWatchStateChange(t *testing.T) {