github.com/lingyao2333/mo-zero@v1.4.1/zrpc/server_test.go (about)

     1  package zrpc
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/lingyao2333/mo-zero/core/discov"
     8  	"github.com/lingyao2333/mo-zero/core/logx"
     9  	"github.com/lingyao2333/mo-zero/core/service"
    10  	"github.com/lingyao2333/mo-zero/core/stat"
    11  	"github.com/lingyao2333/mo-zero/core/stores/redis"
    12  	"github.com/lingyao2333/mo-zero/zrpc/internal"
    13  	"github.com/lingyao2333/mo-zero/zrpc/internal/serverinterceptors"
    14  	"github.com/stretchr/testify/assert"
    15  	"google.golang.org/grpc"
    16  )
    17  
    18  func TestServer_setupInterceptors(t *testing.T) {
    19  	server := new(mockedServer)
    20  	err := setupInterceptors(server, RpcServerConf{
    21  		Auth: true,
    22  		Redis: redis.RedisKeyConf{
    23  			RedisConf: redis.RedisConf{
    24  				Host: "any",
    25  				Type: redis.NodeType,
    26  			},
    27  			Key: "foo",
    28  		},
    29  		CpuThreshold: 10,
    30  		Timeout:      100,
    31  	}, new(stat.Metrics))
    32  	assert.Nil(t, err)
    33  	assert.Equal(t, 3, len(server.unaryInterceptors))
    34  	assert.Equal(t, 1, len(server.streamInterceptors))
    35  }
    36  
    37  func TestServer(t *testing.T) {
    38  	DontLogContentForMethod("foo")
    39  	SetServerSlowThreshold(time.Second)
    40  	svr := MustNewServer(RpcServerConf{
    41  		ServiceConf: service.ServiceConf{
    42  			Log: logx.LogConf{
    43  				ServiceName: "foo",
    44  				Mode:        "console",
    45  			},
    46  		},
    47  		ListenOn:      "localhost:8080",
    48  		Etcd:          discov.EtcdConf{},
    49  		Auth:          false,
    50  		Redis:         redis.RedisKeyConf{},
    51  		StrictControl: false,
    52  		Timeout:       0,
    53  		CpuThreshold:  0,
    54  	}, func(server *grpc.Server) {
    55  	})
    56  	svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
    57  	svr.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor)
    58  	svr.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
    59  	go svr.Start()
    60  	svr.Stop()
    61  }
    62  
    63  func TestServerError(t *testing.T) {
    64  	_, err := NewServer(RpcServerConf{
    65  		ServiceConf: service.ServiceConf{
    66  			Log: logx.LogConf{
    67  				ServiceName: "foo",
    68  				Mode:        "console",
    69  			},
    70  		},
    71  		ListenOn: "localhost:8080",
    72  		Etcd: discov.EtcdConf{
    73  			Hosts: []string{"localhost"},
    74  		},
    75  		Auth:  true,
    76  		Redis: redis.RedisKeyConf{},
    77  	}, func(server *grpc.Server) {
    78  	})
    79  	assert.NotNil(t, err)
    80  }
    81  
    82  func TestServer_HasEtcd(t *testing.T) {
    83  	svr := MustNewServer(RpcServerConf{
    84  		ServiceConf: service.ServiceConf{
    85  			Log: logx.LogConf{
    86  				ServiceName: "foo",
    87  				Mode:        "console",
    88  			},
    89  		},
    90  		ListenOn: "localhost:8080",
    91  		Etcd: discov.EtcdConf{
    92  			Hosts: []string{"notexist"},
    93  			Key:   "any",
    94  		},
    95  		Redis: redis.RedisKeyConf{},
    96  	}, func(server *grpc.Server) {
    97  	})
    98  	svr.AddOptions(grpc.ConnectionTimeout(time.Hour))
    99  	svr.AddUnaryInterceptors(serverinterceptors.UnaryCrashInterceptor)
   100  	svr.AddStreamInterceptors(serverinterceptors.StreamCrashInterceptor)
   101  	go svr.Start()
   102  	svr.Stop()
   103  }
   104  
   105  func TestServer_StartFailed(t *testing.T) {
   106  	svr := MustNewServer(RpcServerConf{
   107  		ServiceConf: service.ServiceConf{
   108  			Log: logx.LogConf{
   109  				ServiceName: "foo",
   110  				Mode:        "console",
   111  			},
   112  		},
   113  		ListenOn: "localhost:aaa",
   114  	}, func(server *grpc.Server) {
   115  	})
   116  
   117  	assert.Panics(t, svr.Start)
   118  }
   119  
   120  type mockedServer struct {
   121  	unaryInterceptors  []grpc.UnaryServerInterceptor
   122  	streamInterceptors []grpc.StreamServerInterceptor
   123  }
   124  
   125  func (m *mockedServer) AddOptions(_ ...grpc.ServerOption) {
   126  }
   127  
   128  func (m *mockedServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
   129  	m.streamInterceptors = append(m.streamInterceptors, interceptors...)
   130  }
   131  
   132  func (m *mockedServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
   133  	m.unaryInterceptors = append(m.unaryInterceptors, interceptors...)
   134  }
   135  
   136  func (m *mockedServer) SetName(_ string) {
   137  }
   138  
   139  func (m *mockedServer) Start(_ internal.RegisterFn) error {
   140  	return nil
   141  }