diff --git a/example/demo/hos_write_demo.cpp b/example/demo/hos_write_demo.cpp index 95402a17..7092c0a0 100644 --- a/example/demo/hos_write_demo.cpp +++ b/example/demo/hos_write_demo.cpp @@ -98,7 +98,7 @@ int main(int argc, char *argv[]) hos_instance = hos_get_instance(); if (hos_instance == NULL) { - hos_instance = hos_init_instance(conf_file, module_name, 1, bucket); + hos_instance = hos_init_instance(conf_file, module_name, 1); } if (hos_instance == NULL) { diff --git a/example/performance/HosClientPerformance.cpp b/example/performance/HosClientPerformance.cpp index fbc95790..120c4194 100644 --- a/example/performance/HosClientPerformance.cpp +++ b/example/performance/HosClientPerformance.cpp @@ -470,7 +470,7 @@ int main(int argc, char *argv[]) return -1; } //初始化hos instance - hos_instance hos_instance = hos_init_instance(conf_path, module, thread_sum, bucket); + hos_instance hos_instance = hos_init_instance(conf_path, module, thread_sum); if (hos_instance == NULL) { printf("error:hos_client_handle\n error:[%d]%s\n", hos_get_init_instance_errorcode(), hos_get_init_instance_errormsg()); diff --git a/gtest/CMakeLists.txt b/gtest/CMakeLists.txt index db8c5f56..3dabf565 100644 --- a/gtest/CMakeLists.txt +++ b/gtest/CMakeLists.txt @@ -13,7 +13,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-cove add_definitions(-g -W -Wall -std=c++11) #add_executable(gtest_hos_client gtest_hos_init_instance.cpp gtest_hos_get_instance.cpp gtest_hos_close_fd.cpp gtest_hos_open_fd.cpp) -add_executable(gtest_hos_client CheckHosClient.cpp gtest_hos_write.cpp) -#add_executable(gtest_hos_client ${SRCS}) +#add_executable(gtest_hos_client CheckHosClient.cpp gtest_hos_write.cpp) +add_executable(gtest_hos_client ${SRCS}) target_link_libraries(gtest_hos_client hos-client-cpp gtest gtest_main pthread) diff --git a/gtest/CheckHosClient.cpp b/gtest/CheckHosClient.cpp index b9f64d1d..644c4ab0 100644 --- a/gtest/CheckHosClient.cpp +++ b/gtest/CheckHosClient.cpp @@ -109,7 +109,7 @@ void CheckStructGHosFdContext(hos_fd_context_t *actual, hos_fd_context_t *expect void CheckHosInstance(hos_instance actual, hos_instance expect) { - EXPECT_EQ(actual->result, expect->result); + EXPECT_EQ(actual->status, expect->status); EXPECT_EQ(actual->error_code, expect->error_code); EXPECT_STREQ(actual->error_message, expect->error_message); EXPECT_STREQ(actual->hos_url_prefix, expect->hos_url_prefix); diff --git a/gtest/conf/default.conf b/gtest/conf/default.conf index 8f4e081f..7715c654 100644 --- a/gtest/conf/default.conf +++ b/gtest/conf/default.conf @@ -97,7 +97,7 @@ hos_log_path="./hoslog" #default hos_log_level=30 #default -hos_poolsize=0 +hos_poolsize=10 #default hos_cache_size=102400 #default diff --git a/gtest/gtest_hos_close_fd.cpp b/gtest/gtest_hos_close_fd.cpp index 0311440a..bfbdc3b7 100644 --- a/gtest/gtest_hos_close_fd.cpp +++ b/gtest/gtest_hos_close_fd.cpp @@ -43,7 +43,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -71,7 +71,7 @@ TEST(hos_close_fd, normal) hos_fd_context_t expect_fd_info; int thread_num = 2; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -90,7 +90,7 @@ TEST(hos_close_fd, normal) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = INSTANCE_UNINIT_STATE; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -114,7 +114,7 @@ TEST(hos_close_fd, paramer_error) hos_fd_context_t expect_fd_info; int thread_num = 2; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -133,7 +133,7 @@ TEST(hos_close_fd, paramer_error) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = INSTANCE_UNINIT_STATE; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -164,7 +164,7 @@ TEST(hos_close_fd, fd_not_exits) hos_fd_context_t expect_fd_info; int thread_num = 2; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -177,7 +177,7 @@ TEST(hos_close_fd, fd_not_exits) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = INSTANCE_UNINIT_STATE; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -193,4 +193,4 @@ TEST(hos_close_fd, fd_not_exits) memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } -#endif \ No newline at end of file +#endif diff --git a/gtest/gtest_hos_get_instance.cpp b/gtest/gtest_hos_get_instance.cpp index 03528aea..548d0b5e 100644 --- a/gtest/gtest_hos_get_instance.cpp +++ b/gtest/gtest_hos_get_instance.cpp @@ -43,7 +43,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -55,11 +55,10 @@ TEST(hos_get_instance, normal) hos_client_handle_t expect_hos_handle; hos_instance hos_instance = hos_get_instance(); - memset(&expect_hos_instance, 0, sizeof(hos_instance_s)); - expect_hos_instance.result = false; - CheckHosInstance(hos_instance, &expect_hos_instance); + ASSERT_EQ(hos_instance, nullptr); - hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); + hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1); + ASSERT_NE(hos_instance, nullptr); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 1); @@ -77,7 +76,7 @@ TEST(hos_get_instance, normal) int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); diff --git a/gtest/gtest_hos_init_instance.cpp b/gtest/gtest_hos_init_instance.cpp index bf27c431..2102e7a7 100644 --- a/gtest/gtest_hos_init_instance.cpp +++ b/gtest/gtest_hos_init_instance.cpp @@ -48,7 +48,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -59,7 +59,8 @@ TEST(hos_init_instance, normal) hos_instance_s expect_hos_instance; hos_client_handle_t expect_hos_handle; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1); + ASSERT_NE(hos_instance, nullptr); gtest_hos_instance_init(&expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 1); CheckHosInstance(hos_instance, &expect_hos_instance); @@ -87,52 +88,60 @@ TEST(hos_init_instance, param_error) { hos_instance_s expect_hos_instance; - hos_instance hos_instance = hos_init_instance(NULL, "hos_default_conf", 1, HOS_BUCKET); - expect_hos_instance.result = false; + hos_instance hos_instance = hos_init_instance(NULL, "hos_default_conf", 1); + ASSERT_EQ(hos_instance, nullptr); + expect_hos_instance.status = INSTANCE_UNINIT_STATE; expect_hos_instance.error_code = HOS_PARAMETER_ERROR; - const char *err_msg = "param error:conf_path:(null), module:hos_default_conf, thread_num:1, bucket:firewall_hos_bucket"; + const char *err_msg = "param error:conf_path:(null), module:hos_default_conf, thread_num:1"; memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); expect_hos_instance.hos_url_prefix = NULL; - CheckHosInstance(hos_instance, &expect_hos_instance); -} - -TEST(hos_init_instance, bucket_not_exits) -{ - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, "hos_bucket_not_exits"); - - hos_instance_s expect_hos_instance; - expect_hos_instance.result = false; - expect_hos_instance.error_code = HOS_BUCKET_NOT_EXIST; - const char *err_msg = "bucket:hos_bucket_not_exits not exits."; - memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); - expect_hos_instance.hos_url_prefix = NULL; - CheckHosInstance(hos_instance, &expect_hos_instance); - - hos_client_handle_t expect_hos_handle; - memset(&expect_hos_handle, 0, sizeof(expect_hos_handle)); - CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + CheckHosInstance(&g_hos_instance, &expect_hos_instance); } TEST(hos_init_instance, conf_error) { - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_conf", 1, "hos_bucket_not_exits"); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_conf", 1); + ASSERT_EQ(hos_instance, nullptr); hos_instance_s expect_hos_instance; - expect_hos_instance.result = false; + expect_hos_instance.status = INSTANCE_UNINIT_STATE; expect_hos_instance.error_code = HOS_CONF_ERROR; const char *err_msg = "hos param error:hos ip:, hos port:0, accesskeyid:, secretkey:"; memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); expect_hos_instance.hos_url_prefix = NULL; - CheckHosInstance(hos_instance, &expect_hos_instance); + CheckHosInstance(&g_hos_instance, &expect_hos_instance); } TEST(hos_init_instance, server_conn_failed) { - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_server_conf", 1, "hos_bucket_not_exits"); hos_instance_s expect_hos_instance; - expect_hos_instance.result = false; + hos_client_handle_t expect_hos_handle; + + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_server_conf", 1); + memset(&expect_hos_instance, 0, sizeof(expect_hos_instance)); + expect_hos_instance.status = INSTANCE_ATTEMPT_STATE; expect_hos_instance.error_code = NETWORK_CONNECTION; const char *err_msg = "curlCode: 7, Couldn't connect to server"; memcpy(expect_hos_instance.error_message, err_msg, strlen(err_msg)+1); - expect_hos_instance.hos_url_prefix = NULL; + expect_hos_instance.hos_url_prefix = "http://192.168.40.146:9098/hos/"; CheckHosInstance(hos_instance, &expect_hos_instance); + gtest_hos_handle_init(&expect_hos_handle, 1); + memcpy(expect_hos_handle.hos_config.ip, "192.168.40.146", INET_ADDRSTRLEN); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); + + int ret = hos_shutdown_instance(); + EXPECT_EQ(ret, HOS_CLIENT_OK); + memset(&expect_hos_instance, 0, sizeof(expect_hos_instance)); + CheckHosInstance(hos_instance, &expect_hos_instance); + + Aws::Vector().swap(g_hos_handle.buckets); + data_info_t *data_info = (data_info_t *)expect_hos_handle.hos_func.fs2_info.reserved; + free(data_info->cache); + free(data_info->rx_bytes); + free(data_info->rx_pkts); + free(data_info->tx_bytes); + free(data_info->tx_pkts); + free(data_info->tx_failed_bytes); + free(data_info->tx_failed_pkts); + memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); + CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); } \ No newline at end of file diff --git a/gtest/gtest_hos_open_fd.cpp b/gtest/gtest_hos_open_fd.cpp index 4fe22d13..401e2ed9 100644 --- a/gtest/gtest_hos_open_fd.cpp +++ b/gtest/gtest_hos_open_fd.cpp @@ -43,7 +43,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -70,7 +70,8 @@ TEST(hos_open_fd, normal) hos_client_handle_t expect_hos_handle; hos_fd_context_t expect_fd_info[2]; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); + ASSERT_NE(hos_instance, nullptr); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -100,7 +101,7 @@ TEST(hos_open_fd, normal) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -122,7 +123,7 @@ TEST(hos_open_fd, paramer_error) hos_instance_s expect_hos_instance; hos_client_handle_t expect_hos_handle; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -135,7 +136,7 @@ TEST(hos_open_fd, paramer_error) int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -157,7 +158,7 @@ TEST(hos_open_fd, over_threadnums) hos_instance_s expect_hos_instance; hos_client_handle_t expect_hos_handle; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -170,7 +171,7 @@ TEST(hos_open_fd, over_threadnums) int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -190,5 +191,5 @@ TEST(hos_open_fd, over_threadnums) TEST(hos_open_fd, not_init_instance) { int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0); - EXPECT_EQ(fd, HOS_INSTANCE_NOT_INIT); + EXPECT_EQ(fd, HOS_INSTANCE_NOT_ENABLE); } \ No newline at end of file diff --git a/gtest/gtest_hos_shutdown_instance.cpp b/gtest/gtest_hos_shutdown_instance.cpp index b371029a..dbebf8df 100644 --- a/gtest/gtest_hos_shutdown_instance.cpp +++ b/gtest/gtest_hos_shutdown_instance.cpp @@ -48,7 +48,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -61,7 +61,7 @@ TEST(hos_shutdown_instance, normal) hos_client_handle_t expect_hos_handle; hos_fd_context_t expect_fd_info; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 1); @@ -69,7 +69,7 @@ TEST(hos_shutdown_instance, normal) int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -84,7 +84,6 @@ TEST(hos_shutdown_instance, normal) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_shutdown_instance, no_init) @@ -99,7 +98,7 @@ TEST(hos_shutdown_instance, shutdown_more) hos_client_handle_t expect_hos_handle; hos_fd_context_t expect_fd_info; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 1); @@ -107,7 +106,7 @@ TEST(hos_shutdown_instance, shutdown_more) int ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -122,11 +121,9 @@ TEST(hos_shutdown_instance, shutdown_more) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_INSTANCE_NOT_INIT); CheckHosInstance(hos_instance, &expect_hos_instance); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } diff --git a/gtest/gtest_hos_upload_buff.cpp b/gtest/gtest_hos_upload_buff.cpp index 2edca10a..9ac78b06 100644 --- a/gtest/gtest_hos_upload_buff.cpp +++ b/gtest/gtest_hos_upload_buff.cpp @@ -44,7 +44,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -75,7 +75,7 @@ TEST(hos_upload_buff, normal) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -93,7 +93,7 @@ TEST(hos_upload_buff, normal) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -107,7 +107,6 @@ TEST(hos_upload_buff, normal) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_buff, bucket_not_exits) @@ -117,7 +116,7 @@ TEST(hos_upload_buff, bucket_not_exits) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -135,7 +134,7 @@ TEST(hos_upload_buff, bucket_not_exits) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -149,7 +148,6 @@ TEST(hos_upload_buff, bucket_not_exits) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_buff, param_error) @@ -159,7 +157,7 @@ TEST(hos_upload_buff, param_error) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -173,7 +171,7 @@ TEST(hos_upload_buff, param_error) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -187,11 +185,10 @@ TEST(hos_upload_buff, param_error) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_buff, not_init_instance) { int ret = hos_upload_buf(HOS_BUCKET, "object", HOS_BUFF, strlen(HOS_BUFF), hos_callback, (void *)"object", 0); - EXPECT_EQ(ret, HOS_INSTANCE_NOT_INIT); + EXPECT_EQ(ret, HOS_INSTANCE_NOT_ENABLE); } diff --git a/gtest/gtest_hos_upload_file.cpp b/gtest/gtest_hos_upload_file.cpp index 4f886fa3..57a22e72 100644 --- a/gtest/gtest_hos_upload_file.cpp +++ b/gtest/gtest_hos_upload_file.cpp @@ -44,7 +44,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -75,7 +75,7 @@ TEST(hos_upload_file, normal) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -95,7 +95,7 @@ TEST(hos_upload_file, normal) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -109,7 +109,6 @@ TEST(hos_upload_file, normal) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_file, param_error) @@ -119,7 +118,7 @@ TEST(hos_upload_file, param_error) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -133,7 +132,7 @@ TEST(hos_upload_file, param_error) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -147,7 +146,6 @@ TEST(hos_upload_file, param_error) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_file, file_not_exits) @@ -157,7 +155,7 @@ TEST(hos_upload_file, file_not_exits) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -171,7 +169,7 @@ TEST(hos_upload_file, file_not_exits) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -185,13 +183,12 @@ TEST(hos_upload_file, file_not_exits) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } TEST(hos_upload_file, not_init_instance) { int ret = hos_upload_file(HOS_BUCKET, HOS_BUFF, hos_callback, (void *)"object", 0); - EXPECT_EQ(ret, HOS_INSTANCE_NOT_INIT); + EXPECT_EQ(ret, HOS_INSTANCE_NOT_ENABLE); } TEST(hos_upload_file, bucket_not_exits) @@ -201,7 +198,7 @@ TEST(hos_upload_file, bucket_not_exits) hos_fd_context_t expect_fd_info; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, 2); @@ -221,7 +218,7 @@ TEST(hos_upload_file, bucket_not_exits) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -235,5 +232,4 @@ TEST(hos_upload_file, bucket_not_exits) free(data_info->tx_failed_pkts); memset(&expect_hos_handle, 0, sizeof(hos_client_handle_s)); CheckStructGHosHandle(&g_hos_handle, &expect_hos_handle); - EXPECT_EQ((void *)g_fd_context, (void *)NULL); } diff --git a/gtest/gtest_hos_write.cpp b/gtest/gtest_hos_write.cpp index c84b1016..9f46d6f3 100644 --- a/gtest/gtest_hos_write.cpp +++ b/gtest/gtest_hos_write.cpp @@ -45,7 +45,7 @@ static void gtest_hos_handle_init(hos_client_handle_t *hos_handle, int thread_nu static void gtest_hos_instance_init(hos_instance instance) { memset(instance, 0, sizeof(hos_instance_s)); - instance->result = true; + instance->status = INSTANCE_ENABLE_STATE; instance->error_code = 0; instance->error_message[0] ='\0'; instance->hos_url_prefix = "http://127.0.0.1:9098/hos/"; @@ -110,7 +110,7 @@ TEST(hos_write, normal) hos_fd_context_t expect_fd_info[2]; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -187,7 +187,7 @@ TEST(hos_write, normal) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -211,7 +211,7 @@ TEST(hos_write, bucket_not_exits) int thread_num = 3; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -287,7 +287,7 @@ TEST(hos_write, bucket_not_exits) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -311,7 +311,7 @@ TEST(hos_write, sync_mode) int thread_num = 3; data_info_t * data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -382,7 +382,7 @@ TEST(hos_write, sync_mode) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -406,7 +406,7 @@ TEST(hos_write, sync_mode_bucket_not_exits) int thread_num = 3; data_info_t *data_info = NULL; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -480,7 +480,7 @@ TEST(hos_write, sync_mode_bucket_not_exits) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -503,7 +503,7 @@ TEST(hos_write, paramer_error) hos_fd_context_t expect_fd_info; int thread_num = 2; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -524,7 +524,7 @@ TEST(hos_write, paramer_error) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -549,7 +549,7 @@ TEST(hos_write, fd_not_find) hos_fd_context_t expect_fd_info; int thread_num = 2; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -562,7 +562,7 @@ TEST(hos_write, fd_not_find) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -587,7 +587,7 @@ TEST(hos_write, over_threadnums) hos_fd_context_t expect_fd_info; int thread_num = 2; - hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); gtest_hos_instance_init(&expect_hos_instance); CheckHosInstance(hos_instance, &expect_hos_instance); gtest_hos_handle_init(&expect_hos_handle, thread_num); @@ -607,7 +607,7 @@ TEST(hos_write, over_threadnums) ret = hos_shutdown_instance(); EXPECT_EQ(ret, HOS_CLIENT_OK); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); @@ -627,7 +627,7 @@ TEST(hos_write, over_threadnums) TEST(hos_write, not_init_instance) { int ret = hos_write(3, HOS_BUFF, strlen(HOS_CONF), 0); - EXPECT_EQ(ret, HOS_INSTANCE_NOT_INIT); + EXPECT_EQ(ret, HOS_INSTANCE_NOT_ENABLE); } static void read_file(char *path, char **content, int *len) @@ -664,9 +664,9 @@ static void *hos_function(void *ptr) { hos_instance = hos_get_instance(); - if (hos_instance->result == false) + if (hos_instance == NULL) { - hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num, HOS_BUCKET); + hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", thread_num); } } gtest_hos_instance_init(&expect_hos_instance); @@ -723,7 +723,7 @@ static void *hos_function(void *ptr) ret = hos_shutdown_instance(); EXPECT_EQ(ret, 0); - expect_hos_instance.result = 0; + expect_hos_instance.status = 0; expect_hos_instance.hos_url_prefix = NULL; CheckHosInstance(hos_instance, &expect_hos_instance); diff --git a/src/hos_client.cpp b/src/hos_client.cpp index 36056d3d..a6cbf4e6 100644 --- a/src/hos_client.cpp +++ b/src/hos_client.cpp @@ -199,7 +199,34 @@ static int hos_attempt_connection() } return g_hos_instance.error_code; } + #if 0 + //不主动验证bucket + //验证bucket存不存在 + for (Aws::S3::Model::Bucket &new_bucket : g_hos_handle.buckets) + { + if (strcmp(new_bucket.GetName().c_str(), g_hos_handle.hos_config.bucket) == 0) + { + MESA_HANDLE_RUNTIME_LOG(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__, "[%s] debug: bucket:%s exits", + g_hos_instance.hos_url_prefix, g_hos_handle.hos_config.bucket); + bucket_exit = true; + } + } + if (bucket_exit == false) + { + MESA_HANDLE_RUNTIME_LOG(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "[%s]error: bucket:%s not exist.", + g_hos_instance.hos_url_prefix, g_hos_handle.hos_config.bucket); + char url[1024]; + snprintf(url, 1024, g_hos_instance.hos_url_prefix); + char bucket[1024]; + snprintf(bucket, 1024, g_hos_handle.hos_config.bucket); + hos_shutdown_instance(); + atomic_set(&g_hos_handle.count, 0); //立即shutdown + g_hos_instance.error_code = HOS_BUCKET_NOT_EXIST; + snprintf(g_hos_instance.error_message, 1024, "[%s] error: bucket %s not exits.", url, bucket); + return HOS_BUCKET_NOT_EXIST; + } g_hos_handle.buckets = outcome.GetResult().GetBuckets(); + #endif atomic_set(&g_hos_instance.status, INSTANCE_ENABLE_STATE); return HOS_CLIENT_OK; } @@ -633,17 +660,20 @@ const char *hos_get_upload_endpoint() return g_hos_instance.hos_url_prefix; } -hos_instance hos_init_instance(const char *conf_path, const char *module, size_t thread_num, const char *bucket) +hos_instance hos_init_instance(const char *conf_path, const char *module, size_t thread_num) { std::lock_guard locker(m_client_lock); hos_config_t *hos_conf = &g_hos_handle.hos_config; - if (conf_path == NULL || thread_num == 0 || module == NULL || bucket == NULL) + memset(&g_hos_handle, 0, sizeof(g_hos_handle)); + memset(&g_hos_instance, 0, sizeof(g_hos_instance)); + + if (conf_path == NULL || thread_num == 0 || module == NULL) { g_hos_instance.error_code = HOS_PARAMETER_ERROR; snprintf(g_hos_instance.error_message, HOS_ERROR_MESSAGE_SIZE, - "param error:conf_path:%s, module:%s, thread_num:%lu, bucket:%s", conf_path, module, thread_num, bucket); - return &g_hos_instance; + "param error:conf_path:%s, module:%s, thread_num:%lu", conf_path, module, thread_num); + return NULL; } MESA_load_profile_string_nodef(conf_path, module, "hos_serverip", hos_conf->ip, MAX_HOS_STRING_LEN); @@ -816,7 +846,7 @@ int hos_upload_file(const char *bucket, const char *file_path, put_finished_call struct stat buffer; if (g_hos_instance.status != INSTANCE_ENABLE_STATE) { - return HOS_INSTANCE_NOT_INIT; + return HOS_INSTANCE_NOT_ENABLE; } if ((bucket == NULL) || (file_path == NULL) || (thread_id > g_hos_handle.hos_config.thread_num)) @@ -839,7 +869,7 @@ int hos_upload_buf(const char *bucket, const char *object, const char *buf, size { if (g_hos_instance.status != INSTANCE_ENABLE_STATE) { - return HOS_INSTANCE_NOT_INIT; + return HOS_INSTANCE_NOT_ENABLE; } if ((bucket == NULL) || (object == NULL) || (buf == NULL) || (buf_len == 0) @@ -857,7 +887,7 @@ long hos_open_fd(const char *bucket, const char *object, put_finished_callback c { if (g_hos_instance.status != INSTANCE_ENABLE_STATE) { - return HOS_INSTANCE_NOT_INIT; + return HOS_INSTANCE_NOT_ENABLE; } if ((bucket == NULL) || (object == NULL) || (thread_id > g_hos_handle.hos_config.thread_num) || strlen(bucket) == 0 || strlen(object) == 0) { @@ -897,7 +927,7 @@ int hos_write(size_t fd, const char *stream, size_t stream_len, size_t thread_id if (g_hos_instance.status != INSTANCE_ENABLE_STATE) { - return HOS_INSTANCE_NOT_INIT; + return HOS_INSTANCE_NOT_ENABLE; } if ((stream == NULL) || (thread_id > hos_conf->thread_num)) @@ -996,7 +1026,7 @@ int hos_close_fd(size_t fd, size_t thread_id) hos_config_t *hos_conf = &g_hos_handle.hos_config; size_t upload_len = 0; - if (g_hos_instance.status == INSTANCE_ENABLE_STATE) + if (g_hos_instance.status == INSTANCE_UNINIT_STATE) { return HOS_INSTANCE_NOT_INIT; } diff --git a/src/hos_client.h b/src/hos_client.h index e695c453..b1b85491 100644 --- a/src/hos_client.h +++ b/src/hos_client.h @@ -87,7 +87,7 @@ typedef void (*put_finished_callback)(bool result, const char *bucket, const cha * thread_num 线程数 * 返回值: hos 实例创建结果 *************************************************************************************/ -hos_instance hos_init_instance(const char *conf_path, const char *module, size_t thread_num, const char *bucket); +hos_instance hos_init_instance(const char *conf_path, const char *module, size_t thread_num); /************************************************************************************* * 函数名: hos_get_instance * 返回值: hos_instance 成功,result 为true