client_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package internal
  2. import (
  3. "context"
  4. "net"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "google.golang.org/grpc"
  10. )
  11. func TestWithDialOption(t *testing.T) {
  12. var options ClientOptions
  13. agent := grpc.WithUserAgent("chrome")
  14. opt := WithDialOption(agent)
  15. opt(&options)
  16. assert.Contains(t, options.DialOptions, agent)
  17. }
  18. func TestWithTimeout(t *testing.T) {
  19. var options ClientOptions
  20. opt := WithTimeout(time.Second)
  21. opt(&options)
  22. assert.Equal(t, time.Second, options.Timeout)
  23. }
  24. func TestWithNonBlock(t *testing.T) {
  25. var options ClientOptions
  26. opt := WithNonBlock()
  27. opt(&options)
  28. assert.True(t, options.NonBlock)
  29. }
  30. func TestWithStreamClientInterceptor(t *testing.T) {
  31. var options ClientOptions
  32. opt := WithStreamClientInterceptor(func(ctx context.Context, desc *grpc.StreamDesc,
  33. cc *grpc.ClientConn, method string, streamer grpc.Streamer,
  34. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  35. return nil, nil
  36. })
  37. opt(&options)
  38. assert.Equal(t, 1, len(options.DialOptions))
  39. }
  40. func TestWithTransportCredentials(t *testing.T) {
  41. var options ClientOptions
  42. opt := WithTransportCredentials(nil)
  43. opt(&options)
  44. assert.Equal(t, 1, len(options.DialOptions))
  45. }
  46. func TestWithUnaryClientInterceptor(t *testing.T) {
  47. var options ClientOptions
  48. opt := WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  49. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  50. return nil
  51. })
  52. opt(&options)
  53. assert.Equal(t, 1, len(options.DialOptions))
  54. }
  55. func TestBuildDialOptions(t *testing.T) {
  56. c := client{
  57. middlewares: ClientMiddlewaresConf{
  58. Trace: true,
  59. Duration: true,
  60. Prometheus: true,
  61. Breaker: true,
  62. Timeout: true,
  63. },
  64. }
  65. agent := grpc.WithUserAgent("chrome")
  66. opts := c.buildDialOptions(WithDialOption(agent))
  67. assert.Contains(t, opts, agent)
  68. }
  69. func TestClientDial(t *testing.T) {
  70. server := grpc.NewServer()
  71. go func() {
  72. lis, err := net.Listen("tcp", "localhost:54321")
  73. assert.NoError(t, err)
  74. defer lis.Close()
  75. server.Serve(lis)
  76. }()
  77. time.Sleep(time.Millisecond)
  78. c, err := NewClient("localhost:54321", ClientMiddlewaresConf{
  79. Trace: true,
  80. Duration: true,
  81. Prometheus: true,
  82. Breaker: true,
  83. Timeout: true,
  84. })
  85. assert.NoError(t, err)
  86. assert.NotNil(t, c.Conn())
  87. server.Stop()
  88. }
  89. func TestClientDialFail(t *testing.T) {
  90. _, err := NewClient("localhost:54321", ClientMiddlewaresConf{
  91. Trace: true,
  92. Duration: true,
  93. Prometheus: true,
  94. Breaker: true,
  95. Timeout: true,
  96. })
  97. assert.Error(t, err)
  98. assert.True(t, strings.Contains(err.Error(), "localhost:54321"))
  99. _, err = NewClient("localhost:54321/fail", ClientMiddlewaresConf{
  100. Trace: true,
  101. Duration: true,
  102. Prometheus: true,
  103. Breaker: true,
  104. Timeout: true,
  105. })
  106. assert.Error(t, err)
  107. assert.True(t, strings.Contains(err.Error(), "localhost:54321/fail"))
  108. }