2 // Copyright 2021 gRPC authors.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "src/core/lib/transport/error_utils.h"
19 #include <gtest/gtest.h>
21 #include "absl/status/status.h"
23 #include "src/core/lib/slice/slice_internal.h"
24 #include "test/core/util/test_config.h"
28 // ---- Ok Status ----
29 TEST(ErrorUtilsTest, AbslOkToGrpcError) {
30 grpc_error_handle error = absl_status_to_grpc_error(absl::OkStatus());
31 ASSERT_EQ(GRPC_ERROR_NONE, error);
32 GRPC_ERROR_UNREF(error);
35 TEST(ErrorUtilsTest, GrpcSpecialErrorNoneToAbslStatus) {
36 absl::Status status = grpc_error_to_absl_status(GRPC_ERROR_NONE);
37 ASSERT_TRUE(status.ok());
38 ASSERT_EQ(status.message(), "");
41 // ---- Asymmetry of conversions of "Special" errors ----
42 TEST(ErrorUtilsTest, AbslStatusToGrpcErrorDoesNotReturnSpecialVariables) {
43 grpc_error_handle error =
44 absl_status_to_grpc_error(absl::CancelledError("Cancelled"));
45 ASSERT_NE(error, GRPC_ERROR_CANCELLED);
46 GRPC_ERROR_UNREF(error);
49 TEST(ErrorUtilsTest, GrpcSpecialErrorCancelledToAbslStatus) {
50 absl::Status status = grpc_error_to_absl_status(GRPC_ERROR_CANCELLED);
51 ASSERT_TRUE(absl::IsCancelled(status));
52 ASSERT_EQ(status.message(), "Cancelled");
55 TEST(ErrorUtilsTest, GrpcSpecialErrorOOMToAbslStatus) {
56 absl::Status status = grpc_error_to_absl_status(GRPC_ERROR_OOM);
57 ASSERT_TRUE(absl::IsResourceExhausted(status));
58 ASSERT_EQ(status.message(), "Out of memory");
61 // ---- Ordinary statuses ----
62 TEST(ErrorUtilsTest, AbslUnavailableToGrpcError) {
63 grpc_error_handle error =
64 absl_status_to_grpc_error(absl::UnavailableError("Making tea"));
67 ASSERT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &code));
68 ASSERT_EQ(static_cast<grpc_status_code>(code), GRPC_STATUS_UNAVAILABLE);
69 // Status message checks
71 ASSERT_TRUE(grpc_error_get_str(error, GRPC_ERROR_STR_DESCRIPTION, &message));
72 absl::string_view str = grpc_core::StringViewFromSlice(message);
73 ASSERT_EQ(str, "Making tea");
74 grpc_slice_unref(message);
75 GRPC_ERROR_UNREF(error);
78 TEST(ErrorUtilsTest, GrpcErrorUnavailableToAbslStatus) {
79 grpc_error_handle error = grpc_error_set_int(
80 GRPC_ERROR_CREATE_FROM_STATIC_STRING(
81 "weighted_target: all children report state TRANSIENT_FAILURE"),
82 GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNAVAILABLE);
83 absl::Status status = grpc_error_to_absl_status(error);
84 ASSERT_TRUE(absl::IsUnavailable(status));
85 ASSERT_EQ(status.message(),
86 "weighted_target: all children report state TRANSIENT_FAILURE");
87 GRPC_ERROR_UNREF(error);
92 int main(int argc, char** argv) {
93 testing::InitGoogleTest(&argc, argv);
94 grpc::testing::TestEnvironment env(argc, argv);
95 return RUN_ALL_TESTS();