|
|
|
@ -20,117 +20,114 @@ |
|
|
|
|
|
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
#include <gmock/gmock.h> |
|
|
|
|
|
|
|
|
|
#include <grpc/grpc.h> |
|
|
|
|
#include <grpc/support/alloc.h> |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
|
|
|
|
|
#include "test/core/util/test_config.h" |
|
|
|
|
|
|
|
|
|
static void test_set_get_int() { |
|
|
|
|
TEST(ErrorTest, SetGetInt) { |
|
|
|
|
grpc_error_handle error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Test"); |
|
|
|
|
GPR_ASSERT(error != GRPC_ERROR_NONE); |
|
|
|
|
EXPECT_NE(error, GRPC_ERROR_NONE); |
|
|
|
|
intptr_t i = 0; |
|
|
|
|
GPR_ASSERT(grpc_error_get_int(error, GRPC_ERROR_INT_FILE_LINE, &i)); |
|
|
|
|
GPR_ASSERT(i); // line set will never be 0
|
|
|
|
|
GPR_ASSERT(!grpc_error_get_int(error, GRPC_ERROR_INT_ERRNO, &i)); |
|
|
|
|
GPR_ASSERT(!grpc_error_get_int(error, GRPC_ERROR_INT_SIZE, &i)); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_FILE_LINE, &i)); |
|
|
|
|
EXPECT_TRUE(i); // line set will never be 0
|
|
|
|
|
EXPECT_TRUE(!grpc_error_get_int(error, GRPC_ERROR_INT_ERRNO, &i)); |
|
|
|
|
EXPECT_TRUE(!grpc_error_get_int(error, GRPC_ERROR_INT_SIZE, &i)); |
|
|
|
|
|
|
|
|
|
intptr_t errnumber = 314; |
|
|
|
|
error = grpc_error_set_int(error, GRPC_ERROR_INT_ERRNO, errnumber); |
|
|
|
|
GPR_ASSERT(grpc_error_get_int(error, GRPC_ERROR_INT_ERRNO, &i)); |
|
|
|
|
GPR_ASSERT(i == errnumber); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_ERRNO, &i)); |
|
|
|
|
EXPECT_EQ(i, errnumber); |
|
|
|
|
|
|
|
|
|
intptr_t http = 2; |
|
|
|
|
error = grpc_error_set_int(error, GRPC_ERROR_INT_HTTP2_ERROR, http); |
|
|
|
|
GPR_ASSERT(grpc_error_get_int(error, GRPC_ERROR_INT_HTTP2_ERROR, &i)); |
|
|
|
|
GPR_ASSERT(i == http); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_HTTP2_ERROR, &i)); |
|
|
|
|
EXPECT_EQ(i, http); |
|
|
|
|
|
|
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_set_get_str() { |
|
|
|
|
TEST(ErrorTest, SetGetStr) { |
|
|
|
|
grpc_error_handle error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Test"); |
|
|
|
|
|
|
|
|
|
grpc_slice str; |
|
|
|
|
GPR_ASSERT(!grpc_error_get_str(error, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
GPR_ASSERT(!grpc_error_get_str(error, GRPC_ERROR_STR_TSI_ERROR, &str)); |
|
|
|
|
std::string str; |
|
|
|
|
EXPECT_TRUE(!grpc_error_get_str(error, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
EXPECT_TRUE(!grpc_error_get_str(error, GRPC_ERROR_STR_TSI_ERROR, &str)); |
|
|
|
|
|
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error, GRPC_ERROR_STR_FILE, &str)); |
|
|
|
|
GPR_ASSERT(strstr((char*)GRPC_SLICE_START_PTR(str), |
|
|
|
|
"error_test.c")); // __FILE__ expands differently on
|
|
|
|
|
// Windows. All should at least
|
|
|
|
|
// contain error_test.c
|
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error, GRPC_ERROR_STR_FILE, &str)); |
|
|
|
|
EXPECT_THAT(str, testing::HasSubstr("error_test.c")); |
|
|
|
|
// __FILE__ expands differently on
|
|
|
|
|
// Windows. All should at least
|
|
|
|
|
// contain error_test.c
|
|
|
|
|
|
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error, GRPC_ERROR_STR_DESCRIPTION, &str)); |
|
|
|
|
GPR_ASSERT(!strncmp((char*)GRPC_SLICE_START_PTR(str), "Test", |
|
|
|
|
GRPC_SLICE_LENGTH(str))); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error, GRPC_ERROR_STR_DESCRIPTION, &str)); |
|
|
|
|
EXPECT_EQ(str, "Test"); |
|
|
|
|
|
|
|
|
|
error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
grpc_slice_from_static_string("longer message")); |
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, &str)); |
|
|
|
|
GPR_ASSERT(!strncmp((char*)GRPC_SLICE_START_PTR(str), "longer message", |
|
|
|
|
GRPC_SLICE_LENGTH(str))); |
|
|
|
|
error = |
|
|
|
|
grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, "longer message"); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, &str)); |
|
|
|
|
EXPECT_EQ(str, "longer message"); |
|
|
|
|
|
|
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_copy_and_unref() { |
|
|
|
|
TEST(ErrorTest, CopyAndUnRef) { |
|
|
|
|
// error1 has one ref
|
|
|
|
|
grpc_error_handle error1 = grpc_error_set_str( |
|
|
|
|
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Test"), GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
grpc_slice_from_static_string("message")); |
|
|
|
|
grpc_slice str; |
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error1, GRPC_ERROR_STR_GRPC_MESSAGE, &str)); |
|
|
|
|
GPR_ASSERT(!strncmp((char*)GRPC_SLICE_START_PTR(str), "message", |
|
|
|
|
GRPC_SLICE_LENGTH(str))); |
|
|
|
|
grpc_error_handle error1 = |
|
|
|
|
grpc_error_set_str(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Test"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, "message"); |
|
|
|
|
std::string str; |
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error1, GRPC_ERROR_STR_GRPC_MESSAGE, &str)); |
|
|
|
|
EXPECT_EQ(str, "message"); |
|
|
|
|
|
|
|
|
|
// error 1 has two refs
|
|
|
|
|
GRPC_ERROR_REF(error1); |
|
|
|
|
// this gives error3 a ref to the new error, and decrements error1 to one ref
|
|
|
|
|
grpc_error_handle error3 = grpc_error_set_str( |
|
|
|
|
error1, GRPC_ERROR_STR_SYSCALL, grpc_slice_from_static_string("syscall")); |
|
|
|
|
GPR_ASSERT(error3 != error1); // should not be the same because of extra ref
|
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error3, GRPC_ERROR_STR_GRPC_MESSAGE, &str)); |
|
|
|
|
GPR_ASSERT(!strncmp((char*)GRPC_SLICE_START_PTR(str), "message", |
|
|
|
|
GRPC_SLICE_LENGTH(str))); |
|
|
|
|
grpc_error_handle error3 = |
|
|
|
|
grpc_error_set_str(error1, GRPC_ERROR_STR_SYSCALL, "syscall"); |
|
|
|
|
EXPECT_NE(error3, error1); // should not be the same because of extra ref
|
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error3, GRPC_ERROR_STR_GRPC_MESSAGE, &str)); |
|
|
|
|
EXPECT_EQ(str, "message"); |
|
|
|
|
|
|
|
|
|
// error 1 should not have a syscall but 3 should
|
|
|
|
|
GPR_ASSERT(!grpc_error_get_str(error1, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error3, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
GPR_ASSERT(!strncmp((char*)GRPC_SLICE_START_PTR(str), "syscall", |
|
|
|
|
GRPC_SLICE_LENGTH(str))); |
|
|
|
|
EXPECT_TRUE(!grpc_error_get_str(error1, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error3, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
EXPECT_EQ(str, "syscall"); |
|
|
|
|
|
|
|
|
|
GRPC_ERROR_UNREF(error1); |
|
|
|
|
GRPC_ERROR_UNREF(error3); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_create_referencing() { |
|
|
|
|
grpc_error_handle child = grpc_error_set_str( |
|
|
|
|
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, grpc_slice_from_static_string("message")); |
|
|
|
|
TEST(ErrorTest, CreateReferencing) { |
|
|
|
|
grpc_error_handle child = |
|
|
|
|
grpc_error_set_str(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, "message"); |
|
|
|
|
grpc_error_handle parent = |
|
|
|
|
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING("Parent", &child, 1); |
|
|
|
|
GPR_ASSERT(parent != GRPC_ERROR_NONE); |
|
|
|
|
EXPECT_NE(parent, GRPC_ERROR_NONE); |
|
|
|
|
|
|
|
|
|
GRPC_ERROR_UNREF(child); |
|
|
|
|
GRPC_ERROR_UNREF(parent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_create_referencing_many() { |
|
|
|
|
TEST(ErrorTest, CreateReferencingMany) { |
|
|
|
|
grpc_error_handle children[3]; |
|
|
|
|
children[0] = grpc_error_set_str( |
|
|
|
|
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child1"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, grpc_slice_from_static_string("message")); |
|
|
|
|
children[0] = |
|
|
|
|
grpc_error_set_str(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child1"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, "message"); |
|
|
|
|
children[1] = |
|
|
|
|
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child2"), |
|
|
|
|
GRPC_ERROR_INT_HTTP2_ERROR, 5); |
|
|
|
|
children[2] = grpc_error_set_str( |
|
|
|
|
GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child3"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, grpc_slice_from_static_string("message 3")); |
|
|
|
|
children[2] = |
|
|
|
|
grpc_error_set_str(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Child3"), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, "message 3"); |
|
|
|
|
|
|
|
|
|
grpc_error_handle parent = |
|
|
|
|
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING("Parent", children, 3); |
|
|
|
|
GPR_ASSERT(parent != GRPC_ERROR_NONE); |
|
|
|
|
EXPECT_NE(parent, GRPC_ERROR_NONE); |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 3; ++i) { |
|
|
|
|
GRPC_ERROR_UNREF(children[i]); |
|
|
|
@ -138,29 +135,26 @@ static void test_create_referencing_many() { |
|
|
|
|
GRPC_ERROR_UNREF(parent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void print_error_string() { |
|
|
|
|
TEST(ErrorTest, PrintErrorString) { |
|
|
|
|
grpc_error_handle error = |
|
|
|
|
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Error"), |
|
|
|
|
GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNIMPLEMENTED); |
|
|
|
|
error = grpc_error_set_int(error, GRPC_ERROR_INT_SIZE, 666); |
|
|
|
|
error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
grpc_slice_from_static_string("message")); |
|
|
|
|
error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, "message"); |
|
|
|
|
// gpr_log(GPR_DEBUG, "%s", grpc_error_std_string(error).c_str());
|
|
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void print_error_string_reference() { |
|
|
|
|
TEST(ErrorTest, PrintErrorStringReference) { |
|
|
|
|
grpc_error_handle children[2]; |
|
|
|
|
children[0] = grpc_error_set_str( |
|
|
|
|
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING("1"), |
|
|
|
|
GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNIMPLEMENTED), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
grpc_slice_from_static_string("message for child 1")); |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, "message for child 1"); |
|
|
|
|
children[1] = grpc_error_set_str( |
|
|
|
|
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING("2sd"), |
|
|
|
|
GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_INTERNAL), |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
grpc_slice_from_static_string("message for child 2")); |
|
|
|
|
GRPC_ERROR_STR_GRPC_MESSAGE, "message for child 2"); |
|
|
|
|
|
|
|
|
|
grpc_error_handle parent = |
|
|
|
|
GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING("Parent", children, 2); |
|
|
|
@ -171,23 +165,22 @@ static void print_error_string_reference() { |
|
|
|
|
GRPC_ERROR_UNREF(parent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_os_error() { |
|
|
|
|
TEST(ErrorTest, TestOsError) { |
|
|
|
|
int fake_errno = 5; |
|
|
|
|
const char* syscall = "syscall name"; |
|
|
|
|
grpc_error_handle error = GRPC_OS_ERROR(fake_errno, syscall); |
|
|
|
|
|
|
|
|
|
intptr_t i = 0; |
|
|
|
|
GPR_ASSERT(grpc_error_get_int(error, GRPC_ERROR_INT_ERRNO, &i)); |
|
|
|
|
GPR_ASSERT(i == fake_errno); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_ERRNO, &i)); |
|
|
|
|
EXPECT_EQ(i, fake_errno); |
|
|
|
|
|
|
|
|
|
grpc_slice str; |
|
|
|
|
GPR_ASSERT(grpc_error_get_str(error, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
GPR_ASSERT(!strncmp((char*)GRPC_SLICE_START_PTR(str), syscall, |
|
|
|
|
GRPC_SLICE_LENGTH(str))); |
|
|
|
|
std::string str; |
|
|
|
|
EXPECT_TRUE(grpc_error_get_str(error, GRPC_ERROR_STR_SYSCALL, &str)); |
|
|
|
|
EXPECT_EQ(str, syscall); |
|
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void test_overflow() { |
|
|
|
|
TEST(ErrorTest, Overflow) { |
|
|
|
|
// absl::Status doesn't have a limit so there is no overflow
|
|
|
|
|
#ifndef GRPC_ERROR_IS_ABSEIL_STATUS |
|
|
|
|
grpc_error_handle error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Overflow"); |
|
|
|
@ -198,19 +191,18 @@ static void test_overflow() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
error = grpc_error_set_int(error, GRPC_ERROR_INT_HTTP2_ERROR, 5); |
|
|
|
|
error = |
|
|
|
|
grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
grpc_slice_from_static_string("message for child 2")); |
|
|
|
|
error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, |
|
|
|
|
"message for child 2"); |
|
|
|
|
error = grpc_error_set_int(error, GRPC_ERROR_INT_GRPC_STATUS, 5); |
|
|
|
|
|
|
|
|
|
intptr_t i; |
|
|
|
|
GPR_ASSERT(grpc_error_get_int(error, GRPC_ERROR_INT_HTTP2_ERROR, &i)); |
|
|
|
|
GPR_ASSERT(i == 5); |
|
|
|
|
GPR_ASSERT(!grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &i)); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_HTTP2_ERROR, &i)); |
|
|
|
|
EXPECT_EQ(i, 5); |
|
|
|
|
EXPECT_TRUE(!grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &i)); |
|
|
|
|
|
|
|
|
|
error = grpc_error_set_int(error, GRPC_ERROR_INT_HTTP2_ERROR, 10); |
|
|
|
|
GPR_ASSERT(grpc_error_get_int(error, GRPC_ERROR_INT_HTTP2_ERROR, &i)); |
|
|
|
|
GPR_ASSERT(i == 10); |
|
|
|
|
EXPECT_TRUE(grpc_error_get_int(error, GRPC_ERROR_INT_HTTP2_ERROR, &i)); |
|
|
|
|
EXPECT_EQ(i, 10); |
|
|
|
|
|
|
|
|
|
GRPC_ERROR_UNREF(error); |
|
|
|
|
#endif |
|
|
|
@ -218,17 +210,9 @@ static void test_overflow() { |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
grpc::testing::TestEnvironment env(argc, argv); |
|
|
|
|
::testing::InitGoogleTest(&argc, argv); |
|
|
|
|
grpc_init(); |
|
|
|
|
test_set_get_int(); |
|
|
|
|
test_set_get_str(); |
|
|
|
|
test_copy_and_unref(); |
|
|
|
|
print_error_string(); |
|
|
|
|
print_error_string_reference(); |
|
|
|
|
test_os_error(); |
|
|
|
|
test_create_referencing(); |
|
|
|
|
test_create_referencing_many(); |
|
|
|
|
test_overflow(); |
|
|
|
|
int retval = RUN_ALL_TESTS(); |
|
|
|
|
grpc_shutdown(); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
return retval; |
|
|
|
|
} |
|
|
|
|