github.com/TeaOSLab/EdgeNode@v1.3.8/internal/rpc/rpc_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package rpc_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
     7  	"github.com/TeaOSLab/EdgeNode/internal/rpc"
     8  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     9  	_ "github.com/iwind/TeaGo/bootstrap"
    10  	timeutil "github.com/iwind/TeaGo/utils/time"
    11  	"sync"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestRPCConcurrentCall(t *testing.T) {
    17  	if !testutils.IsSingleTesting() {
    18  		return
    19  	}
    20  
    21  	rpcClient, err := rpc.SharedRPC()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	var before = time.Now()
    27  	defer func() {
    28  		t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
    29  	}()
    30  
    31  	var concurrent = 3
    32  
    33  	var wg = sync.WaitGroup{}
    34  	wg.Add(concurrent)
    35  
    36  	for i := 0; i < concurrent; i++ {
    37  		go func() {
    38  			defer wg.Done()
    39  
    40  			_, err = rpcClient.NodeRPC.FindCurrentNodeConfig(rpcClient.Context(), &pb.FindCurrentNodeConfigRequest{})
    41  			if err != nil {
    42  				t.Log(err)
    43  			}
    44  		}()
    45  	}
    46  
    47  	wg.Wait()
    48  }
    49  
    50  func TestRPC_Retry(t *testing.T) {
    51  	if !testutils.IsSingleTesting() {
    52  		return
    53  	}
    54  
    55  	rpcClient, err := rpc.SharedRPC()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	var ticker = time.NewTicker(1 * time.Second)
    61  	for range ticker.C {
    62  		go func() {
    63  			_, err = rpcClient.NodeRPC.FindCurrentNodeConfig(rpcClient.Context(), &pb.FindCurrentNodeConfigRequest{})
    64  			if err != nil {
    65  				t.Log(timeutil.Format("H:i:s"), err)
    66  			} else {
    67  				t.Log(timeutil.Format("H:i:s"), "success")
    68  			}
    69  		}()
    70  	}
    71  }