fix http resvoler test case (#1496)

pull/1498/head
longxboy 3 years ago committed by GitHub
parent 423b60b330
commit 3cbe4755ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      transport/http/resolver_test.go

@ -3,7 +3,10 @@ package http
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"strconv"
"testing" "testing"
"time"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/selector" "github.com/go-kratos/kratos/v2/selector"
@ -36,23 +39,26 @@ type mockRebalancer struct{}
func (m *mockRebalancer) Apply(nodes []selector.Node) {} func (m *mockRebalancer) Apply(nodes []selector.Node) {}
type mockDiscoverys struct{} type mockDiscoverys struct {
isSecure bool
}
func (*mockDiscoverys) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) { func (d *mockDiscoverys) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) {
return nil, nil return nil, nil
} }
func (*mockDiscoverys) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { func (d *mockDiscoverys) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) {
return &mockWatch{}, nil return &mockWatch{isSecure: d.isSecure}, nil
} }
type mockWatch struct { type mockWatch struct {
isSecure bool
count int count int
} }
func (m *mockWatch) Next() ([]*registry.ServiceInstance, error) { func (m *mockWatch) Next() ([]*registry.ServiceInstance, error) {
if m.count == 0 {
m.count++ m.count++
if m.count == 1 {
return nil, errors.New("mock test error") return nil, errors.New("mock test error")
} }
instance := &registry.ServiceInstance{ instance := &registry.ServiceInstance{
@ -60,7 +66,10 @@ func (m *mockWatch) Next() ([]*registry.ServiceInstance, error) {
Name: "kratos", Name: "kratos",
Version: "v1", Version: "v1",
Metadata: map[string]string{}, Metadata: map[string]string{},
Endpoints: []string{}, Endpoints: []string{fmt.Sprintf("http://127.0.0.1:9001?isSecure=%s", strconv.FormatBool(m.isSecure))},
}
if m.count > 3 {
time.Sleep(time.Millisecond * 500)
} }
return []*registry.ServiceInstance{instance}, nil return []*registry.ServiceInstance{instance}, nil
} }
@ -73,10 +82,10 @@ func TestResolver(t *testing.T) {
ta := &Target{ ta := &Target{
Scheme: "http", Scheme: "http",
Authority: "", Authority: "",
Endpoint: "http://127.0.0.1:9001", Endpoint: "discovery://helloworld",
} }
_, err := newResolver(context.Background(), &mockDiscoverys{}, ta, &mockRebalancer{}, false, false) _, err := newResolver(context.Background(), &mockDiscoverys{true}, ta, &mockRebalancer{}, false, false)
assert.Nil(t, err) assert.Nil(t, err)
_, err = newResolver(context.Background(), &mockDiscoverys{}, ta, &mockRebalancer{}, true, false) _, err = newResolver(context.Background(), &mockDiscoverys{false}, ta, &mockRebalancer{}, true, true)
assert.Nil(t, err) assert.Nil(t, err)
} }

Loading…
Cancel
Save