log_test.cpp 2.6 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021
//

#include "log_test.h"

#include "gtest/gtest.h"

#include "common/log/log.h"

using namespace common;

L
Longda 已提交
23 24
LogTest::LogTest()
{
羽飞's avatar
羽飞 已提交
25 26 27
  // Auto-generated constructor stub
}

L
Longda 已提交
28 29
LogTest::~LogTest()
{
羽飞's avatar
羽飞 已提交
30 31 32
  // Auto-generated destructor stub
}

L
Longda 已提交
33 34
int LogTest::init(const std::string &logFile)
{
羽飞's avatar
羽飞 已提交
35 36 37 38 39 40 41 42

  LoggerFactory::init_default(logFile);

  g_log->set_rotate_type(LOG_ROTATE_BYSIZE);

  return 0;
}

L
Longda 已提交
43 44 45
void *LogTest::log_loop(void *param)
{
  int index = *(int *)param;
羽飞's avatar
羽飞 已提交
46 47 48 49 50 51 52 53 54
  int i = 0;
  while (i < 100) {
    i++;
    LOG_INFO("index:%d --> %d", index, i);
  }

  return NULL;
}

L
Longda 已提交
55 56
void checkRotate()
{
羽飞's avatar
羽飞 已提交
57 58 59 60 61 62 63 64 65 66 67
  LogTest test;

  test.init();
  ASSERT_EQ(g_log->get_rotate_type(), LOG_ROTATE_BYSIZE);

  int index = 30;
  test.log_loop(&index);
}

TEST(checkRotateTest, CheckRoateTest)
{
L
Longda 已提交
68
  checkRotate();
羽飞's avatar
羽飞 已提交
69 70
}

L
Longda 已提交
71 72
void testEnableTest()
{
羽飞's avatar
羽飞 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  LogTest test;

  test.init();

  ASSERT_EQ(g_log->check_output(LOG_LEVEL_PANIC, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_ERR, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_WARN, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_INFO, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_DEBUG, __FILE__), false);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_TRACE, __FILE__), false);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_LAST, __FILE__), false);

  g_log->set_default_module(__FILE__);

  ASSERT_EQ(g_log->check_output(LOG_LEVEL_PANIC, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_ERR, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_WARN, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_INFO, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_DEBUG, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_TRACE, __FILE__), true);
  ASSERT_EQ(g_log->check_output(LOG_LEVEL_LAST, __FILE__), true);
}

TEST(testEnableTest, CheckEnableTest)
{
L
Longda 已提交
98
  testEnableTest();
羽飞's avatar
羽飞 已提交
99 100
}

L
Longda 已提交
101 102
int main(int argc, char **argv)
{
羽飞's avatar
羽飞 已提交
103 104 105 106 107 108 109 110

  // 分析gtest程序的命令行参数
  testing::InitGoogleTest(&argc, argv);

  // 调用RUN_ALL_TESTS()运行所有测试用例
  // main函数返回RUN_ALL_TESTS()的运行结果
  return RUN_ALL_TESTS();
}