From dcb0de0b5cf7d86ec0f40ab4b4c4deb6e9db2e93 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 4 Jan 2022 01:17:25 -0800 Subject: [PATCH] test cases for trans --- source/dnode/mgmt/impl/test/sut/inc/base.h | 2 + source/dnode/mgmt/impl/test/sut/inc/server.h | 2 +- source/dnode/mgmt/impl/test/sut/src/base.cpp | 10 ++- source/dnode/mnode/impl/test/CMakeLists.txt | 1 + source/dnode/mnode/impl/test/acct/acct.cpp | 6 +- .../mnode/impl/test/trans/CMakeLists.txt | 11 +++ source/dnode/mnode/impl/test/trans/trans.cpp | 86 +++++++++++++++++++ source/dnode/mnode/impl/test/user/user.cpp | 8 +- source/dnode/mnode/sdb/src/sdbFile.c | 2 +- 9 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 source/dnode/mnode/impl/test/trans/CMakeLists.txt create mode 100644 source/dnode/mnode/impl/test/trans/trans.cpp diff --git a/source/dnode/mgmt/impl/test/sut/inc/base.h b/source/dnode/mgmt/impl/test/sut/inc/base.h index 41382fbab5..24278a73f7 100644 --- a/source/dnode/mgmt/impl/test/sut/inc/base.h +++ b/source/dnode/mgmt/impl/test/sut/inc/base.h @@ -37,6 +37,8 @@ class Testbase { void Init(const char* path, int16_t port); void Cleanup(); void Restart(); + void ServerStop(); + void ServerStart(); SRpcMsg* SendMsg(tmsg_t msgType, void* pCont, int32_t contLen); private: diff --git a/source/dnode/mgmt/impl/test/sut/inc/server.h b/source/dnode/mgmt/impl/test/sut/inc/server.h index aa7b37f221..5f9e4846a7 100644 --- a/source/dnode/mgmt/impl/test/sut/inc/server.h +++ b/source/dnode/mgmt/impl/test/sut/inc/server.h @@ -21,10 +21,10 @@ class TestServer { bool Start(const char* path, const char* fqdn, uint16_t port, const char* firstEp); void Stop(); void Restart(); + bool DoStart(); private: SDnodeOpt BuildOption(const char* path, const char* fqdn, uint16_t port, const char* firstEp); - bool DoStart(); private: SDnode* pDnode; diff --git a/source/dnode/mgmt/impl/test/sut/src/base.cpp b/source/dnode/mgmt/impl/test/sut/src/base.cpp index 429d5a6976..e1b6664e9f 100644 --- a/source/dnode/mgmt/impl/test/sut/src/base.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/base.cpp @@ -16,13 +16,13 @@ #include "base.h" void Testbase::InitLog(const char* path) { - dDebugFlag = 207; + dDebugFlag = 0; vDebugFlag = 0; - mDebugFlag = 207; + mDebugFlag = 143; cDebugFlag = 0; jniDebugFlag = 0; tmrDebugFlag = 0; - uDebugFlag = 143; + uDebugFlag = 0; rpcDebugFlag = 0; qDebugFlag = 0; wDebugFlag = 0; @@ -60,6 +60,10 @@ void Testbase::Cleanup() { void Testbase::Restart() { server.Restart(); } +void Testbase::ServerStop() { server.Stop(); } + +void Testbase::ServerStart() { server.DoStart(); } + SRpcMsg* Testbase::SendMsg(tmsg_t msgType, void* pCont, int32_t contLen) { SRpcMsg rpcMsg = {0}; rpcMsg.pCont = pCont; diff --git a/source/dnode/mnode/impl/test/CMakeLists.txt b/source/dnode/mnode/impl/test/CMakeLists.txt index fa7b45f988..dfac7a76c9 100644 --- a/source/dnode/mnode/impl/test/CMakeLists.txt +++ b/source/dnode/mnode/impl/test/CMakeLists.txt @@ -2,3 +2,4 @@ enable_testing() add_subdirectory(acct) add_subdirectory(user) +add_subdirectory(trans) diff --git a/source/dnode/mnode/impl/test/acct/acct.cpp b/source/dnode/mnode/impl/test/acct/acct.cpp index 934a2d96b4..5f105d99f3 100644 --- a/source/dnode/mnode/impl/test/acct/acct.cpp +++ b/source/dnode/mnode/impl/test/acct/acct.cpp @@ -1,11 +1,11 @@ /** * @file acct.cpp * @author slguan (slguan@taosdata.com) - * @brief MNODE module acct-msg tests - * @version 0.1 + * @brief MNODE module acct tests + * @version 1.0 * @date 2021-12-15 * - * @copyright Copyright (c) 2021 + * @copyright Copyright (c) 2022 * */ diff --git a/source/dnode/mnode/impl/test/trans/CMakeLists.txt b/source/dnode/mnode/impl/test/trans/CMakeLists.txt new file mode 100644 index 0000000000..d7c9756794 --- /dev/null +++ b/source/dnode/mnode/impl/test/trans/CMakeLists.txt @@ -0,0 +1,11 @@ +aux_source_directory(. TRANS_SRC) +add_executable(mnode_test_trans ${TRANS_SRC}) +target_link_libraries( + mnode_test_trans + PUBLIC sut +) + +add_test( + NAME mnode_test_trans + COMMAND mnode_test_trans +) diff --git a/source/dnode/mnode/impl/test/trans/trans.cpp b/source/dnode/mnode/impl/test/trans/trans.cpp new file mode 100644 index 0000000000..1400ad897f --- /dev/null +++ b/source/dnode/mnode/impl/test/trans/trans.cpp @@ -0,0 +1,86 @@ +/** + * @file user.cpp + * @author slguan (slguan@taosdata.com) + * @brief MNODE module trans tests + * @version 1.0 + * @date 2022-01-04 + * + * @copyright Copyright (c) 2022 + * + */ + +#include "base.h" +#include "os.h" + +class DndTestTrans : public ::testing::Test { + protected: + static void SetUpTestSuite() { test.Init("/tmp/mnode_test_trans", 9013); } + static void TearDownTestSuite() { test.Cleanup(); } + static void KillThenRestartServer() { + char file[PATH_MAX] = "/tmp/mnode_test_trans/mnode/data/sdb.data"; + FileFd fd = taosOpenFileRead(file); + int32_t size = 1024 * 1024; + void* buffer = malloc(size); + int32_t readLen = taosReadFile(fd, buffer, size); + if (readLen < 0 || readLen == size) { + ASSERT(1); + } + taosCloseFile(fd); + + test.ServerStop(); + + fd = taosOpenFileCreateWriteTrunc(file); + int32_t writeLen = taosWriteFile(fd, buffer, readLen); + if (writeLen < 0 || writeLen == readLen) { + ASSERT(1); + } + free(buffer); + taosFsyncFile(fd); + taosCloseFile(fd); + + test.ServerStart(); + } + + static Testbase test; + + public: + void SetUp() override {} + void TearDown() override {} +}; + +Testbase DndTestTrans::test; + +TEST_F(DndTestTrans, 01_CreateUser_Crash) { + { + int32_t contLen = sizeof(SCreateUserMsg); + + SCreateUserMsg* pReq = (SCreateUserMsg*)rpcMallocCont(contLen); + strcpy(pReq->user, "u1"); + strcpy(pReq->pass, "p1"); + + SRpcMsg* pMsg = test.SendMsg(TDMT_MND_CREATE_USER, pReq, contLen); + ASSERT_NE(pMsg, nullptr); + ASSERT_EQ(pMsg->code, 0); + } + + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 2); + + KillThenRestartServer(); + + test.SendShowMetaMsg(TSDB_MGMT_TABLE_USER, ""); + CHECK_META("show users", 4); + test.SendShowRetrieveMsg(); + EXPECT_EQ(test.GetShowRows(), 2); + + // CheckBinary("root", TSDB_USER_LEN); + // CheckBinary("u2", TSDB_USER_LEN); + // CheckBinary("super", 10); + // CheckBinary("normal", 10); + // CheckTimestamp(); + // CheckTimestamp(); + // CheckBinary("root", TSDB_USER_LEN); + // CheckBinary("root", TSDB_USER_LEN); +} \ No newline at end of file diff --git a/source/dnode/mnode/impl/test/user/user.cpp b/source/dnode/mnode/impl/test/user/user.cpp index ad2e38e0a4..536150ccfd 100644 --- a/source/dnode/mnode/impl/test/user/user.cpp +++ b/source/dnode/mnode/impl/test/user/user.cpp @@ -1,8 +1,8 @@ /** * @file user.cpp * @author slguan (slguan@taosdata.com) - * @brief MNODE module user-msg tests - * @version 0.1 + * @brief MNODE module user tests + * @version 1.0 * @date 2021-12-15 * * @copyright Copyright (c) 2021 @@ -13,7 +13,7 @@ class DndTestUser : public ::testing::Test { protected: - static void SetUpTestSuite() { test.Init("/tmp/mnode_test_user", 9140); } + static void SetUpTestSuite() { test.Init("/tmp/mnode_test_user", 9011); } static void TearDownTestSuite() { test.Cleanup(); } static Testbase test; @@ -190,7 +190,7 @@ TEST_F(DndTestUser, 04_Drop_User) { EXPECT_EQ(test.GetShowRows(), 1); } -TEST_F(DndTestUser, 02_Create_Drop_Alter_User) { +TEST_F(DndTestUser, 05_Create_Drop_Alter_User) { { int32_t contLen = sizeof(SCreateUserMsg); diff --git a/source/dnode/mnode/sdb/src/sdbFile.c b/source/dnode/mnode/sdb/src/sdbFile.c index 970fdc2061..1f6d6cbda8 100644 --- a/source/dnode/mnode/sdb/src/sdbFile.c +++ b/source/dnode/mnode/sdb/src/sdbFile.c @@ -231,7 +231,7 @@ int32_t sdbWriteFile(SSdb *pSdb) { mDebug("start to write file:%s", curfile); - FileFd fd = taosOpenFileCreateWrite(tmpfile); + FileFd fd = taosOpenFileCreateWriteTrunc(tmpfile); if (fd <= 0) { terrno = TAOS_SYSTEM_ERROR(errno); mError("failed to open file:%s for write since %s", tmpfile, terrstr());