Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions thriftrpc/failedcall/failedcall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"time"

"github.com/apache/thrift/lib/go/thrift"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/endpoint"

"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice"
"github.com/cloudwego/kitex-tests/pkg/test"
Expand Down Expand Up @@ -59,13 +61,13 @@ func TestMain(m *testing.M) {
svr.Stop()
}

func getKitexClient(p transport.Protocol) stservice.Client {
func getKitexClient(p transport.Protocol, opts ...client.Option) stservice.Client {
return thriftrpc.CreateKitexClient(&thriftrpc.ClientInitParam{
TargetServiceName: "cloudwego.kitex.testa",
HostPorts: []string{":9001"},
Protocol: p,
ConnMode: thriftrpc.LongConnection,
})
}, opts...)
}

// TestSTReq method mock STRequest param read failed in server
Expand Down Expand Up @@ -141,3 +143,29 @@ func TestVisitOneway(t *testing.T) {
err = cli.VisitOneway(ctx, stReq)
test.Assert(t, err == nil, err)
}

// TestClientMWPanic mock panic in customized mw
func TestClientMWPanic(t *testing.T) {
cli = getKitexClient(transport.Framed, client.WithMiddleware(panicMW))

// case1: panic without timeout
ctx, objReq := thriftrpc.CreateObjReq(context.Background())
objResp, err := cli.TestObjReq(ctx, objReq)
test.Assert(t, err != nil)
test.Assert(t, strings.Contains(err.Error(), panicMsg), err.Error())

// case2: panic with timeout
ctx, objReq = thriftrpc.CreateObjReq(context.Background())
objResp, err = cli.TestObjReq(ctx, objReq, callopt.WithRPCTimeout(time.Second))
test.Assert(t, err != nil)
test.Assert(t, objResp == nil)
test.Assert(t, strings.Contains(err.Error(), panicMsg), err.Error())
}

func panicMW(endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request, response interface{}) (err error) {
panic(panicMsg)
}
}

const panicMsg = "mock panic"