TSG-6704 增加gtest用例

This commit is contained in:
彭宣正
2021-04-28 18:29:29 +08:00
parent 60b4540fb8
commit 4c9a8f89c4
17 changed files with 1297 additions and 126 deletions

16
conf/main.conf Normal file
View File

@@ -0,0 +1,16 @@
[HOS_CONF]
hos_serverip=192.168.44.12
hos_serverport=9098
hos_accesskeyid="default"
hos_secretkey="default"
hos_log_path="./tsglog/hoslog" #default
hos_log_level=30 #default
hos_poolsize=0 #default
hos_thread_sum=32 #default
hos_cache_size=102400 #default
hos_cache_count=10 #default
hos_fd_live_time_ms=1000 #default
hos_fs2_serverip=127.0.0.1
hos_fs2_serverport=10086
hos_fs2_path="./tsglog/hos/hos_fs2_log" #default
hos_fs2_format=0 #default

View File

@@ -89,15 +89,11 @@ int main(int argc, char *argv[])
buf = (char *)calloc(1, buffer.st_size +1); buf = (char *)calloc(1, buffer.st_size +1);
#if 0
if (file_to_buffer(file_name, buf, buffer.st_size) == -1) if (file_to_buffer(file_name, buf, buffer.st_size) == -1)
{ {
free(buf); free(buf);
return -1; return -1;
} }
#else
snprintf(buf, 1024, "this is a test");
#endif
printf("hos_init_instance start ...\n"); printf("hos_init_instance start ...\n");
hos_instance = hos_get_instance(); hos_instance = hos_get_instance();

View File

@@ -1,12 +1,14 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(gtest_hos_client) project(gtest_hos_client)
#aux_source_directory(. SRCS) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRCS)
include_directories("/opt/MESA/include") include_directories("/opt/MESA/include")
link_directories("/opt/MESA/lib") link_directories("/opt/MESA/lib")
#link_libraries(hos-client-cpp gtest gtest_main pthread) #link_libraries(hos-client-cpp gtest gtest_main pthread)
add_definitions(-g -W -Wall) add_definitions(-g -W -Wall -std=c++11)
add_executable(gtest_hos_client gtest_hos_init_instance.cpp) #add_executable(gtest_hos_client gtest_hos_init_instance.cpp gtest_hos_get_instance.cpp)
#add_executable(gtest_hos_client gtest_hos_write.cpp)
add_executable(gtest_hos_client ${SRCS})
target_link_libraries(gtest_hos_client hos-client-cpp gtest gtest_main pthread) target_link_libraries(gtest_hos_client hos-client-cpp gtest gtest_main pthread)

123
gtest/conf/default.conf Normal file
View File

@@ -0,0 +1,123 @@
[hos_default_conf]
hos_serverip=192.168.44.12
hos_serverport=9098
hos_accesskeyid="default"
hos_secretkey="default"
hos_log_path="./hoslog"
#default
hos_log_level=30
#default
hos_poolsize=10
#default
hos_thread_sum=32
#default
hos_cache_size=102400
#default
hos_cache_count=10
#default
hos_fd_live_time_ms=1000
#default
hos_fs2_serverip=127.0.0.1
hos_fs2_serverport=10086
hos_fs2_path="./log/hos_fs2_log"
#default
hos_fs2_format=0
#defaul
[hos_sync_conf]
hos_serverip=192.168.44.12
hos_serverport=9098
hos_accesskeyid="default"
hos_secretkey="default"
hos_log_path="./log/hoslog"
#default
hos_log_level=30
#default
hos_poolsize=0
#default
hos_thread_sum=32
#default
hos_cache_size=102400
#default
hos_cache_count=10
#default
hos_fd_live_time_ms=1000
#default
hos_fs2_serverip=127.0.0.1
hos_fs2_serverport=10086
hos_fs2_path="./log/hos_fs2_log"
#default
hos_fs2_format=0
#default
[hos_no_fs2_conf]
hos_serverip=192.168.44.12
hos_serverport=9098
hos_accesskeyid="default"
hos_secretkey="default"
hos_log_path="./log/hoslog"
#default
hos_log_level=30
#default
hos_poolsize=10
#default
hos_thread_sum=32
#default
hos_cache_size=102400
#default
hos_cache_count=10
#default
hos_fd_live_time_ms=1000
#default
hos_fs2_path="./log/hos_fs2_log"
#default
hos_fs2_format=0
#default
[hos_default_conf]
hos_log_path="./log/hoslog"
#default
hos_log_level=30
#default
hos_poolsize=10
#default
hos_thread_sum=32
#default
hos_cache_size=102400
#default
hos_cache_count=10
#default
hos_fd_live_time_ms=1000
#default
hos_fs2_serverip=127.0.0.1
hos_fs2_serverport=10086
hos_fs2_path="./log/hos_fs2_log"
#default
hos_fs2_format=0
#default
[hos_error_server_conf]
hos_serverip=192.168.40.146
hos_serverport=9098
hos_accesskeyid="default"
hos_secretkey="default"
hos_log_path="./log/hoslog"
#default
hos_log_level=30
#default
hos_poolsize=0
#default
hos_thread_sum=32
#default
hos_cache_size=102400
#default
hos_cache_count=10
#default
hos_fd_live_time_ms=1000
#default
hos_fs2_serverip=127.0.0.1
hos_fs2_serverport=10086
hos_fs2_path="./log/hos_fs2_log"
#default
hos_fs2_format=0
#default

View File

