feat: support the specified timeout of rpc methods (#2742)

Co-authored-by: hanzijian <hanzijian@52tt.com>
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
vankillua
2023-10-25 21:01:57 +08:00
committed by GitHub
parent 2a335c7608
commit 842c4d81cc
10 changed files with 378 additions and 29 deletions

View File

@@ -41,32 +41,37 @@ func dialer() func(context.Context, string) (net.Conn, error) {
func TestDepositServer_Deposit(t *testing.T) {
tests := []struct {
name string
amount float32
res *mock.DepositResponse
errCode codes.Code
errMsg string
name string
amount float32
timeoutCallOption time.Duration
res *mock.DepositResponse
errCode codes.Code
errMsg string
}{
{
"invalid request with negative amount",
-1.11,
nil,
codes.InvalidArgument,
fmt.Sprintf("cannot deposit %v", -1.11),
name: "invalid request with negative amount",
amount: -1.11,
errCode: codes.InvalidArgument,
errMsg: fmt.Sprintf("cannot deposit %v", -1.11),
},
{
"valid request with non negative amount",
0.00,
&mock.DepositResponse{Ok: true},
codes.OK,
"",
name: "valid request with non negative amount",
res: &mock.DepositResponse{Ok: true},
errCode: codes.OK,
},
{
"valid request with long handling time",
2000.00,
nil,
codes.DeadlineExceeded,
"context deadline exceeded",
name: "valid request with long handling time",
amount: 2000.00,
errCode: codes.DeadlineExceeded,
errMsg: "context deadline exceeded",
},
{
name: "valid request with timeout call option",
amount: 2000.00,
timeoutCallOption: time.Second * 3,
res: &mock.DepositResponse{Ok: true},
errCode: codes.OK,
errMsg: "",
},
}
@@ -156,9 +161,22 @@ func TestDepositServer_Deposit(t *testing.T) {
client := client
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := mock.NewDepositServiceClient(client.Conn())
request := &mock.DepositRequest{Amount: tt.amount}
response, err := cli.Deposit(context.Background(), request)
var (
ctx = context.Background()
response *mock.DepositResponse
err error
)
if tt.timeoutCallOption > 0 {
response, err = cli.Deposit(ctx, request, WithTimeoutCallOption(tt.timeoutCallOption))
} else {
response, err = cli.Deposit(ctx, request)
}
if response != nil {
assert.True(t, len(response.String()) > 0)
if response.GetOk() != tt.res.GetOk() {