github.com/kaydxh/golang@v0.0.131/go/net/grpc/example/data.repository_test.go (about)

     1  package date_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"context"
     8  
     9  	grpc_ "github.com/kaydxh/golang/go/net/grpc"
    10  	date_ "github.com/kaydxh/golang/go/net/grpc/example"
    11  	_ "github.com/kaydxh/golang/go/net/resolver/dns"
    12  	_ "github.com/kaydxh/golang/go/net/resolver/passthrough"
    13  	"google.golang.org/grpc"
    14  )
    15  
    16  func TestNow(t *testing.T) {
    17  
    18  	ctx := context.Background()
    19  	/*
    20  		repository, err := grpc_.NewRepository(ctx, grpc_.FactoryConfig[date_.DateServiceClient]{
    21  			Addr:    "localhost:10001",
    22  			Timeout: 5 * time.Second,
    23  			NewServiceClient: func(c *grpc.ClientConn) date_.DateServiceClient {
    24  				return date_.NewDateServiceClient(c)
    25  			},
    26  		},
    27  		)
    28  	*/
    29  
    30  	factory, err := grpc_.NewFactory(grpc_.FactoryConfig[date_.DateServiceClient]{
    31  		//Addr:    "localhost:10001",
    32  		Addr:    "127.0.0.1:10001",
    33  		Timeout: 5 * time.Second,
    34  		NewServiceClient: func(c *grpc.ClientConn) date_.DateServiceClient {
    35  			return date_.NewDateServiceClient(c)
    36  		},
    37  	},
    38  	)
    39  	if err != nil {
    40  		t.Errorf("failed to new factory, err: %v", err)
    41  	}
    42  	repository, err := factory.NewClient(ctx)
    43  	if err != nil {
    44  		t.Errorf("failed to new respository client, err: %v", err)
    45  	}
    46  
    47  	respWrap := date_.Repository{
    48  		Repository: repository,
    49  	}
    50  	//resp, err := respWrap.Now(ctx, &date_.NowRequest{})
    51  	var resp any
    52  	err = respWrap.Call(ctx,
    53  		func(ctx context.Context) error {
    54  			// short connection
    55  			newClient, conn, err := respWrap.NewConnect(ctx)
    56  			if err != nil {
    57  				return err
    58  			}
    59  			defer conn.Close()
    60  
    61  			resp, err = newClient.Now(ctx, &date_.NowRequest{})
    62  			//long connection
    63  			//resp, err = respWrap.Now(ctx, &date_.NowRequest{})
    64  			return err
    65  		})
    66  	if err != nil {
    67  		t.Errorf("failed to call Now, err: %v", err)
    68  		return
    69  	}
    70  
    71  	//t.Logf("resp: %v", resp.RequestId)
    72  	t.Logf("resp: %v", resp)
    73  
    74  }