From 7f003a8742eeb529e2d85177cb2296bdfd44d77e Mon Sep 17 00:00:00 2001 From: haiyux <99347745@qq.com> Date: Fri, 31 Dec 2021 11:15:39 +0800 Subject: [PATCH] test(transport): add transport Listener test (#1735) * test(tansport): add transport Listener test --- transport/grpc/server_test.go | 8 ++++++++ transport/http/client_test.go | 22 ++++++++++++++++------ transport/http/server_test.go | 8 ++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/transport/grpc/server_test.go b/transport/grpc/server_test.go index 14079ce4b4c..29f0b2246b4 100644 --- a/transport/grpc/server_test.go +++ b/transport/grpc/server_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "fmt" + "net" "net/url" "strings" "testing" @@ -214,3 +215,10 @@ func TestServer_unaryServerInterceptor(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "hi", rv.(*testResp).Data) } + +func TestListener(t *testing.T) { + lis := &net.TCPListener{} + s := &Server{} + Listener(lis)(s) + assert.Equal(t, s.lis, lis) +} diff --git a/transport/http/client_test.go b/transport/http/client_test.go index dd5af2395e3..2da84f75fdc 100644 --- a/transport/http/client_test.go +++ b/transport/http/client_test.go @@ -5,12 +5,14 @@ import ( "context" "crypto/tls" "encoding/json" + "fmt" "io" nethttp "net/http" + "strconv" "testing" "time" - "github.com/go-kratos/kratos/v2/errors" + kratosErrors "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/registry" "github.com/stretchr/testify/assert" @@ -116,8 +118,16 @@ func (*mockDiscovery) Watch(ctx context.Context, serviceName string) (registry.W type mockWatcher struct{} -func (*mockWatcher) Next() ([]*registry.ServiceInstance, error) { - return nil, nil +func (m *mockWatcher) Next() ([]*registry.ServiceInstance, error) { + instance := ®istry.ServiceInstance{ + ID: "1", + Name: "kratos", + Version: "v1", + Metadata: map[string]string{}, + Endpoints: []string{fmt.Sprintf("http://127.0.0.1:9001?isSecure=%s", strconv.FormatBool(false))}, + } + time.Sleep(time.Millisecond * 500) + return []*registry.ServiceInstance{instance}, nil } func (*mockWatcher) Stop() error { @@ -202,9 +212,9 @@ func TestDefaultErrorDecoder(t *testing.T) { } err2 := DefaultErrorDecoder(context.TODO(), resp2) assert.Error(t, err2) - assert.Equal(t, int32(500), err2.(*errors.Error).GetCode()) - assert.Equal(t, "hi", err2.(*errors.Error).GetMessage()) - assert.Equal(t, "FOO", err2.(*errors.Error).GetReason()) + assert.Equal(t, int32(500), err2.(*kratosErrors.Error).GetCode()) + assert.Equal(t, "hi", err2.(*kratosErrors.Error).GetMessage()) + assert.Equal(t, "FOO", err2.(*kratosErrors.Error).GetReason()) } func TestCodecForResponse(t *testing.T) { diff --git a/transport/http/server_test.go b/transport/http/server_test.go index b5238bb061e..f41dc042c41 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "net" "net/http" "strings" "testing" @@ -236,3 +237,10 @@ func TestTLSConfig(t *testing.T) { TLSConfig(v)(o) assert.Equal(t, v, o.tlsConf) } + +func TestListener(t *testing.T) { + lis := &net.TCPListener{} + s := &Server{} + Listener(lis)(s) + assert.Equal(t, s.lis, lis) +}