tracinginterceptor_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package clientinterceptors
  2. import (
  3. "context"
  4. "sync"
  5. "sync/atomic"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/tal-tech/go-zero/core/trace"
  9. "google.golang.org/grpc"
  10. "google.golang.org/grpc/metadata"
  11. )
  12. func TestUnaryTracingInterceptor(t *testing.T) {
  13. var run int32
  14. var wg sync.WaitGroup
  15. wg.Add(1)
  16. cc := new(grpc.ClientConn)
  17. err := UnaryTracingInterceptor(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. defer wg.Done()
  21. atomic.AddInt32(&run, 1)
  22. return nil
  23. })
  24. wg.Wait()
  25. assert.Nil(t, err)
  26. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  27. }
  28. func TestStreamTracingInterceptor(t *testing.T) {
  29. var run int32
  30. var wg sync.WaitGroup
  31. wg.Add(1)
  32. cc := new(grpc.ClientConn)
  33. _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",
  34. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  35. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  36. defer wg.Done()
  37. atomic.AddInt32(&run, 1)
  38. return nil, nil
  39. })
  40. wg.Wait()
  41. assert.Nil(t, err)
  42. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  43. }
  44. func TestUnaryTracingInterceptor_GrpcFormat(t *testing.T) {
  45. var run int32
  46. var wg sync.WaitGroup
  47. wg.Add(1)
  48. md := metadata.New(map[string]string{
  49. "foo": "bar",
  50. })
  51. carrier, err := trace.Inject(trace.GrpcFormat, md)
  52. assert.Nil(t, err)
  53. ctx, _ := trace.StartServerSpan(context.Background(), carrier, "user", "/foo")
  54. cc := new(grpc.ClientConn)
  55. err = UnaryTracingInterceptor(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. atomic.AddInt32(&run, 1)
  60. return nil
  61. })
  62. wg.Wait()
  63. assert.Nil(t, err)
  64. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  65. }
  66. func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
  67. var run int32
  68. var wg sync.WaitGroup
  69. wg.Add(1)
  70. md := metadata.New(map[string]string{
  71. "foo": "bar",
  72. })
  73. carrier, err := trace.Inject(trace.GrpcFormat, md)
  74. assert.Nil(t, err)
  75. ctx, _ := trace.StartServerSpan(context.Background(), carrier, "user", "/foo")
  76. cc := new(grpc.ClientConn)
  77. _, err = StreamTracingInterceptor(ctx, nil, cc, "/foo",
  78. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  79. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  80. defer wg.Done()
  81. atomic.AddInt32(&run, 1)
  82. return nil, nil
  83. })
  84. wg.Wait()
  85. assert.Nil(t, err)
  86. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  87. }