timeoutinterceptor_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "google.golang.org/grpc/codes"
  5. "google.golang.org/grpc/status"
  6. "sync"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "google.golang.org/grpc"
  11. )
  12. func TestUnaryTimeoutInterceptor(t *testing.T) {
  13. interceptor := UnaryTimeoutInterceptor(time.Millisecond * 10)
  14. _, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  15. FullMethod: "/",
  16. }, func(ctx context.Context, req interface{}) (interface{}, error) {
  17. return nil, nil
  18. })
  19. assert.Nil(t, err)
  20. }
  21. func TestUnaryTimeoutInterceptor_panic(t *testing.T) {
  22. interceptor := UnaryTimeoutInterceptor(time.Millisecond * 10)
  23. assert.Panics(t, func() {
  24. _, _ = interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
  25. FullMethod: "/",
  26. }, func(ctx context.Context, req interface{}) (interface{}, error) {
  27. panic("any")
  28. })
  29. })
  30. }
  31. func TestUnaryTimeoutInterceptor_timeout(t *testing.T) {
  32. const timeout = time.Millisecond * 10
  33. interceptor := UnaryTimeoutInterceptor(timeout)
  34. ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  35. defer cancel()
  36. var wg sync.WaitGroup
  37. wg.Add(1)
  38. _, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
  39. FullMethod: "/",
  40. }, func(ctx context.Context, req interface{}) (interface{}, error) {
  41. defer wg.Done()
  42. tm, ok := ctx.Deadline()
  43. assert.True(t, ok)
  44. assert.True(t, tm.Before(time.Now().Add(timeout+time.Millisecond)))
  45. return nil, nil
  46. })
  47. wg.Wait()
  48. assert.Nil(t, err)
  49. }
  50. func TestUnaryTimeoutInterceptor_timeoutExpire(t *testing.T) {
  51. const timeout = time.Millisecond * 10
  52. interceptor := UnaryTimeoutInterceptor(timeout)
  53. ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
  54. defer cancel()
  55. var wg sync.WaitGroup
  56. wg.Add(1)
  57. _, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
  58. FullMethod: "/",
  59. }, func(ctx context.Context, req interface{}) (interface{}, error) {
  60. defer wg.Done()
  61. time.Sleep(time.Millisecond * 50)
  62. return nil, nil
  63. })
  64. wg.Wait()
  65. assert.EqualValues(t, status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error()), err)
  66. }
  67. func TestUnaryTimeoutInterceptor_cancel(t *testing.T) {
  68. const timeout = time.Minute * 10
  69. interceptor := UnaryTimeoutInterceptor(timeout)
  70. ctx, cancel := context.WithCancel(context.Background())
  71. cancel()
  72. var wg sync.WaitGroup
  73. wg.Add(1)
  74. _, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
  75. FullMethod: "/",
  76. }, func(ctx context.Context, req interface{}) (interface{}, error) {
  77. defer wg.Done()
  78. time.Sleep(time.Millisecond * 50)
  79. return nil, nil
  80. })
  81. wg.Wait()
  82. assert.EqualValues(t, status.Error(codes.Canceled, context.Canceled.Error()), err)
  83. }