github.com/cloudwego/kitex@v0.9.0/client/callopt/options_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package callopt 18 19 import ( 20 "context" 21 "fmt" 22 "strings" 23 "testing" 24 "time" 25 26 "github.com/cloudwego/kitex/internal/client" 27 "github.com/cloudwego/kitex/internal/test" 28 "github.com/cloudwego/kitex/pkg/fallback" 29 "github.com/cloudwego/kitex/pkg/http" 30 "github.com/cloudwego/kitex/pkg/retry" 31 "github.com/cloudwego/kitex/pkg/rpcinfo" 32 "github.com/cloudwego/kitex/pkg/rpcinfo/remoteinfo" 33 ) 34 35 // Mock value 36 var ( 37 option Option 38 applyRes string 39 rpcConfig = rpcinfo.AsMutableRPCConfig(rpcinfo.NewRPCConfig()) 40 remoteInfo = remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{ 41 ServiceName: "service", 42 Method: "method0", 43 }, "method1") 44 ) 45 46 const ( 47 mockHostPort = "127.0.0.1:8888" 48 mockURL = "www.cloudwego.com" 49 mockHTTPHost = "www.cloudwego.com" 50 mockRPCTimeout = 1000 * time.Millisecond 51 mockRPCTimeoutStr = "1000ms" 52 mockConnectTimeout = 500 * time.Millisecond 53 mockConnectTimeoutStr = "500ms" 54 mockKey = "mock_key" 55 mockVal = "mock_val" 56 ) 57 58 // TestApply apply different option 59 func TestApply(t *testing.T) { 60 // WithHostPort 61 option = WithHostPort(mockHostPort) 62 applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 63 test.Assert(t, applyRes == fmt.Sprintf("[WithHostPort(%s),]", mockHostPort)) 64 65 // WithURL 66 option = WithURL(mockURL) 67 applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 68 test.Assert(t, applyRes == fmt.Sprintf("[WithURL(%s),]", mockURL)) 69 70 // WithHTTPHost 71 option = WithHTTPHost(mockHTTPHost) 72 applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 73 httpHost, exist := remoteInfo.Tag(rpcinfo.HTTPHost) 74 test.Assert(t, httpHost == mockHTTPHost, applyRes) 75 test.Assert(t, exist) 76 77 // WithRPCTimeout 78 option = WithRPCTimeout(mockRPCTimeout) 79 applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 80 test.Assert(t, applyRes == "[WithRPCTimeout()]", applyRes) 81 test.Assert(t, rpcConfig.ImmutableView().RPCTimeout() == mockRPCTimeout, rpcConfig.ImmutableView().RPCTimeout()) 82 test.Assert(t, rpcConfig.ImmutableView().ReadWriteTimeout() == mockRPCTimeout, rpcConfig.ImmutableView().ReadWriteTimeout()) 83 84 // WithConnectTimeout 85 option = WithConnectTimeout(mockConnectTimeout) 86 applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 87 test.Assert(t, applyRes == "[WithConnectTimeout()]", applyRes) 88 test.Assert(t, rpcConfig.ImmutableView().ConnectTimeout() == mockConnectTimeout, rpcConfig.ImmutableView().ConnectTimeout()) 89 90 // WithTag 91 option = WithTag(mockKey, mockVal) 92 applyRes, _ = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 93 v, exist := remoteInfo.Tag(mockKey) 94 test.Assert(t, exist) 95 test.Assert(t, v == mockVal, v) 96 97 // WithRetryPolicy 98 option = WithRetryPolicy(retry.BuildFailurePolicy(retry.NewFailurePolicy())) 99 _, co := Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 100 test.Assert(t, co.RetryPolicy.Enable) 101 test.Assert(t, co.RetryPolicy.FailurePolicy != nil) 102 103 // WithRetryPolicy pass empty struct 104 option = WithRetryPolicy(retry.Policy{}) 105 _, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 106 test.Assert(t, !co.RetryPolicy.Enable) 107 108 // WithFallback 109 option = WithFallback(fallback.ErrorFallback(fallback.UnwrapHelper(func(ctx context.Context, req, resp interface{}, err error) (fbResp interface{}, fbErr error) { 110 return 111 })).EnableReportAsFallback()) 112 _, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 113 test.Assert(t, co.Fallback != nil) 114 115 // WithFallback pass nil 116 option = WithFallback(nil) 117 _, co = Apply([]Option{option}, rpcConfig, remoteInfo, client.NewConfigLocks(), http.NewDefaultResolver()) 118 test.Assert(t, co.Fallback == nil) 119 } 120 121 func BenchmarkStringsBuilder(b *testing.B) { 122 var cos []Option 123 cos = append(cos, WithRPCTimeout(time.Second)) 124 cos = append(cos, WithTag("cluster", "mock")) 125 cos = append(cos, WithTag("idc", "mock")) 126 b.ReportAllocs() 127 b.ResetTimer() 128 b.RunParallel(func(pb *testing.PB) { 129 for pb.Next() { 130 co := callOptionsPool.Get().(*CallOptions) 131 co.configs = rpcinfo.NewRPCConfig().(rpcinfo.MutableRPCConfig) 132 co.svr = remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{}, "method") 133 var buf strings.Builder 134 buf.Grow(64) 135 136 buf.WriteByte('[') 137 for i := range cos { 138 if i > 0 { 139 buf.WriteByte(',') 140 } 141 cos[i].f(co, &buf) 142 } 143 buf.WriteByte(']') 144 _ = buf.String() 145 } 146 }) 147 }