From 5017ced9fd49c84ab56f84b74562e64baf945d7b Mon Sep 17 00:00:00 2001 From: wangyunlai Date: Mon, 15 May 2023 15:30:32 +0800 Subject: [PATCH] fix unittest (#9) (#177) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix unittest; add unittest to github action ### What problem were solved in this pull request? Issue Number: close #176 close #163 Problem: 单元测试执行失败; github action 没有单元测试检查 --- .github/workflows/build.yml | 22 +++++++++++++---- CMakeLists.txt | 1 + src/observer/storage/clog/clog.cpp | 3 +++ .../storage/record/record_manager.cpp | 2 ++ unittest/CMakeLists.txt | 2 -- unittest/bp_manager_test.cpp | 5 ++++ unittest/clog_test.cpp | 24 +++++++++++-------- 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 00abb55..fe31a7e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,7 +11,7 @@ env: BUILD_TYPE: Release jobs: - build-on-ubuntu: + build-and-test-on-ubuntu: # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. # You can convert this to a matrix build if you need cross-platform coverage. # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix @@ -20,10 +20,24 @@ jobs: steps: - name: Checkout repository and submodules uses: actions/checkout@v2 - - - name: Build + + - name: Init shell: bash - run: sudo bash build.sh init && bash build.sh release --make -j4 + run: sudo bash build.sh init + + - name: BuildRelease + shell: bash + run: bash build.sh release --make -j4 + + - name: BuildDebug + shell: bash + run: bash build.sh debug --make -j4 + + - name: Test + shell: bash + run: | + cd build_debug + make test build-on-mac: # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. diff --git a/CMakeLists.txt b/CMakeLists.txt index 64131dc..518961a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ ADD_SUBDIRECTORY(test/perf) ADD_SUBDIRECTORY(benchmark) IF(WITH_UNIT_TESTS) + enable_testing() ADD_SUBDIRECTORY(unittest) ENDIF() diff --git a/src/observer/storage/clog/clog.cpp b/src/observer/storage/clog/clog.cpp index 9605404..741177c 100644 --- a/src/observer/storage/clog/clog.cpp +++ b/src/observer/storage/clog/clog.cpp @@ -255,6 +255,7 @@ RC CLogBuffer::block_copy(int32_t offset, CLogBlock *log_block) // CLogFile::CLogFile(const char *path) { + // TODO move to init routine log_file_ = new PersistHandler(); RC rc = RC::SUCCESS; std::string clog_file_path = std::string(path) + common::FILE_PATH_SPLIT_STR + CLOG_FILE_NAME; @@ -265,6 +266,8 @@ CLogFile::CLogFile(const char *path) } else if (rc == RC::FILE_EXIST) { log_file_->open_file(clog_file_path.c_str()); log_file_->read_at(0, CLOG_BLOCK_SIZE, (char *)&log_fhd_); + } else { + LOG_WARN("failed to create clog file: %s, rc=%s", clog_file_path.c_str(), strrc(rc)); } } diff --git a/src/observer/storage/record/record_manager.cpp b/src/observer/storage/record/record_manager.cpp index 9ade2b7..4b5df6a 100644 --- a/src/observer/storage/record/record_manager.cpp +++ b/src/observer/storage/record/record_manager.cpp @@ -580,6 +580,8 @@ RC RecordFileScanner::close_scan() condition_filter_ = nullptr; } + record_page_handler_.cleanup(); + return RC::SUCCESS; } diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 987ae42..cd429d6 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -25,10 +25,8 @@ ELSE () LINK_DIRECTORIES(/usr/local/lib) ENDIF () - find_package(GTest CONFIG REQUIRED) -enable_testing() include(GoogleTest) #get_filename_component( FileName # PATH|ABSOLUTE|NAME|EXT|NAME_WE|REALPATH diff --git a/unittest/bp_manager_test.cpp b/unittest/bp_manager_test.cpp index 2f3a002..835a9f8 100644 --- a/unittest/bp_manager_test.cpp +++ b/unittest/bp_manager_test.cpp @@ -26,6 +26,7 @@ void test_get(BPFrameManager &frame_manager) frame1->set_page_num(page_num); ASSERT_EQ(frame1, frame_manager.get(file_desc, 1)); + frame1->unpin(); Frame *frame2 = frame_manager.alloc(file_desc, 2); ASSERT_NE(frame2, nullptr); @@ -33,6 +34,7 @@ void test_get(BPFrameManager &frame_manager) frame2->set_page_num(2); ASSERT_EQ(frame1, frame_manager.get(file_desc, 1)); + frame1->unpin(); Frame *frame3 = frame_manager.alloc(file_desc, 3); ASSERT_NE(frame3, nullptr); @@ -41,6 +43,7 @@ void test_get(BPFrameManager &frame_manager) frame2 = frame_manager.get(file_desc, 2); ASSERT_NE(frame2, nullptr); + frame2->unpin(); Frame *frame4 = frame_manager.alloc(file_desc, 4); frame4->set_file_desc(file_desc); @@ -51,8 +54,10 @@ void test_get(BPFrameManager &frame_manager) ASSERT_EQ(frame1, nullptr); ASSERT_EQ(frame3, frame_manager.get(file_desc, 3)); + frame3->unpin(); ASSERT_EQ(frame4, frame_manager.get(file_desc, 4)); + frame4->unpin(); frame_manager.free(file_desc, frame2->page_num(), frame2); frame_manager.free(file_desc, frame3->page_num(), frame3); diff --git a/unittest/clog_test.cpp b/unittest/clog_test.cpp index 7b287ba..158b7a6 100644 --- a/unittest/clog_test.cpp +++ b/unittest/clog_test.cpp @@ -14,9 +14,12 @@ See the Mulan PSL v2 for more details. */ #include +#include "common/log/log.h" #include "storage/clog/clog.h" #include "gtest/gtest.h" +using namespace common; + Record *gen_ins_record(int32_t page_num, int32_t slot_num, int data_len) { Record *rec = new Record(); @@ -36,7 +39,7 @@ Record *gen_del_record(int32_t page_num, int32_t slot_num) TEST(test_clog, test_clog) { - CLogManager *log_mgr = new CLogManager("/home/huhaosheng.hhs/Alibaba/miniob"); + CLogManager *log_mgr = new CLogManager("./"); CLogRecord *log_rec[6]; CLogRecord *log_mtr_rec = nullptr; @@ -44,7 +47,7 @@ TEST(test_clog, test_clog) // log_mgr->clog_gen_record(REDO_MTR_BEGIN, 1, log_mtr_rec); log_mgr->clog_append_record(log_mtr_rec); // NOTE: 需要保留log_rec - delete log_mtr_rec; + // delete log_mtr_rec; rec = gen_ins_record(1, 1, 100); log_mgr->clog_gen_record(REDO_INSERT, 1, log_rec[0], "table1", 100, rec); @@ -60,7 +63,7 @@ TEST(test_clog, test_clog) log_mgr->clog_gen_record(REDO_MTR_BEGIN, 2, log_mtr_rec); log_mgr->clog_append_record(log_mtr_rec); - delete log_mtr_rec; + // delete log_mtr_rec; rec = gen_ins_record(1, 1, 200); log_mgr->clog_gen_record(REDO_INSERT, 1, log_rec[2], "table3", 200, rec); @@ -82,7 +85,7 @@ TEST(test_clog, test_clog) log_mgr->clog_gen_record(REDO_MTR_COMMIT, 2, log_mtr_rec); log_mgr->clog_append_record(log_mtr_rec); - delete log_mtr_rec; + // delete log_mtr_rec; rec = gen_del_record(1, 1); log_mgr->clog_gen_record(REDO_DELETE, 1, log_rec[5], "table1", 0, rec); @@ -91,7 +94,7 @@ TEST(test_clog, test_clog) log_mgr->clog_gen_record(REDO_MTR_COMMIT, 1, log_mtr_rec); log_mgr->clog_append_record(log_mtr_rec); - delete log_mtr_rec; + // delete log_mtr_rec; log_mgr->recover(); @@ -102,6 +105,8 @@ TEST(test_clog, test_clog) ASSERT_EQ(6, log_mtr_mgr->log_redo_list.size()); + /* + // record 已经被删掉了,不能再访问 int i = 0; for (auto iter = log_mtr_mgr->log_redo_list.begin(); iter != log_mtr_mgr->log_redo_list.end(); iter++) { @@ -110,10 +115,7 @@ TEST(test_clog, test_clog) delete tmp; i++; } - - for (i = 0; i < 6; i++) { - delete log_rec[i]; - } + */ } int main(int argc, char **argv) @@ -121,7 +123,9 @@ int main(int argc, char **argv) // 分析gtest程序的命令行参数 testing::InitGoogleTest(&argc, argv); + LoggerFactory::init_default("test.log", LOG_LEVEL_TRACE); + // 调用RUN_ALL_TESTS()运行所有测试用例 // main函数返回RUN_ALL_TESTS()的运行结果 return RUN_ALL_TESTS(); -} \ No newline at end of file +} -- GitLab