client_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 TestWithUnaryClientInterceptor(t *testing.T) {
  29. var options ClientOptions
  30. opt := WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
  31. cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
  32. return nil
  33. })
  34. opt(&options)
  35. assert.Equal(t, 1, len(options.DialOptions))
  36. }
  37. func TestBuildDialOptions(t *testing.T) {
  38. var c client
  39. agent := grpc.WithUserAgent("chrome")
  40. opts := c.buildDialOptions(WithDialOption(agent))
  41. assert.Contains(t, opts, agent)
  42. }