test_util.cpp 21.7 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

Y
youny626 已提交
18 19
#include "db/engine/ExecutionEngine.h"
#include "utils/BlockingQueue.h"
G
groot 已提交
20 21
#include "utils/CommonUtil.h"
#include "utils/Error.h"
Y
youny626 已提交
22 23
#include "utils/LogUtil.h"
#include "utils/SignalUtil.h"
G
groot 已提交
24
#include "utils/StringHelpFunctions.h"
G
groot 已提交
25
#include "utils/TimeRecorder.h"
S
starlord 已提交
26
#include "utils/ValidationUtil.h"
S
shengjh 已提交
27
#include "utils/ThreadPool.h"
G
groot 已提交
28

Y
youny626 已提交
29
#include <gtest/gtest.h>
30
#include <sys/stat.h>
Y
youny626 已提交
31
#include <sys/types.h>
32
#include <boost/filesystem.hpp>
Y
youny626 已提交
33
#include <thread>
S
shengjh 已提交
34 35 36 37
#include <src/utils/Exception.h>

#include <fiu-local.h>
#include <fiu-control.h>
38

G
groot 已提交
39
namespace {
G
groot 已提交
40

S
starlord 已提交
41
static const char* LOG_FILE_PATH = "./milvus/conf/log_config.conf";
G
groot 已提交
42

S
starlord 已提交
43
void
S
starlord 已提交
44
CopyStatus(milvus::Status& st1, milvus::Status& st2) {
S
starlord 已提交
45 46 47
    st1 = st2;
}

Y
youny626 已提交
48
}  // namespace
G
groot 已提交
49

G
groot 已提交
50 51
TEST(UtilTest, EXCEPTION_TEST) {
    std::string err_msg = "failed";
S
starlord 已提交
52 53
    milvus::server::ServerException ex(milvus::SERVER_UNEXPECTED_ERROR, err_msg);
    ASSERT_EQ(ex.error_code(), milvus::SERVER_UNEXPECTED_ERROR);
G
groot 已提交
54 55
    std::string msg = ex.what();
    ASSERT_EQ(msg, err_msg);
S
shengjh 已提交
56 57 58 59 60 61 62 63 64 65 66

    milvus::Exception ex1(milvus::SERVER_UNEXPECTED_ERROR, err_msg);
    ASSERT_EQ(ex1.code(), milvus::SERVER_UNEXPECTED_ERROR);
    msg = ex1.what();
    ASSERT_EQ(msg, err_msg);

    std::string empty_err_msg;
    milvus::Exception empty_ex(milvus::SERVER_UNEXPECTED_ERROR, empty_err_msg);
    ASSERT_EQ(empty_ex.code(), milvus::SERVER_UNEXPECTED_ERROR);
    msg = empty_ex.what();
    ASSERT_NE(msg, empty_err_msg);
G
groot 已提交
67 68
}

S
starlord 已提交
69
TEST(UtilTest, SIGNAL_TEST) {
S
starlord 已提交
70
    milvus::server::SignalUtil::PrintStacktrace();
S
starlord 已提交
71 72
}

