client_test.go 1.3 KB

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