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

     1  package clientinterceptors
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"google.golang.org/grpc"
    11  )
    12  
    13  func TestDurationInterceptor(t *testing.T) {
    14  	tests := []struct {
    15  		name string
    16  		err  error
    17  	}{
    18  		{
    19  			name: "nil",
    20  			err:  nil,
    21  		},
    22  		{
    23  			name: "with error",
    24  			err:  errors.New("mock"),
    25  		},
    26  	}
    27  	for _, test := range tests {
    28  		t.Run(test.name, func(t *testing.T) {
    29  			cc := new(grpc.ClientConn)
    30  			err := DurationInterceptor(context.Background(), "/foo", nil, nil, cc,
    31  				func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
    32  					opts ...grpc.CallOption) error {
    33  					return test.err
    34  				})
    35  			assert.Equal(t, test.err, err)
    36  		})
    37  	}
    38  }
    39  
    40  func TestSetSlowThreshold(t *testing.T) {
    41  	assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
    42  	SetSlowThreshold(time.Second)
    43  	assert.Equal(t, time.Second, slowThreshold.Load())
    44  }