G
groot 已提交
73
TEST(UtilTest, COMMON_TEST) {
S
starlord 已提交
74
    uint64_t total_mem = 0, free_mem = 0;
S
starlord 已提交
75
    milvus::server::CommonUtil::GetSystemMemInfo(total_mem, free_mem);
G
groot 已提交
76 77 78
    ASSERT_GT(total_mem, 0);
    ASSERT_GT(free_mem, 0);

Y
yudong.cai 已提交
79
    int64_t thread_cnt = 0;
S
starlord 已提交
80
    milvus::server::CommonUtil::GetSystemAvailableThreads(thread_cnt);
G
groot 已提交
81 82
    ASSERT_GT(thread_cnt, 0);

S
shengjh 已提交
83 84 85 86 87 88 89
    fiu_init(0);
    fiu_enable("CommonUtil.GetSystemAvailableThreads.zero_thread", 1, NULL, 0);
    milvus::server::CommonUtil::GetSystemAvailableThreads(thread_cnt);
    ASSERT_GT(thread_cnt, 0);
    fiu_disable("CommonUtil.GetSystemAvailableThreads.zero_thread");

    std::string empty_path = "";
G
groot 已提交
90 91 92
    std::string path1 = "/tmp/milvus_test/";
    std::string path2 = path1 + "common_test_12345/";
    std::string path3 = path2 + "abcdef";
S
starlord 已提交
93
    milvus::Status status = milvus::server::CommonUtil::CreateDirectory(path3);
S
starlord 已提交
94
    ASSERT_TRUE(status.ok());
S
shengjh 已提交
95 96 97 98

    status = milvus::server::CommonUtil::CreateDirectory(empty_path);
    ASSERT_TRUE(status.ok());

Y
youny626 已提交
99
    // test again
S
starlord 已提交
100
    status = milvus::server::CommonUtil::CreateDirectory(path3);
S
starlord 已提交
101
    ASSERT_TRUE(status.ok());
G
groot 已提交
102

S
starlord 已提交
103
    ASSERT_TRUE(milvus::server::CommonUtil::IsDirectoryExist(path3));
G
groot 已提交
104

S
shengjh 已提交
105 106 107
    status = milvus::server::CommonUtil::DeleteDirectory(empty_path);
    ASSERT_TRUE(status.ok());

S
starlord 已提交
108
    status = milvus::server::CommonUtil::DeleteDirectory(path1);
S
starlord 已提交
109
    ASSERT_TRUE(status.ok());
Y
youny626 已提交
110
    // test again
S
starlord 已提交
111
    status = milvus::server::CommonUtil::DeleteDirectory(path1);
S
starlord 已提交
112
    ASSERT_TRUE(status.ok());
G
groot 已提交
113

S
starlord 已提交
114 115
    ASSERT_FALSE(milvus::server::CommonUtil::IsDirectoryExist(path1));
    ASSERT_FALSE(milvus::server::CommonUtil::IsFileExist(path1));
G
groot 已提交
116

S
starlord 已提交
117
    std::string exe_path = milvus::server::CommonUtil::GetExePath();
G
groot 已提交
118 119
    ASSERT_FALSE(exe_path.empty());

S
shengjh 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    fiu_enable("CommonUtil.GetExePath.readlink_fail", 1, NULL, 0);
    exe_path = milvus::server::CommonUtil::GetExePath();
    ASSERT_FALSE(!exe_path.empty());
    fiu_disable("CommonUtil.GetExePath.readlink_fail");

    fiu_enable("CommonUtil.GetExePath.exe_path_error", 1, NULL, 0);
    exe_path = milvus::server::CommonUtil::GetExePath();
    ASSERT_FALSE(exe_path.empty());
    fiu_disable("CommonUtil.GetExePath.exe_path_error");

    fiu_enable("CommonUtil.CreateDirectory.create_parent_fail", 1, NULL, 0);
    status = milvus::server::CommonUtil::CreateDirectory(path3);
    ASSERT_FALSE(status.ok());
    fiu_disable("CommonUtil.CreateDirectory.create_parent_fail");

    fiu_enable("CommonUtil.CreateDirectory.create_dir_fail", 1, NULL, 0);
    status = milvus::server::CommonUtil::CreateDirectory(path3);
    ASSERT_FALSE(status.ok());
    fiu_disable("CommonUtil.CreateDirectory.create_dir_fail");

G
groot 已提交
140
    time_t tt;
S
starlord 已提交
141
    time(&tt);
G
groot 已提交
142 143
    tm time_struct;
    memset(&time_struct, 0, sizeof(tm));
S
starlord 已提交
144
    milvus::server::CommonUtil::ConvertTime(tt, time_struct);
145 146 147
    ASSERT_GE(time_struct.tm_year, 0);
    ASSERT_GE(time_struct.tm_mon, 0);
    ASSERT_GE(time_struct.tm_mday, 0);
S
starlord 已提交
148
    milvus::server::CommonUtil::ConvertTime(time_struct, tt);
G
groot 已提交
149 150
    ASSERT_GT(tt, 0);

S
starlord 已提交
151
    bool res = milvus::server::CommonUtil::TimeStrToTime("2019-03-23", tt, time_struct);
G
groot 已提交
152 153 154 155 156
    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 已提交
157 158 159
}

