Commit cfab9e1f authored by zhujiashun's avatar zhujiashun

* improve grpc status to error code conversion

* improve grpc UT
parent 165499ef
......@@ -61,13 +61,17 @@ GrpcStatus ErrorCodeToGrpcStatus(int error_code) {
case EINVAL:
return GRPC_INVALIDARGUMENT;
case ELIMIT:
return GRPC_RESOURCEEXHAUSTED;
case ELOGOFF:
return GRPC_UNAVAILABLE;
case EPERM:
return GRPC_PERMISSIONDENIED;
case ERPCTIMEDOUT:
return GRPC_DEADLINEEXCEEDED;
case ETIMEDOUT:
return GRPC_INTERNAL;
case ECANCELED:
return GRPC_CANCELED;
default:
return GRPC_INTERNAL;
}
......@@ -80,6 +84,7 @@ int GrpcStatusToErrorCode(GrpcStatus grpc_status) {
case GRPC_CANCELED:
return ECANCELED;
case GRPC_UNKNOWN:
return EINTERNAL;
case GRPC_INVALIDARGUMENT:
return EINVAL;
case GRPC_DEADLINEEXCEEDED:
......@@ -99,9 +104,8 @@ int GrpcStatusToErrorCode(GrpcStatus grpc_status) {
case GRPC_UNIMPLEMENTED:
return ENOMETHOD;
case GRPC_INTERNAL:
return EINTERNAL;
case GRPC_UNAVAILABLE:
return EAGAIN;
return EINTERNAL;
case GRPC_DATALOSS:
return EINTERNAL;
case GRPC_UNAUTHENTICATED:
......@@ -109,7 +113,6 @@ int GrpcStatusToErrorCode(GrpcStatus grpc_status) {
default:
return EINTERNAL;
}
}
void PercentEncode(const std::string& str, std::string* str_out) {
......
......@@ -55,16 +55,13 @@ public:
brpc::ClosureGuard done_guard(done);
EXPECT_EQ(g_req, req->message());
if (req->has_gzip() && req->gzip()) {
if (req->gzip()) {
cntl->set_response_compress_type(brpc::COMPRESS_TYPE_GZIP);
}
res->set_message(g_prefix + req->message());
if (req->has_error_code()) {
const std::string err_msg =
butil::string_printf("%s%d", g_prefix.c_str(), req->error_code());
// TODO(zhujiashun): set and check error_code when GrpcStatusToErrorCode is done
cntl->SetFailed(err_msg.c_str());
if (req->return_error()) {
cntl->SetFailed(brpc::EINTERNAL, "%s", g_prefix.c_str());
return;
}
}
......@@ -105,6 +102,7 @@ protected:
}
req.set_message(g_req);
req.set_gzip(res_gzip);
req.set_return_error(false);
test::GrpcService_Stub stub(&_channel);
stub.Method(&cntl, &req, &res, NULL);
......@@ -154,20 +152,17 @@ TEST_F(GrpcTest, sanity) {
}
TEST_F(GrpcTest, return_error) {
// GRPC_OK(0) is skipped
for (int i = 1; i < (int)brpc::GRPC_MAX; ++i) {
test::GrpcRequest req;
test::GrpcResponse res;
brpc::Controller cntl;
req.set_message(g_req);
req.set_error_code(i);
req.set_gzip(false);
req.set_return_error(true);
test::GrpcService_Stub stub(&_channel);
stub.Method(&cntl, &req, &res, NULL);
EXPECT_TRUE(cntl.Failed());
// TODO(zhujiashun): set and check error_code when GrpcStatusToErrorCode is done
EXPECT_EQ(cntl.ErrorCode(), brpc::EINTERNAL);
EXPECT_TRUE(butil::StringPiece(cntl.ErrorText()).ends_with(butil::string_printf("%s%d", g_prefix.c_str(), i)));
}
EXPECT_TRUE(butil::StringPiece(cntl.ErrorText()).ends_with(butil::string_printf("%s", g_prefix.c_str())));
}
TEST_F(GrpcTest, RpcTimedOut) {
......@@ -181,6 +176,8 @@ TEST_F(GrpcTest, RpcTimedOut) {
test::GrpcResponse res;
brpc::Controller cntl;
req.set_message(g_req);
req.set_gzip(false);
req.set_return_error(false);
test::GrpcService_Stub stub(&_channel);
stub.MethodTimeOut(&cntl, &req, &res, NULL);
EXPECT_TRUE(cntl.Failed());
......@@ -192,10 +189,11 @@ TEST_F(GrpcTest, MethodNotExist) {
test::GrpcResponse res;
brpc::Controller cntl;
req.set_message(g_req);
req.set_gzip(false);
req.set_return_error(false);
test::GrpcService_Stub stub(&_channel);
stub.MethodNotExist(&cntl, &req, &res, NULL);
EXPECT_TRUE(cntl.Failed());
// TODO(zhujiashun): set and check error_code when GrpcStatusToErrorCode is done
EXPECT_EQ(cntl.ErrorCode(), brpc::EINTERNAL);
ASSERT_TRUE(butil::StringPiece(cntl.ErrorText()).ends_with("Method MethodNotExist() not implemented."));
}
......
......@@ -5,8 +5,8 @@ package test;
message GrpcRequest {
required string message = 1;
optional bool gzip = 2;
optional int32 error_code = 3;
required bool gzip = 2;
required bool return_error = 3;
};
message GrpcResponse {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment