Kevin Wan пре 4 година
родитељ
комит
ec6132b754
1 измењених фајлова са 150 додато и 0 уклоњено
  1. 150 0
      zrpc/server_test.go

+ 150 - 0
zrpc/server_test.go

@@ -0,0 +1,150 @@
+package zrpc
+
+import (
+	"testing"
+	"time"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/tal-tech/go-zero/core/discov"
+	"github.com/tal-tech/go-zero/core/logx"
+	"github.com/tal-tech/go-zero/core/netx"
+	"github.com/tal-tech/go-zero/core/service"
+	"github.com/tal-tech/go-zero/core/stat"
+	"github.com/tal-tech/go-zero/core/stores/redis"
+	"github.com/tal-tech/go-zero/zrpc/internal"
+	"github.com/tal-tech/go-zero/zrpc/internal/serverinterceptors"
+	"google.golang.org/grpc"
+)
+
+func TestFigureOutListenOn(t *testing.T) {
+	tests := []struct {
+		input  string
+		expect string
+	}{
+		{
+			input:  "192.168.0.5:1234",
+			expect: "192.168.0.5:1234",
+		},
+		{
+			input:  "0.0.0.0:8080",
+			expect: netx.InternalIp() + ":8080",
+		},
+		{
+			input:  ":8080",
+			expect: netx.InternalIp() + ":8080",
+		},
+	}
+
+	for _, test := range tests {
+		val := figureOutListenOn(test.input)
+		assert.Equal(t, test.expect, val)
+	}
+}
+
+func TestServer_setupInterceptors(t *testing.T) {
+	server := new(mockedServer)
+	err := setupInterceptors(server, RpcServerConf{
+		Auth: true,
+		Redis: redis.RedisKeyConf{
+			RedisConf: redis.RedisConf{
+				Host: "any",
+				Type: redis.NodeType,
+			},
+			Key: "foo",
+		},
+		CpuThreshold: 10,
+		Timeout:      100,
+	}, new(stat.Metrics))
+	assert.Nil(t, err)
+	assert.Equal(t, 3, len(server.unaryInterceptors))
+	assert.Equal(t, 1, len(server.streamInterceptors))
+}
+
+func TestServer(t *testing.T) {
+	srv := MustNewServer(RpcServerConf{
+		ServiceConf: service.ServiceConf{
+			Log: logx.LogConf{
+				ServiceName: "foo",
+				Mode:        "console",
+			},
+		},
+		ListenOn:      ":8080",
+		Etcd:          discov.EtcdConf{},
+		Auth:          false,
+		Redis:         redis.RedisKeyConf{},
+		StrictControl: false,
+		Timeout:       0,
+		CpuThreshold:  0,
+	}, func(server *grpc.Server) {
+	})
+	srv.AddOptions(grpc.ConnectionTimeout(time.Hour))
+	srv.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor())
+	srv.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
+	go srv.Start()
+	srv.Stop()
+}
+
+func TestServerError(t *testing.T) {
+	_, err := NewServer(RpcServerConf{
+		ServiceConf: service.ServiceConf{
+			Log: logx.LogConf{
+				ServiceName: "foo",
+				Mode:        "console",
+			},
+		},
+		ListenOn: ":8080",
+		Etcd: discov.EtcdConf{
+			Hosts: []string{"localhost"},
+		},
+		Auth:  true,
+		Redis: redis.RedisKeyConf{},
+	}, func(server *grpc.Server) {
+	})
+	assert.NotNil(t, err)
+}
+
+func TestServer_HasEtcd(t *testing.T) {
+	srv := MustNewServer(RpcServerConf{
+		ServiceConf: service.ServiceConf{
+			Log: logx.LogConf{
+				ServiceName: "foo",
+				Mode:        "console",
+			},
+		},
+		ListenOn: ":8080",
+		Etcd: discov.EtcdConf{
+			Hosts: []string{"notexist"},
+			Key:   "any",
+		},
+		Redis: redis.RedisKeyConf{},
+	}, func(server *grpc.Server) {
+	})
+	srv.AddOptions(grpc.ConnectionTimeout(time.Hour))
+	srv.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor())
+	srv.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
+	go srv.Start()
+	srv.Stop()
+}
+
+type mockedServer struct {
+	unaryInterceptors  []grpc.UnaryServerInterceptor
+	streamInterceptors []grpc.StreamServerInterceptor
+}
+
+func (m *mockedServer) AddOptions(options ...grpc.ServerOption) {
+}
+
+func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
+	m.streamInterceptors = append(m.streamInterceptors, interceptors...)
+}
+
+func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
+	m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
+}
+
+func (m *mockedServer) SetName(s string) {
+}
+
+func (m *mockedServer) Start(register internal.RegisterFn) error {
+	return nil
+}