TEST(UtilTest, STRINGFUNCTIONS_TEST) {
S
starlord 已提交
160
    std::string str = " test str";
S
starlord 已提交
161
    milvus::server::StringHelpFunctions::TrimStringBlank(str);
S
starlord 已提交
162
    ASSERT_EQ(str, "test str");
G
groot 已提交
163

S
starlord 已提交
164
    str = "\"test str\"";
S
starlord 已提交
165
    milvus::server::StringHelpFunctions::TrimStringQuote(str, "\"");
S
starlord 已提交
166
    ASSERT_EQ(str, "test str");
G
groot 已提交
167

G
groot 已提交
168 169
    str = "a,b,c";
    std::vector<std::string> result;
Z
Zhiru Zhu 已提交
170
    milvus::server::StringHelpFunctions::SplitStringByDelimeter(str, ",", result);
G
groot 已提交
171 172
    ASSERT_EQ(result.size(), 3UL);

G
groot 已提交
173 174 175
    std::string merge_str;
    milvus::server::StringHelpFunctions::MergeStringWithDelimeter(result, ",", merge_str);
    ASSERT_EQ(merge_str, "a,b,c");
G
groot 已提交
176
    result.clear();
G
groot 已提交
177 178 179
    milvus::server::StringHelpFunctions::MergeStringWithDelimeter(result, ",", merge_str);
    ASSERT_TRUE(merge_str.empty());

Z
Zhiru Zhu 已提交
180
    auto status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "\"", result);
S
starlord 已提交
181
    ASSERT_TRUE(status.ok());
G
groot 已提交
182 183 184
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
S
starlord 已提交
185
    status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "", result);
S
starlord 已提交
186
    ASSERT_TRUE(status.ok());
G
groot 已提交
187 188 189 190
    ASSERT_EQ(result.size(), 3UL);

    str = "55,\"aa,gg,yy\",b";
    result.clear();
S
starlord 已提交
191
    status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "\"", result);
S
starlord 已提交
192
    ASSERT_TRUE(status.ok());
G
groot 已提交
193
    ASSERT_EQ(result.size(), 3UL);
G
groot 已提交
194

S
shengjh 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    fiu_init(0);
    fiu_enable("StringHelpFunctions.SplitStringByQuote.invalid_index", 1, NULL, 0);
    result.clear();
    status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "\"", result);
    ASSERT_FALSE(status.ok());
    fiu_disable("StringHelpFunctions.SplitStringByQuote.invalid_index");

    fiu_enable("StringHelpFunctions.SplitStringByQuote.index_gt_last", 1, NULL, 0);
    result.clear();
    status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "\"", result);
    ASSERT_TRUE(status.ok());
    fiu_disable("StringHelpFunctions.SplitStringByQuote.index_gt_last");

    fiu_enable("StringHelpFunctions.SplitStringByQuote.invalid_index2", 1, NULL, 0);
    result.clear();
    status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "\"", result);
    ASSERT_FALSE(status.ok());
    fiu_disable("StringHelpFunctions.SplitStringByQuote.invalid_index2");

    fiu_enable("StringHelpFunctions.SplitStringByQuote.last_is_end", 1, NULL, 0);
    result.clear();
    status = milvus::server::StringHelpFunctions::SplitStringByQuote(str, ",", "\"", result);
    ASSERT_TRUE(status.ok());
    fiu_disable("StringHelpFunctions.SplitStringByQuote.last_is_end2");

G
groot 已提交
220 221 222
    ASSERT_TRUE(milvus::server::StringHelpFunctions::IsRegexMatch("abc", "abc"));
    ASSERT_TRUE(milvus::server::StringHelpFunctions::IsRegexMatch("a8c", "a\\d."));
    ASSERT_FALSE(milvus::server::StringHelpFunctions::IsRegexMatch("abc", "a\\dc"));
G
groot 已提交
223 224 225
}

TEST(UtilTest, BLOCKINGQUEUE_TEST) {
S
starlord 已提交
226
    milvus::server::BlockingQueue<std::string> bq;
G
groot 已提交
227 228 229 230

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

S
starlord 已提交
231
    for (size_t i = 1; i <= count; i++) {
G
groot 已提交
232 233
        std::string id = "No." + std::to_string(i);
        bq.Put(id);
G
groot 已提交
234 235
    }

G
groot 已提交
236 237 238 239 240 241 242 243 244
    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));

