tracinginterceptor_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package clientinterceptors
  2. import (
  3. "context"
  4. "errors"
  5. "sync"
  6. "sync/atomic"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/tal-tech/go-zero/core/trace"
  10. "google.golang.org/grpc"
  11. )
  12. func TestOpenTracingInterceptor(t *testing.T) {
  13. trace.StartAgent(trace.Config{
  14. Name: "go-zero-test",
  15. Endpoint: "http://localhost:14268/api/traces",
  16. Batcher: "jaeger",
  17. Sampler: 1.0,
  18. })
  19. cc := new(grpc.ClientConn)
  20. err := UnaryTracingInterceptor(context.Background(), "/ListUser", nil, nil, cc,
  21. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  22. opts ...grpc.CallOption) error {
  23. return nil
  24. })
  25. assert.Nil(t, err)
  26. }
  27. func TestUnaryTracingInterceptor(t *testing.T) {
  28. var run int32
  29. var wg sync.WaitGroup
  30. wg.Add(1)
  31. cc := new(grpc.ClientConn)
  32. err := UnaryTracingInterceptor(context.Background(), "/foo", nil, nil, cc,
  33. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  34. opts ...grpc.CallOption) error {
  35. defer wg.Done()
  36. atomic.AddInt32(&run, 1)
  37. return nil
  38. })
  39. wg.Wait()
  40. assert.Nil(t, err)
  41. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  42. }
  43. func TestUnaryTracingInterceptor_WithError(t *testing.T) {
  44. var run int32
  45. var wg sync.WaitGroup
  46. wg.Add(1)
  47. cc := new(grpc.ClientConn)
  48. err := UnaryTracingInterceptor(context.Background(), "/foo", nil, nil, cc,
  49. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  50. opts ...grpc.CallOption) error {
  51. defer wg.Done()
  52. atomic.AddInt32(&run, 1)
  53. return errors.New("dummy")
  54. })
  55. wg.Wait()
  56. assert.NotNil(t, err)
  57. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  58. }
  59. func TestStreamTracingInterceptor(t *testing.T) {
  60. var run int32
  61. var wg sync.WaitGroup
  62. wg.Add(1)
  63. cc := new(grpc.ClientConn)
  64. _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",
  65. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  66. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  67. defer wg.Done()
  68. atomic.AddInt32(&run, 1)
  69. return nil, nil
  70. })
  71. wg.Wait()
  72. assert.Nil(t, err)
  73. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  74. }
  75. func TestStreamTracingInterceptor_WithError(t *testing.T) {
  76. var run int32
  77. var wg sync.WaitGroup
  78. wg.Add(1)
  79. cc := new(grpc.ClientConn)
  80. _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",
  81. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  82. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  83. defer wg.Done()
  84. atomic.AddInt32(&run, 1)
  85. return nil, errors.New("dummy")
  86. })
  87. wg.Wait()
  88. assert.NotNil(t, err)
  89. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  90. }
  91. func TestUnaryTracingInterceptor_GrpcFormat(t *testing.T) {
  92. var run int32
  93. var wg sync.WaitGroup
  94. wg.Add(1)
  95. cc := new(grpc.ClientConn)
  96. err := UnaryTracingInterceptor(context.Background(), "/foo", nil, nil, cc,
  97. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  98. opts ...grpc.CallOption) error {
  99. defer wg.Done()
  100. atomic.AddInt32(&run, 1)
  101. return nil
  102. })
  103. wg.Wait()
  104. assert.Nil(t, err)
  105. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  106. }
  107. func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
  108. var run int32
  109. var wg sync.WaitGroup
  110. wg.Add(1)
  111. cc := new(grpc.ClientConn)
  112. _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",
  113. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  114. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  115. defer wg.Done()
  116. atomic.AddInt32(&run, 1)
  117. return nil, nil
  118. })
  119. wg.Wait()
  120. assert.Nil(t, err)
  121. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  122. }