fix: remove deprecated dependencies (#1837)
* fix: remove deprecated dependencies * backup * fix test error
This commit is contained in:
@@ -3,15 +3,16 @@ package internal
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
type directBuilder struct{}
|
||||
|
||||
func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
|
||||
func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (
|
||||
resolver.Resolver, error) {
|
||||
var addrs []resolver.Address
|
||||
endpoints := strings.FieldsFunc(target.Endpoint, func(r rune) bool {
|
||||
endpoints := strings.FieldsFunc(targets.GetEndpoints(target), func(r rune) bool {
|
||||
return r == EndpointSepChar
|
||||
})
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -31,9 +32,11 @@ func TestDirectBuilder_Build(t *testing.T) {
|
||||
}
|
||||
var b directBuilder
|
||||
cc := new(mockedClientConn)
|
||||
_, err := b.Build(resolver.Target{
|
||||
Scheme: DirectScheme,
|
||||
Endpoint: strings.Join(servers, ","),
|
||||
target := fmt.Sprintf("%s:///%s", DirectScheme, strings.Join(servers, ","))
|
||||
uri, err := url.Parse(target)
|
||||
assert.Nil(t, err)
|
||||
_, err = b.Build(resolver.Target{
|
||||
URL: *uri,
|
||||
}, cc, resolver.BuildOptions{})
|
||||
assert.Nil(t, err)
|
||||
size := mathx.MinInt(test, subsetSize)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/discov"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
@@ -12,10 +13,10 @@ type discovBuilder struct{}
|
||||
|
||||
func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (
|
||||
resolver.Resolver, error) {
|
||||
hosts := strings.FieldsFunc(target.Authority, func(r rune) bool {
|
||||
hosts := strings.FieldsFunc(targets.GetAuthority(target), func(r rune) bool {
|
||||
return r == EndpointSepChar
|
||||
})
|
||||
sub, err := discov.NewSubscriber(hosts, target.Endpoint)
|
||||
sub, err := discov.NewSubscriber(hosts, targets.GetEndpoints(target))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc/resolver/internal/targets"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
@@ -25,14 +26,15 @@ type Service struct {
|
||||
// ParseTarget parses the resolver.Target.
|
||||
func ParseTarget(target resolver.Target) (Service, error) {
|
||||
var service Service
|
||||
service.Namespace = target.Authority
|
||||
service.Namespace = targets.GetAuthority(target)
|
||||
if len(service.Namespace) == 0 {
|
||||
service.Namespace = defaultNamespace
|
||||
}
|
||||
|
||||
segs := strings.SplitN(target.Endpoint, colon, 2)
|
||||
endpoints := targets.GetEndpoints(target)
|
||||
segs := strings.SplitN(endpoints, colon, 2)
|
||||
if len(segs) < 2 {
|
||||
return emptyService, fmt.Errorf("bad endpoint: %s", target.Endpoint)
|
||||
return emptyService, fmt.Errorf("bad endpoint: %s", endpoints)
|
||||
}
|
||||
|
||||
service.Name = segs[0]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package kube
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -10,17 +11,13 @@ import (
|
||||
func TestParseTarget(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input resolver.Target
|
||||
input string
|
||||
expect Service
|
||||
hasErr bool
|
||||
}{
|
||||
{
|
||||
name: "normal case",
|
||||
input: resolver.Target{
|
||||
Scheme: "k8s",
|
||||
Authority: "ns1",
|
||||
Endpoint: "my-svc:8080",
|
||||
},
|
||||
name: "normal case",
|
||||
input: "k8s://ns1/my-svc:8080",
|
||||
expect: Service{
|
||||
Namespace: "ns1",
|
||||
Name: "my-svc",
|
||||
@@ -28,12 +25,8 @@ func TestParseTarget(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "normal case",
|
||||
input: resolver.Target{
|
||||
Scheme: "k8s",
|
||||
Authority: "",
|
||||
Endpoint: "my-svc:8080",
|
||||
},
|
||||
name: "normal case",
|
||||
input: "k8s:///my-svc:8080",
|
||||
expect: Service{
|
||||
Namespace: defaultNamespace,
|
||||
Name: "my-svc",
|
||||
@@ -41,37 +34,27 @@ func TestParseTarget(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no port",
|
||||
input: resolver.Target{
|
||||
Scheme: "k8s",
|
||||
Authority: "ns1",
|
||||
Endpoint: "my-svc:",
|
||||
},
|
||||
name: "no port",
|
||||
input: "k8s://ns1/my-svc:",
|
||||
hasErr: true,
|
||||
},
|
||||
{
|
||||
name: "no port, no colon",
|
||||
input: resolver.Target{
|
||||
Scheme: "k8s",
|
||||
Authority: "ns1",
|
||||
Endpoint: "my-svc",
|
||||
},
|
||||
name: "no port, no colon",
|
||||
input: "k8s://ns1/my-svc",
|
||||
hasErr: true,
|
||||
},
|
||||
{
|
||||
name: "bad port",
|
||||
input: resolver.Target{
|
||||
Scheme: "k8s",
|
||||
Authority: "ns1",
|
||||
Endpoint: "my-svc:800a",
|
||||
},
|
||||
name: "bad port",
|
||||
input: "k8s://ns1/my-svc:800a",
|
||||
hasErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
svc, err := ParseTarget(test.input)
|
||||
uri, err := url.Parse(test.input)
|
||||
assert.Nil(t, err)
|
||||
svc, err := ParseTarget(resolver.Target{URL: *uri})
|
||||
if test.hasErr {
|
||||
assert.NotNil(t, err)
|
||||
} else {
|
||||
|
||||
19
zrpc/resolver/internal/targets/endpoints.go
Normal file
19
zrpc/resolver/internal/targets/endpoints.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package targets
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
const slashSeparator = "/"
|
||||
|
||||
// GetAuthority returns the authority of the target.
|
||||
func GetAuthority(target resolver.Target) string {
|
||||
return target.URL.Host
|
||||
}
|
||||
|
||||
// GetEndpoints returns the endpoints from the given target.
|
||||
func GetEndpoints(target resolver.Target) string {
|
||||
return strings.Trim(target.URL.Path, slashSeparator)
|
||||
}
|
||||
89
zrpc/resolver/internal/targets/endpoints_test.go
Normal file
89
zrpc/resolver/internal/targets/endpoints_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package targets
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
func TestGetAuthority(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "test",
|
||||
url: "direct://my_authority/localhost",
|
||||
want: "my_authority",
|
||||
},
|
||||
{
|
||||
name: "test with port",
|
||||
url: "direct://my_authority/localhost:8080",
|
||||
want: "my_authority",
|
||||
},
|
||||
{
|
||||
name: "test with multiple hosts",
|
||||
url: "direct://my_authority1,my_authority2/localhost,localhost",
|
||||
want: "my_authority1,my_authority2",
|
||||
},
|
||||
{
|
||||
name: "test with multiple hosts with port",
|
||||
url: "direct://my_authority1:3000,my_authority2:3001/localhost:8080,localhost:8081",
|
||||
want: "my_authority1:3000,my_authority2:3001",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
uri, err := url.Parse(test.url)
|
||||
assert.Nil(t, err)
|
||||
target := resolver.Target{
|
||||
URL: *uri,
|
||||
}
|
||||
assert.Equal(t, test.want, GetAuthority(target))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEndpoints(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "test",
|
||||
url: "direct:///localhost",
|
||||
want: "localhost",
|
||||
},
|
||||
{
|
||||
name: "test with port",
|
||||
url: "direct:///localhost:8080",
|
||||
want: "localhost:8080",
|
||||
},
|
||||
{
|
||||
name: "test with multiple hosts",
|
||||
url: "direct:///localhost,localhost",
|
||||
want: "localhost,localhost",
|
||||
},
|
||||
{
|
||||
name: "test with multiple hosts with port",
|
||||
url: "direct:///localhost:8080,localhost:8081",
|
||||
want: "localhost:8080,localhost:8081",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
uri, err := url.Parse(test.url)
|
||||
assert.Nil(t, err)
|
||||
target := resolver.Target{
|
||||
URL: *uri,
|
||||
}
|
||||
assert.Equal(t, test.want, GetEndpoints(target))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user