S
starlord 已提交
245
    for (size_t i = 1; i <= count; i++) {
G
groot 已提交
246 247 248 249
        std::string id = "No." + std::to_string(i);
        str = bq.Take();
        ASSERT_EQ(id, str);
    }
G
groot 已提交
250 251

    ASSERT_EQ(bq.Size(), 0);
G
groot 已提交
252 253
}

G
groot 已提交
254
TEST(UtilTest, LOG_TEST) {
S
starlord 已提交
255
    auto status = milvus::server::InitLog(LOG_FILE_PATH);
S
starlord 已提交
256
    ASSERT_TRUE(status.ok());
G
groot 已提交
257

S
starlord 已提交
258 259 260
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::NewLineForContainer));
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::LogDetailedCrashReason));

S
starlord 已提交
261
    std::string fname = milvus::server::CommonUtil::GetFileName(LOG_FILE_PATH);
G
groot 已提交
262 263
    ASSERT_EQ(fname, "log_config.conf");
}
S
starlord 已提交
264

S
starlord 已提交
265
TEST(UtilTest, TIMERECORDER_TEST) {
S
starlord 已提交
266 267
    for (int64_t log_level = 0; log_level <= 6; log_level++) {
        if (log_level == 5) {
Y
youny626 已提交
268
            continue;  // skip fatal
S
starlord 已提交
269
        }
S
starlord 已提交
270
        milvus::TimeRecorder rc("time", log_level);
S
starlord 已提交
271 272 273 274
        rc.RecordSection("end");
    }
}

S
shengjh 已提交
275 276 277 278 279
TEST(UtilTest, TIMERECOREDRAUTO_TEST) {
    milvus::TimeRecorderAuto rc("time");
    rc.RecordSection("end");
}

S
starlord 已提交
280
TEST(UtilTest, STATUS_TEST) {
S
starlord 已提交
281
    auto status = milvus::Status::OK();
S
starlord 已提交
282 283 284
    std::string str = status.ToString();
    ASSERT_FALSE(str.empty());

S
shengjh 已提交
285 286 287 288 289
    status = milvus::Status(milvus::DB_SUCCESS, "success");
    ASSERT_EQ(status.code(), milvus::DB_SUCCESS);
    str = status.ToString();
    ASSERT_FALSE(str.empty());

S
starlord 已提交
290 291
    status = milvus::Status(milvus::DB_ERROR, "mistake");
    ASSERT_EQ(status.code(), milvus::DB_ERROR);
S
starlord 已提交
292 293 294
    str = status.ToString();
    ASSERT_FALSE(str.empty());

S
starlord 已提交
295 296
    status = milvus::Status(milvus::DB_NOT_FOUND, "mistake");
    ASSERT_EQ(status.code(), milvus::DB_NOT_FOUND);
S
starlord 已提交
297 298 299
    str = status.ToString();
    ASSERT_FALSE(str.empty());

S
starlord 已提交
300 301
    status = milvus::Status(milvus::DB_ALREADY_EXIST, "mistake");
    ASSERT_EQ(status.code(), milvus::DB_ALREADY_EXIST);
S
starlord 已提交
302 303 304
    str = status.ToString();
    ASSERT_FALSE(str.empty());

G
groot 已提交
305 306 307 308 309
    status = milvus::Status(milvus::DB_INVALID_PATH, "mistake");
    ASSERT_EQ(status.code(), milvus::DB_INVALID_PATH);
    str = status.ToString();
    ASSERT_FALSE(str.empty());

S
starlord 已提交
310 311
    status = milvus::Status(milvus::DB_META_TRANSACTION_FAILED, "mistake");
    ASSERT_EQ(status.code(), milvus::DB_META_TRANSACTION_FAILED);
S
starlord 已提交
312 313 314
    str = status.ToString();
    ASSERT_FALSE(str.empty());

S
starlord 已提交
315
    auto status_copy = milvus::Status::OK();
S
starlord 已提交
316
    CopyStatus(status_copy, status);
S
starlord 已提交
317
    ASSERT_EQ(status.code(), milvus::DB_META_TRANSACTION_FAILED);
S
starlord 已提交
318 319 320 321 322 323 324 325 326 327

    auto status_ref(status);
    ASSERT_EQ(status_ref.code(), status.code());
    ASSERT_EQ(status_ref.ToString(), status.ToString());

    auto status_move = std::move(status);
    ASSERT_EQ(status_move.code(), status_ref.code());
    ASSERT_EQ(status_move.ToString(), status_ref.ToString());
}

