123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package zrpc
- import (
- "testing"
- "time"
- "github.com/alicebob/miniredis/v2"
- "github.com/stretchr/testify/assert"
- "github.com/wuntsong-org/go-zero-plus/core/discov"
- "github.com/wuntsong-org/go-zero-plus/core/logx"
- "github.com/wuntsong-org/go-zero-plus/core/service"
- "github.com/wuntsong-org/go-zero-plus/core/stat"
- "github.com/wuntsong-org/go-zero-plus/core/stores/redis"
- "github.com/wuntsong-org/go-zero-plus/zrpc/internal"
- "github.com/wuntsong-org/go-zero-plus/zrpc/internal/serverinterceptors"
- "google.golang.org/grpc"
- )
- func TestServer_setupInterceptors(t *testing.T) {
- rds, err := miniredis.Run()
- assert.NoError(t, err)
- defer rds.Close()
- server := new(mockedServer)
- conf := RpcServerConf{
- Auth: true,
- Redis: redis.RedisKeyConf{
- RedisConf: redis.RedisConf{
- Host: rds.Addr(),
- Type: redis.NodeType,
- },
- Key: "foo",
- },
- CpuThreshold: 10,
- Timeout: 100,
- Middlewares: ServerMiddlewaresConf{
- Trace: true,
- Recover: true,
- Stat: true,
- Prometheus: true,
- Breaker: true,
- },
- MethodTimeouts: []MethodTimeoutConf{
- {
- FullMethod: "/foo",
- Timeout: 5 * time.Second,
- },
- },
- }
- err = setupInterceptors(server, conf, new(stat.Metrics))
- assert.Nil(t, err)
- assert.Equal(t, 3, len(server.unaryInterceptors))
- assert.Equal(t, 1, len(server.streamInterceptors))
- rds.SetError("mock error")
- err = setupInterceptors(server, conf, new(stat.Metrics))
- assert.Error(t, err)
- }
- func TestServer(t *testing.T) {
- DontLogContentForMethod("foo")
- SetServerSlowThreshold(time.Second)
- svr := MustNewServer(RpcServerConf{
- ServiceConf: service.ServiceConf{
- Log: logx.LogConf{
- ServiceName: "foo",
- Mode: "console",
- },
- },
- ListenOn: "localhost:8080",
- Etcd: discov.EtcdConf{},
- Auth: false,
- Redis: redis.RedisKeyConf{},
- StrictControl: false,
- Timeout: 0,
- CpuThreshold: 0,
- Middlewares: ServerMiddlewaresConf{
- Trace: true,
- Recover: true,
- Stat: true,
- Prometheus: true,
- Breaker: true,
- },
- MethodTimeouts: []MethodTimeoutConf{
- {
- FullMethod: "/foo",
- Timeout: time.Second,
- },
- },
- }, func(server *grpc.Server) {
- })
- svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
- svr.AddUnaryInterceptors(serverinterceptors.UnaryRecoverInterceptor)
- svr.AddStreamInterceptors(serverinterceptors.StreamRecoverInterceptor)
- go svr.Start()
- svr.Stop()
- }
- func TestServerError(t *testing.T) {
- _, err := NewServer(RpcServerConf{
- ServiceConf: service.ServiceConf{
- Log: logx.LogConf{
- ServiceName: "foo",
- Mode: "console",
- },
- },
- ListenOn: "localhost:8080",
- Etcd: discov.EtcdConf{
- Hosts: []string{"localhost"},
- },
- Auth: true,
- Redis: redis.RedisKeyConf{},
- Middlewares: ServerMiddlewaresConf{
- Trace: true,
- Recover: true,
- Stat: true,
- Prometheus: true,
- Breaker: true,
- },
- MethodTimeouts: []MethodTimeoutConf{},
- }, func(server *grpc.Server) {
- })
- assert.NotNil(t, err)
- }
- func TestServer_HasEtcd(t *testing.T) {
- svr := MustNewServer(RpcServerConf{
- ServiceConf: service.ServiceConf{
- Log: logx.LogConf{
- ServiceName: "foo",
- Mode: "console",
- },
- },
- ListenOn: "localhost:8080",
- Etcd: discov.EtcdConf{
- Hosts: []string{"notexist"},
- Key: "any",
- },
- Redis: redis.RedisKeyConf{},
- Middlewares: ServerMiddlewaresConf{
- Trace: true,
- Recover: true,
- Stat: true,
- Prometheus: true,
- Breaker: true,
- },
- MethodTimeouts: []MethodTimeoutConf{},
- }, func(server *grpc.Server) {
- })
- svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
- svr.AddUnaryInterceptors(serverinterceptors.UnaryRecoverInterceptor)
- svr.AddStreamInterceptors(serverinterceptors.StreamRecoverInterceptor)
- go svr.Start()
- svr.Stop()
- }
- func TestServer_StartFailed(t *testing.T) {
- svr := MustNewServer(RpcServerConf{
- ServiceConf: service.ServiceConf{
- Log: logx.LogConf{
- ServiceName: "foo",
- Mode: "console",
- },
- },
- ListenOn: "localhost:aaa",
- Middlewares: ServerMiddlewaresConf{
- Trace: true,
- Recover: true,
- Stat: true,
- Prometheus: true,
- Breaker: true,
- },
- }, func(server *grpc.Server) {
- })
- assert.Panics(t, svr.Start)
- }
- type mockedServer struct {
- unaryInterceptors []grpc.UnaryServerInterceptor
- streamInterceptors []grpc.StreamServerInterceptor
- }
- func (m *mockedServer) AddOptions(_ ...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(_ string) {
- }
- func (m *mockedServer) Start(_ internal.RegisterFn) error {
- return nil
- }
|