Skip to content

Commit

Permalink
test(transport): add transport Listener test (#1735)
Browse files Browse the repository at this point in the history
* test(tansport): add transport Listener test
  • Loading branch information
daemon365 committed Dec 31, 2021
1 parent 3625634 commit 7f003a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
8 changes: 8 additions & 0 deletions transport/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"net"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
22 changes: 16 additions & 6 deletions transport/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 := &registry.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 {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions transport/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}

0 comments on commit 7f003a8

Please sign in to comment.