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

     1  package internal
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/lingyao2333/mo-zero/core/stat"
     8  	"github.com/stretchr/testify/assert"
     9  	"google.golang.org/grpc"
    10  )
    11  
    12  func TestBaseRpcServer_AddOptions(t *testing.T) {
    13  	metrics := stat.NewMetrics("foo")
    14  	server := newBaseRpcServer("foo", &rpcServerOptions{metrics: metrics})
    15  	server.SetName("bar")
    16  	var opt grpc.EmptyServerOption
    17  	server.AddOptions(opt)
    18  	assert.Contains(t, server.options, opt)
    19  }
    20  
    21  func TestBaseRpcServer_AddStreamInterceptors(t *testing.T) {
    22  	metrics := stat.NewMetrics("foo")
    23  	server := newBaseRpcServer("foo", &rpcServerOptions{metrics: metrics})
    24  	server.SetName("bar")
    25  	var vals []int
    26  	f := func(_ interface{}, _ grpc.ServerStream, _ *grpc.StreamServerInfo, _ grpc.StreamHandler) error {
    27  		vals = append(vals, 1)
    28  		return nil
    29  	}
    30  	server.AddStreamInterceptors(f)
    31  	for _, each := range server.streamInterceptors {
    32  		assert.Nil(t, each(nil, nil, nil, nil))
    33  	}
    34  	assert.ElementsMatch(t, []int{1}, vals)
    35  }
    36  
    37  func TestBaseRpcServer_AddUnaryInterceptors(t *testing.T) {
    38  	metrics := stat.NewMetrics("foo")
    39  	server := newBaseRpcServer("foo", &rpcServerOptions{metrics: metrics})
    40  	server.SetName("bar")
    41  	var vals []int
    42  	f := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (
    43  		resp interface{}, err error) {
    44  		vals = append(vals, 1)
    45  		return nil, nil
    46  	}
    47  	server.AddUnaryInterceptors(f)
    48  	for _, each := range server.unaryInterceptors {
    49  		_, err := each(context.Background(), nil, nil, nil)
    50  		assert.Nil(t, err)
    51  	}
    52  	assert.ElementsMatch(t, []int{1}, vals)
    53  }