timeoutinterceptor_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package clientinterceptors
  2. import (
  3. "context"
  4. "strconv"
  5. "sync"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "google.golang.org/grpc"
  10. )
  11. func TestTimeoutInterceptor(t *testing.T) {
  12. timeouts := []time.Duration{0, time.Millisecond * 10}
  13. for _, timeout := range timeouts {
  14. t.Run(strconv.FormatInt(int64(timeout), 10), func(t *testing.T) {
  15. interceptor := TimeoutInterceptor(timeout)
  16. cc := new(grpc.ClientConn)
  17. err := interceptor(context.Background(), "/foo", nil, nil, cc,
  18. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  19. opts ...grpc.CallOption) error {
  20. return nil
  21. },
  22. )
  23. assert.Nil(t, err)
  24. })
  25. }
  26. }
  27. func TestTimeoutInterceptor_timeout(t *testing.T) {
  28. const timeout = time.Millisecond * 10
  29. interceptor := TimeoutInterceptor(timeout)
  30. ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  31. defer cancel()
  32. var wg sync.WaitGroup
  33. wg.Add(1)
  34. cc := new(grpc.ClientConn)
  35. err := interceptor(ctx, "/foo", nil, nil, cc,
  36. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  37. opts ...grpc.CallOption) error {
  38. defer wg.Done()
  39. tm, ok := ctx.Deadline()
  40. assert.True(t, ok)
  41. assert.True(t, tm.Before(time.Now().Add(timeout+time.Millisecond)))
  42. return nil
  43. })
  44. wg.Wait()
  45. assert.Nil(t, err)
  46. }
  47. func TestTimeoutInterceptor_timeoutExpire(t *testing.T) {
  48. const timeout = time.Millisecond * 10
  49. interceptor := TimeoutInterceptor(timeout)
  50. ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
  51. defer cancel()
  52. var wg sync.WaitGroup
  53. wg.Add(1)
  54. cc := new(grpc.ClientConn)
  55. err := interceptor(ctx, "/foo", nil, nil, cc,
  56. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  57. opts ...grpc.CallOption) error {
  58. defer wg.Done()
  59. time.Sleep(time.Millisecond * 50)
  60. return nil
  61. })
  62. wg.Wait()
  63. assert.Equal(t, context.DeadlineExceeded, err)
  64. }
  65. func TestTimeoutInterceptor_panic(t *testing.T) {
  66. timeouts := []time.Duration{0, time.Millisecond * 10}
  67. for _, timeout := range timeouts {
  68. t.Run(strconv.FormatInt(int64(timeout), 10), func(t *testing.T) {
  69. interceptor := TimeoutInterceptor(timeout)
  70. cc := new(grpc.ClientConn)
  71. assert.Panics(t, func() {
  72. _ = interceptor(context.Background(), "/foo", nil, nil, cc,
  73. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  74. opts ...grpc.CallOption) error {
  75. panic("any")
  76. },
  77. )
  78. })
  79. })
  80. }
  81. }