S
starlord 已提交
328
TEST(ValidationUtilTest, VALIDATE_TABLENAME_TEST) {
S
starlord 已提交
329
    std::string table_name = "Normal123_";
S
starlord 已提交
330
    auto status = milvus::server::ValidationUtil::ValidateTableName(table_name);
S
starlord 已提交
331
    ASSERT_TRUE(status.ok());
S
starlord 已提交
332 333

    table_name = "12sds";
S
starlord 已提交
334 335
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
S
starlord 已提交
336 337

    table_name = "";
S
starlord 已提交
338 339
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
S
starlord 已提交
340 341

    table_name = "_asdasd";
S
starlord 已提交
342 343
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
344 345

    table_name = "!@#!@";
S
starlord 已提交
346 347
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
S
starlord 已提交
348

S
starlord 已提交
349
    table_name = "_!@#!@";
S
starlord 已提交
350 351
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
S
starlord 已提交
352

S
starlord 已提交
353
    table_name = "中文";
S
starlord 已提交
354 355
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
S
starlord 已提交
356

S
starlord 已提交
357
    table_name = std::string(10000, 'a');
S
starlord 已提交
358 359
    status = milvus::server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
G
groot 已提交
360 361 362 363

    table_name = "";
    status = milvus::server::ValidationUtil::ValidatePartitionName(table_name);
    ASSERT_EQ(status.code(), milvus::SERVER_INVALID_TABLE_NAME);
S
starlord 已提交
364 365
}

S
starlord 已提交
366
TEST(ValidationUtilTest, VALIDATE_DIMENSION_TEST) {
S
starlord 已提交
367 368 369 370
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableDimension(-1).code(),
              milvus::SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableDimension(0).code(),
              milvus::SERVER_INVALID_VECTOR_DIMENSION);
G
groot 已提交
371
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableDimension(32769).code(),
S
starlord 已提交
372
              milvus::SERVER_INVALID_VECTOR_DIMENSION);
G
groot 已提交
373
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableDimension(32768).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
374
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableDimension(1).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
375 376
}

S
starlord 已提交
377
TEST(ValidationUtilTest, VALIDATE_INDEX_TEST) {
S
starlord 已提交
378 379 380
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexType((int)milvus::engine::EngineType::INVALID).code(),
              milvus::SERVER_INVALID_INDEX_TYPE);
    for (int i = 1; i <= (int)milvus::engine::EngineType::MAX_VALUE; i++) {
S
starlord 已提交
381 382
#ifndef CUSTOMIZATION
        if (i == (int)milvus::engine::EngineType::FAISS_IVFSQ8H) {
S
shengjh 已提交
383
            ASSERT_NE(milvus::server::ValidationUtil::ValidateTableIndexType(i).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
384 385 386
            continue;
        }
#endif
S
starlord 已提交
387
        ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexType(i).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
388
    }
S
shengjh 已提交
389

Y
youny626 已提交
390 391 392
    ASSERT_EQ(
        milvus::server::ValidationUtil::ValidateTableIndexType((int)milvus::engine::EngineType::MAX_VALUE + 1).code(),
        milvus::SERVER_INVALID_INDEX_TYPE);
S
starlord 已提交
393

S
starlord 已提交
394 395
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexNlist(0).code(), milvus::SERVER_INVALID_INDEX_NLIST);
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexNlist(100).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
396

S
starlord 已提交
397 398 399
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexFileSize(0).code(),
              milvus::SERVER_INVALID_INDEX_FILE_SIZE);
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexFileSize(100).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
400

S
starlord 已提交
401 402 403 404
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexMetricType(0).code(),
              milvus::SERVER_INVALID_INDEX_METRIC_TYPE);
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexMetricType(1).code(), milvus::SERVER_SUCCESS);
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateTableIndexMetricType(2).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
405 406
}

S
starlord 已提交
407
TEST(ValidationUtilTest, VALIDATE_TOPK_TEST) {
S
starlord 已提交
408 409 410 411
    milvus::engine::meta::TableSchema schema;
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateSearchTopk(10, schema).code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateSearchTopk(65536, schema).code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateSearchTopk(0, schema).code(), milvus::SERVER_SUCCESS);
412 413
}

