client_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 TestWithRetry(t *testing.T) {
  23. var options ClientOptions
  24. opt := WithRetry()
  25. opt(&options)
  26. assert.True(t, options.Retry)
  27. }
  28. func TestWithNonBlock(t *testing.T) {
  29. var options ClientOptions
  30. opt := WithNonBlock()
  31. opt(&options)
  32. assert.True(t, options.NonBlock)
  33. }
  34. func TestWithTransportCredentials(t *testing.T) {
  35. var options ClientOptions
  36. opt := WithTransportCredentials(nil)
  37. opt(&options)
  38. assert.Equal(t, 1, len(options.DialOptions))
  39. }
  40. func TestWithUnaryClientInterceptor(t *testing.T) {
  41. var options ClientOptions
  42. opt := WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  43. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  44. return nil
  45. })
  46. opt(&options)
  47. assert.Equal(t, 1, len(options.DialOptions))
  48. }
  49. func TestBuildDialOptions(t *testing.T) {
  50. var c client
  51. agent := grpc.WithUserAgent("chrome")
  52. opts := c.buildDialOptions(WithDialOption(agent))
  53. assert.Contains(t, opts, agent)
  54. }