github.com/lingyao2333/mo-zero@v1.4.1/zrpc/internal/client_test.go (about)

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