chainserverinterceptors_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package internal
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "google.golang.org/grpc"
  7. )
  8. func TestWithStreamServerInterceptors(t *testing.T) {
  9. opts := WithStreamServerInterceptors()
  10. assert.NotNil(t, opts)
  11. }
  12. func TestWithUnaryServerInterceptors(t *testing.T) {
  13. opts := WithUnaryServerInterceptors()
  14. assert.NotNil(t, opts)
  15. }
  16. func TestChainStreamServerInterceptors_zero(t *testing.T) {
  17. var vals []int
  18. interceptors := chainStreamServerInterceptors()
  19. err := interceptors(nil, nil, nil, func(srv interface{}, stream grpc.ServerStream) error {
  20. vals = append(vals, 1)
  21. return nil
  22. })
  23. assert.Nil(t, err)
  24. assert.ElementsMatch(t, []int{1}, vals)
  25. }
  26. func TestChainStreamServerInterceptors_one(t *testing.T) {
  27. var vals []int
  28. interceptors := chainStreamServerInterceptors(func(srv interface{}, ss grpc.ServerStream,
  29. info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  30. vals = append(vals, 1)
  31. return handler(srv, ss)
  32. })
  33. err := interceptors(nil, nil, nil, func(srv interface{}, stream grpc.ServerStream) error {
  34. vals = append(vals, 2)
  35. return nil
  36. })
  37. assert.Nil(t, err)
  38. assert.ElementsMatch(t, []int{1, 2}, vals)
  39. }
  40. func TestChainStreamServerInterceptors_more(t *testing.T) {
  41. var vals []int
  42. interceptors := chainStreamServerInterceptors(func(srv interface{}, ss grpc.ServerStream,
  43. info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  44. vals = append(vals, 1)
  45. return handler(srv, ss)
  46. }, func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  47. vals = append(vals, 2)
  48. return handler(srv, ss)
  49. })
  50. err := interceptors(nil, nil, nil, func(srv interface{}, stream grpc.ServerStream) error {
  51. vals = append(vals, 3)
  52. return nil
  53. })
  54. assert.Nil(t, err)
  55. assert.ElementsMatch(t, []int{1, 2, 3}, vals)
  56. }
  57. func TestChainUnaryServerInterceptors_zero(t *testing.T) {
  58. var vals []int
  59. interceptors := chainUnaryServerInterceptors()
  60. _, err := interceptors(context.Background(), nil, nil,
  61. func(ctx context.Context, req interface{}) (interface{}, error) {
  62. vals = append(vals, 1)
  63. return nil, nil
  64. })
  65. assert.Nil(t, err)
  66. assert.ElementsMatch(t, []int{1}, vals)
  67. }
  68. func TestChainUnaryServerInterceptors_one(t *testing.T) {
  69. var vals []int
  70. interceptors := chainUnaryServerInterceptors(func(ctx context.Context, req interface{},
  71. info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  72. vals = append(vals, 1)
  73. return handler(ctx, req)
  74. })
  75. _, err := interceptors(context.Background(), nil, nil,
  76. func(ctx context.Context, req interface{}) (interface{}, error) {
  77. vals = append(vals, 2)
  78. return nil, nil
  79. })
  80. assert.Nil(t, err)
  81. assert.ElementsMatch(t, []int{1, 2}, vals)
  82. }
  83. func TestChainUnaryServerInterceptors_more(t *testing.T) {
  84. var vals []int
  85. interceptors := chainUnaryServerInterceptors(func(ctx context.Context, req interface{},
  86. info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  87. vals = append(vals, 1)
  88. return handler(ctx, req)
  89. }, func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
  90. handler grpc.UnaryHandler) (resp interface{}, err error) {
  91. vals = append(vals, 2)
  92. return handler(ctx, req)
  93. })
  94. _, err := interceptors(context.Background(), nil, nil,
  95. func(ctx context.Context, req interface{}) (interface{}, error) {
  96. vals = append(vals, 3)
  97. return nil, nil
  98. })
  99. assert.Nil(t, err)
  100. assert.ElementsMatch(t, []int{1, 2, 3}, vals)
  101. }