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