topic.cpp 5.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 * @file topic.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief MNODE module topic tests
 * @version 1.0
 * @date 2022-02-16
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "sut.h"

class MndTestTopic : public ::testing::Test {
 protected:
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_topic", 9039); }
  static void TearDownTestSuite() { test.Cleanup(); }

  static Testbase test;

 public:
  void SetUp() override {}
  void TearDown() override {}

  void* BuildCreateDbReq(const char* dbname, int32_t* pContLen);
  void* BuildCreateTopicReq(const char* topicName, const char* sql, int32_t* pContLen);
  void* BuildDropTopicReq(const char* topicName, int32_t* pContLen);
};

Testbase MndTestTopic::test;

void* MndTestTopic::BuildCreateDbReq(const char* dbname, int32_t* pContLen) {
  SCreateDbReq createReq = {0};
  strcpy(createReq.db, dbname);
  createReq.numOfVgroups = 2;
S
Shengliang Guan 已提交
36 37 38 39 40 41 42
  createReq.buffer = -1;
  createReq.pageSize = -1;
  createReq.pages = -1;
  createReq.durationPerFile = 10 * 1440;
  createReq.durationToKeep0 = 3650 * 1440;
  createReq.durationToKeep1 = 3650 * 1440;
  createReq.durationToKeep2 = 3650 * 1440;
S
Shengliang Guan 已提交
43 44 45 46 47 48 49 50
  createReq.minRows = 100;
  createReq.maxRows = 4096;
  createReq.commitTime = 3600;
  createReq.fsyncPeriod = 3000;
  createReq.walLevel = 1;
  createReq.precision = 0;
  createReq.compression = 2;
  createReq.replications = 1;
S
Shengliang Guan 已提交
51
  createReq.strict = 1;
S
Shengliang Guan 已提交
52 53
  createReq.update = 0;
  createReq.cacheLastRow = 0;
S
Shengliang Guan 已提交
54
  createReq.ttl = 1;
S
Shengliang Guan 已提交
55 56 57 58 59 60 61 62 63 64 65
  createReq.ignoreExist = 1;

  int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSCreateDbReq(pReq, contLen, &createReq);

  *pContLen = contLen;
  return pReq;
}

void* MndTestTopic::BuildCreateTopicReq(const char* topicName, const char* sql, int32_t* pContLen) {
L
Liu Jicong 已提交
66
  SCMCreateTopicReq createReq = {0};
S
Shengliang Guan 已提交
67 68 69
  strcpy(createReq.name, topicName);
  createReq.igExists = 0;
  createReq.sql = (char*)sql;
70
  createReq.ast = NULL;
S
Shengliang Guan 已提交
71

L
Liu Jicong 已提交
72
  int32_t contLen = tSerializeSCMCreateTopicReq(NULL, 0, &createReq);
S
Shengliang Guan 已提交
73
  void*   pReq = rpcMallocCont(contLen);
L
Liu Jicong 已提交
74
  tSerializeSCMCreateTopicReq(pReq, contLen, &createReq);
S
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  *pContLen = contLen;
  return pReq;
}

void* MndTestTopic::BuildDropTopicReq(const char* topicName, int32_t* pContLen) {
  SMDropTopicReq dropReq = {0};
  strcpy(dropReq.name, topicName);

  int32_t contLen = tSerializeSMDropTopicReq(NULL, 0, &dropReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSMDropTopicReq(pReq, contLen, &dropReq);

  *pContLen = contLen;
  return pReq;
}

TEST_F(MndTestTopic, 01_Create_Topic) {
L
Liu Jicong 已提交
93 94
  // TODO add valid ast for unit test
#if 0
S
Shengliang Guan 已提交
95 96 97 98 99 100 101 102 103 104 105
  const char* dbname = "1.d1";
  const char* topicName = "1.d1.t1";

  {
    int32_t  contLen = 0;
    void*    pReq = BuildCreateDbReq(dbname, &contLen);
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

S
Shengliang Guan 已提交
106
  { test.SendShowReq(TSDB_MGMT_TABLE_TOPICS, ""); }
S
Shengliang Guan 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  {
    int32_t  contLen = 0;
    void*    pReq = BuildCreateTopicReq("t1", "sql", &contLen);
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_TOPIC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_DB_NOT_SELECTED);
  }

  {
    int32_t  contLen = 0;
    void*    pReq = BuildCreateTopicReq(topicName, "sql", &contLen);
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_TOPIC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    int32_t  contLen = 0;
    void*    pReq = BuildCreateTopicReq(topicName, "sql", &contLen);
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_TOPIC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_TOPIC_ALREADY_EXIST);
  }

  {
S
Shengliang Guan 已提交
133
    test.SendShowReq(TSDB_MGMT_TABLE_TOPICS, dbname);
S
Shengliang Guan 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    CHECK_META("show topics", 3);

    CHECK_SCHEMA(0, TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, "name");
    CHECK_SCHEMA(1, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time");
    CHECK_SCHEMA(2, TSDB_DATA_TYPE_BINARY, TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE, "sql");

    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckBinary("t1", TSDB_TABLE_NAME_LEN);
    CheckTimestamp();
    CheckBinary("sql", TSDB_SHOW_SQL_LEN);

    // restart
    test.Restart();

S
Shengliang Guan 已提交
150
    test.SendShowReq(TSDB_MGMT_TABLE_TOPICS, dbname);
S
Shengliang Guan 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 1);

    CheckBinary("t1", TSDB_TABLE_NAME_LEN);
    CheckTimestamp();
    CheckBinary("sql", TSDB_SHOW_SQL_LEN);
  }

  {
    int32_t  contLen = 0;
    void*    pReq = BuildDropTopicReq(topicName, &contLen);
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_TOPIC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    int32_t  contLen = 0;
    void*    pReq = BuildDropTopicReq(topicName, &contLen);
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_TOPIC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_TOPIC_NOT_EXIST);

S
Shengliang Guan 已提交
174
    test.SendShowReq(TSDB_MGMT_TABLE_TOPICS, dbname);
S
Shengliang Guan 已提交
175 176 177
    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 0);
  }
L
Liu Jicong 已提交
178
#endif
S
Shengliang Guan 已提交
179
}