@@ -0,0 +1,78 @@
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
TEST(hos_close_fd, normal)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_close_fd, paramer_error)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int fd = hos_open_fd(NULL, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_close_fd, not_init_instance)
{
int ret = hos_close_fd(1,1);
EXPECT_EQ(ret, HOS_INSTANCE_NOT_INIT);
}
TEST(hos_close_fd, fd_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_close_fd(7, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}

View File

@@ -1,21 +0,0 @@
/*************************************************************************
> File Name: gtest_hos_create_bucket.cpp
> Author: pxz
> Created Time: Tue 29 Sep 2020 10:32:14 AM CST
************************************************************************/
#include <gtest/gtest.h>
#include "hos_client.h"
TEST(hos_create_bucket, normal)
{
hos_client_handle handle = hos_client_create("http://192.168.40.151/hos/", "default", "default", 1);
int ret = hos_create_bucket(handle, "mybucket");
EXPECT(HOS_CLIENT_OK, ret);
}
TEST(hos_create_bucket, parameter_error)
{
hos_client_handle handle = hos_client_create("http://192.168.40.151/hos/", "default", "default", 1);
int ret = hos_create_bucket(handle, NULL);
EXPECT(HOS_CLIENT_OK, HOS_PARAMETER_ERROR);
}

View File

@@ -0,0 +1,32 @@
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
TEST(hos_get_instance, normal)
{
hos_instance hos_instance = hos_get_instance();
EXPECT_EQ(hos_instance->result, false);
hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
hos_instance = hos_get_instance();
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
hos_shutdown_instance();
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
hos_shutdown_instance();
EXPECT_EQ(hos_instance->result, false);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}

View File

@@ -6,20 +6,69 @@
#include <aws/external/gtest.h> #include <aws/external/gtest.h>
#include "hos_client.h" #include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
TEST(hos_init_instance, normal) TEST(hos_init_instance, normal)
{ {
hos_instance hos_instance = hos_init_instance("../conf/default.conf", "hos_default_conf", 1, "hos_test_bucket"); hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true); EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance(); int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK); EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
} }
TEST(hos_open_fd, buffer) TEST(hos_init_instance, param_error)
{ {
hos_instance hos_instance = hos_init_instance("../conf/default.conf", "hos_default_conf", 1, "hos_test_bucket"); hos_instance hos_instance = hos_init_instance(NULL, "hos_default_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, false);
EXPECT_EQ(hos_instance->error_code, HOS_PARAMETER_ERROR);
EXPECT_STREQ(hos_instance->error_message, "param error:conf_path:(null), module:hos_default_conf, thread_num:1, bucket:hos_test_bucket");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_init_instance, no_fs2)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_no_fs2_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true); EXPECT_EQ(hos_instance->result, true);
int fd = hos_open_fd("hos_test_bucket", "hos_test_object", NULL, NULL, 0, BUFF_MODE); EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_EQ(fd, 3); EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance(); int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK); EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_init_instance, bucket_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, "hos_bucket_not_exits");
EXPECT_EQ(hos_instance->result, false);
EXPECT_EQ(hos_instance->error_code, HOS_BUCKET_NOT_EXIST);
EXPECT_STREQ(hos_instance->error_message, "bucket:hos_bucket_not_exits not exits.");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_init_instance, conf_error)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_error_conf", 1, "hos_bucket_not_exits");
EXPECT_EQ(hos_instance->result, false);
EXPECT_EQ(hos_instance->error_code, HOS_CONF_ERROR);
EXPECT_STREQ(hos_instance->error_message, "hos param error:hos ip:, hos port:0, accesskeyid:, secretkey:");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
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");
EXPECT_EQ(hos_instance->result, false);
EXPECT_EQ(hos_instance->error_code, NETWORK_CONNECTION);
EXPECT_STREQ(hos_instance->error_message, "curlCode: 7, Couldn't connect to server");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
} }

111
gtest/gtest_hos_open_fd.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
TEST(hos_open_fd, normal)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd1 = hos_open_fd(HOS_BUCKET, "object", NULL, NULL, 1, BUFF_MODE);
EXPECT_EQ(fd1, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd1, 1);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_open_fd, paramer_error)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int fd = hos_open_fd(NULL, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_open_fd, over_threadnums)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 3, BUFF_MODE);
EXPECT_EQ(fd, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_open_fd, fd_not_enough)
{
int i = 0, fd = 0;
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
for (i = 0; i < 65533; i++)
{
fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, i+3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
}
fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, HOS_FD_NOT_ENOUGH);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_open_fd, not_init_instance)
{
int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, HOS_INSTANCE_NOT_INIT);
}

View File

@@ -0,0 +1,49 @@
/*************************************************************************
> File Name: gtest_hos_cleint_create.cpp
> Author: pxz
> Created Time: Tue 29 Sep 2020 10:20:49 AM CST
************************************************************************/
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
TEST(hos_shutdown_instance, normal)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_shutdown_instance, no_init)
{
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
}
TEST(hos_shutdown_instance, shutdown_more)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}

View File

@@ -0,0 +1,87 @@
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
#define HOS_BUFF "This is a googletest"
static void hos_callback(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, true);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, NULL);
}
static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, false);
EXPECT_STREQ(bucket, "bucket_not_exits");
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, "The specified bucket does not exist.");
}
TEST(hos_upload_buff, normal)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_buf(HOS_BUCKET, "object", HOS_BUFF, strlen(HOS_BUFF), hos_callback, (void *)"object", 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_upload_buff, bucket_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_buf("bucket_not_exits", "object", HOS_BUFF, strlen(HOS_BUFF), hos_bucket_not_exits_cb, (void *)"object", 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_upload_buff, param_error)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_buf(NULL, "object", HOS_BUFF, strlen(HOS_BUFF), hos_callback, (void *)"object", 0);
EXPECT_EQ(ret, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, 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);
}

