1
0

durationinterceptor_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 any, cc *grpc.ClientConn,
  29. opts ...grpc.CallOption) error {
  30. return test.err
  31. })
  32. assert.Equal(t, test.err, err)
  33. })
  34. }
  35. DontLogContentForMethod("/foo")
  36. t.Cleanup(func() {
  37. notLoggingContentMethods.Delete("/foo")
  38. })
  39. for _, test := range tests {
  40. t.Run(test.name, func(t *testing.T) {
  41. cc := new(grpc.ClientConn)
  42. err := DurationInterceptor(context.Background(), "/foo", nil, nil, cc,
  43. func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn,
  44. opts ...grpc.CallOption) error {
  45. return test.err
  46. })
  47. assert.Equal(t, test.err, err)
  48. })
  49. }
  50. }
  51. func TestDurationInterceptorWithSlowThreshold(t *testing.T) {
  52. SetSlowThreshold(time.Microsecond)
  53. t.Cleanup(func() {
  54. SetSlowThreshold(defaultSlowThreshold)
  55. })
  56. tests := []struct {
  57. name string
  58. err error
  59. }{
  60. {
  61. name: "nil",
  62. err: nil,
  63. },
  64. {
  65. name: "with error",
  66. err: errors.New("mock"),
  67. },
  68. }
  69. for _, test := range tests {
  70. t.Run(test.name, func(t *testing.T) {
  71. cc := new(grpc.ClientConn)
  72. err := DurationInterceptor(context.Background(), "/foo", nil, nil, cc,
  73. func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn,
  74. opts ...grpc.CallOption) error {
  75. time.Sleep(time.Millisecond * 10)
  76. return test.err
  77. })
  78. assert.Equal(t, test.err, err)
  79. })
  80. }
  81. DontLogContentForMethod("/foo")
  82. t.Cleanup(func() {
  83. notLoggingContentMethods.Delete("/foo")
  84. })
  85. for _, test := range tests {
  86. t.Run(test.name, func(t *testing.T) {
  87. cc := new(grpc.ClientConn)
  88. err := DurationInterceptor(context.Background(), "/foo", nil, nil, cc,
  89. func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn,
  90. opts ...grpc.CallOption) error {
  91. time.Sleep(time.Millisecond * 10)
  92. return test.err
  93. })
  94. assert.Equal(t, test.err, err)
  95. })
  96. }
  97. }
  98. func TestSetSlowThreshold(t *testing.T) {
  99. assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
  100. SetSlowThreshold(time.Second)
  101. assert.Equal(t, time.Second, slowThreshold.Load())
  102. }