Skip to content

Commit

Permalink
Change use of Endpoint field to Endpoint() method
Browse files Browse the repository at this point in the history
This is based on the upstream breaking-change in v1.53
grpc/grpc-go#5852
  • Loading branch information
mcpherrinm committed Mar 9, 2023
1 parent 276b2d2 commit 29a1c20
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion grpc/internal/resolver/dns/dns_resolver.go
Expand Up @@ -111,7 +111,7 @@ type srvBuilder struct {
// Build creates and starts a DNS resolver that watches the name resolution of the target.
func (b *srvBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
var names []name
for _, i := range strings.Split(target.Endpoint, ",") {
for _, i := range strings.Split(target.Endpoint(), ",") {
service, domain, err := parseServiceDomain(i)
if err != nil {
return nil, err
Expand Down
21 changes: 13 additions & 8 deletions grpc/internal/resolver/dns/dns_resolver_test.go
Expand Up @@ -225,7 +225,7 @@ func testDNSResolver(t *testing.T) {
for _, a := range tests {
b := NewDefaultSRVBuilder()
cc := &testClientConn{target: a.target}
r, err := b.Build(resolver.Target{Endpoint: a.target}, cc, resolver.BuildOptions{})
r, err := b.Build(resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("scheme:///%s", a.target))}, cc, resolver.BuildOptions{})
if err != nil {
t.Fatalf("%v\n", err)
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestDNSResolverExponentialBackoff(t *testing.T) {
cc := &testClientConn{target: target}
// Cause ClientConn to return an error.
cc.updateStateErr = balancer.ErrBadResolverState
r, err := b.Build(resolver.Target{Endpoint: target}, cc, resolver.BuildOptions{})
r, err := b.Build(resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("scheme:///%s", target))}, cc, resolver.BuildOptions{})
if err != nil {
t.Fatalf("Error building resolver for target %v: %v", target, err)
}
Expand Down Expand Up @@ -397,7 +397,7 @@ func testDNSResolveNow(t *testing.T) {
for _, a := range tests {
b := NewDefaultSRVBuilder()
cc := &testClientConn{target: a.target}
r, err := b.Build(resolver.Target{Endpoint: a.target}, cc, resolver.BuildOptions{})
r, err := b.Build(resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("scheme:///%s", a.target))}, cc, resolver.BuildOptions{})
if err != nil {
t.Fatalf("%v\n", err)
}
Expand Down Expand Up @@ -448,7 +448,7 @@ func TestDNSResolverRetry(t *testing.T) {
b := NewDefaultSRVBuilder()
target := "foo.ipv4.single.fake"
cc := &testClientConn{target: target}
r, err := b.Build(resolver.Target{Endpoint: target}, cc, resolver.BuildOptions{})
r, err := b.Build(resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("scheme:///%s", target))}, cc, resolver.BuildOptions{})
if err != nil {
t.Fatalf("%v\n", err)
}
Expand Down Expand Up @@ -586,9 +586,14 @@ func TestCustomAuthority(t *testing.T) {
}
}

mockEndpointTarget := "foo.bar.com"
b := NewDefaultSRVBuilder()
cc := &testClientConn{target: "foo.bar.com", errChan: make(chan error, 1)}
r, err := b.Build(resolver.Target{Endpoint: "foo.bar.com", Authority: a.authority}, cc, resolver.BuildOptions{})
cc := &testClientConn{target: mockEndpointTarget, errChan: make(chan error, 1)}
target := resolver.Target{
Authority: a.authority,
URL: *testutils.MustParseURL(fmt.Sprintf("scheme://%s/%s", a.authority, mockEndpointTarget)),
}
r, err := b.Build(target, cc, resolver.BuildOptions{})

if err == nil {
r.Close()
Expand Down Expand Up @@ -643,7 +648,7 @@ func TestRateLimitedResolve(t *testing.T) {
b := NewDefaultSRVBuilder()
cc := &testClientConn{target: target}

r, err := b.Build(resolver.Target{Endpoint: target}, cc, resolver.BuildOptions{})
r, err := b.Build(resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("scheme:///%s", target))}, cc, resolver.BuildOptions{})
if err != nil {
t.Fatalf("resolver.Build() returned error: %v\n", err)
}
Expand Down Expand Up @@ -752,7 +757,7 @@ func TestReportError(t *testing.T) {
cc := &testClientConn{target: target, errChan: make(chan error)}
totalTimesCalledError := 0
b := NewDefaultSRVBuilder()
r, err := b.Build(resolver.Target{Endpoint: target}, cc, resolver.BuildOptions{})
r, err := b.Build(resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("scheme:///%s", target))}, cc, resolver.BuildOptions{})
if err != nil {
t.Fatalf("Error building resolver for target %v: %v", target, err)
}
Expand Down
34 changes: 34 additions & 0 deletions grpc/internal/testutils/parse_url.go
@@ -0,0 +1,34 @@
/*
*
* Copyright 2023 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 testutils

import (
"fmt"
"net/url"
)

// MustParseURL attempts to parse the provided target using url.Parse()
// and panics if parsing fails.
func MustParseURL(target string) *url.URL {
u, err := url.Parse(target)
if err != nil {
panic(fmt.Sprintf("Error parsing target(%s): %v", target, err))
}
return u
}
2 changes: 1 addition & 1 deletion grpc/resolver.go
Expand Up @@ -23,7 +23,7 @@ func newStaticBuilder() resolver.Builder {
// which implements the `resolver.Resolver` interface.
func (sb *staticBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
var resolverAddrs []resolver.Address
for _, address := range strings.Split(target.Endpoint, ",") {
for _, address := range strings.Split(target.Endpoint(), ",") {
parsedAddress, err := parseResolverIPAddress(address)
if err != nil {
return nil, err
Expand Down

0 comments on commit 29a1c20

Please sign in to comment.