util_test.cpp 6.9 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
G
groot 已提交
7
#include <thread>
G
groot 已提交
8

G
groot 已提交
9 10
#include "utils/CommonUtil.h"
#include "utils/Error.h"
G
groot 已提交
11
#include "utils/StringHelpFunctions.h"
G
groot 已提交
12 13
#include "utils/TimeRecorder.h"
#include "utils/BlockingQueue.h"
G
groot 已提交
14
#include "utils/LogUtil.h"
G
groot 已提交
15 16
#include "utils/ValidationUtil.h"
#include "db/engine/ExecutionEngine.h"
G
groot 已提交
17

J
jinhai 已提交
18
using namespace zilliz::milvus;
G
groot 已提交
19

G
groot 已提交
20
namespace {
G
groot 已提交
21

J
jinhai 已提交
22
static const char* LOG_FILE_PATH = "./milvus/conf/log_config.conf";
G
groot 已提交
23

G
groot 已提交
24 25
}

G
groot 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
TEST(UtilTest, EXCEPTION_TEST) {
    std::string err_msg = "failed";
    server::ServerException ex(server::SERVER_UNEXPECTED_ERROR, err_msg);
    ASSERT_EQ(ex.error_code(), server::SERVER_UNEXPECTED_ERROR);
    std::string msg = ex.what();
    ASSERT_EQ(msg, err_msg);
}

TEST(UtilTest, COMMON_TEST) {
    unsigned long total_mem = 0, free_mem = 0;
    server::CommonUtil::GetSystemMemInfo(total_mem, free_mem);
    ASSERT_GT(total_mem, 0);
    ASSERT_GT(free_mem, 0);

    unsigned int thread_cnt = 0;
    server::CommonUtil::GetSystemAvailableThreads(thread_cnt);
    ASSERT_GT(thread_cnt, 0);

G
groot 已提交
44 45 46 47 48
    std::string path1 = "/tmp/milvus_test/";
    std::string path2 = path1 + "common_test_12345/";
    std::string path3 = path2 + "abcdef";
    server::ServerError err = server::CommonUtil::CreateDirectory(path3);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
G
groot 已提交
49 50 51
    //test again
    err = server::CommonUtil::CreateDirectory(path3);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
G
groot 已提交
52

G
groot 已提交
53
    ASSERT_TRUE(server::CommonUtil::IsDirectoryExist(path3));
G
groot 已提交
54

G
groot 已提交
55 56 57
    err = server::CommonUtil::DeleteDirectory(path1);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
    //test again
G
groot 已提交
58
    err = server::CommonUtil::DeleteDirectory(path1);
G
groot 已提交
59 60
    ASSERT_EQ(err, server::SERVER_SUCCESS);

G
groot 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    ASSERT_FALSE(server::CommonUtil::IsDirectoryExist(path1));
    ASSERT_FALSE(server::CommonUtil::IsFileExist(path1));

    std::string exe_path = server::CommonUtil::GetExePath();
    ASSERT_FALSE(exe_path.empty());

    time_t tt;
    time( &tt );
    tm time_struct;
    memset(&time_struct, 0, sizeof(tm));
    server::CommonUtil::ConvertTime(tt, time_struct);
    ASSERT_GT(time_struct.tm_year, 0);
    ASSERT_GT(time_struct.tm_mon, 0);
    ASSERT_GT(time_struct.tm_mday, 0);
    server::CommonUtil::ConvertTime(time_struct, tt);
    ASSERT_GT(tt, 0);

    bool res = server::CommonUtil::TimeStrToTime("2019-03-23", tt, time_struct);
    ASSERT_EQ(time_struct.tm_year, 119);
    ASSERT_EQ(time_struct.tm_mon, 2);
    ASSERT_EQ(time_struct.tm_mday, 23);
    ASSERT_GT(tt, 0);
    ASSERT_TRUE(res);
G
groot 已提交
84 85 86 87 88 89 90
}