View File

@@ -0,0 +1,106 @@
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
#define HOS_BUFF "Makefile"
static void hos_callback(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, true);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, NULL);
}
static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, false);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, "The specified bucket does not exist.");
}
TEST(hos_upload_file, normal)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_file(HOS_BUCKET, HOS_BUFF, hos_callback, (void *)HOS_BUFF, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_upload_file, param_error)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_file(NULL, "object", hos_callback, (void *)"object", 0);
EXPECT_EQ(ret, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_upload_file, file_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_file(HOS_BUCKET, "file_not_exits", hos_callback, (void *)"object", 0);
EXPECT_EQ(ret, HOS_FILE_NOT_EXIST);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, 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);
}
TEST(hos_upload_file, bucket_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_upload_file(HOS_BUCKET, HOS_CONF, hos_bucket_not_exits_cb, (void *)HOS_CONF, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}

View File

@@ -3,15 +3,54 @@
> Author: pxz > Author: pxz
> Created Time: Tue 29 Sep 2020 10:32:14 AM CST > Created Time: Tue 29 Sep 2020 10:32:14 AM CST
************************************************************************/ ************************************************************************/
#include <gtest/gtest.h> #include <aws/external/gtest.h>
#include "hos_client.h" #include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
TEST(hos_verify_bucket, normal) TEST(hos_verify_bucket, normal)
{ {
int ret = -1; hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET);
hos_client_handle handle = hos_client_create("http://192.168.40.151/hos/", "default", "default", 1); EXPECT_EQ(hos_instance->result, true);
if (hos_create_bucket(handle, "mybcuket")) EXPECT_EQ(hos_instance->error_code, 0);
ret = hos_verify_bucket(handle, "mybucket"); EXPECT_STREQ(hos_instance->error_message, "");
EXPECT(HOS_CLIENT_OK, ret); EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
bool result = hos_verify_bucket(HOS_BUCKET);
EXPECT_EQ(result, true);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
} }
TEST(hos_verify_bucket, not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 1, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
bool result = hos_verify_bucket("hos_not_exits_bucket");
EXPECT_EQ(result, false);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_verify_bucket, no_instance)
{
bool result = hos_verify_bucket("hos_not_exits_bucket");
EXPECT_EQ(result, false);
}

491
gtest/gtest_hos_write.cpp Normal file
View File

