durationinterceptor_test.go 933 B

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