tracinginterceptor_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. )
  11. func TestOpenTracingInterceptor(t *testing.T) {
  12. trace.StartAgent(trace.Config{
  13. Name: "go-zero-test",
  14. Endpoint: "http://localhost:14268/api/traces",
  15. Batcher: "jaeger",
  16. Sampler: 1.0,
  17. })
  18. cc := new(grpc.ClientConn)
  19. err := UnaryTracingInterceptor(context.Background(), "/ListUser", nil, nil, cc,
  20. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  21. opts ...grpc.CallOption) error {
  22. return nil
  23. })
  24. assert.Nil(t, err)
  25. }
  26. func TestUnaryTracingInterceptor(t *testing.T) {
  27. var run int32
  28. var wg sync.WaitGroup
  29. wg.Add(1)
  30. cc := new(grpc.ClientConn)
  31. err := UnaryTracingInterceptor(context.Background(), "/foo", nil, nil, cc,
  32. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  33. opts ...grpc.CallOption) error {
  34. defer wg.Done()
  35. atomic.AddInt32(&run, 1)
  36. return nil
  37. })
  38. wg.Wait()
  39. assert.Nil(t, err)
  40. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  41. }
  42. func TestStreamTracingInterceptor(t *testing.T) {
  43. var run int32
  44. var wg sync.WaitGroup
  45. wg.Add(1)
  46. cc := new(grpc.ClientConn)
  47. _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",
  48. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  49. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  50. defer wg.Done()
  51. atomic.AddInt32(&run, 1)
  52. return nil, nil
  53. })
  54. wg.Wait()
  55. assert.Nil(t, err)
  56. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  57. }
  58. func TestUnaryTracingInterceptor_GrpcFormat(t *testing.T) {
  59. var run int32
  60. var wg sync.WaitGroup
  61. wg.Add(1)
  62. cc := new(grpc.ClientConn)
  63. err := UnaryTracingInterceptor(context.Background(), "/foo", nil, nil, cc,
  64. func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
  65. opts ...grpc.CallOption) error {
  66. defer wg.Done()
  67. atomic.AddInt32(&run, 1)
  68. return nil
  69. })
  70. wg.Wait()
  71. assert.Nil(t, err)
  72. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  73. }
  74. func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {
  75. var run int32
  76. var wg sync.WaitGroup
  77. wg.Add(1)
  78. cc := new(grpc.ClientConn)
  79. _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",
  80. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  81. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  82. defer wg.Done()
  83. atomic.AddInt32(&run, 1)
  84. return nil, nil
  85. })
  86. wg.Wait()
  87. assert.Nil(t, err)
  88. assert.Equal(t, int32(1), atomic.LoadInt32(&run))
  89. }