@@ -0,0 +1,491 @@
#include <aws/external/gtest.h>
#include "hos_client.h"
#define HOS_CONF "../conf/default.conf"
#define HOS_BUCKET "hos_test_bucket"
#define HOS_BUFF "This a googleTest"
#define HOS_FILE "../conf/default.conf"
static void hos_callback(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, true);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, NULL);
}
static void hos_write_buff_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, true);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, NULL);
}
static void hos_write_append_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, true);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, NULL);
}
static void hos_write_file_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, true);
EXPECT_STREQ(bucket, HOS_BUCKET);
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, NULL);
}
static void hos_bucket_not_exits_cb(bool result, const char *bucket, const char *object, const char *error, void *userdata)
{
SUCCEED();
EXPECT_EQ(result, false);
EXPECT_STREQ(bucket, "bucket_not_exits");
EXPECT_STREQ(object, (char *)userdata);
EXPECT_STREQ(error, "The specified bucket does not exist.");
}
TEST(hos_write, normal)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 3, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd = hos_open_fd(HOS_BUCKET, "object_buff", hos_write_buff_cb, (void *)"object_buff", 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(fd, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd1 = hos_open_fd(HOS_BUCKET, "object_append", hos_write_append_cb, (void *)"object_append", 1, BUFF_MODE | APPEND_MODE);
EXPECT_EQ(fd1, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd2 = hos_open_fd(HOS_BUCKET, "object_file", hos_write_file_cb, (void *)"object_file", 2, FILE_MODE);
EXPECT_EQ(fd2, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd1, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd2, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, bucket_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 3, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd = hos_open_fd("bucket_not_exits", "object_buff", hos_bucket_not_exits_cb, (void *)"object_buff", 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(fd, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd1 = hos_open_fd("bucket_not_exits", "object_append", hos_bucket_not_exits_cb, (void *)"object_append", 1, BUFF_MODE | APPEND_MODE);
EXPECT_EQ(fd1, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd2 = hos_open_fd("bucket_not_exits", "object_file", hos_bucket_not_exits_cb, (void *)"object_file", 2, FILE_MODE);
EXPECT_EQ(fd2, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd1, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd2, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, sync_mode)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", 3, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(fd, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd1 = hos_open_fd(HOS_BUCKET, "object_append", NULL, NULL, 1, BUFF_MODE | APPEND_MODE);
EXPECT_EQ(fd1, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd2 = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 2, FILE_MODE);
EXPECT_EQ(fd2, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd2, HOS_FILE, strlen(HOS_BUFF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd1, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd2, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, sync_mode_bucket_not_exits)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_sync_conf", 3, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(fd, HOS_BUFF, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd1 = hos_open_fd(HOS_BUCKET, "object_append", NULL, NULL, 1, BUFF_MODE | APPEND_MODE);
EXPECT_EQ(fd1, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd1, HOS_BUFF, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
size_t fd2 = hos_open_fd(HOS_BUCKET, "object_buff", NULL, NULL, 2, FILE_MODE);
EXPECT_EQ(fd2, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_write(fd2, HOS_FILE, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd1, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_close_fd(fd2, 0);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, paramer_error)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int fd = hos_open_fd(NULL, "object", hos_callback, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(0, HOS_BUFF, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, fd_not_find)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(3, HOS_BUFF, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_HASH_NOT_FIND);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, file_not_exit)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, FILE_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(3, "not_exit_file", strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_FILE_NOT_EXIST);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, over_threadnums)
{
hos_instance hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 2, HOS_BUCKET);
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int fd = hos_open_fd(HOS_CONF, "object", NULL, NULL, 0, BUFF_MODE);
EXPECT_EQ(fd, 3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
int ret = hos_write(3, HOS_BUFF, strlen(HOS_CONF), 6);
EXPECT_EQ(ret, HOS_PARAMETER_ERROR);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
ret = hos_shutdown_instance();
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
}
TEST(hos_write, not_init_instance)
{
int ret = hos_write(3, HOS_BUFF, strlen(HOS_CONF), 0);
EXPECT_EQ(ret, HOS_INSTANCE_NOT_INIT);
}
static void *hos_function(void *ptr)
{
#define HOS_FD_NUMS_LOCAL 20
//int thread_id = (int)ptr;
size_t thread_id = reinterpret_cast<size_t>(ptr);
hos_instance hos_instance = NULL;
int i = 0;
int fd[HOS_FD_NUMS_LOCAL] = {0};
char object[HOS_FD_NUMS_LOCAL][1024];
int ret = 0;
{
hos_instance = hos_get_instance();
if (hos_instance->result == false)
{
hos_instance = hos_init_instance(HOS_CONF, "hos_default_conf", 32, HOS_BUCKET);
}
}
EXPECT_EQ(hos_instance->result, true);
EXPECT_EQ(hos_instance->error_code, HOS_CLIENT_OK);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
for (i = 0; i < 20; i++)
{
snprintf(object[i], 1024, "object_%lu_%d", thread_id, i);
fd[i] = hos_open_fd(HOS_CONF, object[i], hos_callback, object[i], 0, BUFF_MODE | APPEND_MODE);
EXPECT_EQ(fd[i], i+3);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
}
for (i = 0; i < 20; i++)
{
ret = hos_write(fd[i], HOS_BUFF, strlen(HOS_BUFF), i);
EXPECT_EQ(ret, HOS_CLIENT_OK);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
}
for (i = 0; i < 20; i++)
{
ret = hos_close_fd(fd[i], i);
EXPECT_EQ(ret, 0);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, "http://192.168.44.12:9098/hos/");
}
ret = hos_shutdown_instance();
EXPECT_EQ(ret, 0);
EXPECT_EQ(hos_instance->error_code, 0);
EXPECT_STREQ(hos_instance->error_message, "");
EXPECT_STREQ(hos_instance->hos_url_prefix, NULL);
pthread_exit(NULL);
#undef HOS_FD_NUMS_LOCAL
}
TEST(hos_write, mutil_thread)
{
pthread_t thread_num[32];
for (size_t i = 0; i < 32; i++)
{
pthread_create(&thread_num[i], NULL, hos_function, (void *)i);
}
}

View File

@@ -4,7 +4,6 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC -std=c++11")
include_directories(${CMAKE_INSTALL_PREFIX}/include/MESA) include_directories(${CMAKE_INSTALL_PREFIX}/include/MESA)
link_directories(${CMAKE_INSTALL_PREFIX}/lib) link_directories(${CMAKE_INSTALL_PREFIX}/lib)
set(CMKAE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC")
add_library(${lib_name}_shared SHARED hos_client.cpp hos_hash.cpp) add_library(${lib_name}_shared SHARED hos_client.cpp hos_hash.cpp)
target_link_libraries(${lib_name}_shared target_link_libraries(${lib_name}_shared
@@ -14,6 +13,7 @@ target_link_libraries(${lib_name}_shared
libaws-c-event-stream.a libaws-c-event-stream.a
libaws-cpp-sdk-core.a libaws-cpp-sdk-core.a
libaws-cpp-sdk-s3.a libaws-cpp-sdk-s3.a
libtesting-resources.a
"-Wl,--no-whole-archive" "-Wl,--no-whole-archive"
libcurl.so libcurl.so
libpthread.so libpthread.so

View File

@@ -19,6 +19,10 @@ extern "C"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <aws/external/gtest.h>
#include <aws/testing/platform/PlatformTesting.h>
#include <aws/testing/TestingEnvironment.h>
#include <aws/testing/MemoryTesting.h>
#include "hos_client.h" #include "hos_client.h"
#include "hos_hash.h" #include "hos_hash.h"
#include "field_stat2.h" #include "field_stat2.h"
@@ -296,8 +300,21 @@ static void hos_client_create()
g_hos_instance.result = true; g_hos_instance.result = true;
} }
static bool hos_verify_bucket(const char *bucket) bool hos_verify_bucket(const char *bucket)
{ {
if (bucket == NULL)
{
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__,
"bucket is null");
return false;
}
if (g_hos_instance.result != true || g_hos_handle.S3Client == NULL)
{
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__,
"g_hos_instance.result:%s, g_hos_handle.S3Client:%s",
g_hos_instance.result, (g_hos_handle.S3Client==NULL)?(NULL):("not null"));
return false;
}
Aws::S3::Model::ListBucketsOutcome outcome = g_hos_handle.S3Client->ListBuckets(); Aws::S3::Model::ListBucketsOutcome outcome = g_hos_handle.S3Client->ListBuckets();
if (outcome.IsSuccess()) if (outcome.IsSuccess())
@@ -541,7 +558,7 @@ static void hos_expand_fs2()
return ; return ;
} }
static bool hos_putobject_async(Aws::S3::Model::PutObjectRequest& request, size_t stream_len, static int hos_putobject_async(Aws::S3::Model::PutObjectRequest& request, size_t stream_len,
size_t thread_id, size_t fd, const char *bucket, const char *object) size_t thread_id, size_t fd, const char *bucket, const char *object)
{ {
char buf[128]; char buf[128];
@@ -561,6 +578,8 @@ static bool hos_putobject_async(Aws::S3::Model::PutObjectRequest& request, size_
//不算真正成功需要等到PutObjectAsyncFinished的结果 //不算真正成功需要等到PutObjectAsyncFinished的结果
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__, MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__,
"PutObjectAsync success. [%s:%s]", bucket, object); "PutObjectAsync success. [%s:%s]", bucket, object);
return HOS_CLIENT_OK;
} }
else else
{ {
@@ -576,12 +595,11 @@ static bool hos_putobject_async(Aws::S3::Model::PutObjectRequest& request, size_
data_info->tx_failed_bytes[thread_id] += stream_len; data_info->tx_failed_bytes[thread_id] += stream_len;
} }
} }
return HOS_SEND_FAILED;
} }
return ret;
} }
static bool hos_putobject_sync(Aws::S3::Model::PutObjectRequest& request, size_t stream_len, size_t thread_id, size_t fd, static int hos_putobject_sync(Aws::S3::Model::PutObjectRequest& request, size_t stream_len, size_t thread_id, size_t fd,
const char *bucket, const char *object) const char *bucket, const char *object)
{ {
hos_func_thread_t *hos_func = &g_hos_handle.hos_func; hos_func_thread_t *hos_func = &g_hos_handle.hos_func;
@@ -606,14 +624,12 @@ static bool hos_putobject_sync(Aws::S3::Model::PutObjectRequest& request, size_t
"PutObject success. [%s:%s]", bucket, object); "PutObject success. [%s:%s]", bucket, object);
} }
return HOS_CLIENT_OK;
return true;
} }
else else
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__, MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__,
"PutObject failed. [%s:%s]", bucket, object); "PutObject failed. [%s:%s] cause:%s", bucket, object, Outcome.GetError().GetMessage().c_str());
if (hos_func->fs2_info[FS2_DATA_FLOW_STATE].fs2_handle && hos_func->fs2_info[FS2_DATA_FLOW_STATE].reserved) if (hos_func->fs2_info[FS2_DATA_FLOW_STATE].fs2_handle && hos_func->fs2_info[FS2_DATA_FLOW_STATE].reserved)
{ {
@@ -622,15 +638,10 @@ static bool hos_putobject_sync(Aws::S3::Model::PutObjectRequest& request, size_t
data_info->tx_failed_bytes[thread_id] += stream_len; data_info->tx_failed_bytes[thread_id] += stream_len;
} }
return false; return (int)Outcome.GetError().GetErrorType() + 1;
} }
} }
void hos_init_log()
{
MESA_handle_runtime_log_creation("./log");
}
hos_instance hos_get_instance() hos_instance hos_get_instance()
{ {
if (g_hos_handle.S3Client != NULL) if (g_hos_handle.S3Client != NULL)
@@ -648,11 +659,12 @@ hos_instance hos_init_instance(const char *conf_path, const char *module, size_t
hos_config_t *hos_conf = &g_hos_handle.hos_config; hos_config_t *hos_conf = &g_hos_handle.hos_config;
char hos_url[1024]; char hos_url[1024];
if (conf_path == NULL || thread_num == 0) if (conf_path == NULL || thread_num == 0 || module == NULL || bucket == NULL)
{ {
g_hos_instance.result = false; g_hos_instance.result = false;
g_hos_instance.error_code = HOS_PARAMETER_ERROR; g_hos_instance.error_code = HOS_PARAMETER_ERROR;
snprintf(g_hos_instance.error_message, HOS_ERROR_MESSAGE_SIZE, "param error:conf_path:%s, thread_num:%lu", conf_path, thread_num); 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; return &g_hos_instance;
} }
@@ -663,7 +675,6 @@ hos_instance hos_init_instance(const char *conf_path, const char *module, size_t
MESA_load_profile_string_def(conf_path, module, "hos_log_path", hos_conf->log_path, MAX_HOS_STRING_LEN, HOS_LOG_PATH); MESA_load_profile_string_def(conf_path, module, "hos_log_path", hos_conf->log_path, MAX_HOS_STRING_LEN, HOS_LOG_PATH);
MESA_load_profile_uint_def(conf_path, module, "hos_log_level", &hos_conf->log_level, 30); MESA_load_profile_uint_def(conf_path, module, "hos_log_level", &hos_conf->log_level, 30);
MESA_load_profile_uint_def(conf_path, module, "hos_poolsize", &hos_conf->pool_thread_size, 0); MESA_load_profile_uint_def(conf_path, module, "hos_poolsize", &hos_conf->pool_thread_size, 0);
MESA_load_profile_uint_def(conf_path, module, "hos_thread_sum", &hos_conf->thread_num, 32);
MESA_load_profile_uint_def(conf_path, module, "hos_cache_size", &hos_conf->cache_size, 102400); MESA_load_profile_uint_def(conf_path, module, "hos_cache_size", &hos_conf->cache_size, 102400);
MESA_load_profile_uint_def(conf_path, module, "hos_cache_count", &hos_conf->cache_count, 10); MESA_load_profile_uint_def(conf_path, module, "hos_cache_count", &hos_conf->cache_count, 10);
MESA_load_profile_uint_def(conf_path, module, "hos_fd_live_time_ms", &hos_conf->timeout, 1000); MESA_load_profile_uint_def(conf_path, module, "hos_fd_live_time_ms", &hos_conf->timeout, 1000);
@@ -683,20 +694,19 @@ hos_instance hos_init_instance(const char *conf_path, const char *module, size_t
} }
snprintf(hos_url, sizeof(hos_url), "http://%s:%d/hos/", hos_conf->ip, hos_conf->port); snprintf(hos_url, sizeof(hos_url), "http://%s:%d/hos/", hos_conf->ip, hos_conf->port);
hos_conf->thread_num = thread_num;
hos_client_create(); hos_client_create();
if (g_hos_instance.result == true) if (g_hos_instance.result == true)
{ {
if(hos_verify_bucket(bucket) == false) if(hos_verify_bucket(bucket) == false)
{ {
g_hos_instance.result = false;
g_hos_instance.error_code = HOS_BUCKET_NOT_EXIST;
snprintf(g_hos_instance.error_message, HOS_ERROR_MESSAGE_SIZE, "runtime log create failed.");
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "bucket:%s not exist.", bucket); MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "bucket:%s not exist.", bucket);
hos_shutdown_instance(); hos_shutdown_instance();
g_hos_instance.result = false;
g_hos_instance.error_code = HOS_BUCKET_NOT_EXIST;
snprintf(g_hos_instance.error_message, HOS_ERROR_MESSAGE_SIZE, "bucket:%s not exits.", bucket);
return &g_hos_instance; return &g_hos_instance;
} }
g_hos_instance.hos_url_prefix = (const char *)calloc(1, strlen(hos_url) + 1);
memcpy((void *)g_hos_instance.hos_url_prefix, hos_url, strlen(hos_url));
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__, "Instance init completed"); MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_DEBUG, __FUNCTION__, "Instance init completed");
if (hos_conf->fs2_ip && hos_conf->fs2_port) if (hos_conf->fs2_ip && hos_conf->fs2_port)
{ {
@@ -706,6 +716,10 @@ hos_instance hos_init_instance(const char *conf_path, const char *module, size_t
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "hos fs2 function not starup"); MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "hos fs2 function not starup");
} }
g_hos_instance.error_code = 0;
g_hos_instance.error_message[0]='\0';
g_hos_instance.hos_url_prefix = (const char *)calloc(1, strlen(hos_url) + 1);
memcpy((void *)g_hos_instance.hos_url_prefix, hos_url, strlen(hos_url));
} }
return &g_hos_instance; return &g_hos_instance;
} }
@@ -815,10 +829,6 @@ static int hos_upload_stream(const char *bucket, const char *object, const char
//设置回调函数 //设置回调函数
size_t fd = hash_get_min_free_fd(thread_id); size_t fd = hash_get_min_free_fd(thread_id);
std::shared_ptr<Aws::Client::AsyncCallerContext> context =
Aws::MakeShared<Aws::Client::AsyncCallerContext>("");
sprintf(buf, "%lu %lu", thread_id, fd);
context->SetUUID(buf);
hos_fd_context_t info = {fd, 0, (char *)bucket, (char *)object, (void *)callback, userdata, NULL, 0, 0, 0 }; hos_fd_context_t info = {fd, 0, (char *)bucket, (char *)object, (void *)callback, userdata, NULL, 0, 0, 0 };
add_fd_context(&g_fd_context[thread_id], &info); add_fd_context(&g_fd_context[thread_id], &info);
@@ -832,25 +842,25 @@ static int hos_upload_stream(const char *bucket, const char *object, const char
ret = hos_putobject_sync(request, data_len, thread_id, fd, bucket, object); ret = hos_putobject_sync(request, data_len, thread_id, fd, bucket, object);
} }
if (ret == true) return ret;
{
return HOS_CLIENT_OK;
}
else
{
return HOS_SEND_FAILED;
}
} }
int hos_upload_file(const char *bucket, const char *file_path, put_finished_callback callback, void *userdata, size_t thread_id) int hos_upload_file(const char *bucket, const char *file_path, put_finished_callback callback, void *userdata, size_t thread_id)
{ {
struct stat buffer; struct stat buffer;
if (g_hos_instance.result == false || g_hos_handle.S3Client == NULL)
{
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__,
"error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s",
g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null"));
return HOS_INSTANCE_NOT_INIT;
}
if ((g_hos_handle.S3Client == NULL) || (bucket == NULL) || (file_path == NULL) || (thread_id > g_hos_handle.hos_config.thread_num)) if ((bucket == NULL) || (file_path == NULL) || (thread_id > g_hos_handle.hos_config.thread_num))
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_upload_file", MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_upload_file",
"s3client:%s, bucket:%s, file_path:%s, thread_id:%d, thread_num:%d", "s3client:%s, bucket:%s, file_path:%s, thread_id:%d, thread_num:%d",
g_hos_handle.S3Client?"not null":"null", bucket, file_path, thread_id, g_hos_handle.hos_config.thread_num); bucket, file_path, thread_id, g_hos_handle.hos_config.thread_num);
return HOS_PARAMETER_ERROR; return HOS_PARAMETER_ERROR;
} }
@@ -864,14 +874,20 @@ int hos_upload_file(const char *bucket, const char *file_path, put_finished_call
int hos_upload_buf(const char *bucket, const char *object, const char *buf, size_t buf_len, put_finished_callback callback, void *userdata, size_t thread_id) int hos_upload_buf(const char *bucket, const char *object, const char *buf, size_t buf_len, put_finished_callback callback, void *userdata, size_t thread_id)
{ {
if ((g_hos_handle.S3Client == NULL) || (bucket == NULL) || (object == NULL) if (g_hos_instance.result == false || g_hos_handle.S3Client == NULL)
|| (buf == NULL) || (buf_len == 0) {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__,
"error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s",
g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null"));
return HOS_INSTANCE_NOT_INIT;
}
if ((bucket == NULL) || (object == NULL) || (buf == NULL) || (buf_len == 0)
|| (thread_id > g_hos_handle.hos_config.thread_num)) || (thread_id > g_hos_handle.hos_config.thread_num))
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_upload_buf", MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_upload_buf",
"s3client:%s, bucket:%s, object:%s, buf:%s, buf_len:%d, thread_id:%d, thread_num:%d", "bucket:%s, object:%s, buf:%s, buf_len:%d, thread_id:%d, thread_num:%d",
g_hos_handle.S3Client?"not null":"null", bucket, object, bucket, object, buf?"not null":"null", buf_len, thread_id, g_hos_handle.hos_config.thread_num);
buf?"not null":"null", buf_len, thread_id, g_hos_handle.hos_config.thread_num);
return HOS_PARAMETER_ERROR; return HOS_PARAMETER_ERROR;
} }
return hos_upload_stream(bucket, object, buf, buf_len, callback, userdata, thread_id); return hos_upload_stream(bucket, object, buf, buf_len, callback, userdata, thread_id);
@@ -922,11 +938,18 @@ static void *hos_fd_manage(void *ptr)
int hos_open_fd(const char *bucket, const char *object, put_finished_callback callback, void *userdata, size_t thread_id, int mode) int hos_open_fd(const char *bucket, const char *object, put_finished_callback callback, void *userdata, size_t thread_id, int mode)
{ {
if ((g_hos_handle.S3Client == NULL) || (bucket == NULL) || (object == NULL) || (thread_id > g_hos_handle.hos_config.thread_num) || strlen(bucket) == 0 || strlen(object) == 0) if (g_hos_instance.result == false || g_hos_handle.S3Client == NULL)
{
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__,
"error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s",
g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null"));
return HOS_INSTANCE_NOT_INIT;
}
if ((bucket == NULL) || (object == NULL) || (thread_id > g_hos_handle.hos_config.thread_num) || strlen(bucket) == 0 || strlen(object) == 0)
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_open_fd", MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_open_fd",
"parameter error:s3client:%s, bucket:%s, obejct:%s, thread_id:%s", "bucket:%s, obejct:%s, thread_id:%s",
g_hos_handle.S3Client, bucket, object, thread_id); bucket, object, thread_id);
return HOS_PARAMETER_ERROR; return HOS_PARAMETER_ERROR;
} }
@@ -973,7 +996,15 @@ int hos_write(size_t fd, const char *stream, size_t stream_len, size_t thread_id
hos_func_thread_t *hos_func = &g_hos_handle.hos_func; hos_func_thread_t *hos_func = &g_hos_handle.hos_func;
size_t upload_len = 0; size_t upload_len = 0;
if ((fd < 3) || (stream == NULL) || (thread_id > hos_conf->thread_num)) if (g_hos_instance.result == false || g_hos_handle.S3Client == NULL)
{
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__,
"error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s",
g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null"));
return HOS_INSTANCE_NOT_INIT;
}
if ((fd < 3) || fd > MAX_HOS_CLIENT_FD_NUM || (stream == NULL) || (thread_id > hos_conf->thread_num))
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL,
"hos_write", "error: fd:%d, stream:%s, stream_len:%s, thread_id:%d.", "hos_write", "error: fd:%d, stream:%s, stream_len:%s, thread_id:%d.",
@@ -1085,23 +1116,14 @@ int hos_write(size_t fd, const char *stream, size_t stream_len, size_t thread_id
//恢复fd 的cache设置 //恢复fd 的cache设置
if (a_fd_context->mode & APPEND_MODE) if (a_fd_context->mode & APPEND_MODE)
{ {
a_fd_context->cache->seekg(0, std::ios_base::end); data_info->cache[thread_id] -= upload_len;
data_info->cache[thread_id] += upload_len;
a_fd_context->cache->seekg(0, std::ios_base::beg);
a_fd_context->cache.reset(); a_fd_context->cache.reset();
a_fd_context->cache = NULL; a_fd_context->cache = NULL;
a_fd_context->cache_rest = hos_conf->cache_size; a_fd_context->cache_rest = hos_conf->cache_size;
a_fd_context->cache_count = hos_conf->cache_count; a_fd_context->cache_count = hos_conf->cache_count;
} }
if (ret == true) return ret;
{
return HOS_CLIENT_OK;
}
else
{
return HOS_SEND_FAILED;
}
} }
int hos_close_fd(size_t fd, size_t thread_id) int hos_close_fd(size_t fd, size_t thread_id)
@@ -1111,7 +1133,15 @@ int hos_close_fd(size_t fd, size_t thread_id)
hos_config_t *hos_conf = &g_hos_handle.hos_config; hos_config_t *hos_conf = &g_hos_handle.hos_config;
size_t upload_len = 0; size_t upload_len = 0;
if (fd < 3 || thread_id > hos_conf->thread_num) if (g_hos_instance.result == false || g_hos_handle.S3Client == NULL)
{
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__,
"error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s",
g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null"));
return HOS_INSTANCE_NOT_INIT;
}
if (fd < 3 || fd > 65533 || thread_id > hos_conf->thread_num)
{ {
MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_close_fd", MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_close_fd",
"error:fd:%d, thread_id:%d, thread_sum:%d.", "error:fd:%d, thread_id:%d, thread_sum:%d.",
@@ -1217,7 +1247,6 @@ int hos_shutdown_instance()
{ {
if (i == 0) if (i == 0)
{ {
#if 1
data_info_t * data_info = (data_info_t *)hos_func->fs2_info[i].reserved; data_info_t * data_info = (data_info_t *)hos_func->fs2_info[i].reserved;
if (data_info->rx_pkts) if (data_info->rx_pkts)
free(data_info->rx_pkts); free(data_info->rx_pkts);
@@ -1233,16 +1262,6 @@ int hos_shutdown_instance()
free(data_info->tx_failed_pkts); free(data_info->tx_failed_pkts);
if (data_info->cache) if (data_info->cache)
free(data_info->cache); free(data_info->cache);
#else
if (data_info->rx_pkts_last)
free(data_info->rx_pkts_last);
if (data_info->rx_bytes_last)
free(data_info->rx_bytes_last);
if (data_info->tx_pkts_last)
free(data_info->tx_pkts_last);
if (data_info->tx_bytes_last)
free(data_info->tx_bytes_last);
#endif
} }
free(hos_func->fs2_info[i].reserved); free(hos_func->fs2_info[i].reserved);
hos_func->fs2_info[i].reserved = NULL; hos_func->fs2_info[i].reserved = NULL;
@@ -1287,7 +1306,7 @@ int hos_shutdown_instance()
memset(&g_hos_handle, 0 , sizeof(g_hos_handle)); memset(&g_hos_handle, 0 , sizeof(g_hos_handle));
if (g_hos_instance.hos_url_prefix) if (g_hos_instance.hos_url_prefix)
free((void *)g_hos_instance.hos_url_prefix); free((void *)g_hos_instance.hos_url_prefix);
memset(&g_hos_instance, 0, sizeof(g_hos_handle)); memset(&g_hos_instance, 0, sizeof(g_hos_instance));
return HOS_CLIENT_OK; return HOS_CLIENT_OK;
} }

View File

@@ -32,6 +32,7 @@ enum hoserrors
HOS_RUNTIME_LOG_FAILED = -6, HOS_RUNTIME_LOG_FAILED = -6,
HOS_CONF_ERROR = -7, HOS_CONF_ERROR = -7,
HOS_BUCKET_NOT_EXIST = -8, HOS_BUCKET_NOT_EXIST = -8,
HOS_INSTANCE_NOT_INIT = -9,
}; };
@@ -83,19 +84,6 @@ enum s3errors
typedef void (*put_finished_callback)(bool, const char *, const char *, const char *, void *); typedef void (*put_finished_callback)(bool, const char *, const char *, const char *, void *);
/*//FIXME 改为static不再对外提供
*************************************************************************************
* 函数名: hos_instance
* 参数: const char *serverip 目的地址,如"192.168.44.12"
* size_t port 端口号
* const char *accesskeyid AWS access key ID如"default"
* const char *secretkey AWS secret key如"default"
* siez_t pool_size 线程池大小
* size_t thread_sum 线程总数
* 返回值: 成功返回一个实例失败返回NULL。
*************************************************************************************
hos_instance hos_client_create(const char *serverip, size_t port, const char *accesskeyid, const char *secretkey, size_t pool_size);
*/
/************************************************************************************* /*************************************************************************************
* 函数名: hos_init_instance * 函数名: hos_init_instance
@@ -109,6 +97,12 @@ hos_instance hos_init_instance(const char *conf_path, const char *module, size_t
* 返回值: hos_instance 成功result 为true * 返回值: hos_instance 成功result 为true
*************************************************************************************/ *************************************************************************************/
hos_instance hos_get_instance(); hos_instance hos_get_instance();
/*************************************************************************************
* 函数名: hos_verify_bucket
* 参数: const char * bucket 桶名称
* 返回值: bool 成功返回true失败返回false
*************************************************************************************/
bool hos_verify_bucket(const char *bucket);
/************************************************************************************* /*************************************************************************************
* 函数名: hos_create_bucket * 函数名: hos_create_bucket
* 参数: const char * bucket 桶名称 * 参数: const char * bucket 桶名称
@@ -125,7 +119,7 @@ int hos_create_bucket(hos_instance instance, const char *bucket);
* size_t thread_id 当前线程id * size_t thread_id 当前线程id
* 返回值 int 成功返回0失败返回hoserros错误码 * 返回值 int 成功返回0失败返回hoserros错误码
*************************************************************************************/ *************************************************************************************/
int hos_upload_file(hos_instance instance, const char *bucket, const char *file_path, put_finished_callback callback, void* userdata, size_t thread_id); int hos_upload_file(const char *bucket, const char *file_path, put_finished_callback callback, void* userdata, size_t thread_id);
/************************************************************************************* /*************************************************************************************
* 函数名: hos_upload_buf * 函数名: hos_upload_buf
* 参数: hos_instance instance 非空句柄 * 参数: hos_instance instance 非空句柄
@@ -138,7 +132,7 @@ int hos_upload_file(hos_instance instance, const char *bucket, const char *file_
* size_t thread_id 当前线程id * size_t thread_id 当前线程id
* 返回值 int 成功返回0失败返回hoserros错误码 * 返回值 int 成功返回0失败返回hoserros错误码
*************************************************************************************/ *************************************************************************************/
int hos_upload_buf(hos_instance instance, const char *bucket, const char *object, const char *buf, size_t buf_len, put_finished_callback callback, void *userdata, size_t thread_id); int hos_upload_buf(const char *bucket, const char *object, const char *buf, size_t buf_len, put_finished_callback callback, void *userdata, size_t thread_id);
/************************************************************************************* /*************************************************************************************
* 函数名: hos_open_fd * 函数名: hos_open_fd
* 参数: const char * bucket 桶名称 * 参数: const char * bucket 桶名称