TEST(UtilTest, STRINGFUNCTIONS_TEST) {
    std::string str = " test zilliz";
    server::StringHelpFunctions::TrimStringBlank(str);
    ASSERT_EQ(str, "test zilliz");

G
groot 已提交
91 92 93 94
    str = "\"test zilliz\"";
    server::StringHelpFunctions::TrimStringQuote(str, "\"");
    ASSERT_EQ(str, "test zilliz");

G
groot 已提交
95 96
    str = "a,b,c";
    std::vector<std::string> result;
G
groot 已提交
97 98 99 100 101 102 103 104 105 106 107 108
    server::ServerError err = server::StringHelpFunctions::SplitStringByDelimeter(str , ",", result);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "", result);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
G
groot 已提交
109 110 111 112
    ASSERT_EQ(result.size(), 3UL);

    str = "55,\"aa,gg,yy\",b";
    result.clear();
G
groot 已提交
113 114
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
    ASSERT_EQ(err, server::SERVER_SUCCESS);
G
groot 已提交
115
    ASSERT_EQ(result.size(), 3UL);
G
groot 已提交
116 117


G
groot 已提交
118 119 120 121 122 123 124 125 126 127 128
}

TEST(UtilTest, BLOCKINGQUEUE_TEST) {
    server::BlockingQueue<std::string> bq;

    static const size_t count = 10;
    bq.SetCapacity(count);

    for(size_t i = 1; i <= count; i++) {
        std::string id = "No." + std::to_string(i);
        bq.Put(id);
G
groot 已提交
129 130
    }

G
groot 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    ASSERT_EQ(bq.Size(), count);
    ASSERT_FALSE(bq.Empty());

    std::string str = bq.Front();
    ASSERT_EQ(str, "No.1");

    str = bq.Back();
    ASSERT_EQ(str, "No." + std::to_string(count));

    for(size_t i = 1; i <= count; i++) {
        std::string id = "No." + std::to_string(i);
        str = bq.Take();
        ASSERT_EQ(id, str);
    }
G
groot 已提交
145 146

    ASSERT_EQ(bq.Size(), 0);
G
groot 已提交
147 148
}

G
groot 已提交
149 150 151 152
TEST(UtilTest, LOG_TEST) {
    int32_t res = server::InitLog(LOG_FILE_PATH);
    ASSERT_EQ(res, 0);

G
groot 已提交
153 154 155
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::NewLineForContainer));
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::LogDetailedCrashReason));

G
groot 已提交
156 157 158
    std::string fname = server::GetFileName(LOG_FILE_PATH);
    ASSERT_EQ(fname, "log_config.conf");
}
G
groot 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

TEST(UtilTest, VALIDATE_TABLENAME_TEST) {
    std::string table_name = "Normal123_";
    server:: ServerError res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_SUCCESS);

    table_name = "12sds";
    res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_INVALID_TABLE_NAME);

    table_name = "";
    res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_INVALID_TABLE_NAME);

    table_name = "_asdasd";
    res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_SUCCESS);

    table_name = "!@#!@";
    res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_INVALID_TABLE_NAME);

    table_name = "中文";
    res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_INVALID_TABLE_NAME);


    table_name = std::string('a', 32768);
    res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, server::SERVER_INVALID_TABLE_NAME);
}

TEST(UtilTest, VALIDATE_DIMENSIONTEST) {
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(-1), server::SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(0), server::SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(16385), server::SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(16384), server::SERVER_SUCCESS);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(1), server::SERVER_SUCCESS);
}

TEST(UtilTest, VALIDATE_INDEXTYPE_TEST) {
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType((int)engine::EngineType::INVALID), server::SERVER_INVALID_INDEX_TYPE);
    for(int i = 1; i <= (int)engine::EngineType::MAX_VALUE; i++) {
        ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType(i), server::SERVER_SUCCESS);
    }
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType((int)engine::EngineType::MAX_VALUE + 1), server::SERVER_INVALID_INDEX_TYPE);
}