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

     1  package clientinterceptors
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/lingyao2333/mo-zero/core/prometheus"
     9  	"github.com/stretchr/testify/assert"
    10  	"google.golang.org/grpc"
    11  )
    12  
    13  func TestPromMetricInterceptor(t *testing.T) {
    14  	tests := []struct {
    15  		name   string
    16  		enable bool
    17  		err    error
    18  	}{
    19  		{
    20  			name:   "nil",
    21  			enable: true,
    22  			err:    nil,
    23  		},
    24  		{
    25  			name:   "with error",
    26  			enable: true,
    27  			err:    errors.New("mock"),
    28  		},
    29  		{
    30  			name: "disabled",
    31  		},
    32  	}
    33  	for _, test := range tests {
    34  		t.Run(test.name, func(t *testing.T) {
    35  			if test.enable {
    36  				prometheus.StartAgent(prometheus.Config{
    37  					Host: "localhost",
    38  					Path: "/",
    39  				})
    40  			}
    41  			cc := new(grpc.ClientConn)
    42  			err := PrometheusInterceptor(context.Background(), "/foo", nil, nil, cc,
    43  				func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
    44  					opts ...grpc.CallOption) error {
    45  					return test.err
    46  				})
    47  			assert.Equal(t, test.err, err)
    48  		})
    49  	}
    50  }