diff --git a/go.mod b/go.mod index 758f2f8e3..5fabfadc3 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( go.opentelemetry.io/otel v1.0.0-RC3 go.opentelemetry.io/otel/sdk v1.0.0-RC3 go.opentelemetry.io/otel/trace v1.0.0-RC3 + golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c google.golang.org/genproto v0.0.0-20210805201207-89edb61ffb67 google.golang.org/grpc v1.39.1 diff --git a/transport/http/binding/test.pb.go b/internal/testdata/binding/test.pb.go similarity index 99% rename from transport/http/binding/test.pb.go rename to internal/testdata/binding/test.pb.go index b4bd28681..5f2318c7a 100644 --- a/transport/http/binding/test.pb.go +++ b/internal/testdata/binding/test.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.27.1-devel -// protoc v3.17.2 +// protoc-gen-go v1.27.1 +// protoc v3.17.3 // source: test.proto package binding diff --git a/transport/http/binding/test.proto b/internal/testdata/binding/test.proto similarity index 100% rename from transport/http/binding/test.proto rename to internal/testdata/binding/test.proto diff --git a/transport/http/binding/encode_test.go b/transport/http/binding/encode_test.go index bf2ee291e..9cba7bc9f 100644 --- a/transport/http/binding/encode_test.go +++ b/transport/http/binding/encode_test.go @@ -3,34 +3,36 @@ package binding import ( "fmt" "testing" + + "github.com/go-kratos/kratos/v2/internal/testdata/binding" ) func TestProtoPath(t *testing.T) { - url := EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &HelloRequest{Name: "test", Sub: &Sub{Name: "2233!!!"}}, false) + url := EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &binding.HelloRequest{Name: "test", Sub: &binding.Sub{Name: "2233!!!"}}, false) fmt.Println(url) if url != `http://helloworld.Greeter/helloworld/test/sub/2233!!!` { t.Fatalf("proto path not expected!actual: %s ", url) } - url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.naming}", &HelloRequest{Name: "test", Sub: &Sub{Name: "5566!!!"}}, false) + url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.naming}", &binding.HelloRequest{Name: "test", Sub: &binding.Sub{Name: "5566!!!"}}, false) fmt.Println(url) if url != `http://helloworld.Greeter/helloworld/test/sub/5566!!!` { t.Fatalf("proto path not expected!actual: %s ", url) } - url = EncodeURL("http://helloworld.Greeter/helloworld/sub", &HelloRequest{Name: "test", Sub: &Sub{Name: "2233!!!"}}, false) + url = EncodeURL("http://helloworld.Greeter/helloworld/sub", &binding.HelloRequest{Name: "test", Sub: &binding.Sub{Name: "2233!!!"}}, false) fmt.Println(url) if url != `http://helloworld.Greeter/helloworld/sub` { t.Fatalf("proto path not expected!actual: %s ", url) } - url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &HelloRequest{Name: "test"}, false) + url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &binding.HelloRequest{Name: "test"}, false) fmt.Println(url) if url != `http://helloworld.Greeter/helloworld/test/sub/` { t.Fatalf("proto path not expected!actual: %s ", url) } - url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name33}", &HelloRequest{Name: "test"}, false) + url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name33}", &binding.HelloRequest{Name: "test"}, false) fmt.Println(url) if url != `http://helloworld.Greeter/helloworld/test/sub/{sub.name33}` { t.Fatalf("proto path not expected!actual: %s ", url) diff --git a/transport/http/resolver_test.go b/transport/http/resolver_test.go index 641fdfdc5..115982371 100644 --- a/transport/http/resolver_test.go +++ b/transport/http/resolver_test.go @@ -1,8 +1,12 @@ package http import ( + "context" + "errors" "testing" + "github.com/go-kratos/kratos/v2/registry" + "github.com/go-kratos/kratos/v2/selector" "github.com/stretchr/testify/assert" ) @@ -27,3 +31,52 @@ func TestParseTarget(t *testing.T) { assert.Nil(t, err) assert.Equal(t, &Target{Scheme: "https", Authority: "127.0.0.1:8000"}, target) } + +type mockRebalancer struct{} + +func (m *mockRebalancer) Apply(nodes []selector.Node) {} + +type mockDiscoverys struct{} + +func (*mockDiscoverys) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { + return nil, nil +} + +func (*mockDiscoverys) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { + return &mockWatch{}, nil +} + +type mockWatch struct { + count int +} + +func (m *mockWatch) Next() ([]*registry.ServiceInstance, error) { + if m.count == 0 { + m.count++ + return nil, errors.New("mock test error") + } + instance := ®istry.ServiceInstance{ + ID: "1", + Name: "kratos", + Version: "v1", + Metadata: map[string]string{}, + Endpoints: []string{}, + } + return []*registry.ServiceInstance{instance}, nil +} + +func (m *mockWatch) Stop() error { + return nil +} + +func TestResolver(t *testing.T) { + ta := &Target{ + Scheme: "http", + Authority: "", + Endpoint: "http://127.0.0.1:9001", + } + _, err := newResolver(context.Background(), &mockDiscoverys{}, ta, &mockRebalancer{}, false, false) + assert.Nil(t, err) + _, err = newResolver(context.Background(), &mockDiscoverys{}, ta, &mockRebalancer{}, true, false) + assert.Nil(t, err) +}