123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package zrpc
- import (
- "testing"
- "time"
- "git.i2edu.net/i2/go-zero/core/discov"
- "git.i2edu.net/i2/go-zero/core/logx"
- "git.i2edu.net/i2/go-zero/core/service"
- "git.i2edu.net/i2/go-zero/core/stat"
- "git.i2edu.net/i2/go-zero/core/stores/redis"
- "git.i2edu.net/i2/go-zero/zrpc/internal"
- "git.i2edu.net/i2/go-zero/zrpc/internal/serverinterceptors"
- "github.com/stretchr/testify/assert"
- "google.golang.org/grpc"
- )
- 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
- }
|