From b53a09d830318f69a7a8350be7284810c6925262 Mon Sep 17 00:00:00 2001 From: Kyle Xiao Date: Mon, 23 Sep 2024 11:24:52 +0800 Subject: [PATCH] tests: reduced 1m30s time for testing * *: use pkg/utils/serverutils for listening addrs * *: rm time.Sleep for most packages * run.sh: run in parallel, simplify install protoc func * thriftrpc: run in parallel * thrift_streaming: updated exitserver, use update_xxx_dep.sh if needed * ci: seperate compliant check and run tests * common: remove the pkg, moved to pkg/utils/serverutils --- .github/workflows/pr-check.yml | 18 +- .gitignore | 5 + common/common.go | 32 ---- generic/http/client_test.go | 10 +- generic/http/server.go | 6 +- generic/json/client_test.go | 24 ++- generic/json/server.go | 12 +- generic/map/client_test.go | 12 +- generic/map/server.go | 6 +- kitexgrpc/abc/abc_test.go | 6 +- kitexgrpc/compressor/grpc_compressor_test.go | 10 +- kitexgrpc/multi_service/multi_service_test.go | 12 +- kitexgrpc/normalcall/normalcall_test.go | 30 +-- .../unknown_service_handler_test.go | 6 +- .../error_handler/errhandler_test.go | 17 +- pbrpc/multiservicecall/multi_service_test.go | 30 +-- pbrpc/muxcall/muxcall_test.go | 11 +- pbrpc/normalcall/normalcall_test.go | 10 +- pbrpc/server.go | 3 + .../{client.go => clientutils/clientutils.go} | 2 +- .../clientutils_test.go} | 2 +- pkg/utils/serverutils/serverutils.go | 64 +++++++ pkg/utils/serverutils/serverutils_test.go | 36 ++++ run.sh | 160 ++++++++-------- thrift_streaming/exitserver/go.mod | 13 +- thrift_streaming/exitserver/go.sum | 13 +- thrift_streaming/exitserver/update_go_mod.sh | 34 ++++ thrift_streaming/generate.sh | 18 +- thrift_streaming/grpcpb_test.go | 5 +- thrift_streaming/main_test.go | 46 ++--- thrift_streaming/thrift_slim_others_test.go | 29 --- ...slim_amd64_test.go => thrift_slim_test.go} | 9 +- thrift_streaming/thrift_test.go | 22 +-- thrift_streaming/thrift_tracing_test.go | 18 +- thriftrpc/abctest/abc_test.go | 18 +- .../error_handler/errhandler_test.go | 30 +-- thriftrpc/failedcall/failedcall_test.go | 26 +-- .../error_handler/errhandler_test.go | 19 +- thriftrpc/failedmux/failedmux_test.go | 19 +- .../multiservicecall/multi_service_test.go | 34 ++-- thriftrpc/muxcall/thriftmux_test.go | 43 +++-- thriftrpc/normalcall/normalcall_test.go | 158 ++++++++-------- .../server_processing_timeout_test.go | 63 ++++--- thriftrpc/retrycall/mixedretry_test.go | 175 ++++++------------ thriftrpc/retrycall/percentage_limit_test.go | 18 +- thriftrpc/retrycall/retrycall_handler.go | 141 +++++++++----- thriftrpc/retrycall/retrycall_test.go | 151 +++++++-------- 47 files changed, 864 insertions(+), 762 deletions(-) delete mode 100644 common/common.go rename pkg/utils/{client.go => clientutils/clientutils.go} (99%) rename pkg/utils/{client_test.go => clientutils/clientutils_test.go} (98%) create mode 100644 pkg/utils/serverutils/serverutils.go create mode 100644 pkg/utils/serverutils/serverutils_test.go create mode 100755 thrift_streaming/exitserver/update_go_mod.sh delete mode 100644 thrift_streaming/thrift_slim_others_test.go rename thrift_streaming/{thrift_slim_amd64_test.go => thrift_slim_test.go} (98%) diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 46aa350..6942161 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -3,22 +3,26 @@ name: Push Check on: [pull_request] jobs: - build: - runs-on: ubuntu-latest + run-tests: + runs-on: [ self-hosted, X64 ] steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v5 with: go-version: stable + cache: false + - name: Run Tests + run: ./run.sh + compliant-checks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 - name: Check License Header uses: apache/skywalking-eyes/header@v0.4.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: typos-action - uses: crate-ci/typos@v1.2.1 - - - name: Run Tests - run: ./run.sh + - name: Check Spell + uses: crate-ci/typos@v1.13.14 diff --git a/.gitignore b/.gitignore index 787f28f..ef05245 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,10 @@ output/* *.test testdata/ +# Go workspace +go.work +go.work.sum + # Files produced by run.sh kitex_gen/ kitex_gen_slim/ @@ -49,6 +53,7 @@ kitex_gen_noDefSerdes/ grpc_gen/ go.mod go.sum +*.bak bin thrift_streaming/binaries thrift_streaming/kitex_gen/ diff --git a/common/common.go b/common/common.go deleted file mode 100644 index 40b1421..0000000 --- a/common/common.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2024 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 common - -import ( - "net" - "time" - - "github.com/cloudwego/kitex/pkg/klog" -) - -func WaitServer(hostPort string) { - for begin := time.Now(); time.Since(begin) < time.Second; { - if _, err := net.Dial("tcp", hostPort); err == nil { - klog.Infof("server %s is up", hostPort) - return - } - time.Sleep(time.Millisecond * 10) - } -} diff --git a/generic/http/client_test.go b/generic/http/client_test.go index 7f8aa70..3437f59 100644 --- a/generic/http/client_test.go +++ b/generic/http/client_test.go @@ -23,15 +23,19 @@ import ( "time" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/client/callopt" "github.com/cloudwego/kitex/pkg/generic" "github.com/cloudwego/kitex/pkg/klog" ) +var testaddr string + func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() klog.SetLevel(klog.LevelFatal) - svc := runServer() - time.Sleep(100 * time.Millisecond) + svc := runServer(testaddr) + serverutils.Wait(testaddr) m.Run() svc.Stop() } @@ -41,7 +45,7 @@ func TestClient(t *testing.T) { test.Assert(t, err == nil, err) g, err := generic.HTTPThriftGeneric(p) test.Assert(t, err == nil, err) - cli := newGenericClient("a.b.c", g, address) + cli := newGenericClient("a.b.c", g, testaddr) body := map[string]interface{}{ "text": "text", diff --git a/generic/http/server.go b/generic/http/server.go index 18d9394..6a2ac1c 100644 --- a/generic/http/server.go +++ b/generic/http/server.go @@ -34,10 +34,8 @@ func assert(expected, actual interface{}) error { return nil } -const address = "localhost:9009" - -func runServer() server.Server { - addr, _ := net.ResolveTCPAddr("tcp", address) +func runServer(listenaddr string) server.Server { + addr, _ := net.ResolveTCPAddr("tcp", listenaddr) svc := bizservice.NewServer(new(BizServiceImpl), server.WithServiceAddr(addr)) go func() { if err := svc.Run(); err != nil { diff --git a/generic/json/client_test.go b/generic/json/client_test.go index e864cbf..d1d145c 100644 --- a/generic/json/client_test.go +++ b/generic/json/client_test.go @@ -22,6 +22,7 @@ import ( "time" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/client/genericclient" "github.com/cloudwego/kitex/pkg/generic" @@ -30,10 +31,18 @@ import ( "github.com/cloudwego/kitex/transport" ) +var ( + testaddr string + genericAddr string +) + func TestMain(m *testing.M) { - svc := runServer() - gsvc := runGenericServer() - time.Sleep(100 * time.Millisecond) + testaddr = serverutils.NextListenAddr() + genericAddr = serverutils.NextListenAddr() + svc := runServer(testaddr) + gsvc := runGenericServer(genericAddr) + serverutils.Wait(testaddr) + serverutils.Wait(genericAddr) m.Run() svc.Stop() gsvc.Stop() @@ -45,7 +54,7 @@ func TestClient(t *testing.T) { g, err := generic.JSONThriftGeneric(p) test.Assert(t, err == nil) - cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(address)) + cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(testaddr)) test.Assert(t, err == nil) req := map[string]interface{}{ @@ -78,7 +87,7 @@ func TestGeneric(t *testing.T) { g, err := generic.JSONThriftGeneric(p) test.Assert(t, err == nil) - cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(address)) + cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(testaddr)) test.Assert(t, err == nil) req := map[string]interface{}{ @@ -117,7 +126,10 @@ func TestBizErr(t *testing.T) { g, err := generic.JSONThriftGeneric(p) test.Assert(t, err == nil) - cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(genericAddress), client.WithMetaHandler(transmeta.ClientTTHeaderHandler), client.WithTransportProtocol(transport.TTHeader)) + cli, err := genericclient.NewClient("a.b.c", g, + client.WithHostPorts(genericAddr), + client.WithMetaHandler(transmeta.ClientTTHeaderHandler), + client.WithTransportProtocol(transport.TTHeader)) test.Assert(t, err == nil) _, err = cli.GenericCall(context.Background(), "Echo", nil) bizerr, ok := kerrors.FromBizStatusError(err) diff --git a/generic/json/server.go b/generic/json/server.go index a03bd22..23db762 100644 --- a/generic/json/server.go +++ b/generic/json/server.go @@ -34,10 +34,8 @@ func assert(expected, actual interface{}) error { return nil } -const address = "localhost:9009" - -func runServer() server.Server { - addr, _ := net.ResolveTCPAddr("tcp", address) +func runServer(listenaddr string) server.Server { + addr, _ := net.ResolveTCPAddr("tcp", listenaddr) svc := echoservice.NewServer(new(EchoServiceImpl), server.WithServiceAddr(addr)) go func() { if err := svc.Run(); err != nil { @@ -47,10 +45,8 @@ func runServer() server.Server { return svc } -const genericAddress = "localhost:9010" - -func runGenericServer() server.Server { - addr, _ := net.ResolveTCPAddr("tcp", genericAddress) +func runGenericServer(listenaddr string) server.Server { + addr, _ := net.ResolveTCPAddr("tcp", listenaddr) p, err := generic.NewThriftFileProvider("../../idl/tenant.thrift") if err != nil { panic(err) diff --git a/generic/map/client_test.go b/generic/map/client_test.go index 4e5fc98..cf4cd57 100644 --- a/generic/map/client_test.go +++ b/generic/map/client_test.go @@ -21,6 +21,7 @@ import ( "time" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/client/genericclient" "github.com/cloudwego/kitex/pkg/generic" @@ -29,10 +30,13 @@ import ( "github.com/cloudwego/kitex/transport" ) +var testaddr string + func TestMain(m *testing.M) { - svc := runServer() + testaddr = serverutils.NextListenAddr() + svc := runServer(testaddr) gsvc := runGenericServer() - time.Sleep(100 * time.Millisecond) + serverutils.Wait(testaddr) m.Run() svc.Stop() gsvc.Stop() @@ -44,7 +48,7 @@ func TestClient(t *testing.T) { g, err := generic.MapThriftGeneric(p) test.Assert(t, err == nil) - cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(address)) + cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(testaddr)) test.Assert(t, err == nil) req := map[string]interface{}{ @@ -95,7 +99,7 @@ func TestGeneric(t *testing.T) { g, err := generic.MapThriftGeneric(p) test.Assert(t, err == nil) - cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(address)) + cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(testaddr)) test.Assert(t, err == nil) req := map[string]interface{}{ diff --git a/generic/map/server.go b/generic/map/server.go index 9012e3b..602022a 100644 --- a/generic/map/server.go +++ b/generic/map/server.go @@ -34,10 +34,8 @@ func assert(expected, actual interface{}) error { return nil } -const address = "localhost:9009" - -func runServer() server.Server { - addr, _ := net.ResolveTCPAddr("tcp", address) +func runServer(listenaddr string) server.Server { + addr, _ := net.ResolveTCPAddr("tcp", listenaddr) svc := echoservice.NewServer(new(EchoServiceImpl), server.WithServiceAddr(addr)) go func() { if err := svc.Run(); err != nil { diff --git a/kitexgrpc/abc/abc_test.go b/kitexgrpc/abc/abc_test.go index b891791..2e1f68d 100644 --- a/kitexgrpc/abc/abc_test.go +++ b/kitexgrpc/abc/abc_test.go @@ -17,12 +17,12 @@ package abc import ( "testing" - "github.com/cloudwego/kitex-tests/common" "github.com/cloudwego/kitex-tests/kitexgrpc/abc/consts" "github.com/cloudwego/kitex-tests/kitexgrpc/abc/servicea" "github.com/cloudwego/kitex-tests/kitexgrpc/abc/serviceb" "github.com/cloudwego/kitex-tests/kitexgrpc/abc/servicec" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" ) func TestMain(m *testing.M) { @@ -43,8 +43,8 @@ func TestMain(m *testing.M) { } }() - common.WaitServer(consts.ServiceCAddr) - common.WaitServer(consts.ServiceBAddr) + serverutils.Wait(consts.ServiceCAddr) + serverutils.Wait(consts.ServiceBAddr) m.Run() } diff --git a/kitexgrpc/compressor/grpc_compressor_test.go b/kitexgrpc/compressor/grpc_compressor_test.go index 95623fa..a60bc0e 100644 --- a/kitexgrpc/compressor/grpc_compressor_test.go +++ b/kitexgrpc/compressor/grpc_compressor_test.go @@ -23,7 +23,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_demo/servicea" "github.com/cloudwego/kitex-tests/pkg/test" - "github.com/cloudwego/kitex-tests/pkg/utils" + "github.com/cloudwego/kitex-tests/pkg/utils/clientutils" client_opt "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/client/callopt" "github.com/cloudwego/kitex/pkg/endpoint" @@ -46,7 +46,7 @@ func TestKitexWithoutCompressor(t *testing.T) { time.Sleep(50 * time.Millisecond) client, err := GetClient(hostport) test.Assert(t, err == nil, err) - defer utils.CallClose(client) + defer clientutils.CallClose(client) resp, err := client.RunUnary() test.Assert(t, err == nil, err) test.Assert(t, resp != nil && resp.Message == "Kitex Hello!") @@ -73,7 +73,7 @@ func TestKitexCompressor(t *testing.T) { time.Sleep(50 * time.Millisecond) client, err := GetClient(hostport) test.Assert(t, err == nil, err) - defer utils.CallClose(client) + defer clientutils.CallClose(client) resp, err := client.RunUnary(callopt.WithGRPCCompressor(kitex_gzip.Name)) test.Assert(t, err == nil, err) @@ -105,7 +105,7 @@ func TestKitexCompressorWithGRPCClient(t *testing.T) { defer conn.Close() client, err := GetGRPCClient(hostport) test.Assert(t, err == nil, err) - defer utils.CallClose(client) + defer clientutils.CallClose(client) resp, err := client.RunUnary(grpc.UseCompressor(gzip.Name)) test.Assert(t, err == nil, err) @@ -145,7 +145,7 @@ func TestKitexCompressorWithGRPCServer(t *testing.T) { client, err := GetClient(hostport, client_opt.WithMiddleware(ServiceNameMW)) test.Assert(t, err == nil, err) - defer utils.CallClose(client) + defer clientutils.CallClose(client) resp, err := client.RunUnary(callopt.WithGRPCCompressor(kitex_gzip.Name)) test.Assert(t, err == nil, err) diff --git a/kitexgrpc/multi_service/multi_service_test.go b/kitexgrpc/multi_service/multi_service_test.go index e212948..433ef14 100644 --- a/kitexgrpc/multi_service/multi_service_test.go +++ b/kitexgrpc/multi_service/multi_service_test.go @@ -31,7 +31,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_multi_service_2" servicea2 "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_multi_service_2/servicea" "github.com/cloudwego/kitex-tests/pkg/test" - "github.com/cloudwego/kitex-tests/pkg/utils" + "github.com/cloudwego/kitex-tests/pkg/utils/clientutils" ) type ServiceAImpl struct{} @@ -94,7 +94,7 @@ func TestMultiService(t *testing.T) { clientA, err := servicea.NewClient("ServiceA", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(hostport)) test.Assert(t, err == nil, err) - defer utils.CallClose(clientA) + defer clientutils.CallClose(clientA) streamCliA, err := clientA.EchoA(context.Background()) test.Assert(t, err == nil, err) @@ -106,7 +106,7 @@ func TestMultiService(t *testing.T) { clientB, err := serviceb.NewClient("ServiceB", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(hostport)) test.Assert(t, err == nil, err) - defer utils.CallClose(clientB) + defer clientutils.CallClose(clientB) streamCliB, err := clientB.EchoB(context.Background()) test.Assert(t, err == nil, err) @@ -127,7 +127,7 @@ func TestUnknownException(t *testing.T) { clientC, err := servicec.NewClient("ServiceC", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(hostport)) test.Assert(t, err == nil, err) - defer utils.CallClose(clientC) + defer clientutils.CallClose(clientC) streamCliC, err := clientC.EchoC(context.Background()) test.Assert(t, err == nil, err) @@ -146,7 +146,7 @@ func TestUnknownExceptionWithMultiService(t *testing.T) { // unknown service error clientC, err := servicec.NewClient("ServiceC", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(hostport)) test.Assert(t, err == nil, err) - defer utils.CallClose(clientC) + defer clientutils.CallClose(clientC) streamCliC, err := clientC.EchoC(context.Background()) test.Assert(t, err == nil, err) @@ -158,7 +158,7 @@ func TestUnknownExceptionWithMultiService(t *testing.T) { // unknown method error clientA, err := servicea2.NewClient("ServiceA", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(hostport)) test.Assert(t, err == nil, err) - defer utils.CallClose(clientA) + defer clientutils.CallClose(clientA) streamCliA, err := clientA.Echo(context.Background()) test.Assert(t, err == nil, err) diff --git a/kitexgrpc/normalcall/normalcall_test.go b/kitexgrpc/normalcall/normalcall_test.go index 6c1286b..990578e 100644 --- a/kitexgrpc/normalcall/normalcall_test.go +++ b/kitexgrpc/normalcall/normalcall_test.go @@ -16,6 +16,7 @@ package normalcall import ( "context" + "fmt" "io" "net" "os" @@ -34,7 +35,8 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_demo" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/grpc_demo/servicea" "github.com/cloudwego/kitex-tests/pkg/test" - "github.com/cloudwego/kitex-tests/pkg/utils" + "github.com/cloudwego/kitex-tests/pkg/utils/clientutils" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" ) type ServerAHandler struct { @@ -72,10 +74,12 @@ func TestDisableRPCInfoReuse(t *testing.T) { backupState := rpcinfo.PoolEnabled() defer rpcinfo.EnablePool(backupState) + addr := serverutils.NextListenAddr() + var ri rpcinfo.RPCInfo svr := servicea.NewServer( &ServerAHandler{}, - server.WithServiceAddr(serverAddr("127.0.0.1:9005")), + server.WithServiceAddr(serverAddr(addr)), server.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) error { ri = rpcinfo.GetRPCInfo(ctx) @@ -91,8 +95,8 @@ func TestDisableRPCInfoReuse(t *testing.T) { time.Sleep(50 * time.Millisecond) defer svr.Stop() - cli := servicea.MustNewClient("servicea", client.WithHostPorts("127.0.0.1:9005")) - defer utils.CallClose(cli) + cli := servicea.MustNewClient("servicea", client.WithHostPorts(addr)) + defer clientutils.CallClose(cli) ctx := context.Background() @@ -111,8 +115,7 @@ func TestDisableRPCInfoReuse(t *testing.T) { } func TestShortConnection(t *testing.T) { - svrIP, svrPort := "127.0.0.1", "19005" - svrAddrStr := svrIP + ":" + svrPort + svrAddrStr := serverutils.NextListenAddr() svr := servicea.NewServer( &ServerAHandler{}, server.WithServiceAddr(serverAddr(svrAddrStr)), @@ -122,7 +125,7 @@ func TestShortConnection(t *testing.T) { panic(err) } }() - time.Sleep(50 * time.Millisecond) + serverutils.Wait(svrAddrStr) defer svr.Stop() cli, err := servicea.NewClient("servicea", @@ -130,17 +133,17 @@ func TestShortConnection(t *testing.T) { client.WithShortConnection(), ) test.Assert(t, err == nil) - clientStream, err := cli.CallClientStream(context.Background()) test.Assert(t, err == nil) err = clientStream.Send(&grpc_demo.Request{Name: "1"}) test.Assert(t, err == nil) _, err = clientStream.CloseAndRecv() test.Assert(t, err == nil) - time.Sleep(50 * time.Millisecond) + time.Sleep(20 * time.Millisecond) // the connection should not exist after the call - exist, err := checkEstablishedConnection(svrIP, svrPort) + _, port, _ := net.SplitHostPort(svrAddrStr) + exist, err := checkEstablishedConnection(port) if err != nil { klog.Warnf("check established connection failed, error=%v", err) } else { @@ -150,16 +153,17 @@ func TestShortConnection(t *testing.T) { } // return true if current pid established connection with the input remote ip and port -func checkEstablishedConnection(ip, port string) (bool, error) { +func checkEstablishedConnection(port string) (bool, error) { pid := os.Getpid() conns, err := netutil.ConnectionsPid("all", int32(pid)) if err != nil { return false, err } - for _, c := range conns { if c.Status == "ESTABLISHED" { - if strconv.Itoa(int(c.Raddr.Port)) == port && c.Raddr.IP == ip { + if strconv.Itoa(int(c.Raddr.Port)) == port && + net.ParseIP(c.Raddr.IP).IsLoopback() { // only need to check IsLoopback for localhost + fmt.Println(c.Laddr, c.Raddr) return true, nil } } diff --git a/kitexgrpc/unknown_handler/unknown_service_handler_test.go b/kitexgrpc/unknown_handler/unknown_service_handler_test.go index 2b431af..84d45b6 100644 --- a/kitexgrpc/unknown_handler/unknown_service_handler_test.go +++ b/kitexgrpc/unknown_handler/unknown_service_handler_test.go @@ -30,7 +30,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/unknown_handler/servicea" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/unknown_handler/serviceb" "github.com/cloudwego/kitex-tests/pkg/test" - "github.com/cloudwego/kitex-tests/pkg/utils" + "github.com/cloudwego/kitex-tests/pkg/utils/clientutils" ) type ServiceAImpl struct{} @@ -69,7 +69,7 @@ func TestUnknownServiceError(t *testing.T) { time.Sleep(50 * time.Millisecond) client, err := servicea.NewClient("grpcService", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(ip)) test.Assert(t, err == nil, err) - defer utils.CallClose(client) + defer clientutils.CallClose(client) req := &unknown_handler.Request{Name: "kitex"} _, err = client.Echo(context.Background(), req) @@ -87,7 +87,7 @@ func TestUnknownServiceHandler(t *testing.T) { time.Sleep(50 * time.Millisecond) client, err := servicea.NewClient("grpcService", client.WithTransportProtocol(transport.GRPC), client.WithHostPorts(ip)) test.Assert(t, err == nil, err) - defer utils.CallClose(client) + defer clientutils.CallClose(client) req := &unknown_handler.Request{Name: "kitex"} resp, err := client.Echo(context.Background(), req) diff --git a/pbrpc/failedcall/error_handler/errhandler_test.go b/pbrpc/failedcall/error_handler/errhandler_test.go index 2a3cc2e..d4d1598 100644 --- a/pbrpc/failedcall/error_handler/errhandler_test.go +++ b/pbrpc/failedcall/error_handler/errhandler_test.go @@ -19,12 +19,12 @@ import ( "reflect" "strings" "testing" - "time" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/stability" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/stability/stservice" "github.com/cloudwego/kitex-tests/pbrpc" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/kerrors" "github.com/cloudwego/kitex/pkg/remote" @@ -35,21 +35,22 @@ import ( "github.com/cloudwego/kitex/transport" ) -var cli stservice.Client +var testaddr string func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := pbrpc.RunServer(&pbrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9001", + Address: testaddr, }, &STServiceHandler{}, server.WithMetaHandler(transmeta.ServerTTHeaderHandler)) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } func TestHandlerReturnNormalError(t *testing.T) { // Kitex Protobuf - cli = getKitexClient(transport.Framed) + cli := getKitexClient(transport.Framed) ctx, stReq := pbrpc.CreateSTRequest(context.Background()) stReq.Name = normalErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -81,7 +82,7 @@ func TestHandlerReturnNormalError(t *testing.T) { } func TestHandlerReturnTransError(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := pbrpc.CreateSTRequest(context.Background()) stReq.Name = kitexTransErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -113,7 +114,7 @@ func TestHandlerReturnTransError(t *testing.T) { } func TestHandlerReturnStatusError(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := pbrpc.CreateSTRequest(context.Background()) stReq.Name = grpcStatus.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -222,7 +223,7 @@ func TestHandlerPanic(t *testing.T) { func getKitexClient(p transport.Protocol) stservice.Client { return pbrpc.CreateKitexClient(&pbrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:9001"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: pbrpc.LongConnection, }, client.WithMetaHandler(transmeta.ClientTTHeaderHandler)) diff --git a/pbrpc/multiservicecall/multi_service_test.go b/pbrpc/multiservicecall/multi_service_test.go index 6581d07..cc1e1b5 100644 --- a/pbrpc/multiservicecall/multi_service_test.go +++ b/pbrpc/multiservicecall/multi_service_test.go @@ -16,14 +16,15 @@ package multiservicecall import ( "context" - "github.com/cloudwego/kitex/client" - "github.com/cloudwego/kitex/pkg/transmeta" - "github.com/cloudwego/kitex/server" "net" "strings" "testing" "time" + "github.com/cloudwego/kitex/client" + "github.com/cloudwego/kitex/pkg/transmeta" + "github.com/cloudwego/kitex/server" + "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/combine_service" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/combine_service/combineservice" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/pb_multi_service" @@ -31,6 +32,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/pb_multi_service/serviceb" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/pb_multi_service/servicec" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" ) type ServiceAHandler struct{} @@ -56,36 +58,38 @@ func (*ServiceCHandler) Chat1(ctx context.Context, req *pb_multi_service.Request func GetServer(hostport string) server.Server { addr, _ := net.ResolveTCPAddr("tcp", hostport) - - return server.NewServer(server.WithServiceAddr(addr)) + return server.NewServer(server.WithServiceAddr(addr), + server.WithExitWaitTime(20*time.Millisecond), + ) } func GetMuxServer(hostport string) server.Server { addr, _ := net.ResolveTCPAddr("tcp", hostport) - - return server.NewServer(server.WithServiceAddr(addr), server.WithMuxTransport()) + return server.NewServer(server.WithServiceAddr(addr), + server.WithExitWaitTime(20*time.Millisecond), + server.WithMuxTransport()) } func TestRegisterService(t *testing.T) { - testRegisterService(t, GetServer, "localhost:9901") + testRegisterService(t, GetServer, serverutils.NextListenAddr()) } func TestMuxRegisterService(t *testing.T) { - testRegisterService(t, GetMuxServer, "localhost:9902") + testRegisterService(t, GetMuxServer, serverutils.NextListenAddr()) } func TestMultiService(t *testing.T) { - ip := "localhost:9903" + ip := serverutils.NextListenAddr() testMultiService(t, GetServer(ip), ip) } func TestMuxMultiService(t *testing.T) { - ip := "localhost:9904" + ip := serverutils.NextListenAddr() testMultiService(t, GetMuxServer(ip), ip) } func TestMultiServiceWithCombineServiceClient(t *testing.T) { - ip := "localhost:9905" + ip := serverutils.NextListenAddr() svr := GetServer(ip) err := servicea.RegisterService(svr, new(ServiceAHandler), server.WithFallbackService()) test.Assert(t, err == nil) @@ -143,7 +147,7 @@ func testMultiService(t *testing.T, svr server.Server, ip string) { servicec.RegisterService(svr, new(ServiceCHandler), server.WithFallbackService()) go svr.Run() defer svr.Stop() - time.Sleep(100 * time.Millisecond) + serverutils.Wait(ip) req := &pb_multi_service.Request{Name: "pb multi_service req"} diff --git a/pbrpc/muxcall/muxcall_test.go b/pbrpc/muxcall/muxcall_test.go index cd51e1a..838bcb4 100644 --- a/pbrpc/muxcall/muxcall_test.go +++ b/pbrpc/muxcall/muxcall_test.go @@ -18,20 +18,23 @@ import ( "context" "strconv" "testing" - "time" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/stability/stservice" "github.com/cloudwego/kitex-tests/pbrpc" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/transport" ) +var testaddr string + func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := pbrpc.RunServer(&pbrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:8002", + Address: testaddr, }, nil) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } @@ -39,7 +42,7 @@ func TestMain(m *testing.M) { func getKitexClient(p transport.Protocol) stservice.Client { return pbrpc.CreateKitexClient(&pbrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:8002"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: pbrpc.ConnectionMultiplexed, }) diff --git a/pbrpc/normalcall/normalcall_test.go b/pbrpc/normalcall/normalcall_test.go index bd3dee4..ebe6cfd 100644 --- a/pbrpc/normalcall/normalcall_test.go +++ b/pbrpc/normalcall/normalcall_test.go @@ -23,16 +23,20 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/stability/stservice" "github.com/cloudwego/kitex-tests/pbrpc" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/client/callopt" "github.com/cloudwego/kitex/transport" ) +var testaddr string + func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := pbrpc.RunServer(&pbrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:8001", + Address: testaddr, }, nil) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } @@ -40,7 +44,7 @@ func TestMain(m *testing.M) { func getKitexClient(p transport.Protocol) stservice.Client { return pbrpc.CreateKitexClient(&pbrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:8001"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: pbrpc.LongConnection, }) diff --git a/pbrpc/server.go b/pbrpc/server.go index ec54e9b..e751e0a 100644 --- a/pbrpc/server.go +++ b/pbrpc/server.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net" + "time" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/instparam" "github.com/cloudwego/kitex-tests/kitex_gen/protobuf/stability" @@ -49,6 +50,7 @@ func RunServer(param *ServerInitParam, handler stability.STService, opts ...serv panic(err) } + opts = append(opts, server.WithExitWaitTime(20*time.Millisecond)) opts = append(opts, server.WithServiceAddr(addr)) opts = append(opts, server.WithLimit(&limit.Option{ MaxConnections: 30000, MaxQPS: 300000, UpdateControl: func(u limit.Updater) {}, @@ -56,6 +58,7 @@ func RunServer(param *ServerInitParam, handler stability.STService, opts ...serv if param.ConnMode == ConnectionMultiplexed { opts = append(opts, server.WithMuxTransport()) } + if handler == nil { handler = new(STServiceHandler) } diff --git a/pkg/utils/client.go b/pkg/utils/clientutils/clientutils.go similarity index 99% rename from pkg/utils/client.go rename to pkg/utils/clientutils/clientutils.go index c8b70d8..a3c5aba 100644 --- a/pkg/utils/client.go +++ b/pkg/utils/clientutils/clientutils.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package utils +package clientutils import ( "io" diff --git a/pkg/utils/client_test.go b/pkg/utils/clientutils/clientutils_test.go similarity index 98% rename from pkg/utils/client_test.go rename to pkg/utils/clientutils/clientutils_test.go index c9d6db1..44510b0 100644 --- a/pkg/utils/client_test.go +++ b/pkg/utils/clientutils/clientutils_test.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package utils +package clientutils import ( "testing" diff --git a/pkg/utils/serverutils/serverutils.go b/pkg/utils/serverutils/serverutils.go new file mode 100644 index 0000000..fb578f1 --- /dev/null +++ b/pkg/utils/serverutils/serverutils.go @@ -0,0 +1,64 @@ +/* + * Copyright 2024 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 serverutils + +import ( + "fmt" + "log" + "net" + "runtime" + "sync/atomic" + "time" +) + +var listenStartPort = int32(10000) // no need `listenEndPort`? + +func init() { + // rand add 0, 300, 600, ... , 4800 for running tests in concurrency + listenStartPort += int32((time.Now().UnixNano() % 17) * 300) +} + +// NextListenAddr returns a local addr that can be used to call net.Listen +func NextListenAddr() string { + for i := 0; i < 100; i++ { + n := atomic.AddInt32(&listenStartPort, 1) + addr := fmt.Sprintf("localhost:%d", n) + ln, err := net.Listen("tcp", addr) + if err != nil { + continue + } + ln.Close() + return addr + } + panic("not able to get listen addr") +} + +// Wait waits utils it's able to connect to the given addr +func Wait(addr string) { + time.Sleep(5 * time.Millisecond) // likely it's up + _, file, no, _ := runtime.Caller(1) + for i := 0; i < 50; i++ { // 5s + if ln, err := net.Dial("tcp", addr); err == nil { + ln.Close() + log.Printf("server %s is up @ %s:%d", addr, file, no) + return + } + log.Printf("waiting server %s @ %s:%d", addr, file, no) + time.Sleep(100 * time.Millisecond) + } + panic("server " + addr + " not ready") +} diff --git a/pkg/utils/serverutils/serverutils_test.go b/pkg/utils/serverutils/serverutils_test.go new file mode 100644 index 0000000..615045c --- /dev/null +++ b/pkg/utils/serverutils/serverutils_test.go @@ -0,0 +1,36 @@ +/* + * Copyright 2024 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 serverutils + +import ( + "net" + "testing" +) + +func TestListenAndWait(t *testing.T) { + var err error + var ln net.Listener + addr := NextListenAddr() + go func() { + ln, err = net.Listen("tcp", addr) + }() + Wait(addr) // will panic if net.Listen err + if err != nil { + t.Fatal(err) + } + ln.Close() +} diff --git a/run.sh b/run.sh index ff292f7..e07727e 100755 --- a/run.sh +++ b/run.sh @@ -14,84 +14,93 @@ # limitations under the License. set -e -set -x - # add time for each command for timing export PS4='[$(date "+%F %T")] ' -export GO111MODULE=on - bits=$(getconf LONG_BIT) if [[ $bits != 64 ]]; then echo "this script runs on 64-bit architectures only" >&2 exit 1 fi -# Install protoc +set -x -get_protoc() { - os=$1 - arch=$2 - out=$3 - suffix=$(echo ${os}-${arch} | sed 's/darwin/osx/' | sed 's/amd64/x86_64/' | sed 's/arm64/aarch_64/') - release=protoc-${PROTOC_VERSION#v}-${suffix}.zip - url=https://github.com/protocolbuffers/protobuf/releases/download/${PROTOC_VERSION}/${release} - wget -q $url || exit 1 - python -m zipfile -e $release $os || exit 1 - chmod +x $os/bin/protoc - mv $os/bin/protoc $out/protoc-${os}-${arch} && rm -rf $os $release -} +PROTOC_OUT=$PWD/bin +PROTOC_VERSION=v3.13.0 # FIXME: this version doesn't support darwin arm64 install_protoc() { - PROTOC_VERSION=v3.13.0 - OUT=$(pwd)/bin - export PATH=$OUT:$PATH - mkdir -p $OUT - - get_protoc darwin amd64 $OUT - get_protoc linux amd64 $OUT - get_protoc linux arm64 $OUT - for p in $OUT/protoc-*; do - "$p" --version 2>/dev/null && ln -s $(basename $p) $OUT/protoc || true - done + echo "installing protoc ... " + os=`uname -s | sed 's/Darwin/osx/'` + arch=`uname -m | sed 's/amd64/x86_64/' | sed 's/arm64/aarch_64/'` + suffix=$(echo ${os}-${arch}) + filename=protoc-${PROTOC_VERSION#v}-${suffix}.zip + url=https://github.com/protocolbuffers/protobuf/releases/download/${PROTOC_VERSION}/${filename} + rm -f $filename + wget -q $url || exit 1 + unzip -o -q $filename -d ./tmp/ + mkdir -p $PROTOC_OUT + mv ./tmp/bin/protoc $PROTOC_OUT + rm -rf ./tmp/ + echo "installing protoc ... done" } go_install() { + echo "installing $@ ..." go install $@ || go get $@ + echo "installing $@ ... done" } kitex_cmd() { kitex --no-dependency-check $@ } -which protoc || install_protoc +export PATH=$PROTOC_OUT:$PATH + +echo -e "\ninstalling missing commands\n" + +# install protoc +which protoc || install_protoc & # install protoc-gen-go and protoc-gen-go-kitexgrpc -which protoc-gen-go || go_install google.golang.org/protobuf/cmd/protoc-gen-go@latest +which protoc-gen-go || go_install google.golang.org/protobuf/cmd/protoc-gen-go@latest & + # install protoc-gen-go and protoc-gen-go-kitexgrpc -which protoc-gen-go-grpc || go_install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest +which protoc-gen-go-grpc || go_install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest & -# Install thriftgo -which thriftgo || go_install github.com/cloudwego/thriftgo@latest +# install thriftgo +which thriftgo || go_install github.com/cloudwego/thriftgo@latest & -# Install kitex and generate codes +# install kitex LOCAL_REPO=$1 if [[ -n $LOCAL_REPO ]]; then cd ${LOCAL_REPO} - go_install ${LOCAL_REPO}/tool/cmd/kitex + go_install ${LOCAL_REPO}/tool/cmd/kitex & cd - else - go_install github.com/cloudwego/kitex/tool/cmd/kitex@develop + go_install github.com/cloudwego/kitex/tool/cmd/kitex@develop & fi +wait +echo -e "\ninstalling missing commands ... done\n" + +# double check commands, +# set -e may not working since commands run in background +which protoc +which protoc-gen-go +which protoc-gen-go-grpc +which thriftgo +which kitex + kitex -version rm -f go.mod # go mod init fails if it already exists - go mod init github.com/cloudwego/kitex-tests -test -d kitex_gen && rm -rf kitex_gen + +echo -e "\ngenerating code for testing ...\n" + +rm -rf kitex_gen kitex_cmd -module github.com/cloudwego/kitex-tests ./idl/stability.thrift kitex_cmd -module github.com/cloudwego/kitex-tests ./idl/http.thrift kitex_cmd -module github.com/cloudwego/kitex-tests ./idl/tenant.thrift @@ -106,19 +115,24 @@ kitex_cmd -module github.com/cloudwego/kitex-tests -I idl ./idl/grpc_multi_servi kitex_cmd -module github.com/cloudwego/kitex-tests -I idl ./idl/pb_multi_service.proto kitex_cmd -module github.com/cloudwego/kitex-tests -I idl -combine-service ./idl/combine_service.proto -test -d kitex_gen_slim && rm -rf kitex_gen_slim +rm -rf kitex_gen_slim kitex_cmd -module github.com/cloudwego/kitex-tests -thrift template=slim -gen-path kitex_gen_slim ./idl/stability.thrift -test -d kitex_gen_noDefSerdes && rm -rf kitex_gen_noDefSerdes +rm -rf kitex_gen_noDefSerdes kitex_cmd -module github.com/cloudwego/kitex-tests -thrift no_default_serdes -gen-path kitex_gen_noDefSerdes ./idl/stability.thrift -# generate thrift streaming code -LOCAL_REPO=$LOCAL_REPO ./thrift_streaming/generate.sh -test -d grpc_gen && rm -rf grpc_gen +rm -rf grpc_gen mkdir grpc_gen protoc --go_out=grpc_gen/. ./idl/grpc_demo_2.proto protoc --go-grpc_out=grpc_gen/. ./idl/grpc_demo_2.proto +# generate thrift streaming code +LOCAL_REPO=$LOCAL_REPO ./thrift_streaming/generate.sh + +echo -e "\ngenerating code for testing ... done\n" + + +echo -e "\nupdating dependencies ... \n" # Init dependencies go get github.com/apache/thrift@v0.13.0 @@ -132,44 +146,26 @@ fi go mod tidy -# static check -go vet -stdmethods=false $(go list ./...) -#go_install mvdan.cc/gofumpt@v0.2.0 -#test -z "$(gofumpt -l -extra .)" +echo -e "\nupdating dependencies ... done\n" # run tests -packages=( -./thriftrpc/normalcall/... -./thriftrpc/muxcall/... -./thriftrpc/retrycall/... -./thriftrpc/failedcall/... -./thriftrpc/multiservicecall/... -./thriftrpc/failedmux/... -./thriftrpc/abctest/... -./thriftrpc/utiltest/... -./pbrpc/normalcall/... -./pbrpc/muxcall/... -./pbrpc/failedcall/... -./pbrpc/multiservicecall/... -./generic/http/... -./generic/map/... -./kitexgrpc/... -./thrift_streaming/... -) - -for pkg in ${packages[@]} -do - if [[ -n $LOCAL_REPO ]]; then - go test -covermode=atomic -coverprofile=${LOCAL_REPO}/coverage.txt.tmp -coverpkg=github.com/cloudwego/kitex/... $pkg - if [[ "$OSTYPE" =~ ^darwin ]]; - then - sed -i '' 1d ${LOCAL_REPO}/coverage.txt.tmp - else - sed -i '1d' ${LOCAL_REPO}/coverage.txt.tmp - fi - cat ${LOCAL_REPO}/coverage.txt.tmp >> ${LOCAL_REPO}/coverage.txt - rm ${LOCAL_REPO}/coverage.txt.tmp - else - go test $pkg - fi -done +# use less cores for stability +# coz some of them are logical cores +nproc=$(nproc) +export GOMAXPROCS=$(( $nproc / 2 )) + +echo -e "\nrunning tests ... \n" + +if [[ -n $LOCAL_REPO ]]; then + go test -failfast -covermode=atomic -coverprofile=${LOCAL_REPO}/coverage.txt -coverpkg=github.com/cloudwego/kitex/... ./... + if [[ "$OSTYPE" =~ ^darwin ]]; # remove `mode: atomic` line + then + sed -i '' 1d ${LOCAL_REPO}/coverage.txt + else + sed -i '1d' ${LOCAL_REPO}/coverage.txt + fi +else + go test -failfast ./... +fi + +echo "PASSED" diff --git a/thrift_streaming/exitserver/go.mod b/thrift_streaming/exitserver/go.mod index bc07e74..b11d672 100644 --- a/thrift_streaming/exitserver/go.mod +++ b/thrift_streaming/exitserver/go.mod @@ -1,8 +1,6 @@ module github.com/cloudwego/kitex-tests/thrift_streaming/exitserver -go 1.16 - -replace github.com/apache/thrift => github.com/apache/thrift v0.13.0 +go 1.21.13 require ( github.com/apache/thrift v0.13.0 @@ -11,14 +9,17 @@ require ( require ( github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect - github.com/bytedance/sonic v1.10.2 // indirect + github.com/bytedance/sonic v1.12.3 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect - github.com/choleraehyq/pid v0.0.18 // indirect + github.com/choleraehyq/pid v0.0.19 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/configmanager v0.2.0 // indirect github.com/cloudwego/dynamicgo v0.2.0 // indirect github.com/cloudwego/fastpb v0.0.4 // indirect github.com/cloudwego/frugal v0.1.13 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect github.com/cloudwego/localsession v0.0.2 // indirect github.com/cloudwego/netpoll v0.5.1 // indirect github.com/cloudwego/thriftgo v0.3.6 // indirect @@ -49,3 +50,5 @@ require ( google.golang.org/protobuf v1.28.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/apache/thrift => github.com/apache/thrift v0.13.0 diff --git a/thrift_streaming/exitserver/go.sum b/thrift_streaming/exitserver/go.sum index 0539f80..1465fcb 100644 --- a/thrift_streaming/exitserver/go.sum +++ b/thrift_streaming/exitserver/go.sum @@ -25,8 +25,12 @@ github.com/bytedance/mockey v1.2.7/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+i github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.8.8/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= -github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU= +github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= @@ -40,12 +44,15 @@ github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI github.com/choleraehyq/pid v0.0.13/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= github.com/choleraehyq/pid v0.0.15/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= github.com/choleraehyq/pid v0.0.16/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= -github.com/choleraehyq/pid v0.0.18 h1:O7LLxPoOyt3YtonlCC8BmNrF9P6Hc8B509UOqlPSVhw= github.com/choleraehyq/pid v0.0.18/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= +github.com/choleraehyq/pid v0.0.19 h1:7lV897BV8V1OBFecw5cpbc0cChpE+HbQCFNE7E3bjQY= +github.com/choleraehyq/pid v0.0.19/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY= github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic= github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/configmanager v0.2.0 h1:niVpVg+wQ+npNqnH3dup96SMbR02Pk+tNErubYCJqKo= github.com/cloudwego/configmanager v0.2.0/go.mod h1:FLIQTjxsZRGjnmDhTttWQTy6f6DghPTatfBVOs2gQLk= github.com/cloudwego/dynamicgo v0.1.0/go.mod h1:Mdsz0XGsIImi15vxhZaHZpspNChEmBMIiWkUfD6JDKg= @@ -58,6 +65,8 @@ github.com/cloudwego/frugal v0.1.3/go.mod h1:b981ViPYdhI56aFYsoMjl9kv6yeqYSO+iEz github.com/cloudwego/frugal v0.1.6/go.mod h1:9ElktKsh5qd2zDBQ5ENhPSQV7F2dZ/mXlr1eaZGDBFs= github.com/cloudwego/frugal v0.1.13 h1:s2G93j/DqANEUnYpvdf3mz760yGdCGs5o3js7dNU4Ig= github.com/cloudwego/frugal v0.1.13/go.mod h1:zFBA63ne4+Tz4qayRZFZf+ZVwGqTzb+1Xe3ZDCq+Wfc= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/cloudwego/kitex v0.3.2/go.mod h1:/XD07VpUD9VQWmmoepASgZ6iw//vgWikVA9MpzLC5i0= github.com/cloudwego/kitex v0.4.4/go.mod h1:3FcH5h9Qw+dhRljSzuGSpWuThttA8DvK0BsL7HUYydo= github.com/cloudwego/kitex v0.6.1/go.mod h1:zI1GBrjT0qloTikcCfQTgxg3Ws+yQMyaChEEOcGNUvA= diff --git a/thrift_streaming/exitserver/update_go_mod.sh b/thrift_streaming/exitserver/update_go_mod.sh new file mode 100755 index 0000000..383e5a2 --- /dev/null +++ b/thrift_streaming/exitserver/update_go_mod.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# +# Copyright 2024 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. + +# This script is used for updating go.mod +# Some packages like bytedance/sonic may not compatible with latest Go version, +# and we have to update go.mod regularly + +rm -f go.mod +rm -f go.sum +go mod init github.com/cloudwego/kitex-tests/thrift_streaming/exitserver + +# using legacy kitex version on purpose +go get github.com/cloudwego/kitex@v0.9.0-rc4 +go get github.com/apache/thrift@v0.13.0 +go mod edit -replace github.com/apache/thrift=github.com/apache/thrift@v0.13.0 + +# updating tricky packages to make sure it works ... +go get github.com/bytedance/sonic@latest +go get github.com/choleraehyq/pid@latest + +go mod tidy diff --git a/thrift_streaming/generate.sh b/thrift_streaming/generate.sh index 8a37d64..482be2b 100755 --- a/thrift_streaming/generate.sh +++ b/thrift_streaming/generate.sh @@ -17,6 +17,8 @@ # Regenerate kitex_gen* directories when there's any related change to codegen (both kitex&thriftgo) +export PS4='[thrift_streaming][$(date "+%F %T")] ' + cd `dirname $0` ROOT=`pwd` @@ -43,7 +45,7 @@ kitex_cmd() { # generate with old kitex and thriftgo WITHOUT thrift streaming support function generate_old() { - echo -e "\n\ngenerate_old\n" + echo -e "\ngenerate_old\n" dir=$OLD export PATH=$OLD:$SAVE_PATH @@ -67,7 +69,7 @@ function generate_old() { } function generate_new() { - echo -e "\n\ngenerate_new\n" + echo -e "\ngenerate_new\n" dir=$NEW export PATH=$dir:$SAVE_PATH @@ -109,7 +111,7 @@ function generate_new() { } function generate_new_thriftgo_old_kitex() { - echo -e "\n\ngenerate_new_thriftgo_old_kitex\n" + echo -e "\ngenerate_new_thriftgo_old_kitex\n" dir=$NEW_THRIFTGO_OLD_KITEX export PATH=$dir:$SAVE_PATH @@ -146,14 +148,6 @@ if [ ! -z "$TEST_GENERATE_OLD" ]; then fi cd exitserver -# XXX: sonic is not compatible with latest Go version... -# but updating the pkg will cause changes of go.mod & go.sum -cp go.mod go.mod.bak -cp go.sum go.sum.bak -go get -u github.com/bytedance/sonic -go get -u github.com/choleraehyq/pid +echo -e "\nbuilding exitserver @ $PWD\nPlease run ./update_go_mod.sh if any Go compatibility issue\n" go build -# recover the change above, make sure git diff is clear -mv go.mod.bak go.mod -mv go.sum.bak go.sum mv exitserver $ROOT/binaries diff --git a/thrift_streaming/grpcpb_test.go b/thrift_streaming/grpcpb_test.go index e251f57..d397594 100644 --- a/thrift_streaming/grpcpb_test.go +++ b/thrift_streaming/grpcpb_test.go @@ -30,6 +30,7 @@ import ( "github.com/cloudwego/kitex/server" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/grpc_pb" grpcpbservice "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/grpc_pb/pbservice" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/kitex_pb" @@ -233,7 +234,7 @@ func TestGRPCPBStreamClient(t *testing.T) { } func TestGRPCPBServerMiddleware(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunGRPCPBServer(&GRPCPBServiceImpl{}, addr, server.WithMiddleware(func(e endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { @@ -350,7 +351,7 @@ func TestGRPCPBServerMiddleware(t *testing.T) { } func TestGRPCPBServiceWithCompatibleMiddleware(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunGRPCPBServer( &GRPCPBServiceImpl{}, addr, diff --git a/thrift_streaming/main_test.go b/thrift_streaming/main_test.go index 30b1b26..2770b24 100644 --- a/thrift_streaming/main_test.go +++ b/thrift_streaming/main_test.go @@ -17,13 +17,11 @@ package thrift_streaming import ( "log" "net" - "strconv" - "sync/atomic" "testing" "time" "github.com/cloudwego/kitex" - "github.com/cloudwego/kitex-tests/common" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/combine/combineservice" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/echo" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/echo/echoservice" @@ -53,7 +51,7 @@ func RunThriftServer(handler echo.EchoService, addr string, opts ...server.Optio panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } @@ -66,7 +64,7 @@ func RunCombineThriftServer(handler combineservice.CombineService, addr string, panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } @@ -79,7 +77,7 @@ func RunThriftCrossServer(handler cross_echo.EchoService, addr string, opts ...s panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } @@ -92,7 +90,7 @@ func RunGRPCPBServer(handler grpc_pb.PBService, addr string, opts ...server.Opti panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } @@ -105,27 +103,26 @@ func RunKitexPBServer(handler kitex_pb.PBService, addr string, opts ...server.Op panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } var ( - initialPort = int32(9000) - thriftAddr = addrAllocator() - crossAddr = addrAllocator() - slimAddr = addrAllocator() - grpcAddr = addrAllocator() - pbAddr = addrAllocator() - combineAddr = addrAllocator() + thriftAddr string + crossAddr string + slimAddr string + grpcAddr string + pbAddr string + combineAddr string ) -func addrAllocator() string { - addr := "127.0.0.1:" + strconv.Itoa(int(atomic.LoadInt32(&initialPort))) - atomic.AddInt32(&initialPort, 1) - return addr -} - func TestMain(m *testing.M) { + thriftAddr = serverutils.NextListenAddr() + crossAddr = serverutils.NextListenAddr() + slimAddr = serverutils.NextListenAddr() + grpcAddr = serverutils.NextListenAddr() + pbAddr = serverutils.NextListenAddr() + combineAddr = serverutils.NextListenAddr() var thriftSvr, thriftCrossSvr, slimServer, grpcServer, pbServer, combineServer server.Server go func() { thriftSvr = RunThriftServer(&EchoServiceImpl{}, thriftAddr) }() go func() { thriftCrossSvr = RunThriftCrossServer(&CrossEchoServiceImpl{}, crossAddr) }() @@ -153,13 +150,6 @@ func TestMain(m *testing.M) { combineServer.Stop() } }() - common.WaitServer(thriftAddr) - common.WaitServer(crossAddr) - common.WaitServer(grpcAddr) - common.WaitServer(pbAddr) - common.WaitServer(slimAddr) - common.WaitServer(combineAddr) log.Printf("testing Kitex %s", kitex.Version) - m.Run() } diff --git a/thrift_streaming/thrift_slim_others_test.go b/thrift_streaming/thrift_slim_others_test.go deleted file mode 100644 index 7ff9247..0000000 --- a/thrift_streaming/thrift_slim_others_test.go +++ /dev/null @@ -1,29 +0,0 @@ -//go:build !amd64 || windows || !go1.16 || go1.22 || disablefrugal -// +build !amd64 windows !go1.16 go1.22 disablefrugal - -// Copyright 2023 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 thrift_streaming - -import ( - "github.com/cloudwego/kitex/server" - - "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen_slim/echo" -) - -func RunSlimThriftServer(handler echo.EchoService, addr string, opts ...server.Option) server.Server { - // frugal + slim not available, return an empty server - return server.NewServer() -} diff --git a/thrift_streaming/thrift_slim_amd64_test.go b/thrift_streaming/thrift_slim_test.go similarity index 98% rename from thrift_streaming/thrift_slim_amd64_test.go rename to thrift_streaming/thrift_slim_test.go index 866be66..7e89804 100644 --- a/thrift_streaming/thrift_slim_amd64_test.go +++ b/thrift_streaming/thrift_slim_test.go @@ -1,6 +1,3 @@ -//go:build amd64 && !windows && go1.16 && !go1.22 && !disablefrugal -// +build amd64,!windows,go1.16,!go1.22,!disablefrugal - // Copyright 2023 CloudWeGo Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,8 +32,8 @@ import ( "github.com/cloudwego/kitex/server" "github.com/cloudwego/kitex/transport" - "github.com/cloudwego/kitex-tests/common" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen_slim/echo" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen_slim/echo/echoservice" ) @@ -53,7 +50,7 @@ func RunSlimThriftServer(handler echo.EchoService, addr string, opts ...server.O panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } @@ -357,7 +354,7 @@ func TestSlimKitexStreamClientMiddlewareServer(t *testing.T) { } func TestSlimKitexServerMiddleware(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() t.Run("pingpong", func(t *testing.T) { svr := RunSlimThriftServer(&SlimEchoServiceImpl{}, addr, svrCodecOpt, server.WithMiddleware(func(e endpoint.Endpoint) endpoint.Endpoint { diff --git a/thrift_streaming/thrift_test.go b/thrift_streaming/thrift_test.go index 8c42892..21cf16a 100644 --- a/thrift_streaming/thrift_test.go +++ b/thrift_streaming/thrift_test.go @@ -28,7 +28,6 @@ import ( "time" "github.com/bytedance/gopkg/cloud/metainfo" - "github.com/cloudwego/kitex-tests/common" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/a/b/c" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/combine" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/combine/combineservice" @@ -46,6 +45,7 @@ import ( "github.com/cloudwego/kitex/transport" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/echo" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/echo/echoservice" ) @@ -339,7 +339,7 @@ func TestKitexStreamClientMiddlewareServer(t *testing.T) { } func TestKitexServerMiddleware(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() t.Run("pingpong", func(t *testing.T) { svr := RunThriftServer(&EchoServiceImpl{}, addr, server.WithMiddleware(func(e endpoint.Endpoint) endpoint.Endpoint { @@ -610,7 +610,7 @@ func TestTimeoutRecvSend(t *testing.T) { }) t.Run("client recv timeout with MetaHandler", func(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer(&thriftServerTimeoutImpl{ Handler: func(stream echo.EchoService_EchoBidirectionalServer) (err error) { time.Sleep(time.Millisecond * 100) @@ -635,7 +635,7 @@ func TestTimeoutRecvSend(t *testing.T) { }) t.Run("server recv timeout with MetaHandler", func(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer( &thriftServerTimeoutImpl{ Handler: func(stream echo.EchoService_EchoBidirectionalServer) (err error) { @@ -708,7 +708,7 @@ func TestThriftStreamingMetaData(t *testing.T) { "1_x": []string{"14"}, } - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer( &thriftServerTimeoutImpl{ @@ -804,7 +804,7 @@ func TestThriftStreamingMetaData(t *testing.T) { }) t.Run("header-failure", func(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer( &thriftServerTimeoutImpl{ Handler: func(stream echo.EchoService_EchoBidirectionalServer) (err error) { @@ -838,7 +838,7 @@ func TestThriftStreamingMetaData(t *testing.T) { }) t.Run("trailer-failure", func(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer( &thriftServerTimeoutImpl{ Handler: func(stream echo.EchoService_EchoBidirectionalServer) (err error) { @@ -902,7 +902,7 @@ func TestThriftStreamLogID(t *testing.T) { return got, got == expectedLogID } - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer( &thriftServerTimeoutImpl{ Handler: func(stream echo.EchoService_EchoBidirectionalServer) (err error) { @@ -995,7 +995,7 @@ func RunABCServer(handler echo.ABCService, addr string, opts ...server.Option) s panic(err) } }() - common.WaitServer(addr) + serverutils.Wait(addr) return svr } @@ -1037,7 +1037,7 @@ func (a abcServerImpl) EchoUnary(ctx context.Context, req1 *c.Request) (r *c.Res } func TestABCService(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunABCServer(&abcServerImpl{}, addr) defer svr.Stop() t.Run("echo", func(t *testing.T) { @@ -1084,7 +1084,7 @@ func TestABCService(t *testing.T) { func TestCustomMetaHandler(t *testing.T) { t.Run("OnReadStream", func(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() var value atomic.Value var expectedValue = "value" svr := RunABCServer(&abcServerImpl{}, addr, diff --git a/thrift_streaming/thrift_tracing_test.go b/thrift_streaming/thrift_tracing_test.go index 2d820bc..b6533ce 100644 --- a/thrift_streaming/thrift_tracing_test.go +++ b/thrift_streaming/thrift_tracing_test.go @@ -26,8 +26,8 @@ import ( "time" "github.com/bytedance/gopkg/cloud/metainfo" - "github.com/cloudwego/kitex-tests/common" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/echo" "github.com/cloudwego/kitex-tests/thrift_streaming/kitex_gen/echo/echoservice" "github.com/cloudwego/kitex/client/streamclient" @@ -173,7 +173,7 @@ func (w *wrapStreamWithDoFinish) DoFinish(err error) { } func TestTracerNormalEndOfStream(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() serverTracer := &testTracer{} svr := RunThriftServer( @@ -427,7 +427,7 @@ func TestTracingSendError(t *testing.T) { func TestTracingServerReturnError(t *testing.T) { mockError := fmt.Errorf("mock error") - addr := addrAllocator() + addr := serverutils.NextListenAddr() serverTracer := &testTracer{} svr := RunThriftServer(&thriftTraceHandler{ echoUnary: func(ctx context.Context, request *echo.EchoRequest) (*echo.EchoResponse, error) { @@ -512,7 +512,7 @@ func TestTracingServerReturnError(t *testing.T) { func TestTracingServerReturnBizError(t *testing.T) { mockError := kerrors.NewGRPCBizStatusError(100, "biz error") - addr := addrAllocator() + addr := serverutils.NextListenAddr() serverTracer := &testTracer{} svr := RunThriftServer(&thriftTraceHandler{ echoUnary: func(ctx context.Context, request *echo.EchoRequest) (*echo.EchoResponse, error) { @@ -600,7 +600,7 @@ func TestTracingServerReturnBizError(t *testing.T) { } func TestTracingClientTimeout(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer(&thriftTraceHandler{ echoUnary: func(ctx context.Context, request *echo.EchoRequest) (*echo.EchoResponse, error) { time.Sleep(time.Millisecond * 100) @@ -679,7 +679,7 @@ func TestTracingClientTimeout(t *testing.T) { } func TestTracingServerStop(t *testing.T) { - addr := "127.0.0.1:9999" + addr := serverutils.NextListenAddr() clientTracer := &testTracer{} cli := echoservice.MustNewStreamClient("server", streamclient.WithHostPorts(addr), @@ -689,7 +689,7 @@ func TestTracingServerStop(t *testing.T) { cmd := exec.Command("binaries/exitserver", "-addr", addr) err := cmd.Start() test.Assert(t, err == nil, err) - common.WaitServer(addr) + serverutils.Wait(addr) return cmd } @@ -778,7 +778,7 @@ func (c *customTracer) ReportStreamEvent(ctx context.Context, ri rpcinfo.RPCInfo } func TestTracerStreamEventEOF(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer(&thriftTraceHandler{ echoServer: func(request *echo.EchoRequest, st echo.EchoService_EchoServerServer) error { _ = st.Send(&echo.EchoResponse{Message: request.Message}) @@ -812,7 +812,7 @@ func TestTracerStreamEventEOF(t *testing.T) { } func TestTracerFinishStream(t *testing.T) { - addr := addrAllocator() + addr := serverutils.NextListenAddr() svr := RunThriftServer(&thriftTraceHandler{ echoServer: func(request *echo.EchoRequest, st echo.EchoService_EchoServerServer) error { _ = st.Send(&echo.EchoResponse{Message: request.Message}) diff --git a/thriftrpc/abctest/abc_test.go b/thriftrpc/abctest/abc_test.go index 19e0e85..5727b92 100644 --- a/thriftrpc/abctest/abc_test.go +++ b/thriftrpc/abctest/abc_test.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "testing" - "time" "github.com/bytedance/gopkg/cloud/metainfo" @@ -26,6 +25,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/remote" @@ -38,25 +38,31 @@ const ( persistentKV = "aservice_persist" ) +var testaddr1, testaddr2 string + func TestMain(m *testing.M) { - cli := getKitexClient(transport.TTHeader, client.WithHostPorts("localhost:9002")) + testaddr1 = serverutils.NextListenAddr() + testaddr2 = serverutils.NextListenAddr() + cli := getKitexClient(transport.TTHeader, client.WithHostPorts(testaddr2)) svrb := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9001", + Address: testaddr1, }, &stServiceHandler{cli: cli}, server.WithMetaHandler(testMetaHandler{})) svrc := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9002", + Address: testaddr2, }, &stServiceHandler{}) - time.Sleep(time.Second) + + serverutils.Wait(testaddr1) + serverutils.Wait(testaddr2) m.Run() svrb.Stop() svrc.Stop() } func TestTransientKV(t *testing.T) { - cli := getKitexClient(transport.TTHeader, client.WithHostPorts("localhost:9001")) + cli := getKitexClient(transport.TTHeader, client.WithHostPorts(testaddr1)) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) ctx = metainfo.WithValue(ctx, transientKV, transientKV) diff --git a/thriftrpc/failedcall/error_handler/errhandler_test.go b/thriftrpc/failedcall/error_handler/errhandler_test.go index 37884c6..80baeee 100644 --- a/thriftrpc/failedcall/error_handler/errhandler_test.go +++ b/thriftrpc/failedcall/error_handler/errhandler_test.go @@ -25,6 +25,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/client/callopt" @@ -38,20 +39,21 @@ import ( "github.com/cloudwego/kitex/transport" ) -var cli stservice.Client +var testaddr string func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9002", + Address: testaddr, }, &STServiceHandler{}, server.WithMetaHandler(transmeta.ServerTTHeaderHandler)) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } func TestHandlerReturnNormalError(t *testing.T) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = normalErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -67,7 +69,7 @@ func TestHandlerReturnNormalError(t *testing.T) { } func TestHandlerReturnTransError(t *testing.T) { - cli = getKitexClient(transport.Framed) + cli := getKitexClient(transport.Framed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = kitexTransErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -83,7 +85,7 @@ func TestHandlerReturnTransError(t *testing.T) { } func TestHandlerReturnStatusError(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = grpcStatus.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -99,7 +101,7 @@ func TestHandlerReturnStatusError(t *testing.T) { } func TestHandlerReturnBizStatusError(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = bizErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -113,7 +115,7 @@ func TestHandlerReturnBizStatusError(t *testing.T) { } func TestHandlerPanic(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = panicStr stResp, err := cli.TestSTReq(ctx, stReq) @@ -137,7 +139,7 @@ func TestFallback4PanicError(t *testing.T) { result.SetSuccess(&stability.STResponse{Str: fallbackStr}) return } - cli = getKitexClient(transport.TTHeader, client.WithFallback(fallback.ErrorFallback(fbFunc))) + cli := getKitexClient(transport.TTHeader, client.WithFallback(fallback.ErrorFallback(fbFunc))) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = panicStr stResp, err := cli.TestSTReq(ctx, stReq) @@ -151,7 +153,7 @@ func TestFallback4Timeout(t *testing.T) { test.Assert(t, errors.Is(err, kerrors.ErrRPCTimeout)) return &stability.STResponse{Str: fallbackStr}, nil } - cli = getKitexClient(transport.TTHeader, client.WithFallback(fallback.ErrorFallback(fallback.UnwrapHelper(fbFunc)))) + cli := getKitexClient(transport.TTHeader, client.WithFallback(fallback.ErrorFallback(fallback.UnwrapHelper(fbFunc)))) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = timeout waitMS := "100ms" @@ -174,7 +176,7 @@ func TestFallback4TimeoutWithCallopt(t *testing.T) { test.Assert(t, errors.Is(err, kerrors.ErrRPCTimeout)) return &stability.STResponse{Str: callFallbackStr}, nil } - cli = getKitexClient(transport.TTHeader, + cli := getKitexClient(transport.TTHeader, client.WithRPCTimeout(20*time.Millisecond), client.WithFallback(fallback.TimeoutAndCBFallback(fallback.UnwrapHelper(cliFBFunc)))) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) @@ -208,7 +210,7 @@ func TestFallbackEnableReportAsFallback(t *testing.T) { stReq.MockCost = &waitMS // case 1: no EnableReportAsFallback, errForReportIsNil is false - cli = getKitexClient(transport.TTHeader, + cli := getKitexClient(transport.TTHeader, client.WithRPCTimeout(20*time.Millisecond), client.WithFallback(fallback.TimeoutAndCBFallback(fallback.UnwrapHelper(cliFBFunc))), client.WithTracer(&mockTracer{startFunc: nil, finishFunc: tracerFinishFunc})) @@ -240,7 +242,7 @@ func TestFallback4Resp(t *testing.T) { stReq.Name = baseRespAsBizStatusErr // case 1: return mock resp - cli = getKitexClient(transport.Framed, + cli := getKitexClient(transport.Framed, client.WithFallback(fallback.NewFallbackPolicy(fallback.UnwrapHelper(fbFunc)))) stResp, err := cli.TestSTReq(ctx, stReq) test.Assert(t, err == nil) @@ -274,7 +276,7 @@ func getKitexClient(p transport.Protocol, opts ...client.Option) stservice.Clien opts = append(opts, client.WithMetaHandler(transmeta.ClientTTHeaderHandler), client.WithTransportProtocol(transport.TTHeader)) return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{":9002"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: thriftrpc.LongConnection, }, opts...) diff --git a/thriftrpc/failedcall/failedcall_test.go b/thriftrpc/failedcall/failedcall_test.go index 268ddba..a974a9f 100644 --- a/thriftrpc/failedcall/failedcall_test.go +++ b/thriftrpc/failedcall/failedcall_test.go @@ -24,6 +24,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" "github.com/cloudwego/kitex/client/callopt" "github.com/cloudwego/kitex/pkg/remote" @@ -32,8 +33,6 @@ import ( "github.com/cloudwego/kitex/transport" ) -var cli stservice.Client - type mockedCodec struct { remote.Codec } @@ -47,14 +46,17 @@ func (mc *mockedCodec) Name() string { return "mockedCodec" } +var testaddr string + func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9001", + Address: testaddr, }, nil, server.WithCodec(&mockedCodec{ Codec: codec.NewDefaultCodec(), })) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } @@ -62,7 +64,7 @@ func TestMain(m *testing.M) { func getKitexClient(p transport.Protocol) stservice.Client { return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{":9001"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: thriftrpc.LongConnection, }) @@ -70,7 +72,7 @@ func getKitexClient(p transport.Protocol) stservice.Client { // TestSTReq method mock STRequest param read failed in server func TestStTReq(t *testing.T) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) var opts []callopt.Option @@ -83,7 +85,7 @@ func TestStTReq(t *testing.T) { } func TestStTReqWithTTHeader(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq) @@ -93,7 +95,7 @@ func TestStTReqWithTTHeader(t *testing.T) { } func TestStTReqWithFramed(t *testing.T) { - cli = getKitexClient(transport.Framed) + cli := getKitexClient(transport.Framed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq, callopt.WithRPCTimeout(1*time.Second)) @@ -103,7 +105,7 @@ func TestStTReqWithFramed(t *testing.T) { } func TestStTReqWithTTHeaderFramed(t *testing.T) { - cli = getKitexClient(transport.TTHeaderFramed) + cli := getKitexClient(transport.TTHeaderFramed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq, callopt.WithRPCTimeout(1*time.Second)) @@ -114,7 +116,7 @@ func TestStTReqWithTTHeaderFramed(t *testing.T) { // TestObjReq method mock ObjResp read failed in client func TestObjReq(t *testing.T) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) ctx, objReq := thriftrpc.CreateObjReq(context.Background()) objReq.FlagMsg = "ObjReq" @@ -126,17 +128,15 @@ func TestObjReq(t *testing.T) { // oneway cannot read failed of server func TestVisitOneway(t *testing.T) { - cli = getKitexClient(transport.TTHeaderFramed) + cli := getKitexClient(transport.TTHeaderFramed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) err := cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) - time.Sleep(time.Second / 2) // wait for the TCP close signal from server ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) err = cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) - time.Sleep(time.Second / 2) // wait for the TCP close signal from server ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) err = cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) diff --git a/thriftrpc/failedmux/error_handler/errhandler_test.go b/thriftrpc/failedmux/error_handler/errhandler_test.go index 43ace06..636b303 100644 --- a/thriftrpc/failedmux/error_handler/errhandler_test.go +++ b/thriftrpc/failedmux/error_handler/errhandler_test.go @@ -18,31 +18,32 @@ import ( "context" "strings" "testing" - "time" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" "github.com/cloudwego/kitex/pkg/kerrors" "github.com/cloudwego/kitex/pkg/remote" "github.com/cloudwego/kitex/transport" ) -var cli stservice.Client +var testaddr string func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9001", + Address: testaddr, ConnMode: thriftrpc.ConnectionMultiplexed, }, &STServiceHandler{}) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } func TestHandlerReturnNormalError(t *testing.T) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = normalErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -58,7 +59,7 @@ func TestHandlerReturnNormalError(t *testing.T) { } func TestHandlerReturnTransError(t *testing.T) { - cli = getKitexClient(transport.Framed) + cli := getKitexClient(transport.Framed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = kitexTransErr.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -74,7 +75,7 @@ func TestHandlerReturnTransError(t *testing.T) { } func TestHandlerReturnStatusError(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = grpcStatus.Error() stResp, err := cli.TestSTReq(ctx, stReq) @@ -90,7 +91,7 @@ func TestHandlerReturnStatusError(t *testing.T) { } func TestHandlerPanic(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.Name = panicStr stResp, err := cli.TestSTReq(ctx, stReq) @@ -108,7 +109,7 @@ func TestHandlerPanic(t *testing.T) { func getKitexClient(p transport.Protocol) stservice.Client { return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:9001"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: thriftrpc.ConnectionMultiplexed, }) diff --git a/thriftrpc/failedmux/failedmux_test.go b/thriftrpc/failedmux/failedmux_test.go index 290f760..79be07f 100644 --- a/thriftrpc/failedmux/failedmux_test.go +++ b/thriftrpc/failedmux/failedmux_test.go @@ -18,12 +18,12 @@ import ( "context" "strings" "testing" - "time" "github.com/apache/thrift/lib/go/thrift" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" "github.com/cloudwego/kitex/pkg/remote" "github.com/cloudwego/kitex/pkg/remote/codec" @@ -31,8 +31,6 @@ import ( "github.com/cloudwego/kitex/transport" ) -var cli stservice.Client - type mockedCodec struct { remote.Codec } @@ -46,15 +44,18 @@ func (mc *mockedCodec) Name() string { return "mockedCodec" } +var testaddr string + func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9002", + Address: testaddr, ConnMode: thriftrpc.ConnectionMultiplexed, }, nil, server.WithCodec(&mockedCodec{ Codec: codec.NewDefaultCodec(), })) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } @@ -62,7 +63,7 @@ func TestMain(m *testing.M) { func getKitexClient(p transport.Protocol) stservice.Client { return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:9002"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: thriftrpc.ConnectionMultiplexed, }) @@ -70,7 +71,7 @@ func getKitexClient(p transport.Protocol) stservice.Client { // TestSTReq method mock STRequest param read failed in server func TestStTReqMux(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq) @@ -81,7 +82,7 @@ func TestStTReqMux(t *testing.T) { // TestObjReq method mock ObjResp read failed in client func TestObjReqMux(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, objReq := thriftrpc.CreateObjReq(context.Background()) objReq.FlagMsg = "ObjReq" @@ -93,7 +94,7 @@ func TestObjReqMux(t *testing.T) { // oneway cannot read failed of server func TestVisitOnewayMux(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) err := cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) diff --git a/thriftrpc/multiservicecall/multi_service_test.go b/thriftrpc/multiservicecall/multi_service_test.go index b32742c..a14b139 100644 --- a/thriftrpc/multiservicecall/multi_service_test.go +++ b/thriftrpc/multiservicecall/multi_service_test.go @@ -17,7 +17,6 @@ package multiservicecall import ( "context" "fmt" - "github.com/cloudwego/kitex-tests/kitex_gen/thrift/multi_service2" "net" "strings" "testing" @@ -34,8 +33,10 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/thrift/multi_service/servicea" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/multi_service/serviceb" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/multi_service/servicec" + "github.com/cloudwego/kitex-tests/kitex_gen/thrift/multi_service2" servicea2 "github.com/cloudwego/kitex-tests/kitex_gen/thrift/multi_service2/servicea" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" ) // ServiceAImpl implements the last servicea interface defined in the IDL. @@ -67,37 +68,40 @@ func (s *ServiceCImpl) Echo1(ctx context.Context, req *multi_service.Request) (r func GetServer(hostport string, opts ...server.Option) server.Server { addr, _ := net.ResolveTCPAddr("tcp", hostport) - opts = append(opts, server.WithServiceAddr(addr)) - + opts = append(opts, server.WithServiceAddr(addr), + server.WithExitWaitTime(20*time.Millisecond), + ) return server.NewServer(opts...) } func TestRegisterService(t *testing.T) { - testRegisterService(t, "localhost:9900") + testRegisterService(t, serverutils.NextListenAddr()) } func TestMuxRegisterService(t *testing.T) { - testRegisterService(t, "localhost:9901", server.WithMuxTransport()) + testRegisterService(t, serverutils.NextListenAddr(), server.WithMuxTransport()) } func TestMultiServiceWithRefuseTrafficWithoutServiceName(t *testing.T) { - testMultiServiceWithRefuseTrafficWithoutServiceName(t, "localhost:9902", server.WithRefuseTrafficWithoutServiceName()) + testMultiServiceWithRefuseTrafficWithoutServiceName(t, + serverutils.NextListenAddr(), server.WithRefuseTrafficWithoutServiceName()) } func TestMuxMultiServiceWithRefuseTrafficWithoutServiceName(t *testing.T) { - testMultiServiceWithRefuseTrafficWithoutServiceName(t, "localhost:9903", server.WithRefuseTrafficWithoutServiceName(), server.WithMuxTransport()) + testMultiServiceWithRefuseTrafficWithoutServiceName(t, serverutils.NextListenAddr(), + server.WithRefuseTrafficWithoutServiceName(), server.WithMuxTransport()) } func TestMultiService(t *testing.T) { - testMultiService(t, "localhost:9904") + testMultiService(t, serverutils.NextListenAddr()) } func TestMuxMultiService(t *testing.T) { - testMultiService(t, "localhost:9905", server.WithMuxTransport()) + testMultiService(t, serverutils.NextListenAddr(), server.WithMuxTransport()) } func TestMultiServiceWithCombineServiceClient(t *testing.T) { - ip := "localhost:9906" + ip := serverutils.NextListenAddr() svr := GetServer(ip) err := servicea.RegisterService(svr, new(ServiceAImpl)) test.Assert(t, err == nil) @@ -119,12 +123,12 @@ func TestMultiServiceWithCombineServiceClient(t *testing.T) { } func TestUnknownException(t *testing.T) { - ip := "localhost:9907" + ip := serverutils.NextListenAddr() svr := GetServer(ip) servicea.RegisterService(svr, new(ServiceAImpl)) go svr.Run() defer svr.Stop() - time.Sleep(100 * time.Millisecond) + serverutils.Wait(ip) clientB, err := serviceb.NewClient("ServiceB", client.WithHostPorts(ip), @@ -137,13 +141,13 @@ func TestUnknownException(t *testing.T) { } func TestUnknownExceptionWithMultiService(t *testing.T) { - hostport := "localhost:9908" + hostport := serverutils.NextListenAddr() svr := GetServer(hostport) servicea.RegisterService(svr, new(ServiceAImpl)) servicec.RegisterService(svr, new(ServiceCImpl), server.WithFallbackService()) go svr.Run() defer svr.Stop() - time.Sleep(100 * time.Millisecond) + serverutils.Wait(hostport) // unknown service error clientB, err := serviceb.NewClient("ServiceB", @@ -219,7 +223,7 @@ func testMultiService(t *testing.T, ip string, opts ...server.Option) { servicec.RegisterService(svr, new(ServiceCImpl), server.WithFallbackService()) go svr.Run() defer svr.Stop() - time.Sleep(100 * time.Millisecond) + serverutils.Wait(ip) req := &multi_service.Request{Message: "multi_service req"} diff --git a/thriftrpc/muxcall/thriftmux_test.go b/thriftrpc/muxcall/thriftmux_test.go index 1e2e8fb..ff8904a 100644 --- a/thriftrpc/muxcall/thriftmux_test.go +++ b/thriftrpc/muxcall/thriftmux_test.go @@ -18,30 +18,32 @@ import ( "context" "strconv" "testing" - "time" "github.com/bytedance/gopkg/cloud/metainfo" - "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" - "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" - "github.com/cloudwego/kitex-tests/pkg/test" - "github.com/cloudwego/kitex-tests/thriftrpc" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/client/callopt" "github.com/cloudwego/kitex/pkg/endpoint" "github.com/cloudwego/kitex/pkg/rpcinfo" "github.com/cloudwego/kitex/server" "github.com/cloudwego/kitex/transport" + + "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" + "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" + "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" + "github.com/cloudwego/kitex-tests/thriftrpc" ) -var cli stservice.Client +var testaddr string func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9002", + Address: testaddr, ConnMode: thriftrpc.ConnectionMultiplexed, }, nil) - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr.Stop() } @@ -49,13 +51,13 @@ func TestMain(m *testing.M) { func getKitexMuxClient(opts ...client.Option) stservice.Client { return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:9002"}, + HostPorts: []string{testaddr}, ConnMode: thriftrpc.ConnectionMultiplexed, }, opts...) } func TestStTReq(t *testing.T) { - cli = getKitexMuxClient() + cli := getKitexMuxClient() ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq) @@ -64,7 +66,7 @@ func TestStTReq(t *testing.T) { } func TestObjReq(t *testing.T) { - cli = getKitexMuxClient() + cli := getKitexMuxClient() ctx, objReq := thriftrpc.CreateObjReq(context.Background()) objReq.FlagMsg = "ObjReq" @@ -74,7 +76,7 @@ func TestObjReq(t *testing.T) { } func TestException(t *testing.T) { - cli = getKitexMuxClient() + cli := getKitexMuxClient() ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) _, err := cli.TestException(ctx, stReq) @@ -86,7 +88,7 @@ func TestException(t *testing.T) { } func TestVisitOneway(t *testing.T) { - cli = getKitexMuxClient() + cli := getKitexMuxClient() ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) err := cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) @@ -96,10 +98,11 @@ func TestDisableRPCInfoReuse(t *testing.T) { backupState := rpcinfo.PoolEnabled() defer rpcinfo.EnablePool(backupState) + addr := serverutils.NextListenAddr() var ri rpcinfo.RPCInfo svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9003", + Address: addr, ConnMode: thriftrpc.ConnectionMultiplexed, }, nil, server.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) error { @@ -107,28 +110,28 @@ func TestDisableRPCInfoReuse(t *testing.T) { return next(ctx, req, resp) } })) - time.Sleep(time.Second) defer svr.Stop() + serverutils.Wait(addr) - cli = getKitexMuxClient(client.WithTransportProtocol(transport.TTHeaderFramed)) + cli := getKitexMuxClient(client.WithTransportProtocol(transport.TTHeaderFramed)) ctx, stReq := thriftrpc.CreateSTRequest(metainfo.WithBackwardValues(context.Background())) t.Run("reuse", func(t *testing.T) { - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort("localhost:9003")) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) test.Assert(t, ri.Invocation().MethodName() == "", ri.Invocation().MethodName()) // zeroed }) t.Run("disable reuse", func(t *testing.T) { rpcinfo.EnablePool(false) - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort("localhost:9003")) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) test.Assert(t, ri.Invocation().MethodName() != "", ri.Invocation().MethodName()) }) } func BenchmarkMuxCall(b *testing.B) { - cli = getKitexMuxClient() + cli := getKitexMuxClient() ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) ctx, objReq := thriftrpc.CreateObjReq(context.Background()) @@ -149,7 +152,7 @@ func BenchmarkMuxCall(b *testing.B) { } func BenchmarkMuxCallParallel(b *testing.B) { - cli = getKitexMuxClient() + cli := getKitexMuxClient() b.ReportAllocs() b.ResetTimer() diff --git a/thriftrpc/normalcall/normalcall_test.go b/thriftrpc/normalcall/normalcall_test.go index 2ae5392..798d28e 100644 --- a/thriftrpc/normalcall/normalcall_test.go +++ b/thriftrpc/normalcall/normalcall_test.go @@ -30,6 +30,7 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/thrift/instparam" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/client/callopt" "github.com/cloudwego/kitex/pkg/circuitbreak" @@ -42,7 +43,6 @@ import ( "github.com/cloudwego/kitex/server" "github.com/cloudwego/kitex/transport" - "github.com/cloudwego/kitex-tests/common" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" stservice_noDefSerdes "github.com/cloudwego/kitex-tests/kitex_gen_noDefSerdes/thrift/stability/stservice" @@ -51,67 +51,24 @@ import ( "github.com/cloudwego/kitex-tests/thriftrpc" ) -var ( - cli stservice.Client - cliSlim stservice_slim.Client - cliNoDefSerdes stservice_noDefSerdes.Client - - addr = "127.0.0.1:9001" - disablePoolAddr = "127.0.0.1:9002" - muxAdr = "127.0.0.1:9003" // used in `muxcall` - slimFrugalAddr = "127.0.0.1:9004" - slimAddr = "127.0.0.1:9005" - serverTimeoutAddr = "127.0.0.1:9006" - noDefSerdesFrugalAddr = "127.0.0.1:9007" - noDefSerdesFastCodecAddr = "127.0.0.1:9008" - serverWithCRCAddr = "127.0.0.1:9009" -) +var testaddr string func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: addr, - }, nil) - - slimSvr := thriftrpc.RunSlimServer(&thriftrpc.ServerInitParam{ - Network: "tcp", - Address: slimAddr, + Address: testaddr, }, nil) - - slimSvrWithFrugalConfigured := thriftrpc.RunSlimServer(&thriftrpc.ServerInitParam{ - Network: "tcp", - Address: slimFrugalAddr, - }, nil, server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite|thrift.FrugalRead))) - - noDefSerdesSvrWithFrugalConfigured := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ - Network: "tcp", - Address: noDefSerdesFrugalAddr, - }, nil, server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite|thrift.FrugalRead|thrift.EnableSkipDecoder))) - - noDefSerdesSvcWithFastCodecConfigured := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ - Network: "tcp", - Address: noDefSerdesFastCodecAddr, - }, nil, server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FastWrite|thrift.FastRead|thrift.EnableSkipDecoder))) - - common.WaitServer(addr) - common.WaitServer(slimAddr) - common.WaitServer(slimFrugalAddr) - common.WaitServer(noDefSerdesFrugalAddr) - common.WaitServer(noDefSerdesFastCodecAddr) + serverutils.Wait(testaddr) m.Run() - svr.Stop() - slimSvr.Stop() - slimSvrWithFrugalConfigured.Stop() - noDefSerdesSvrWithFrugalConfigured.Stop() - noDefSerdesSvcWithFastCodecConfigured.Stop() } func getKitexClient(p transport.Protocol, opts ...client.Option) stservice.Client { return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{":9001"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: thriftrpc.LongConnection, }, opts...) @@ -152,7 +109,7 @@ func testObjArgs(t *testing.T, req *instparam.ObjReq, resp *instparam.ObjResp) { } func TestStTReq(t *testing.T) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq) @@ -169,7 +126,7 @@ func TestStTReq(t *testing.T) { } func TestStTReqWithTTHeader(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq) @@ -178,7 +135,7 @@ func TestStTReqWithTTHeader(t *testing.T) { } func TestStTReqWithFramed(t *testing.T) { - cli = getKitexClient(transport.Framed) + cli := getKitexClient(transport.Framed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq, callopt.WithRPCTimeout(1*time.Second)) @@ -187,7 +144,7 @@ func TestStTReqWithFramed(t *testing.T) { } func TestStTReqWithTTHeaderFramed(t *testing.T) { - cli = getKitexClient(transport.TTHeaderFramed) + cli := getKitexClient(transport.TTHeaderFramed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stResp, err := cli.TestSTReq(ctx, stReq, callopt.WithRPCTimeout(1*time.Second)) @@ -196,7 +153,7 @@ func TestStTReqWithTTHeaderFramed(t *testing.T) { } func TestObjReq(t *testing.T) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) ctx, objReq := thriftrpc.CreateObjReq(context.Background()) objReq.FlagMsg = "ObjReq" @@ -206,7 +163,7 @@ func TestObjReq(t *testing.T) { } func TestException(t *testing.T) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) _, err := cli.TestException(ctx, stReq, callopt.WithRPCTimeout(1*time.Second)) @@ -218,7 +175,7 @@ func TestException(t *testing.T) { } func TestVisitOneway(t *testing.T) { - cli = getKitexClient(transport.TTHeaderFramed) + cli := getKitexClient(transport.TTHeaderFramed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) err := cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) @@ -231,12 +188,12 @@ func TestVisitOneway(t *testing.T) { err = cli.VisitOneway(ctx, stReq) test.Assert(t, err == nil, err) - time.Sleep(200 * time.Millisecond) + time.Sleep(20 * time.Millisecond) test.Assert(t, atomic.LoadInt32(&thriftrpc.CheckNum) == int32(3)) } func TestRPCTimeoutPriority(t *testing.T) { - cli = getKitexClient(transport.TTHeaderFramed, client.WithRPCTimeout(500*time.Millisecond)) + cli := getKitexClient(transport.TTHeaderFramed, client.WithRPCTimeout(500*time.Millisecond)) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) durationStr := "300ms" stReq.MockCost = &durationStr @@ -254,7 +211,7 @@ func TestDisablePoolForRPCInfo(t *testing.T) { t.Run("client", func(t *testing.T) { var ri rpcinfo.RPCInfo ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - cli = getKitexClient(transport.TTHeaderFramed, client.WithMiddleware(func(endpoint endpoint.Endpoint) endpoint.Endpoint { + cli := getKitexClient(transport.TTHeaderFramed, client.WithMiddleware(func(endpoint endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { ri = rpcinfo.GetRPCInfo(ctx) return nil // no need to send the real request @@ -275,6 +232,7 @@ func TestDisablePoolForRPCInfo(t *testing.T) { }) t.Run("server", func(t *testing.T) { + disablePoolAddr := serverutils.NextListenAddr() var ri1, ri2 rpcinfo.RPCInfo svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: disablePoolAddr}, nil, server.WithMiddleware(func(endpoint endpoint.Endpoint) endpoint.Endpoint { @@ -289,10 +247,10 @@ func TestDisablePoolForRPCInfo(t *testing.T) { return err } })) - common.WaitServer(addr) defer svr.Stop() + serverutils.Wait(disablePoolAddr) - cli = getKitexClient(transport.TTHeaderFramed) + cli := getKitexClient(transport.TTHeaderFramed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) t.Run("enable", func(t *testing.T) { @@ -328,6 +286,22 @@ func TestDisablePoolForRPCInfo(t *testing.T) { // When using slim template and users do not func TestFrugalFallback(t *testing.T) { + slimAddr := serverutils.NextListenAddr() + s0 := thriftrpc.RunSlimServer(&thriftrpc.ServerInitParam{ + Network: "tcp", + Address: slimAddr, + }, nil) + defer s0.Stop() + serverutils.Wait(slimAddr) + + slimFrugalAddr := serverutils.NextListenAddr() + s1 := thriftrpc.RunSlimServer(&thriftrpc.ServerInitParam{ + Network: "tcp", + Address: slimFrugalAddr, + }, nil, server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite|thrift.FrugalRead))) + serverutils.Wait(slimFrugalAddr) + defer s1.Stop() + testCases := []struct { desc string hostPorts []string @@ -364,10 +338,10 @@ func TestFrugalFallback(t *testing.T) { } for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - cliSlim = getSlimKitexClient(transport.TTHeader, tc.hostPorts, tc.opts...) + cli := getSlimKitexClient(transport.TTHeader, tc.hostPorts, tc.opts...) ctx, stReq := thriftrpc.CreateSlimSTRequest(context.Background()) for i := 0; i < 3; i++ { - stResp, err := cliSlim.TestSTReq(ctx, stReq) + stResp, err := cli.TestSTReq(ctx, stReq) if tc.expectErr { test.Assert(t, err != nil, err) continue @@ -380,7 +354,7 @@ func TestFrugalFallback(t *testing.T) { } func TestCircuitBreakerCustomErrorTypeFunc(t *testing.T) { - cli = getKitexClient(transport.TTHeader, + cli := getKitexClient(transport.TTHeader, client.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { return nil @@ -409,7 +383,7 @@ func TestCircuitBreakerCustomErrorTypeFunc(t *testing.T) { } func TestCircuitBreakerCustomInstanceErrorTypeFunc(t *testing.T) { - cli = getKitexClient(transport.TTHeader, + cli := getKitexClient(transport.TTHeader, client.WithInstanceMW(func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { return nil @@ -438,6 +412,23 @@ func TestCircuitBreakerCustomInstanceErrorTypeFunc(t *testing.T) { } func TestNoDefaultSerdes(t *testing.T) { + frugalOnlyAddr := serverutils.NextListenAddr() + fastcodecOnlyAddr := serverutils.NextListenAddr() + s0 := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ + Network: "tcp", + Address: frugalOnlyAddr, + }, nil, server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite|thrift.FrugalRead|thrift.EnableSkipDecoder))) + defer s0.Stop() + + s1 := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ + Network: "tcp", + Address: fastcodecOnlyAddr, + }, nil, server.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FastWrite|thrift.FastRead|thrift.EnableSkipDecoder))) + defer s1.Stop() + + serverutils.Wait(frugalOnlyAddr) + serverutils.Wait(fastcodecOnlyAddr) + testCases := []struct { desc string hostPorts []string @@ -446,35 +437,35 @@ func TestNoDefaultSerdes(t *testing.T) { }{ { desc: "use FastCodec and SkipDecoder, connect to Frugal and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFrugalAddr}, + hostPorts: []string{frugalOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FastWrite | thrift.FastRead | thrift.EnableSkipDecoder)), }, }, { desc: "use Frugal and SkipDecoder, connect to Frugal and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFrugalAddr}, + hostPorts: []string{frugalOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite | thrift.FrugalRead | thrift.EnableSkipDecoder)), }, }, { desc: "use FastCodec and SkipDecoder, connect to FastCodec and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFastCodecAddr}, + hostPorts: []string{fastcodecOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FastWrite | thrift.FastRead | thrift.EnableSkipDecoder)), }, }, { desc: "use Frugal and SkipDecoder, connect to FastCodec and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFastCodecAddr}, + hostPorts: []string{fastcodecOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite | thrift.FrugalRead | thrift.EnableSkipDecoder)), }, }, { desc: "use FastCodec, connect to Frugal and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFrugalAddr}, + hostPorts: []string{frugalOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FastWrite | thrift.FastRead)), }, @@ -482,7 +473,7 @@ func TestNoDefaultSerdes(t *testing.T) { }, { desc: "use Frugal, connect to Frugal and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFrugalAddr}, + hostPorts: []string{frugalOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite | thrift.FrugalRead)), }, @@ -490,7 +481,7 @@ func TestNoDefaultSerdes(t *testing.T) { }, { desc: "use FastCodec, connect to FastCodec and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFastCodecAddr}, + hostPorts: []string{fastcodecOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FastWrite | thrift.FastRead)), }, @@ -498,7 +489,7 @@ func TestNoDefaultSerdes(t *testing.T) { }, { desc: "use Frugal, connect to FastCodec and SkipDecoder enabled server", - hostPorts: []string{noDefSerdesFastCodecAddr}, + hostPorts: []string{fastcodecOnlyAddr}, opts: []client.Option{ client.WithPayloadCodec(thrift.NewThriftCodecWithConfig(thrift.FrugalWrite | thrift.FrugalRead)), }, @@ -507,10 +498,10 @@ func TestNoDefaultSerdes(t *testing.T) { } for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { - cliNoDefSerdes = getNoDefSerdesKitexClient(transport.PurePayload, tc.hostPorts, tc.opts...) + cli := getNoDefSerdesKitexClient(transport.PurePayload, tc.hostPorts, tc.opts...) ctx, stReq := thriftrpc.CreateNoDefSerdesSTRequest(context.Background()) for i := 0; i < 3; i++ { - stResp, err := cliNoDefSerdes.TestSTReq(ctx, stReq) + stResp, err := cli.TestSTReq(ctx, stReq) if !tc.expectErr { test.Assert(t, err == nil, err) test.Assert(t, stReq.Str == stResp.Str) @@ -530,28 +521,29 @@ func TestCRC32PayloadValidator(t *testing.T) { // request server without crc32 check config ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) for i := 0; i < 10; i++ { - _, err := crcClient.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) + _, err := crcClient.TestSTReq(ctx, stReq, callopt.WithHostPort(testaddr)) test.Assert(t, err == nil, err) } }) t.Run("serverWithCRC", func(t *testing.T) { // request server with crc config + addr := serverutils.NextListenAddr() svrCodecOpt := server.WithCodec(codec.NewDefaultCodecWithConfig(codec.CodecConfig{CRC32Check: true})) - svrWithCRC := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: serverWithCRCAddr}, nil, svrCodecOpt) - common.WaitServer(serverWithCRCAddr) + svrWithCRC := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: addr}, nil, svrCodecOpt) defer svrWithCRC.Stop() + serverutils.Wait(addr) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) for i := 0; i < 10; i++ { - _, err := crcClient.TestSTReq(ctx, stReq, callopt.WithHostPort(serverWithCRCAddr)) + _, err := crcClient.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) } }) } func BenchmarkThriftCall(b *testing.B) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) ctx, objReq := thriftrpc.CreateObjReq(context.Background()) b.ReportAllocs() @@ -571,7 +563,7 @@ func BenchmarkThriftCall(b *testing.B) { } func BenchmarkThriftCallParallel(b *testing.B) { - cli = getKitexClient(transport.PurePayload) + cli := getKitexClient(transport.PurePayload) b.ReportAllocs() b.ResetTimer() @@ -592,7 +584,7 @@ func BenchmarkThriftCallParallel(b *testing.B) { } func BenchmarkTTHeaderParallel(b *testing.B) { - cli = getKitexClient(transport.TTHeader) + cli := getKitexClient(transport.TTHeader) b.ReportAllocs() b.ResetTimer() @@ -616,7 +608,7 @@ func BenchmarkTTHeaderParallel(b *testing.B) { } func BenchmarkThriftWithComplexData(b *testing.B) { - cli = getKitexClient(transport.Framed) + cli := getKitexClient(transport.Framed) ctx, objReq := createComplexObjReq(context.Background()) b.ReportAllocs() diff --git a/thriftrpc/normalcall/server_processing_timeout_test.go b/thriftrpc/normalcall/server_processing_timeout_test.go index 5e03fd8..1678be0 100644 --- a/thriftrpc/normalcall/server_processing_timeout_test.go +++ b/thriftrpc/normalcall/server_processing_timeout_test.go @@ -31,12 +31,17 @@ import ( "github.com/cloudwego/kitex/server" "github.com/cloudwego/kitex/transport" - "github.com/cloudwego/kitex-tests/common" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" ) +var ( + rpcTimeout = 10 * time.Millisecond + sleepTime = 20 * time.Millisecond +) + type timeoutHandler struct { stability.STService testSTReq func(context.Context, *stability.STRequest) (*stability.STResponse, error) @@ -76,131 +81,131 @@ func (t *timeoutMetaHandler) ReadMeta(ctx context.Context, msg remote.Message) ( func TestServerProcessingTimeout(t *testing.T) { t.Run("both-ttheader-meta-handler/not-enabled", func(t *testing.T) { - svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: serverTimeoutAddr}, + addr := serverutils.NextListenAddr() + svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: addr}, &timeoutHandler{ testSTReq: func(ctx context.Context, request *stability.STRequest) (*stability.STResponse, error) { _, ok := ctx.Deadline() test.Assert(t, !ok) - - time.Sleep(time.Millisecond * 50) + time.Sleep(sleepTime) return &stability.STResponse{Str: request.Str}, nil }, }, server.WithMetaHandler(transmeta.ServerTTHeaderHandler), // server.WithEnableContextTimeout(false), // disable by default ) - common.WaitServer(serverTimeoutAddr) + serverutils.Wait(addr) defer svr.Stop() cli := getKitexClient(transport.TTHeaderFramed, client.WithMetaHandler(transmeta.ClientTTHeaderHandler)) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(serverTimeoutAddr)) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) }) t.Run("both-ttheader-meta-handler/enabled", func(t *testing.T) { - svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: serverTimeoutAddr}, + addr := serverutils.NextListenAddr() + svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: addr}, &timeoutHandler{ testSTReq: func(ctx context.Context, request *stability.STRequest) (*stability.STResponse, error) { ddl, ok := ctx.Deadline() test.Assert(t, ok) test.Assert(t, ddl.After(time.Now())) - - time.Sleep(time.Millisecond * 50) + time.Sleep(sleepTime) return &stability.STResponse{Str: request.Str}, nil }, }, server.WithMetaHandler(transmeta.ServerTTHeaderHandler), server.WithEnableContextTimeout(true), ) - common.WaitServer(serverTimeoutAddr) + serverutils.Wait(addr) defer svr.Stop() cli := getKitexClient(transport.TTHeaderFramed, client.WithMetaHandler(transmeta.ClientTTHeaderHandler)) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(serverTimeoutAddr), callopt.WithRPCTimeout(time.Millisecond*10)) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr), callopt.WithRPCTimeout(rpcTimeout)) test.Assert(t, errors.Is(err, kerrors.ErrRPCTimeout), err) }) t.Run("client-set-ttheader/server-ttheader-meta-handler/not-enabled", func(t *testing.T) { - svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: serverTimeoutAddr}, + addr := serverutils.NextListenAddr() + svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: addr}, &timeoutHandler{ testSTReq: func(ctx context.Context, request *stability.STRequest) (*stability.STResponse, error) { _, ok := ctx.Deadline() test.Assert(t, !ok) - - time.Sleep(time.Millisecond * 50) + time.Sleep(sleepTime) return &stability.STResponse{Str: request.Str}, nil }, }, server.WithMetaHandler(transmeta.ServerTTHeaderHandler), // server.WithEnableContextTimeout(false), // default is false ) - common.WaitServer(serverTimeoutAddr) + serverutils.Wait(addr) defer svr.Stop() cli := getKitexClient(transport.TTHeaderFramed, client.WithMetaHandler(&timeoutMetaHandler{ - timeout: time.Millisecond * 10, + timeout: rpcTimeout, })) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(serverTimeoutAddr)) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) }) t.Run("client-set-timeout/server-ttheader-meta-handler/enabled", func(t *testing.T) { - svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: serverTimeoutAddr}, + addr := serverutils.NextListenAddr() + svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: addr}, &timeoutHandler{ testSTReq: func(ctx context.Context, request *stability.STRequest) (*stability.STResponse, error) { ddl, ok := ctx.Deadline() test.Assert(t, ok) test.Assert(t, ddl.After(time.Now())) - - time.Sleep(time.Millisecond * 50) + time.Sleep(sleepTime) return &stability.STResponse{Str: request.Str}, nil }, }, server.WithMetaHandler(transmeta.ServerTTHeaderHandler), server.WithEnableContextTimeout(true), ) - common.WaitServer(serverTimeoutAddr) + serverutils.Wait(addr) defer svr.Stop() cli := getKitexClient(transport.TTHeaderFramed, client.WithMetaHandler(&timeoutMetaHandler{ - timeout: time.Millisecond * 10, + timeout: rpcTimeout, })) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(serverTimeoutAddr)) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) // no timeout control in both client and server }) t.Run("client-not-set/server-set-timeout/enabled", func(t *testing.T) { - svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: serverTimeoutAddr}, + addr := serverutils.NextListenAddr() + svr := thriftrpc.RunServer(&thriftrpc.ServerInitParam{Network: "tcp", Address: addr}, &timeoutHandler{ testSTReq: func(ctx context.Context, request *stability.STRequest) (*stability.STResponse, error) { ddl, ok := ctx.Deadline() test.Assert(t, ok) test.Assert(t, ddl.After(time.Now())) - - time.Sleep(time.Millisecond * 50) + time.Sleep(sleepTime) return &stability.STResponse{Str: request.Str}, nil }, }, server.WithMetaHandler(&timeoutMetaHandler{ - timeout: time.Millisecond * 10, + timeout: rpcTimeout, }), server.WithEnableContextTimeout(true), ) - common.WaitServer(serverTimeoutAddr) + serverutils.Wait(addr) defer svr.Stop() cli := getKitexClient(transport.TTHeaderFramed) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(serverTimeoutAddr)) + _, err := cli.TestSTReq(ctx, stReq, callopt.WithHostPort(addr)) test.Assert(t, err == nil, err) // no timeout control in both client and server }) } diff --git a/thriftrpc/retrycall/mixedretry_test.go b/thriftrpc/retrycall/mixedretry_test.go index 0921b4c..6676926 100644 --- a/thriftrpc/retrycall/mixedretry_test.go +++ b/thriftrpc/retrycall/mixedretry_test.go @@ -38,20 +38,34 @@ import ( "github.com/cloudwego/kitex-tests/thriftrpc" ) -// Assuming the first request returns at 300ms, the second request costs 150ms -// Configuration: Timeout=200ms、MaxRetryTimes=2 BackupDelay=100ms -// - Mixed Retry: Success, cost 250ms -// - Failure Retry: Success, cost 350ms -// - Backup Retry: Failure, cost 200ms -func TestMockCase1WithDiffRetry(t *testing.T) { - timeout := 200 * time.Millisecond +func durAbs(d time.Duration) time.Duration { + if d < 0 { + return -d + } + return d +} + +// Assuming: +// - the first request costs 60ms (req1Sleep) +// - the second request costs 40ms (req2Sleep) +// Configuration: MaxRetryTimes=2, rpcTimeout=50ms, backupDelay=30ms +// - Mixed Retry: Success, cost backupDelay+req2Sleep +// - Failure Retry: Success, cost rpcTimeout+req2Sleep +// - Backup Retry: Failure, cost 2*backupDelay +func TestMockCaseWithDiffRetry(t *testing.T) { + // backupDelay < req2Sleep < rpcTimeout < req1Sleep + rpcTimeout := 50 * time.Millisecond + req1Sleep := rpcTimeout + 10*time.Millisecond // 60ms + req2Sleep := rpcTimeout - 10*time.Millisecond // 40ms + backupDelay := req2Sleep - 10*time.Millisecond // 30ms + controlCostMW := func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, args, result interface{}) (err error) { _, exit := rpcinfo.GetRPCInfo(ctx).To().Tag(rpcinfo.RetryTag) if !exit { - ctx = metainfo.WithValue(ctx, sleepTimeMsKey, "300") //ms + ctx = withSleepTime(ctx, req1Sleep) } else { - ctx = metainfo.WithValue(ctx, sleepTimeMsKey, "150") //ms + ctx = withSleepTime(ctx, req2Sleep) } return next(ctx, args, result) } @@ -59,22 +73,21 @@ func TestMockCase1WithDiffRetry(t *testing.T) { // mixed retry will success, latency is lowest t.Run("mixed retry", func(t *testing.T) { - mp := retry.NewMixedPolicy(100) + mp := retry.NewMixedPolicy(uint32(backupDelay / time.Millisecond)) mp.WithMaxRetryTimes(2) rCli := getKitexClient( transport.TTHeader, client.WithMixedRetry(mp), - client.WithRPCTimeout(timeout), + client.WithRPCTimeout(rpcTimeout), client.WithMiddleware(controlCostMW), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo start := time.Now() _, err := rCli.TestSTReq(ctx, stReq) - cost := time.Since(start) // 100+150 = 250 + cost := time.Since(start) test.Assert(t, err == nil, err) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-250.0) < 50.0, cost.Milliseconds()) + test.Assert(t, durAbs(cost-(backupDelay+req2Sleep)) < 10*time.Millisecond, cost) }) // failure retry will success, but latency is more than mixed retry @@ -84,155 +97,73 @@ func TestMockCase1WithDiffRetry(t *testing.T) { rCli := getKitexClient( transport.TTHeader, client.WithFailureRetry(fp), - client.WithRPCTimeout(timeout), + client.WithRPCTimeout(rpcTimeout), client.WithMiddleware(controlCostMW), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo start := time.Now() _, err := rCli.TestSTReq(ctx, stReq) cost := time.Since(start) test.Assert(t, err == nil, err) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-350.0) < 50.0, cost.Milliseconds()) + test.Assert(t, durAbs(cost-(rpcTimeout+req2Sleep)) < 10*time.Millisecond, cost) }) // backup request will failure t.Run("backup request", func(t *testing.T) { - bp := retry.NewBackupPolicy(100) + bp := retry.NewBackupPolicy(uint32(backupDelay / time.Millisecond)) bp.WithMaxRetryTimes(2) rCli := getKitexClient( transport.TTHeader, client.WithBackupRequest(bp), - client.WithRPCTimeout(timeout), + client.WithRPCTimeout(rpcTimeout), client.WithMiddleware(controlCostMW), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo start := time.Now() _, err := rCli.TestSTReq(ctx, stReq) cost := time.Since(start) test.Assert(t, err != nil, err) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-200.0) < 50.0, cost.Milliseconds()) - }) -} - -// Assuming the first request returns at 300ms, the second request cost 150ms -// Configuration: Timeout=300ms、MaxRetryTimes=2 BackupDelay=100ms -// - Mixed Retry: Success, cost 250ms (>timeout, same with Backup Retry) -// - Failure Retry: Success, cost 350ms -// - Backup Retry: Failure, cost 200ms (same with Mixed Retry) -func TestMockCase2WithDiffRetry(t *testing.T) { - timeout := 300 * time.Millisecond - controlCostMW := func(next endpoint.Endpoint) endpoint.Endpoint { - return func(ctx context.Context, args, result interface{}) (err error) { - _, exit := rpcinfo.GetRPCInfo(ctx).To().Tag(rpcinfo.RetryTag) - if !exit { - ctx = metainfo.WithValue(ctx, sleepTimeMsKey, "300") //ms - } else { - ctx = metainfo.WithValue(ctx, sleepTimeMsKey, "150") //ms - } - return next(ctx, args, result) - } - } - - // mixed retry will success, latency is lowest - t.Run("mixed retry", func(t *testing.T) { - mp := retry.NewMixedPolicy(100) - mp.WithMaxRetryTimes(2) - rCli := getKitexClient( - transport.TTHeader, - client.WithRetryMethodPolicies(map[string]retry.Policy{ - "testSTReq": retry.BuildMixedPolicy(mp)}), - client.WithRPCTimeout(timeout), - client.WithMiddleware(controlCostMW), - ) - - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo - start := time.Now() - _, err := rCli.TestSTReq(ctx, stReq) - cost := time.Since(start) // 100+150 = 250 - test.Assert(t, err == nil, err) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-250.0) < 50.0, cost.Milliseconds()) - }) - - // failure retry will success, but latency is more than mixed retry - t.Run("failure retry", func(t *testing.T) { - fp := retry.NewFailurePolicy() - fp.WithMaxRetryTimes(2) - rCli := getKitexClient( - transport.TTHeader, - client.WithRetryMethodPolicies(map[string]retry.Policy{ - "testSTReq": retry.BuildFailurePolicy(fp)}), - client.WithRPCTimeout(timeout), - client.WithMiddleware(controlCostMW), - ) - - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo - start := time.Now() - _, err := rCli.TestSTReq(ctx, stReq) - cost := time.Since(start) - test.Assert(t, err == nil, err) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-450.0) < 50.0, cost.Milliseconds()) - }) - - // backup request will failure - t.Run("backup request", func(t *testing.T) { - bp := retry.NewBackupPolicy(100) - bp.WithMaxRetryTimes(2) - rCli := getKitexClient( - transport.TTHeader, - client.WithRetryMethodPolicies(map[string]retry.Policy{ - "testSTReq": retry.BuildBackupRequest(bp)}), - client.WithRPCTimeout(timeout), - client.WithMiddleware(controlCostMW), - ) - - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo - start := time.Now() - _, err := rCli.TestSTReq(ctx, stReq) - cost := time.Since(start) - test.Assert(t, err == nil, err) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-250.0) < 50.0, cost.Milliseconds()) + test.Assert(t, durAbs(cost-2*backupDelay) < 10*time.Millisecond, cost) }) } // Assuming all request timeout -// Configuration: Timeout=100ms、MaxRetryTimes=2 BackupDelay=100ms -// - Mixed Retry: Failure, cost 200ms -// - Failure Retry: Failure, cost 300ms -// - Backup Retry: Failure, cost 100ms (max cost is timeout) -func TestMockCase3WithDiffRetry(t *testing.T) { - timeout := 100 * time.Millisecond +// Configuration: rpcTimeout=100ms、MaxRetryTimes=2 BackupDelay=100ms +// - Mixed Retry: Failure, cost 2*rpcTimeout +// - Failure Retry: Failure, cost 3*rpcTimeout +// - Backup Retry: Failure, cost 1*rpcTimeout +func TestMockCaseWithTimeoutWithDiffRetry(t *testing.T) { + // backupDelay < rpcTimeout < reqSleep + rpcTimeout := 30 * time.Millisecond + backupDelay := rpcTimeout - 10*time.Millisecond // 20ms + reqSleep := rpcTimeout + 10*time.Millisecond // 40ms + controlCostMW := func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, args, result interface{}) (err error) { - ctx = metainfo.WithValue(ctx, sleepTimeMsKey, "200") //ms + ctx = withSleepTime(ctx, reqSleep) return next(ctx, args, result) } } rCli := getKitexClient( transport.TTHeader, - client.WithRPCTimeout(timeout), + client.WithRPCTimeout(rpcTimeout), client.WithMiddleware(controlCostMW), ) // mixed retry will success, cost is least t.Run("mixed retry", func(t *testing.T) { - mp := retry.NewMixedPolicy(100) + mp := retry.NewMixedPolicy(uint32(backupDelay / time.Millisecond)) mp.WithMaxRetryTimes(2) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo start := time.Now() _, err := rCli.TestSTReq(ctx, stReq, callopt.WithRetryPolicy(retry.BuildMixedPolicy(mp))) - cost := time.Since(start) // 100+(100,100) = 200 + cost := time.Since(start) test.Assert(t, err != nil, err) test.Assert(t, errors.Is(err, kerrors.ErrRPCTimeout)) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-200.0) < 50.0, cost.Milliseconds()) + test.Assert(t, durAbs(cost-2*rpcTimeout) < 10*time.Millisecond, cost) }) // failure retry will success, but cost is more than mixed retry @@ -241,13 +172,12 @@ func TestMockCase3WithDiffRetry(t *testing.T) { fp.WithMaxRetryTimes(2) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo start := time.Now() _, err := rCli.TestSTReq(ctx, stReq, callopt.WithRetryPolicy(retry.BuildFailurePolicy(fp))) cost := time.Since(start) test.Assert(t, err != nil, err) test.Assert(t, errors.Is(err, kerrors.ErrRPCTimeout)) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-300.0) < 50.0, cost.Milliseconds()) + test.Assert(t, durAbs(cost-3*rpcTimeout) < 10*time.Millisecond, cost) }) // backup request will failure @@ -256,13 +186,12 @@ func TestMockCase3WithDiffRetry(t *testing.T) { bp.WithMaxRetryTimes(2) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = mockTypeSleepWithMetainfo start := time.Now() _, err := rCli.TestSTReq(ctx, stReq, callopt.WithRetryPolicy(retry.BuildBackupRequest(bp))) cost := time.Since(start) test.Assert(t, err != nil, err) test.Assert(t, errors.Is(err, kerrors.ErrRPCTimeout)) - test.Assert(t, math.Abs(float64(cost.Milliseconds())-100.0) < 50.0, cost.Milliseconds()) + test.Assert(t, durAbs(cost-rpcTimeout) < 10*time.Millisecond, cost) }) } @@ -277,6 +206,8 @@ func TestMockCase3WithDiffRetry(t *testing.T) { // - Failure Retry: Success, cost 750ms // - Backup Retry: Biz Error, cost 250ms func TestMockCase4WithDiffRetry(t *testing.T) { + t.Parallel() + // mixed retry will success, cost is least t.Run("mixed retry", func(t *testing.T) { mp := retry.NewMixedPolicy(100) @@ -343,6 +274,8 @@ func TestMockCase4WithDiffRetry(t *testing.T) { } func TestMixedRetryWithDiffConfigurationMethod(t *testing.T) { + t.Parallel() + rpcCall := func(cli stservice.Client) { ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) stReq.FlagMsg = mockTypeCustomizedResp @@ -415,6 +348,8 @@ func TestMixedRetryWithDiffConfigurationMethod(t *testing.T) { } func TestMixedRetryWithNotifyPolicyChange(t *testing.T) { + t.Parallel() + t.Run("mixed retry with result retry", func(t *testing.T) { mp := retry.NewMixedPolicy(100) mp.WithMaxRetryTimes(3) diff --git a/thriftrpc/retrycall/percentage_limit_test.go b/thriftrpc/retrycall/percentage_limit_test.go index 70be970..81ffe02 100644 --- a/thriftrpc/retrycall/percentage_limit_test.go +++ b/thriftrpc/retrycall/percentage_limit_test.go @@ -31,6 +31,8 @@ import ( ) func TestPercentageLimit(t *testing.T) { + t.Parallel() + backupPolicy := &retry.BackupPolicy{ RetryDelayMS: 10, StopPolicy: retry.StopPolicy{ @@ -59,13 +61,13 @@ func TestPercentageLimit(t *testing.T) { } }), ) - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep - ctx = metainfo.WithPersistentValue(ctx, sleepTimeMsKey, "20") //ms + ctx, stReq := thriftrpc.CreateSTRequest(counterNamespace(t)) + stReq.FlagMsg = circuitBreakRetrySleep + ctx = withSleepTime(ctx, defaultSleepTime) for i := 0; i < 5; i++ { - // CircuitBreakTest will sleep 200ms - _, err := cli.CircuitBreakTest(ctx, stReq, callopt.WithRPCTimeout(time.Second)) + // CircuitBreakTest will 50% sleep defaultSleepTime + _, err := cli.CircuitBreakTest(ctx, stReq, callopt.WithRPCTimeout(defaultRPCTimeout)) // first 10 requests will always success test.Assert(t, err == nil, err) } @@ -94,6 +96,8 @@ func TestPercentageLimit(t *testing.T) { } func TestPercentageLimitAfterUpdate(t *testing.T) { + t.Parallel() + backupPolicy := &retry.BackupPolicy{ RetryDelayMS: 10, StopPolicy: retry.StopPolicy{ @@ -122,8 +126,8 @@ func TestPercentageLimitAfterUpdate(t *testing.T) { } }), ) - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + ctx, stReq := thriftrpc.CreateSTRequest(counterNamespace(t)) + stReq.FlagMsg = circuitBreakRetrySleep ctx = metainfo.WithPersistentValue(ctx, sleepTimeMsKey, "20") //ms for i := 0; i < 46; i++ { diff --git a/thriftrpc/retrycall/retrycall_handler.go b/thriftrpc/retrycall/retrycall_handler.go index 2761721..a3ec936 100644 --- a/thriftrpc/retrycall/retrycall_handler.go +++ b/thriftrpc/retrycall/retrycall_handler.go @@ -17,6 +17,7 @@ package retrycall import ( "context" "strconv" + "sync" "sync/atomic" "time" @@ -27,38 +28,91 @@ import ( "github.com/cloudwego/kitex/pkg/retry" ) +var ( + defaultRPCTimeout = 40 * time.Millisecond + defaultSleepTime = 60 * time.Millisecond // must > defaultRPCTimeout +) + const ( retryMsg = "retry" + counterNamespaceKey = "counterNs" sleepTimeMsKey = "TIME_SLEEP_TIME_MS" skipCounterSleepKey = "TIME_SKIP_COUNTER_SLEEP" respFlagMsgKey = "CUSTOMIZED_RESP_MSG" ) -var _ stability.STService = &STServiceHandler{} - var ( - testSTReqCount int32 - testObjReqCount int32 - testExceptionCount int32 - circuitBreakTestCount int32 + testObjReqCount int32 + testExceptionCount int32 ) type mockType = string const ( - mockTypeMockTimeout mockType = "0" - mockTypeNonRetryReturnError mockType = "1" - mockTypeCustomizedResp mockType = "2" - mockTypeSleepWithMetainfo mockType = "3" - mockTypeBizStatus mockType = "4" - mockTypeReturnTransErr mockType = "5" + mockTypeMockTimeout mockType = "mockTypeMockTimeout" + mockTypeNonRetryReturnError mockType = "mockTypeNonRetryReturnError" + mockTypeCustomizedResp mockType = "mockTypeCustomizedResp" + mockTypeBizStatus mockType = "mockTypeBizStatus" + mockTypeReturnTransErr mockType = "mockTypeReturnTransErr" + mockType10PctSleep mockType = "mockType10PctSleep" retryTransErrCode = 1000 ) +type reqCounters struct { + STReq int32 + CircuitBreak int32 + + // TODO: need to refactor the code if tests can't run with t.Parallel() + // ObjReq int32 + // Exception int32 +} + // STServiceHandler . type STServiceHandler struct{} +var _ stability.STService = &STServiceHandler{} + +var counters sync.Map + +func resetCounters(ns string) { + counters.Store(ns, &reqCounters{}) +} + +func getCounter(ns string) *reqCounters { + v, ok := counters.Load(ns) + if !ok { + return nil + } + return v.(*reqCounters) +} + +type testingT interface { + Name() string +} + +func getTestCounters(t testingT) *reqCounters { + v, ok := counters.Load(t.Name()) + if !ok { + return nil + } + return v.(*reqCounters) +} + +func counterNamespace(t testingT) context.Context { + ns := t.Name() + resetCounters(ns) + return metainfo.WithValue(context.Background(), counterNamespaceKey, ns) +} + +func getCounterNamespace(ctx context.Context) string { + s, _ := metainfo.GetPersistentValue(ctx, counterNamespaceKey) + if s == "" { + s, _ = metainfo.GetValue(ctx, counterNamespaceKey) + } + return s +} + // TestSTReq . func (h *STServiceHandler) TestSTReq(ctx context.Context, req *stability.STRequest) (r *stability.STResponse, err error) { resp := &stability.STResponse{ @@ -66,6 +120,7 @@ func (h *STServiceHandler) TestSTReq(ctx context.Context, req *stability.STReque Mp: req.StringMap, FlagMsg: req.FlagMsg, } + if req.FlagMsg == mockTypeCustomizedResp { // should use ttheader @@ -80,13 +135,6 @@ func (h *STServiceHandler) TestSTReq(ctx context.Context, req *stability.STReque resp.FlagMsg = "success" } } - if sleepTime := getSleepTimeMS(ctx); sleepTime > 0 { - time.Sleep(sleepTime) - } - } else if req.FlagMsg == mockTypeSleepWithMetainfo { - if sleepTime := getSleepTimeMS(ctx); sleepTime > 0 { - time.Sleep(sleepTime) - } } else if req.FlagMsg == mockTypeReturnTransErr { // should use ttheader @@ -100,14 +148,16 @@ func (h *STServiceHandler) TestSTReq(ctx context.Context, req *stability.STReque err = remote.NewTransErrorWithMsg(retryTransErrCode, "mock error") } } - if sleepTime := getSleepTimeMS(ctx); sleepTime > 0 { - time.Sleep(sleepTime) - } - } else { - if v := atomic.AddInt32(&testSTReqCount, 1); v%10 == 0 { - time.Sleep(50 * time.Millisecond) + } else if req.FlagMsg == mockType10PctSleep { + v := getCounter(getCounterNamespace(ctx)) + if (atomic.AddInt32(&v.STReq, 1)-1)%10 == 0 { + time.Sleep(defaultSleepTime) } } + + if sleepTime := getSleepTime(ctx); sleepTime > 0 { + time.Sleep(sleepTime) + } return resp, err } @@ -127,7 +177,7 @@ func (h *STServiceHandler) TestObjReq(ctx context.Context, req *instparam.ObjReq } } else { if atomic.AddInt32(&testObjReqCount, 1)%5 == 0 { - time.Sleep(200 * time.Millisecond) + time.Sleep(defaultSleepTime) } } return resp, nil @@ -152,7 +202,7 @@ func (h *STServiceHandler) TestException(ctx context.Context, req *stability.STR } else { err = &stability.STException{Message: "mock exception"} if atomic.AddInt32(&testExceptionCount, 1)%30 == 0 { - time.Sleep(50 * time.Millisecond) + time.Sleep(defaultSleepTime) } } return nil, err @@ -167,27 +217,28 @@ func (*STServiceHandler) VisitOneway(ctx context.Context, req *stability.STReque type CircuitBreak = string const ( - CircuitBreak50PCT CircuitBreak = "0" - CircuitBreakRetrySleep CircuitBreak = "1" + circuitBreak50PCT CircuitBreak = "0" + circuitBreakRetrySleep CircuitBreak = "1" ) // CircuitBreakTest . func (h *STServiceHandler) CircuitBreakTest(ctx context.Context, req *stability.STRequest) (r *stability.STResponse, err error) { - if req.FlagMsg == CircuitBreakRetrySleep { + if req.FlagMsg == circuitBreakRetrySleep { // use ttheader // first request(non retry request) will sleep to mock timeout, then retry request return directly if _, exist := metainfo.GetPersistentValue(ctx, retry.TransitKey); !exist { - sleepTime := 200 * time.Millisecond - if v := getSleepTimeMS(ctx); v > 0 { + sleepTime := defaultSleepTime + if v := getSleepTime(ctx); v > 0 { sleepTime = v } time.Sleep(sleepTime) } - } else if req.FlagMsg == CircuitBreak50PCT { - // force 50% of the responses to cost over 200ms - if atomic.AddInt32(&circuitBreakTestCount, 1)%2 == 0 { - time.Sleep(200 * time.Millisecond) + } else if req.FlagMsg == circuitBreak50PCT { + // force 50% of the responses to cost defaultSleepTime + v := getCounter(getCounterNamespace(ctx)) + if (atomic.AddInt32(&v.CircuitBreak, 1)-1)%2 == 0 { + time.Sleep(defaultSleepTime) } } resp := &stability.STResponse{ @@ -202,22 +253,12 @@ func setSkipCounterSleep(ctx context.Context) context.Context { return metainfo.WithPersistentValue(ctx, skipCounterSleepKey, "1") } -func skipCounterSleep(ctx context.Context) bool { - if _, exist := metainfo.GetValue(ctx, skipCounterSleepKey); exist { - return true - } - if _, exist := metainfo.GetPersistentValue(ctx, skipCounterSleepKey); exist { - return true - } - return false +func withSleepTime(ctx context.Context, d time.Duration) context.Context { + ms := int(d / time.Millisecond) + return metainfo.WithValue(ctx, sleepTimeMsKey, strconv.Itoa(ms)) } -func getSleepTimeMS(ctx context.Context) time.Duration { - if value, exist := metainfo.GetPersistentValue(ctx, sleepTimeMsKey); exist { - if sleepTimeMS, err := strconv.Atoi(value); err == nil && sleepTimeMS > 0 { - return time.Duration(sleepTimeMS) * time.Millisecond - } - } +func getSleepTime(ctx context.Context) time.Duration { if value, exist := metainfo.GetValue(ctx, sleepTimeMsKey); exist { if sleepTimeMS, err := strconv.Atoi(value); err == nil && sleepTimeMS > 0 { return time.Duration(sleepTimeMS) * time.Millisecond diff --git a/thriftrpc/retrycall/retrycall_test.go b/thriftrpc/retrycall/retrycall_test.go index 90c78be..86bf818 100644 --- a/thriftrpc/retrycall/retrycall_test.go +++ b/thriftrpc/retrycall/retrycall_test.go @@ -18,7 +18,6 @@ import ( "context" "errors" "fmt" - "strconv" "strings" "sync/atomic" "testing" @@ -38,19 +37,21 @@ import ( "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability" "github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice" "github.com/cloudwego/kitex-tests/pkg/test" + "github.com/cloudwego/kitex-tests/pkg/utils/serverutils" "github.com/cloudwego/kitex-tests/thriftrpc" ) -var cli stservice.Client var retryChainStopStr = "chain stop retry" +var testaddr string + func TestMain(m *testing.M) { + testaddr = serverutils.NextListenAddr() svr1 := thriftrpc.RunServer(&thriftrpc.ServerInitParam{ Network: "tcp", - Address: "localhost:9001", + Address: testaddr, }, new(STServiceHandler)) - - time.Sleep(time.Second) + serverutils.Wait(testaddr) m.Run() svr1.Stop() } @@ -58,7 +59,7 @@ func TestMain(m *testing.M) { func getKitexClient(p transport.Protocol, opts ...client.Option) stservice.Client { return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{ TargetServiceName: "cloudwego.kitex.testa", - HostPorts: []string{"localhost:9001"}, + HostPorts: []string{testaddr}, Protocol: p, ConnMode: thriftrpc.LongConnection, }, opts...) @@ -74,17 +75,19 @@ func genCBKey(ri rpcinfo.RPCInfo) string { } func TestRetryCB(t *testing.T) { + t.Parallel() + reqCount := int32(0) - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithRetryContainer(retry.NewRetryContainer()), // use default circuit breaker client.WithFailureRetry(retry.NewFailurePolicy()), - client.WithRPCTimeout(20*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), client.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { count := atomic.AddInt32(&reqCount, 1) if count%10 == 0 { - time.Sleep(50 * time.Millisecond) + time.Sleep(defaultSleepTime) } return nil // no need to send the real request } @@ -104,16 +107,18 @@ func TestRetryCB(t *testing.T) { } func TestRetryCBPercentageLimit(t *testing.T) { + t.Parallel() + reqCount := int32(0) cli := getKitexClient( transport.TTHeaderFramed, client.WithRetryContainer(retry.NewRetryContainerWithPercentageLimit()), client.WithFailureRetry(retry.NewFailurePolicy()), // cb threshold = 10% - client.WithRPCTimeout(20*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), client.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { atomic.AddInt32(&reqCount, 1) - if tm := getSleepTimeMS(ctx); tm > 0 { + if tm := getSleepTime(ctx); tm > 0 { time.Sleep(tm) } return nil // no need to send a real request @@ -126,7 +131,7 @@ func TestRetryCBPercentageLimit(t *testing.T) { for i := 0; i < 300; i++ { reqCtx := ctx if i%5 == 0 { - reqCtx = metainfo.WithPersistentValue(ctx, sleepTimeMsKey, "50") + reqCtx = withSleepTime(ctx, defaultSleepTime) } _, err := cli.TestSTReq(reqCtx, stReq) if err != nil && strings.Contains(err.Error(), "retry circuit break") { @@ -176,7 +181,8 @@ func TestRetryRespOpIsolation(t *testing.T) { } func TestNoCB(t *testing.T) { - atomic.StoreInt32(&testSTReqCount, -1) + t.Parallel() + // retry config fp := retry.NewFailurePolicy() fp.StopPolicy.CBPolicy.ErrorRate = 0.3 @@ -185,8 +191,9 @@ func TestNoCB(t *testing.T) { client.WithFailureRetry(fp), client.WithCircuitBreaker(circuitbreak.NewCBSuite(genCBKey)), ) - cli = getKitexClient(transport.TTHeaderFramed, opts...) - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) + cli := getKitexClient(transport.TTHeaderFramed, opts...) + ctx, stReq := thriftrpc.CreateSTRequest(counterNamespace(t)) + stReq.FlagMsg = mockType10PctSleep _, _ = cli.TestSTReq(ctx, stReq) for i := 0; i < 250; i++ { stResp, err := cli.TestSTReq(ctx, stReq) @@ -196,29 +203,25 @@ func TestNoCB(t *testing.T) { } func TestRetry(t *testing.T) { + t.Parallel() + t.Run("TestNoRetry", func(t *testing.T) { - atomic.StoreInt32(&testSTReqCount, -1) // retry config fp := retry.NewFailurePolicy() fp.StopPolicy.CBPolicy.ErrorRate = 0.3 var opts []client.Option opts = append(opts, client.WithFailureRetry(fp), - client.WithRPCTimeout(20*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) - cli = getKitexClient(transport.Framed, opts...) + cli := getKitexClient(transport.TTHeader, opts...) + ctx, stReq := thriftrpc.CreateSTRequest(counterNamespace(t)) + stReq.FlagMsg = mockType10PctSleep - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) // add a mark to avoid retry - ctx = metainfo.WithPersistentValue(ctx, retry.TransitKey, strconv.Itoa(1)) - ctx = metainfo.WithValue(ctx, skipCounterSleepKey, "1") // do not sleep by global variable - + ctx = metainfo.WithPersistentValue(ctx, retry.TransitKey, "1") for i := 0; i < 250; i++ { - reqCtx := ctx - if i%10 == 0 { - reqCtx = metainfo.WithValue(ctx, sleepTimeMsKey, "100") - } - stResp, err := cli.TestSTReq(reqCtx, stReq) + stResp, err := cli.TestSTReq(ctx, stReq) if i%10 == 0 { test.Assert(t, err != nil) test.Assert(t, strings.Contains(err.Error(), retryChainStopStr), err) @@ -230,36 +233,36 @@ func TestRetry(t *testing.T) { }) t.Run("TestBackupRequest", func(t *testing.T) { - atomic.StoreInt32(&testSTReqCount, -1) // retry config bp := retry.NewBackupPolicy(5) var opts []client.Option opts = append(opts, client.WithBackupRequest(bp), - client.WithRPCTimeout(40*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) - cli = getKitexClient(transport.Framed, opts...) + basectx := counterNamespace(t) + cli := getKitexClient(transport.TTHeader, opts...) for i := 0; i < 300; i++ { - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) + ctx, stReq := thriftrpc.CreateSTRequest(basectx) stReq.Int64 = int64(i) + stReq.FlagMsg = mockType10PctSleep stResp, err := cli.TestSTReq(ctx, stReq) - test.Assert(t, err == nil, err, i, testSTReqCount) + test.Assert(t, err == nil, err, i, getTestCounters(t).STReq) test.Assert(t, stReq.Str == stResp.Str) } }) t.Run("TestServiceCB", func(t *testing.T) { - atomic.StoreInt32(&circuitBreakTestCount, -1) // retry config fp := retry.NewFailurePolicy() var opts []client.Option opts = append(opts, client.WithFailureRetry(fp)) - opts = append(opts, client.WithRPCTimeout(50*time.Millisecond)) + opts = append(opts, client.WithRPCTimeout(defaultRPCTimeout)) opts = append(opts, client.WithCircuitBreaker(circuitbreak.NewCBSuite(genCBKey))) - cli = getKitexClient(transport.TTHeader, opts...) + cli := getKitexClient(transport.TTHeader, opts...) - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreak50PCT + ctx, stReq := thriftrpc.CreateSTRequest(counterNamespace(t)) + stReq.FlagMsg = circuitBreak50PCT cbCount := 0 for i := 0; i < 300; i++ { stResp, err := cli.CircuitBreakTest(ctx, stReq) @@ -306,7 +309,7 @@ func TestRetryWithSpecifiedResultRetry(t *testing.T) { return false } isResultRetry := &retry.ShouldResultRetry{ErrorRetry: isErrorRetry, RespRetry: isRespRetry} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithFailureRetry(retry.NewFailurePolicy()), client.WithRetryMethodPolicies(map[string]retry.Policy{ @@ -315,29 +318,31 @@ func TestRetryWithSpecifiedResultRetry(t *testing.T) { "circuitBreakTest": retry.BuildBackupRequest(retry.NewBackupPolicy(10)), }), client.WithSpecifiedResultRetry(isResultRetry), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) + basectx := counterNamespace(t) + + ctx, stReq := thriftrpc.CreateSTRequest(basectx) stReq.FlagMsg = mockTypeCustomizedResp _, err := cli.TestSTReq(ctx, stReq) test.Assert(t, err == nil, err) test.Assert(t, methodPolicy["testSTReq"]) - ctx, objReq := thriftrpc.CreateObjReq(context.Background()) + ctx, objReq := thriftrpc.CreateObjReq(basectx) objReq.FlagMsg = mockTypeNonRetryReturnError _, err = cli.TestObjReq(ctx, objReq) test.Assert(t, err == nil, err) test.Assert(t, methodPolicy["testObjReq"]) - ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) + ctx, stReq = thriftrpc.CreateSTRequest(basectx) stReq.FlagMsg = mockTypeNonRetryReturnError _, err = cli.TestException(ctx, stReq) test.Assert(t, err == nil, err) test.Assert(t, methodPolicy["testException"]) - ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + ctx, stReq = thriftrpc.CreateSTRequest(basectx) + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq) test.Assert(t, err == nil, err) } @@ -374,7 +379,7 @@ func TestFailureRetryWithSpecifiedResultRetry(t *testing.T) { return false } isResultRetry := &retry.ShouldResultRetry{ErrorRetry: isErrorRetry, RespRetry: isRespRetry} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithFailureRetry(retry.NewFailurePolicy()), client.WithRetryMethodPolicies(map[string]retry.Policy{ @@ -383,7 +388,7 @@ func TestFailureRetryWithSpecifiedResultRetry(t *testing.T) { "circuitBreakTest": retry.BuildBackupRequest(retry.NewBackupPolicy(10)), }), client.WithSpecifiedResultRetry(isResultRetry), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) @@ -405,7 +410,7 @@ func TestFailureRetryWithSpecifiedResultRetry(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq) test.Assert(t, err == nil, err) } @@ -442,7 +447,7 @@ func TestRetryWithSpecifiedResultRetryWithCtx(t *testing.T) { return false } isResultRetry := &retry.ShouldResultRetry{ErrorRetryWithCtx: isErrorRetry, RespRetryWithCtx: isRespRetry} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithFailureRetry(retry.NewFailurePolicy()), client.WithRetryMethodPolicies(map[string]retry.Policy{ @@ -451,7 +456,7 @@ func TestRetryWithSpecifiedResultRetryWithCtx(t *testing.T) { "circuitBreakTest": retry.BuildBackupRequest(retry.NewBackupPolicy(10)), }), client.WithSpecifiedResultRetry(isResultRetry), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) ctx := context.WithValue(context.Background(), ctxKeyVal, ctxKeyVal) @@ -474,7 +479,7 @@ func TestRetryWithSpecifiedResultRetryWithCtx(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(ctx) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq) test.Assert(t, err == nil, err) } @@ -495,12 +500,12 @@ func TestRetryWithSpecifiedResultRetryWithOldAndNew(t *testing.T) { isResultRetry := &retry.ShouldResultRetry{ ErrorRetryWithCtx: isErrorRetryNew, RespRetryWithCtx: isRespRetryNew, ErrorRetry: isErrorRetryOld, RespRetry: isRespRetryOld} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithFailureRetry(retry.NewFailurePolicy()), client.WithSpecifiedResultRetry(isResultRetry), client.WithTransportProtocol(transport.TTHeader), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) @@ -547,7 +552,7 @@ func TestFailureRetryWithSpecifiedResultRetryWithCtx(t *testing.T) { return false } isResultRetry := &retry.ShouldResultRetry{ErrorRetryWithCtx: isErrorRetry, RespRetryWithCtx: isRespRetry} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithFailureRetry(retry.NewFailurePolicyWithResultRetry(isResultRetry)), client.WithRetryMethodPolicies(map[string]retry.Policy{ @@ -555,7 +560,7 @@ func TestFailureRetryWithSpecifiedResultRetryWithCtx(t *testing.T) { "testObjReq": retry.BuildFailurePolicy(retry.NewFailurePolicyWithResultRetry(isResultRetry)), "circuitBreakTest": retry.BuildBackupRequest(retry.NewBackupPolicy(10)), }), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) ctx := context.WithValue(context.Background(), ctxKeyVal, ctxKeyVal) @@ -578,7 +583,7 @@ func TestFailureRetryWithSpecifiedResultRetryWithCtx(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(ctx) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq) test.Assert(t, err == nil, err) } @@ -620,11 +625,11 @@ func TestRetryNotifyHasOldResultRetry(t *testing.T) { rc.NotifyPolicyChange("testObjReq", retry.BuildFailurePolicy(retry.NewFailurePolicy())) rc.NotifyPolicyChange("circuitBreakTest", retry.BuildBackupRequest(retry.NewBackupPolicy(10))) - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithRetryContainer(rc), client.WithSpecifiedResultRetry(isResultRetry), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) @@ -646,7 +651,7 @@ func TestRetryNotifyHasOldResultRetry(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq) test.Assert(t, err == nil, err) @@ -714,11 +719,11 @@ func TestRetryNotifyHasNewResultRetry(t *testing.T) { rc.NotifyPolicyChange("testObjReq", retry.BuildFailurePolicy(retry.NewFailurePolicy())) rc.NotifyPolicyChange("circuitBreakTest", retry.BuildBackupRequest(retry.NewBackupPolicy(10))) - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, client.WithRetryContainer(rc), client.WithSpecifiedResultRetry(isResultRetry), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) @@ -740,7 +745,7 @@ func TestRetryNotifyHasNewResultRetry(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq) test.Assert(t, err == nil, err) @@ -803,7 +808,7 @@ func TestRetryWithCallOptHasOldResultRetry(t *testing.T) { return false } isResultRetry := &retry.ShouldResultRetry{ErrorRetry: isErrorRetry, RespRetry: isRespRetry} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, // setup WithBackupRequest is to check the callopt priority is higher client.WithBackupRequest(retry.NewBackupPolicy(10)), @@ -828,7 +833,7 @@ func TestRetryWithCallOptHasOldResultRetry(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq, callopt.WithRetryPolicy(retry.BuildBackupRequest(retry.NewBackupPolicy(10)))) test.Assert(t, err == nil, err) } @@ -866,7 +871,7 @@ func TestRetryWithCallOptHasNewResultRetry(t *testing.T) { return false } isResultRetry := &retry.ShouldResultRetry{ErrorRetryWithCtx: isErrorRetry, RespRetryWithCtx: isRespRetry} - cli = getKitexClient( + cli := getKitexClient( transport.TTHeader, // setup WithBackupRequest is to check the callopt priority is higher client.WithBackupRequest(retry.NewBackupPolicy(10)), @@ -891,7 +896,7 @@ func TestRetryWithCallOptHasNewResultRetry(t *testing.T) { test.Assert(t, methodPolicy["testException"]) ctx, stReq = thriftrpc.CreateSTRequest(context.Background()) - stReq.FlagMsg = CircuitBreakRetrySleep + stReq.FlagMsg = circuitBreakRetrySleep _, err = cli.CircuitBreakTest(ctx, stReq, callopt.WithRetryPolicy(retry.BuildBackupRequest(retry.NewBackupPolicy(10)))) test.Assert(t, err == nil, err) } @@ -908,17 +913,17 @@ func TestRetryForTimeout(t *testing.T) { mockTimeoutMW := func(endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req, resp interface{}) (err error) { if _, exist := metainfo.GetPersistentValue(ctx, retry.TransitKey); !exist { - time.Sleep(100 * time.Millisecond) + time.Sleep(defaultSleepTime) } return } } // case 1: timeout retry is effective by default isResultRetry := &retry.ShouldResultRetry{ErrorRetry: isErrorRetry} - cli = getKitexClient( + cli := getKitexClient( transport.PurePayload, client.WithMiddleware(mockTimeoutMW), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), client.WithFailureRetry(retry.NewFailurePolicyWithResultRetry(isResultRetry)), ) ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) @@ -937,7 +942,7 @@ func TestRetryForTimeout(t *testing.T) { cli = getKitexClient( transport.PurePayload, client.WithMiddleware(mockTimeoutMW), - client.WithRPCTimeout(50*time.Millisecond), + client.WithRPCTimeout(defaultRPCTimeout), client.WithFailureRetry(retry.NewFailurePolicyWithResultRetry(isResultRetry)), ) isResultRetry = &retry.ShouldResultRetry{ErrorRetry: isErrorRetry, NotRetryForTimeout: true} @@ -948,20 +953,20 @@ func TestRetryForTimeout(t *testing.T) { } func BenchmarkRetryNoCB(b *testing.B) { - atomic.StoreInt32(&testSTReqCount, -1) // retry config bp := retry.NewBackupPolicy(3) bp.StopPolicy.MaxRetryTimes = 2 bp.StopPolicy.CBPolicy.ErrorRate = 0.3 var opts []client.Option opts = append(opts, client.WithBackupRequest(bp)) - cli = getKitexClient(transport.PurePayload, opts...) + cli := getKitexClient(transport.PurePayload, opts...) + basectx := counterNamespace(b) b.ReportAllocs() b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - ctx, stReq := thriftrpc.CreateSTRequest(context.Background()) + ctx, stReq := thriftrpc.CreateSTRequest(basectx) stResp, err := cli.TestSTReq(ctx, stReq) test.Assert(b, err == nil, err) test.Assert(b, stReq.Str == stResp.Str) @@ -976,7 +981,7 @@ func BenchmarkThriftCallParallel(b *testing.B) { fp.StopPolicy.MaxRetryTimes = 5 var opts []client.Option opts = append(opts, client.WithFailureRetry(fp)) - cli = getKitexClient(transport.TTHeader, opts...) + cli := getKitexClient(transport.TTHeader, opts...) b.ReportAllocs() b.ResetTimer()