trpc.group/trpc-go/trpc-go@v1.0.3/trpc_util_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package trpc
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"trpc.group/trpc-go/trpc-go/codec"
    26  )
    27  
    28  // TestCloneContext test CloneContext
    29  func TestCloneContext(t *testing.T) {
    30  	calleeMethod := "1"
    31  	// add msg
    32  	ctx, msg := codec.WithNewMessage(context.Background())
    33  	msg.WithCalleeMethod(calleeMethod)
    34  	// add timeout
    35  	ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
    36  	// add custom key value
    37  	type ctxKey struct{}
    38  	ctx = context.WithValue(ctx, ctxKey{}, "value")
    39  	clonedCtx := CloneContext(ctx)
    40  	updateCalleeMethod := "2"
    41  	codec.Message(clonedCtx).WithCalleeMethod(updateCalleeMethod)
    42  	// cancel
    43  	cancel()
    44  	// test msg cloning
    45  	assert.Equal(t, codec.Message(ctx).CalleeMethod(), calleeMethod)
    46  	assert.Equal(t, codec.Message(clonedCtx).CalleeMethod(), updateCalleeMethod)
    47  	// check timeout
    48  	assert.Equal(t, ctx.Err(), context.Canceled)
    49  	assert.Nil(t, clonedCtx.Err())
    50  	// check getting kv
    51  	assert.Equal(t, ctx.Value(ctxKey{}).(string), "value")
    52  	assert.Equal(t, clonedCtx.Value(ctxKey{}).(string), "value")
    53  }
    54  
    55  func TestGetMetaData(t *testing.T) {
    56  	type args struct {
    57  		ctx context.Context
    58  		key string
    59  	}
    60  	ctx, msg := codec.WithNewMessage(context.Background())
    61  	md := make(map[string][]byte)
    62  	md["testKey"] = []byte("testValue")
    63  	msg.WithServerMetaData(md)
    64  	tests := []struct {
    65  		name string
    66  		args args
    67  		want []byte
    68  	}{
    69  		{
    70  			name: "test case",
    71  			args: args{
    72  				ctx: ctx,
    73  				key: "testKey",
    74  			},
    75  			want: []byte("testValue"),
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			if got := GetMetaData(tt.args.ctx, tt.args.key); !reflect.DeepEqual(got, tt.want) {
    81  				t.Errorf("GetMetaData() = %v, want %v", got, tt.want)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  // TestGetIP test getIP.
    88  func TestGetIP(t *testing.T) {
    89  	nicName := []string{"en1", "utun0"}
    90  	for _, name := range nicName {
    91  		got := getIP(name)
    92  		t.Logf("get ip by name: %v, ip: %v",
    93  			name, got)
    94  		assert.LessOrEqual(t, 0, len(got))
    95  	}
    96  
    97  	// Test None Existed NIC
    98  	NoneExistNIC := "ethNoneExist"
    99  	ip := getIP(NoneExistNIC)
   100  	assert.Empty(t, ip)
   101  }
   102  func TestGoAndWait(t *testing.T) {
   103  	err := GoAndWait(
   104  		func() error {
   105  			return nil
   106  		},
   107  		func() error {
   108  			return errors.New("go and wait3 test error")
   109  		},
   110  	)
   111  	assert.NotNil(t, err)
   112  }
   113  func TestGo(t *testing.T) {
   114  	ctx := BackgroundContext()
   115  	f := func(ctx context.Context) {
   116  		select {
   117  		case <-ctx.Done():
   118  			assert.NotNil(t, ctx.Err())
   119  		}
   120  	}
   121  	err := Go(ctx, time.Millisecond, f)
   122  	assert.Nil(t, err)
   123  	type goImpl struct {
   124  		Goer
   125  		test int
   126  	}
   127  	g := &goImpl{Goer: DefaultGoer}
   128  	f = func(ctx context.Context) {
   129  		select {
   130  		case <-ctx.Done():
   131  			g.test = 1
   132  		}
   133  	}
   134  	err = g.Go(ctx, 10*time.Millisecond, f)
   135  	assert.Nil(t, err)
   136  	assert.NotEqual(t, 1, g.test)
   137  	g = &goImpl{Goer: NewSyncGoer()}
   138  	f = func(ctx context.Context) {
   139  		select {
   140  		case <-ctx.Done():
   141  			g.test = 2
   142  		}
   143  	}
   144  	err = g.Go(ctx, 10*time.Millisecond, f)
   145  	assert.Nil(t, err)
   146  	assert.Equal(t, 2, g.test)
   147  	g = &goImpl{Goer: NewAsyncGoer(1, PanicBufLen, true)}
   148  	err = g.Go(ctx, time.Second, f)
   149  	assert.Nil(t, err)
   150  	panicfunc := func(ctx context.Context) {
   151  		panic("go test1 panic")
   152  	}
   153  	g = &goImpl{Goer: DefaultGoer}
   154  	err = g.Go(ctx, time.Millisecond, panicfunc)
   155  	assert.Nil(t, err)
   156  }
   157  
   158  func TestGoAndWaitWithPanic(t *testing.T) {
   159  	err := GoAndWait(
   160  		func() error {
   161  			return nil
   162  		},
   163  		func() error {
   164  			panic("go and wait2 test panic")
   165  		},
   166  	)
   167  	assert.NotNil(t, err)
   168  }
   169  
   170  // TestNetInterfaceIPEnumAllIP
   171  func TestNetInterfaceIPEnumAllIP(t *testing.T) {
   172  	p := &netInterfaceIP{}
   173  	ips := p.enumAllIP()
   174  	for k, v := range ips {
   175  		t.Logf("enum ips nic: %s, ips: %+v",
   176  			k, *v)
   177  	}
   178  	assert.LessOrEqual(t, 0, len(ips))
   179  }
   180  
   181  // TestNetInterfaceIPGetIPByNic
   182  func TestNetInterfaceIPGetIPByNic(t *testing.T) {
   183  	p := &netInterfaceIP{}
   184  	got := p.getIPByNic("en1")
   185  	t.Logf("get ip by nic name en1 ip: %v", got)
   186  	assert.LessOrEqual(t, 0, len(got))
   187  	got = p.getIPByNic("utun0")
   188  	t.Logf("get ip by nic name utun0 ip: %v", got)
   189  	assert.LessOrEqual(t, 0, len(got))
   190  }
   191  
   192  // TestDeduplicate
   193  func TestDeduplicate(t *testing.T) {
   194  	a := []string{"a1", "a2"}
   195  	b := []string{"b1", "b2", "a2"}
   196  	r := deduplicate(a, b)
   197  	assert.Equal(t, r, []string{"a1", "a2", "b1", "b2"})
   198  }