forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
147 lines (130 loc) · 5.23 KB
/
options_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
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package callopt
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/cloudwego/kitex/internal/client"
"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/fallback"
"github.com/cloudwego/kitex/pkg/http"
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/rpcinfo/remoteinfo"
)
// Mock value
var (
option Option
applyRes string
rpcConfig = rpcinfo.AsMutableRPCConfig(rpcinfo.NewRPCConfig())
remoteInfo = remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: "service",
Method: "method0",
}, "method1")
)
const (
mockHostPort = "127.0.0.1:8888"
mockURL = "www.cloudwego.com"
mockHTTPHost = "www.cloudwego.com"
mockRPCTimeout = 1000 * time.Millisecond
mockRPCTimeoutStr = "1000ms"
mockConnectTimeout = 500 * time.Millisecond
mockConnectTimeoutStr = "500ms"
mockKey = "mock_key"
mockVal = "mock_val"
)
// TestApply apply different option
func TestApply(t *testing.T) {
// WithHostPort
option = WithHostPort(mockHostPort)
applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, applyRes == fmt.Sprintf("[WithHostPort(%s),]", mockHostPort))
// WithURL
option = WithURL(mockURL)
applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, applyRes == fmt.Sprintf("[WithURL(%s),]", mockURL))
// WithHTTPHost
option = WithHTTPHost(mockHTTPHost)
applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
httpHost, exist := remoteInfo.Tag(rpcinfo.HTTPHost)
test.Assert(t, httpHost == mockHTTPHost, applyRes)
test.Assert(t, exist)
// WithRPCTimeout
option = WithRPCTimeout(mockRPCTimeout)
applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, applyRes == "[WithRPCTimeout()]", applyRes)
test.Assert(t, rpcConfig.ImmutableView().RPCTimeout() == mockRPCTimeout, rpcConfig.ImmutableView().RPCTimeout())
test.Assert(t, rpcConfig.ImmutableView().ReadWriteTimeout() == mockRPCTimeout, rpcConfig.ImmutableView().ReadWriteTimeout())
// WithConnectTimeout
option = WithConnectTimeout(mockConnectTimeout)
applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, applyRes == "[WithConnectTimeout()]", applyRes)
test.Assert(t, rpcConfig.ImmutableView().ConnectTimeout() == mockConnectTimeout, rpcConfig.ImmutableView().ConnectTimeout())
// WithTag
option = WithTag(mockKey, mockVal)
applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
v, exist := remoteInfo.Tag(mockKey)
test.Assert(t, exist)
test.Assert(t, v == mockVal, v)
// WithRetryPolicy
option = WithRetryPolicy(retry.BuildFailurePolicy(retry.NewFailurePolicy()))
_, co := Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, co.RetryPolicy.Enable)
test.Assert(t, co.RetryPolicy.FailurePolicy != nil)
// WithRetryPolicy pass empty struct
option = WithRetryPolicy(retry.Policy{})
_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, !co.RetryPolicy.Enable)
// WithFallback
option = WithFallback(fallback.ErrorFallback(fallback.UnwrapHelper(func(ctx context.Context, req, resp interface{}, err error) (fbResp interface{}, fbErr error) {
return
})).EnableReportAsFallback())
_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, co.Fallback != nil)
// WithFallback pass nil
option = WithFallback(nil)
_, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver())
test.Assert(t, co.Fallback == nil)
}
func BenchmarkStringsBuilder(b *testing.B) {
var cos []Option
cos = append(cos, WithRPCTimeout(time.Second))
cos = append(cos, WithTag("cluster", "mock"))
cos = append(cos, WithTag("idc", "mock"))
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
co := callOptionsPool.Get().(*CallOptions)
co.configs = rpcinfo.NewRPCConfig().(rpcinfo.MutableRPCConfig)
co.svr = remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{}, "method")
var buf strings.Builder
buf.Grow(64)
buf.WriteByte('[')
for i := range cos {
if i > 0 {
buf.WriteByte(',')
}
cos[i].f(co, &buf)
}
buf.WriteByte(']')
_ = buf.String()
}
})
}