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

     1  package serverinterceptors
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/lingyao2333/mo-zero/core/lang"
    10  	"github.com/lingyao2333/mo-zero/core/stat"
    11  	"github.com/stretchr/testify/assert"
    12  	"google.golang.org/grpc"
    13  	"google.golang.org/grpc/peer"
    14  )
    15  
    16  func TestSetSlowThreshold(t *testing.T) {
    17  	assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
    18  	SetSlowThreshold(time.Second)
    19  	assert.Equal(t, time.Second, slowThreshold.Load())
    20  }
    21  
    22  func TestUnaryStatInterceptor(t *testing.T) {
    23  	metrics := stat.NewMetrics("mock")
    24  	interceptor := UnaryStatInterceptor(metrics)
    25  	_, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
    26  		FullMethod: "/",
    27  	}, func(ctx context.Context, req interface{}) (interface{}, error) {
    28  		return nil, nil
    29  	})
    30  	assert.Nil(t, err)
    31  }
    32  
    33  func TestLogDuration(t *testing.T) {
    34  	addrs, err := net.InterfaceAddrs()
    35  	assert.Nil(t, err)
    36  	assert.True(t, len(addrs) > 0)
    37  
    38  	tests := []struct {
    39  		name     string
    40  		ctx      context.Context
    41  		req      interface{}
    42  		duration time.Duration
    43  	}{
    44  		{
    45  			name: "normal",
    46  			ctx:  context.Background(),
    47  			req:  "foo",
    48  		},
    49  		{
    50  			name: "bad req",
    51  			ctx:  context.Background(),
    52  			req:  make(chan lang.PlaceholderType), // not marshalable
    53  		},
    54  		{
    55  			name:     "timeout",
    56  			ctx:      context.Background(),
    57  			req:      "foo",
    58  			duration: time.Second,
    59  		},
    60  		{
    61  			name: "timeout",
    62  			ctx: peer.NewContext(context.Background(), &peer.Peer{
    63  				Addr: addrs[0],
    64  			}),
    65  			req: "foo",
    66  		},
    67  		{
    68  			name:     "timeout",
    69  			ctx:      context.Background(),
    70  			req:      "foo",
    71  			duration: slowThreshold.Load() + time.Second,
    72  		},
    73  	}
    74  
    75  	for _, test := range tests {
    76  		test := test
    77  		t.Run(test.name, func(t *testing.T) {
    78  			t.Parallel()
    79  
    80  			assert.NotPanics(t, func() {
    81  				logDuration(test.ctx, "foo", test.req, test.duration)
    82  			})
    83  		})
    84  	}
    85  }
    86  
    87  func TestLogDurationWithoutContent(t *testing.T) {
    88  	addrs, err := net.InterfaceAddrs()
    89  	assert.Nil(t, err)
    90  	assert.True(t, len(addrs) > 0)
    91  
    92  	tests := []struct {
    93  		name     string
    94  		ctx      context.Context
    95  		req      interface{}
    96  		duration time.Duration
    97  	}{
    98  		{
    99  			name: "normal",
   100  			ctx:  context.Background(),
   101  			req:  "foo",
   102  		},
   103  		{
   104  			name: "bad req",
   105  			ctx:  context.Background(),
   106  			req:  make(chan lang.PlaceholderType), // not marshalable
   107  		},
   108  		{
   109  			name:     "timeout",
   110  			ctx:      context.Background(),
   111  			req:      "foo",
   112  			duration: time.Second,
   113  		},
   114  		{
   115  			name: "timeout",
   116  			ctx: peer.NewContext(context.Background(), &peer.Peer{
   117  				Addr: addrs[0],
   118  			}),
   119  			req: "foo",
   120  		},
   121  		{
   122  			name:     "timeout",
   123  			ctx:      context.Background(),
   124  			req:      "foo",
   125  			duration: slowThreshold.Load() + time.Second,
   126  		},
   127  	}
   128  
   129  	DontLogContentForMethod("foo")
   130  	for _, test := range tests {
   131  		test := test
   132  		t.Run(test.name, func(t *testing.T) {
   133  			t.Parallel()
   134  
   135  			assert.NotPanics(t, func() {
   136  				logDuration(test.ctx, "foo", test.req, test.duration)
   137  			})
   138  		})
   139  	}
   140  }