durationinterceptor_test.go 908 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. for _, test := range tests {
  25. t.Run(test.name, func(t *testing.T) {
  26. cc := new(grpc.ClientConn)
  27. err := DurationInterceptor(context.Background(), "/foo", nil, nil, cc,
  28. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  29. opts ...grpc.CallOption) error {
  30. return test.err
  31. })
  32. assert.Equal(t, test.err, err)
  33. })
  34. }
  35. }
  36. func TestSetSlowThreshold(t *testing.T) {
  37. assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
  38. SetSlowThreshold(time.Second)
  39. assert.Equal(t, time.Second, slowThreshold.Load())
  40. }