prometheusinterceptor_test.go 966 B

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