S
starlord 已提交
414
TEST(ValidationUtilTest, VALIDATE_NPROBE_TEST) {
S
starlord 已提交
415
    milvus::engine::meta::TableSchema schema;
416
    schema.nlist_ = 100;
S
starlord 已提交
417 418 419
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateSearchNprobe(10, schema).code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateSearchNprobe(0, schema).code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateSearchNprobe(101, schema).code(), milvus::SERVER_SUCCESS);
420 421
}

G
groot 已提交
422 423 424 425 426 427 428
TEST(ValidationUtilTest, VALIDATE_PARTITION_TAGS) {
    std::vector<std::string> partition_tags = {"abc"};
    ASSERT_EQ(milvus::server::ValidationUtil::ValidatePartitionTags(partition_tags).code(), milvus::SERVER_SUCCESS);
    partition_tags.push_back("");
    ASSERT_NE(milvus::server::ValidationUtil::ValidatePartitionTags(partition_tags).code(), milvus::SERVER_SUCCESS);
}

Y
youny626 已提交
429
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
430
TEST(ValidationUtilTest, VALIDATE_GPU_TEST) {
S
starlord 已提交
431 432
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateGpuIndex(0).code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateGpuIndex(100).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
433

S
shengjh 已提交
434 435 436 437 438
    fiu_init(0);
    fiu_enable("ValidationUtil.ValidateGpuIndex.get_device_count_fail", 1, NULL, 0);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateGpuIndex(0).code(), milvus::SERVER_SUCCESS);
    fiu_disable("ValidationUtil.ValidateGpuIndex.get_device_count_fail");

S
starlord 已提交
439
    size_t memory = 0;
S
starlord 已提交
440 441
    ASSERT_EQ(milvus::server::ValidationUtil::GetGpuMemory(0, memory).code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::GetGpuMemory(100, memory).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
442
}
S
shengjh 已提交
443

Y
youny626 已提交
444
#endif
S
starlord 已提交
445

S
starlord 已提交
446
TEST(ValidationUtilTest, VALIDATE_IPADDRESS_TEST) {
S
starlord 已提交
447 448
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateIpAddress("127.0.0.1").code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateIpAddress("not ip").code(), milvus::SERVER_SUCCESS);
S
shengjh 已提交
449 450 451 452 453

    fiu_init(0);
    fiu_enable("ValidationUtil.ValidateIpAddress.error_ip_result", 1, NULL, 0);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateIpAddress("not ip").code(), milvus::SERVER_SUCCESS);
    fiu_disable("ValidationUtil.ValidateIpAddress.error_ip_result");
S
starlord 已提交
454 455 456
}

TEST(ValidationUtilTest, VALIDATE_NUMBER_TEST) {
S
starlord 已提交
457 458
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateStringIsNumber("1234").code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateStringIsNumber("not number").code(), milvus::SERVER_SUCCESS);
S
shengjh 已提交
459 460 461 462 463

    fiu_init(0);
    fiu_enable("ValidationUtil.ValidateStringIsNumber.throw_exception", 1, NULL, 0);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateStringIsNumber("122").code(), milvus::SERVER_SUCCESS);
    fiu_disable("ValidationUtil.ValidateStringIsNumber.throw_exception");
S
starlord 已提交
464 465 466 467
}

TEST(ValidationUtilTest, VALIDATE_BOOL_TEST) {
    std::string str = "true";
S
starlord 已提交
468
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateStringIsBool(str).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
469
    str = "not bool";
S
starlord 已提交
470
    ASSERT_NE(milvus::server::ValidationUtil::ValidateStringIsBool(str).code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
471 472 473
}

TEST(ValidationUtilTest, VALIDATE_DOUBLE_TEST) {
S
starlord 已提交
474 475
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateStringIsFloat("2.5").code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateStringIsFloat("not double").code(), milvus::SERVER_SUCCESS);
S
starlord 已提交
476 477 478
}

