status_helper_test.cc
Go to the documentation of this file.
1 //
2 // Copyright 2021 the gRPC authors.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include "absl/status/status.h"
23 #include "absl/strings/str_cat.h"
24 #include "absl/time/clock.h"
25 #include "google/rpc/status.upb.h"
26 #include "upb/upb.hpp"
27 
28 namespace grpc_core {
29 namespace {
30 
31 TEST(StatusUtilTest, CreateStatus) {
32  absl::Status s =
36  EXPECT_EQ("Test", s.message());
37 #ifndef NDEBUG
38  EXPECT_EQ(true, StatusGetStr(s, StatusStrProperty::kFile).has_value());
40 #endif
44 }
45 
46 TEST(StatusUtilTest, SetAndGetInt) {
50 }
51 
52 TEST(StatusUtilTest, GetIntNotExistent) {
56 }
57 
58 TEST(StatusUtilTest, SetAndGetStr) {
62 }
63 
64 TEST(StatusUtilTest, GetStrNotExistent) {
68 }
69 
70 TEST(StatusUtilTest, SetAndGetTime) {
75 }
76 
77 TEST(StatusUtilTest, GetTimeNotExistent) {
81 }
82 
83 TEST(StatusUtilTest, AddAndGetChildren) {
85  absl::Status child1 = absl::AbortedError("Message1");
86  absl::Status child2 = absl::DeadlineExceededError("Message2");
88  StatusAddChild(&s, child1);
89  StatusAddChild(&s, child2);
90  StatusAddChild(&s, child3);
92  ::testing::ElementsAre(child1, child2, child3));
93 }
94 
95 TEST(StatusUtilTest, ToAndFromProto) {
96  absl::Status s = absl::CancelledError("Message");
101  size_t len;
102  const char* buf = google_rpc_Status_serialize(msg, arena.ptr(), &len);
105  EXPECT_EQ(s, s2);
106 }
107 
108 TEST(StatusUtilTest, ToAndFromProtoWithNonUTF8Characters) {
109  absl::Status s = absl::CancelledError("_\xAB\xCD\xEF_");
111  StatusSetStr(&s, StatusStrProperty::kOsError, "!\xFF\xCC\xAA!");
114  size_t len;
115  const char* buf = google_rpc_Status_serialize(msg, arena.ptr(), &len);
118  EXPECT_EQ(s, s2);
119 }
120 
121 TEST(StatusUtilTest, OkToString) {
124  EXPECT_EQ("OK", t);
125 }
126 
127 TEST(StatusUtilTest, CancelledErrorToString) {
130  EXPECT_EQ("CANCELLED", t);
131 }
132 
133 TEST(StatusUtilTest, ErrorWithIntPropertyToString) {
134  absl::Status s = absl::CancelledError("Message");
137  EXPECT_EQ("CANCELLED:Message {errno:2021}", t);
138 }
139 
140 TEST(StatusUtilTest, ErrorWithStrPropertyToString) {
141  absl::Status s = absl::CancelledError("Message");
144  EXPECT_EQ("CANCELLED:Message {description:\"Hey\"}", t);
145 }
146 
147 TEST(StatusUtilTest, ErrorWithTimePropertyToString) {
148  absl::Status s = absl::CancelledError("Message");
149  absl::Time t = absl::FromCivil(absl::CivilSecond(2021, 4, 29, 8, 56, 30),
153  absl::StrCat("CANCELLED:Message {created_time:\"",
154  absl::FormatTime(t), "\"}"));
155 }
156 
157 TEST(StatusUtilTest, ComplexErrorWithChildrenToString) {
158  absl::Status s = absl::CancelledError("Message");
160  absl::Status s1 = absl::AbortedError("Message1");
161  StatusAddChild(&s, s1);
162  absl::Status s2 = absl::AlreadyExistsError("Message2");
164  StatusAddChild(&s, s2);
166  EXPECT_EQ(
167  "CANCELLED:Message {errno:2021, children:["
168  "ABORTED:Message1, ALREADY_EXISTS:Message2 {os_error:\"value\"}]}",
169  t);
170 }
171 
172 TEST(StatusUtilTest, AllocHeapPtr) {
174  absl::AbortedError("Message")};
175  for (const auto& s : statuses) {
179  }
180 }
181 
182 TEST(StatusUtilTest, MoveHeapPtr) {
184  absl::AbortedError("Message")};
185  for (const auto& s : statuses) {
188  }
189 }
190 
191 } // namespace
192 } // namespace grpc_core
193 
194 int main(int argc, char** argv) {
195  ::testing::InitGoogleTest(&argc, argv);
196  int ret = RUN_ALL_TESTS();
197  return ret;
198 }
google_rpc_Status
struct google_rpc_Status google_rpc_Status
Definition: google/rpc/status.upb.h:24
grpc_core::internal::StatusFromProto
absl::Status StatusFromProto(google_rpc_Status *msg)
Definition: status_helper.cc:398
status.upb.h
grpc_core::StatusIntProperty::kFileLine
@ kFileLine
LINE from the call site creating the error
absl::AbortedError
Status AbortedError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:323
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_core::StatusGetChildren
std::vector< absl::Status > StatusGetChildren(absl::Status status)
Returns all children status from a status.
Definition: status_helper.cc:276
absl::Time
Definition: third_party/abseil-cpp/absl/time/time.h:642
absl::time_internal::cctz::detail::civil_time< time_internal::second_tag >
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::StatusStrProperty::kFile
@ kFile
source file in which this error occurred
grpc_core::StatusAddChild
void StatusAddChild(absl::Status *status, absl::Status child)
Adds a child status to status.
Definition: status_helper.cc:256
grpc_core::StatusStrProperty::kOsError
@ kOsError
operating system description of this error
absl::CancelledError
Status CancelledError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:331
grpc_core::internal::StatusAllocHeapPtr
uintptr_t StatusAllocHeapPtr(absl::Status s)
Definition: status_helper.cc:421
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::StatusTimeProperty::kCreated
@ kCreated
timestamp of error creation
grpc_core::StatusSetTime
void StatusSetTime(absl::Status *status, StatusTimeProperty key, absl::Time time)
Sets the time property to the status.
Definition: status_helper.cc:227
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
absl::LocalTimeZone
TimeZone LocalTimeZone()
Definition: third_party/abseil-cpp/absl/time/time.h:1109
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
grpc_core::StatusCreate
absl::Status StatusCreate(absl::StatusCode code, absl::string_view msg, const DebugLocation &location, std::vector< absl::Status > children)
Creates a status with given additional information.
Definition: status_helper.cc:168
absl::FormatConversionChar::s
@ s
xds_manager.p
p
Definition: xds_manager.py:60
status_helper.h
grpc_core::StatusGetStr
absl::optional< std::string > StatusGetStr(const absl::Status &status, StatusStrProperty key)
Gets the str property from the status.
Definition: status_helper.cc:217
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
absl::FormatTime
std::string FormatTime(absl::string_view format, absl::Time t, absl::TimeZone tz)
Definition: abseil-cpp/absl/time/format.cc:74
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::ElementsAre
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13040
google_rpc_Status_parse
UPB_INLINE google_rpc_Status * google_rpc_Status_parse(const char *buf, size_t size, upb_Arena *arena)
Definition: google/rpc/status.upb.h:36
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
grpc_core::StatusSetInt
void StatusSetInt(absl::Status *status, StatusIntProperty key, intptr_t value)
Sets the int property to the status.
Definition: status_helper.cc:187
main
int main(int argc, char **argv)
Definition: status_helper_test.cc:194
grpc_core::StatusToString
std::string StatusToString(const absl::Status &status)
Definition: status_helper.cc:282
grpc_core::internal::StatusGetFromHeapPtr
absl::Status StatusGetFromHeapPtr(uintptr_t ptr)
Get the status from a heap ptr.
Definition: status_helper.cc:432
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
grpc_core::StatusSetStr
void StatusSetStr(absl::Status *status, StatusStrProperty key, absl::string_view value)
Sets the str property to the status.
Definition: status_helper.cc:212
grpc_core::StatusStrProperty::kDescription
@ kDescription
top-level textual description of this error
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::internal::StatusFreeHeapPtr
void StatusFreeHeapPtr(uintptr_t ptr)
Frees the allocated status at heap ptr.
Definition: status_helper.cc:427
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
grpc_core::internal::StatusToProto
google_rpc_Status * StatusToProto(const absl::Status &status, upb_Arena *arena)
Definition: status_helper.cc:353
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
upb::Arena
Definition: upb.hpp:68
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::StatusGetTime
absl::optional< absl::Time > StatusGetTime(const absl::Status &status, StatusTimeProperty key)
Gets the time property from the status.
Definition: status_helper.cc:235
upb.hpp
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
absl::StatusCode::kUnknown
@ kUnknown
absl::str_format_internal::LengthMod::t
@ t
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
absl::DeadlineExceededError
Status DeadlineExceededError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:339
absl::FromCivil
Time FromCivil(CivilSecond ct, TimeZone tz)
Definition: third_party/abseil-cpp/absl/time/time.h:1158
grpc_core::internal::StatusMoveFromHeapPtr
absl::Status StatusMoveFromHeapPtr(uintptr_t ptr)
Move the status from a heap ptr. (GetFrom & FreeHeap)
Definition: status_helper.cc:440
google_rpc_Status_serialize
UPB_INLINE char * google_rpc_Status_serialize(const google_rpc_Status *msg, upb_Arena *arena, size_t *len)
Definition: google/rpc/status.upb.h:55
absl::UnimplementedError
Status UnimplementedError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:379
grpc_core::StatusGetInt
absl::optional< intptr_t > StatusGetInt(const absl::Status &status, StatusIntProperty key)
Gets the int property from the status.
Definition: status_helper.cc:192
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
absl::AlreadyExistsError
Status AlreadyExistsError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:327
grpc_core::StatusIntProperty::kErrorNo
@ kErrorNo
'errno' from the operating system


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:17