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