TEST(ValidationUtilTest, VALIDATE_DBURI_TEST) {
S
starlord 已提交
479 480 481 482 483 484 485
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateDbURI("sqlite://:@:/").code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateDbURI("xxx://:@:/").code(), milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateDbURI("not uri").code(), milvus::SERVER_SUCCESS);
    ASSERT_EQ(milvus::server::ValidationUtil::ValidateDbURI("mysql://root:123456@127.0.0.1:3303/milvus").code(),
              milvus::SERVER_SUCCESS);
    ASSERT_NE(milvus::server::ValidationUtil::ValidateDbURI("mysql://root:123456@127.0.0.1:port/milvus").code(),
              milvus::SERVER_SUCCESS);
S
starlord 已提交
486
}
H
Heisenberg 已提交
487

S
starlord 已提交
488
TEST(UtilTest, ROLLOUTHANDLER_TEST) {
H
Heisenberg 已提交
489 490
    std::string dir1 = "/tmp/milvus_test";
    std::string dir2 = "/tmp/milvus_test/log_test";
Y
youny626 已提交
491
    std::string filename[6] = {"log_global.log", "log_debug.log", "log_warning.log",
S
shengjh 已提交
492
                               "log_trace.log", "log_error.log", "log_fatal.log"};
Y
youny626 已提交
493 494

    el::Level list[6] = {el::Level::Global, el::Level::Debug, el::Level::Warning,
S
shengjh 已提交
495
                         el::Level::Trace, el::Level::Error, el::Level::Fatal};
H
Heisenberg 已提交
496 497 498

    mkdir(dir1.c_str(), S_IRWXU);
    mkdir(dir2.c_str(), S_IRWXU);
S
shengjh 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
//    [&]() {
////        std::string tmp = dir2 + "/" + filename[0]+"*@%$";
//        std::string tmp = dir2 + "/" + filename[0] + "*$";
//        std::ofstream file;
//        file.open(tmp.c_str());
//        file << "test" << std::endl;
//        milvus::server::RolloutHandler(tmp.c_str(), 0, el::Level::Unknown);
//        tmp.append(".1");
//        std::ifstream file2;
//        file2.open(tmp);
//        std::string tmp2;
//        file2 >> tmp2;
//        ASSERT_EQ(tmp2, "test");
//    }();

H
Heisenberg 已提交
514 515 516 517 518
    for (int i = 0; i < 6; ++i) {
        std::string tmp = dir2 + "/" + filename[i];

        std::ofstream file;
        file.open(tmp.c_str());
S
starlord 已提交
519
        file << "test" << std::endl;
H
Heisenberg 已提交
520

S
starlord 已提交
521
        milvus::server::RolloutHandler(tmp.c_str(), 0, list[i]);
H
Heisenberg 已提交
522 523 524 525 526 527 528

        tmp.append(".1");
        std::ifstream file2;
        file2.open(tmp);

        std::string tmp2;
        file2 >> tmp2;
S
starlord 已提交
529
        ASSERT_EQ(tmp2, "test");
H
Heisenberg 已提交
530
    }
S
shengjh 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545

    [&]() {
        std::string tmp = dir2 + "/" + filename[0];
        std::ofstream file;
        file.open(tmp.c_str());
        file << "test" << std::endl;
        milvus::server::RolloutHandler(tmp.c_str(), 0, el::Level::Unknown);
        tmp.append(".1");
        std::ifstream file2;
        file2.open(tmp);
        std::string tmp2;
        file2 >> tmp2;
        ASSERT_EQ(tmp2, "test");
    }();

H
Heisenberg 已提交
546
    boost::filesystem::remove_all(dir2);
S
starlord 已提交
547
}
S
shengjh 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

TEST(UtilTest, THREADPOOL_TEST) {
    auto thread_pool_ptr = std::make_unique<milvus::ThreadPool>(3);
    auto fun = [](int i) {
        sleep(1);
    };
    for (int i = 0; i < 10; ++i) {
        thread_pool_ptr->enqueue(fun, i);
    }

    fiu_init(0);
    fiu_enable("ThreadPool.enqueue.stop_is_true", 1, NULL, 0);
    try {
        thread_pool_ptr->enqueue(fun, -1);
    } catch (std::exception& err) {
        std::cout << "catch an error here" << std::endl;
    }
    fiu_disable("ThreadPool.enqueue.stop_is_true");

    thread_pool_ptr.reset();
}