error_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <string.h>
22 
23 #include <gmock/gmock.h>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 
30 
31 TEST(ErrorTest, SetGetInt) {
34  intptr_t i = 0;
35 #ifndef NDEBUG
36  // GRPC_ERROR_INT_FILE_LINE is for debug only
38  EXPECT_TRUE(i); // line set will never be 0
39 #endif
42 
43  intptr_t errnumber = 314;
46  EXPECT_EQ(i, errnumber);
47 
48  intptr_t http = 2;
51  EXPECT_EQ(i, http);
52 
54 }
55 
56 TEST(ErrorTest, SetGetStr) {
58 
62 #ifndef NDEBUG
63  // GRPC_ERROR_STR_FILE is for debug only
65  EXPECT_THAT(str, testing::HasSubstr("error_test.c"));
66  // __FILE__ expands differently on
67  // Windows. All should at least
68  // contain error_test.c
69 #endif
71  EXPECT_EQ(str, "Test");
72 
73  error =
76  EXPECT_EQ(str, "longer message");
77 
79 }
80 
81 TEST(ErrorTest, CopyAndUnRef) {
82  // error1 has one ref
83  grpc_error_handle error1 =
85  GRPC_ERROR_STR_GRPC_MESSAGE, "message");
88  EXPECT_EQ(str, "message");
89 
90  // error 1 has two refs
91  (void)GRPC_ERROR_REF(error1);
92  // this gives error3 a ref to the new error, and decrements error1 to one ref
93  grpc_error_handle error3 =
94  grpc_error_set_str(error1, GRPC_ERROR_STR_SYSCALL, "syscall");
95  EXPECT_NE(error3, error1); // should not be the same because of extra ref
97  EXPECT_EQ(str, "message");
98 
99  // error 1 should not have a syscall but 3 should
102  EXPECT_EQ(str, "syscall");
103 
104  GRPC_ERROR_UNREF(error1);
105  GRPC_ERROR_UNREF(error3);
106 }
107 
108 TEST(ErrorTest, CreateReferencing) {
111  GRPC_ERROR_STR_GRPC_MESSAGE, "message");
112  grpc_error_handle parent =
114  EXPECT_NE(parent, GRPC_ERROR_NONE);
115 
117  GRPC_ERROR_UNREF(parent);
118 }
119 
120 TEST(ErrorTest, CreateReferencingMany) {
122  children[0] =
124  GRPC_ERROR_STR_GRPC_MESSAGE, "message");
125  children[1] =
128  children[2] =
130  GRPC_ERROR_STR_GRPC_MESSAGE, "message 3");
131 
132  grpc_error_handle parent =
134  EXPECT_NE(parent, GRPC_ERROR_NONE);
135 
136  for (size_t i = 0; i < 3; ++i) {
138  }
139  GRPC_ERROR_UNREF(parent);
140 }
141 
142 TEST(ErrorTest, PrintErrorString) {
148  // gpr_log(GPR_DEBUG, "%s", grpc_error_std_string(error).c_str());
150 }
151 
152 TEST(ErrorTest, PrintErrorStringReference) {
157  GRPC_ERROR_STR_GRPC_MESSAGE, "message for child 1");
161  GRPC_ERROR_STR_GRPC_MESSAGE, "message for child 2");
162 
163  grpc_error_handle parent =
165 
166  for (size_t i = 0; i < 2; ++i) {
168  }
169  GRPC_ERROR_UNREF(parent);
170 }
171 
172 TEST(ErrorTest, TestOsError) {
173  int fake_errno = 5;
174  const char* syscall = "syscall name";
176 
177  intptr_t i = 0;
179  EXPECT_EQ(i, fake_errno);
180 
185 }
186 
187 TEST(ErrorTest, Overflow) {
188  // absl::Status doesn't have a limit so there is no overflow
189 #ifndef GRPC_ERROR_IS_ABSEIL_STATUS
191 
192  for (size_t i = 0; i < 150; ++i) {
195  }
196 
199  "message for child 2");
201 
202  intptr_t i;
204  EXPECT_EQ(i, 5);
206 
209  EXPECT_EQ(i, 10);
210 
212 #endif
213 }
214 
215 int main(int argc, char** argv) {
216  grpc::testing::TestEnvironment env(&argc, argv);
217  ::testing::InitGoogleTest(&argc, argv);
218  grpc_init();
219  int retval = RUN_ALL_TESTS();
220  grpc_shutdown();
221  return retval;
222 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
GRPC_ERROR_STR_SYSCALL
@ GRPC_ERROR_STR_SYSCALL
syscall that generated this error
Definition: error.h:114
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
generate.env
env
Definition: generate.py:37
GRPC_ERROR_STR_FILE
@ GRPC_ERROR_STR_FILE
source file in which this error occurred
Definition: error.h:109
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
GRPC_ERROR_INT_HTTP2_ERROR
@ GRPC_ERROR_INT_HTTP2_ERROR
http2 error code associated with the error (see the HTTP2 RFC)
Definition: error.h:77
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)
Definition: error.h:307
TEST
TEST(ErrorTest, SetGetInt)
Definition: error_test.cc:31
GRPC_ERROR_STR_DESCRIPTION
@ GRPC_ERROR_STR_DESCRIPTION
top-level textual description of this error
Definition: error.h:106
grpc_error_set_str
grpc_error_handle grpc_error_set_str(grpc_error_handle src, grpc_error_strs which, absl::string_view str)
Definition: error.cc:650
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_error_add_child
grpc_error_handle grpc_error_add_child(grpc_error_handle src, grpc_error_handle child)
Definition: error.cc:678
GRPC_OS_ERROR
#define GRPC_OS_ERROR(err, call_name)
create an error associated with errno!=0 (an 'operating system' error)
Definition: error.h:352
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
grpc.h
GRPC_ERROR_INT_SIZE
@ GRPC_ERROR_INT_SIZE
context sensitive size associated with the error
Definition: error.h:75
grpc_error_get_int
bool grpc_error_get_int(grpc_error_handle err, grpc_error_ints which, intptr_t *p)
Definition: error.cc:635
syscall
const char * syscall
Definition: third_party/libuv/src/win/internal.h:270
GRPC_ERROR_STR_GRPC_MESSAGE
@ GRPC_ERROR_STR_GRPC_MESSAGE
grpc status message associated with this error
Definition: error.h:120
main
int main(int argc, char **argv)
Definition: error_test.cc:215
googletest-filter-unittest.child
child
Definition: bloaty/third_party/googletest/googletest/test/googletest-filter-unittest.py:62
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
error.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
GRPC_ERROR_REF
#define GRPC_ERROR_REF(err)
Definition: error.h:261
grpc_error_set_int
grpc_error_handle grpc_error_set_int(grpc_error_handle src, grpc_error_ints which, intptr_t value)
Definition: error.cc:613
alloc.h
GRPC_ERROR_INT_ERRNO
@ GRPC_ERROR_INT_ERRNO
'errno' from the operating system
Definition: error.h:56
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
GRPC_ERROR_STR_TSI_ERROR
@ GRPC_ERROR_STR_TSI_ERROR
tsi error string associated with this error
Definition: error.h:126
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
GRPC_STATUS_UNIMPLEMENTED
@ GRPC_STATUS_UNIMPLEMENTED
Definition: include/grpc/impl/codegen/status.h:124
GRPC_ERROR_INT_FILE_LINE
@ GRPC_ERROR_INT_FILE_LINE
LINE from the call site creating the error
Definition: error.h:59
GRPC_STATUS_INTERNAL
@ GRPC_STATUS_INTERNAL
Definition: include/grpc/impl/codegen/status.h:129
grpc_error_get_str
bool grpc_error_get_str(grpc_error_handle err, grpc_error_strs which, std::string *s)
Returns false if the specified string is not set.
Definition: error.cc:659
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
testing::HasSubstr
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8803
children
std::map< std::string, Node * > children
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/field_mask_util.cc:257
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_ERROR_INT_GRPC_STATUS
@ GRPC_ERROR_INT_GRPC_STATUS
grpc status code representing this error
Definition: error.h:66


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:19