client_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package internal
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "google.golang.org/grpc"
  8. )
  9. func TestWithDialOption(t *testing.T) {
  10. var options ClientOptions
  11. agent := grpc.WithUserAgent("chrome")
  12. opt := WithDialOption(agent)
  13. opt(&options)
  14. assert.Contains(t, options.DialOptions, agent)
  15. }
  16. func TestWithTimeout(t *testing.T) {
  17. var options ClientOptions
  18. opt := WithTimeout(time.Second)
  19. opt(&options)
  20. assert.Equal(t, time.Second, options.Timeout)
  21. }
  22. func TestWithNonBlock(t *testing.T) {
  23. var options ClientOptions
  24. opt := WithNonBlock()
  25. opt(&options)
  26. assert.True(t, options.NonBlock)
  27. }
  28. func TestWithStreamClientInterceptor(t *testing.T) {
  29. var options ClientOptions
  30. opt := WithStreamClientInterceptor(func(ctx context.Context, desc *grpc.StreamDesc,
  31. cc *grpc.ClientConn, method string, streamer grpc.Streamer,
  32. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  33. return nil, nil
  34. })
  35. opt(&options)
  36. assert.Equal(t, 1, len(options.DialOptions))
  37. }
  38. func TestWithTransportCredentials(t *testing.T) {
  39. var options ClientOptions
  40. opt := WithTransportCredentials(nil)
  41. opt(&options)
  42. assert.Equal(t, 1, len(options.DialOptions))
  43. }
  44. func TestWithUnaryClientInterceptor(t *testing.T) {
  45. var options ClientOptions
  46. opt := WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  47. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  48. return nil
  49. })
  50. opt(&options)
  51. assert.Equal(t, 1, len(options.DialOptions))
  52. }
  53. func TestBuildDialOptions(t *testing.T) {
  54. var c client
  55. agent := grpc.WithUserAgent("chrome")
  56. opts := c.buildDialOptions(WithDialOption(agent))
  57. assert.Contains(t, opts, agent)
  58. }