stb.cpp 6.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3
/**
 * @file stb.cpp
 * @author slguan (slguan@taosdata.com)
S
Shengliang Guan 已提交
4 5 6
 * @brief MNODE module stb tests
 * @version 1.0
 * @date 2022-01-12
S
Shengliang Guan 已提交
7
 *
S
Shengliang Guan 已提交
8
 * @copyright Copyright (c) 2022
S
Shengliang Guan 已提交
9 10 11
 *
 */

S
Shengliang Guan 已提交
12
#include "sut.h"
S
Shengliang Guan 已提交
13

S
Shengliang Guan 已提交
14
class MndTestStb : public ::testing::Test {
S
Shengliang Guan 已提交
15
 protected:
S
Shengliang Guan 已提交
16
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_stb", 9034); }
S
Shengliang Guan 已提交
17
  static void TearDownTestSuite() { test.Cleanup(); }
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
  static Testbase test;
S
Shengliang Guan 已提交
20 21 22 23 24 25

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

S
Shengliang Guan 已提交
26
Testbase MndTestStb::test;
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
TEST_F(MndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) {
S
Shengliang Guan 已提交
29
  {
S
Shengliang Guan 已提交
30
    int32_t contLen = sizeof(SCreateDbReq);
S
Shengliang Guan 已提交
31

S
Shengliang Guan 已提交
32
    SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
33 34 35 36 37 38 39 40
    strcpy(pReq->db, "1.d1");
    pReq->numOfVgroups = htonl(2);
    pReq->cacheBlockSize = htonl(16);
    pReq->totalBlocks = htonl(10);
    pReq->daysPerFile = htonl(10);
    pReq->daysToKeep0 = htonl(3650);
    pReq->daysToKeep1 = htonl(3650);
    pReq->daysToKeep2 = htonl(3650);
S
Shengliang Guan 已提交
41 42
    pReq->minRows = htonl(100);
    pReq->maxRows = htonl(4096);
S
Shengliang Guan 已提交
43 44 45 46 47 48 49 50 51 52 53
    pReq->commitTime = htonl(3600);
    pReq->fsyncPeriod = htonl(3000);
    pReq->walLevel = 1;
    pReq->precision = 0;
    pReq->compression = 2;
    pReq->replications = 1;
    pReq->quorum = 1;
    pReq->update = 0;
    pReq->cacheLastRow = 0;
    pReq->ignoreExist = 1;

S
Shengliang Guan 已提交
54 55 56
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
57 58 59
  }

  {
S
Shengliang Guan 已提交
60 61
    int32_t cols = 2;
    int32_t tags = 3;
S
Shengliang Guan 已提交
62
    int32_t contLen = (tags + cols) * sizeof(SSchema) + sizeof(SMCreateStbReq);
S
Shengliang Guan 已提交
63

S
Shengliang Guan 已提交
64
    SMCreateStbReq* pReq = (SMCreateStbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
65 66 67 68 69
    strcpy(pReq->name, "1.d1.stb");
    pReq->numOfTags = htonl(tags);
    pReq->numOfColumns = htonl(cols);

    {
S
Shengliang Guan 已提交
70
      SSchema* pSchema = &pReq->pSchemas[0];
S
Shengliang Guan 已提交
71 72 73 74 75 76
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_TIMESTAMP;
      strcpy(pSchema->name, "ts");
    }

    {
S
Shengliang Guan 已提交
77
      SSchema* pSchema = &pReq->pSchemas[1];
S
Shengliang Guan 已提交
78 79 80 81 82 83
      pSchema->bytes = htonl(4);
      pSchema->type = TSDB_DATA_TYPE_INT;
      strcpy(pSchema->name, "col1");
    }

    {
S
Shengliang Guan 已提交
84
      SSchema* pSchema = &pReq->pSchemas[2];
S
Shengliang Guan 已提交
85 86 87 88 89 90
      pSchema->bytes = htonl(2);
      pSchema->type = TSDB_DATA_TYPE_TINYINT;
      strcpy(pSchema->name, "tag1");
    }

    {
S
Shengliang Guan 已提交
91
      SSchema* pSchema = &pReq->pSchemas[3];
S
Shengliang Guan 已提交
92 93 94 95 96 97
      pSchema->bytes = htonl(8);
      pSchema->type = TSDB_DATA_TYPE_BIGINT;
      strcpy(pSchema->name, "tag2");
    }

    {
S
Shengliang Guan 已提交
98
      SSchema* pSchema = &pReq->pSchemas[4];
S
Shengliang Guan 已提交
99 100 101 102 103
      pSchema->bytes = htonl(16);
      pSchema->type = TSDB_DATA_TYPE_BINARY;
      strcpy(pSchema->name, "tag3");
    }

S
Shengliang Guan 已提交
104 105 106
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_STB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
107 108
  }

S
Shengliang Guan 已提交
109
  test.SendShowMetaReq(TSDB_MGMT_TABLE_STB, "1.d1");
S
Shengliang Guan 已提交
110 111 112 113 114 115
  CHECK_META("show stables", 4);

  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_INT, 4, "columns");
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_INT, 4, "tags");
S
Shengliang Guan 已提交
116

S
Shengliang Guan 已提交
117
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
118
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
119
  CheckBinary("stb", TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
120
  CheckTimestamp();
S
Shengliang Guan 已提交
121 122
  CheckInt32(2);
  CheckInt32(3);
S
Shengliang Guan 已提交
123

S
Shengliang Guan 已提交
124
  // ----- meta ------
S
Shengliang Guan 已提交
125
  {
S
Shengliang Guan 已提交
126
    int32_t contLen = sizeof(STableInfoReq);
S
Shengliang Guan 已提交
127

S
Shengliang Guan 已提交
128
    STableInfoReq* pReq = (STableInfoReq*)rpcMallocCont(contLen);
D
dapan1121 已提交
129 130
    strcpy(pReq->dbFName, "1.d1");
    strcpy(pReq->tbName, "stb");
S
Shengliang Guan 已提交
131

S
Shengliang Guan 已提交
132
    SRpcMsg* pMsg = test.SendReq(TDMT_MND_STB_META, pReq, contLen);
S
Shengliang Guan 已提交
133 134 135
    ASSERT_NE(pMsg, nullptr);
    ASSERT_EQ(pMsg->code, 0);

S
Shengliang Guan 已提交
136
    STableMetaRsp* pRsp = (STableMetaRsp*)pMsg->pCont;
S
Shengliang Guan 已提交
137 138 139 140
    pRsp->numOfTags = htonl(pRsp->numOfTags);
    pRsp->numOfColumns = htonl(pRsp->numOfColumns);
    pRsp->sversion = htonl(pRsp->sversion);
    pRsp->tversion = htonl(pRsp->tversion);
D
dapan1121 已提交
141 142 143
    pRsp->suid = be64toh(pRsp->suid);
    pRsp->tuid = be64toh(pRsp->tuid);
    pRsp->vgId = be64toh(pRsp->vgId);
S
Shengliang Guan 已提交
144 145 146 147 148 149
    for (int32_t i = 0; i < pRsp->numOfTags + pRsp->numOfColumns; ++i) {
      SSchema* pSchema = &pRsp->pSchema[i];
      pSchema->colId = htonl(pSchema->colId);
      pSchema->bytes = htonl(pSchema->bytes);
    }

D
dapan 已提交
150 151
    EXPECT_STREQ(pRsp->dbFName, "1.d1");
    EXPECT_STREQ(pRsp->tbName, "stb");
D
dapan1121 已提交
152
    EXPECT_STREQ(pRsp->stbName, "stb");
S
Shengliang Guan 已提交
153 154 155 156 157 158 159 160
    EXPECT_EQ(pRsp->numOfColumns, 2);
    EXPECT_EQ(pRsp->numOfTags, 3);
    EXPECT_EQ(pRsp->precision, TSDB_TIME_PRECISION_MILLI);
    EXPECT_EQ(pRsp->tableType, TSDB_SUPER_TABLE);
    EXPECT_EQ(pRsp->update, 0);
    EXPECT_EQ(pRsp->sversion, 1);
    EXPECT_EQ(pRsp->tversion, 0);
    EXPECT_GT(pRsp->suid, 0);
D
dapan1121 已提交
161
    EXPECT_GT(pRsp->tuid, 0);
S
Shengliang Guan 已提交
162 163 164 165 166
    EXPECT_EQ(pRsp->vgId, 0);

    {
      SSchema* pSchema = &pRsp->pSchema[0];
      EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_TIMESTAMP);
S
Shengliang Guan 已提交
167
      EXPECT_EQ(pSchema->colId, 1);
S
Shengliang Guan 已提交
168 169 170
      EXPECT_EQ(pSchema->bytes, 8);
      EXPECT_STREQ(pSchema->name, "ts");
    }
S
Shengliang Guan 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    {
      SSchema* pSchema = &pRsp->pSchema[1];
      EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_INT);
      EXPECT_EQ(pSchema->colId, 2);
      EXPECT_EQ(pSchema->bytes, 4);
      EXPECT_STREQ(pSchema->name, "col1");
    }

    {
      SSchema* pSchema = &pRsp->pSchema[2];
      EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_TINYINT);
      EXPECT_EQ(pSchema->colId, 3);
      EXPECT_EQ(pSchema->bytes, 2);
      EXPECT_STREQ(pSchema->name, "tag1");
    }

    {
      SSchema* pSchema = &pRsp->pSchema[3];
      EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_BIGINT);
      EXPECT_EQ(pSchema->colId, 4);
      EXPECT_EQ(pSchema->bytes, 8);
      EXPECT_STREQ(pSchema->name, "tag2");
    }

    {
      SSchema* pSchema = &pRsp->pSchema[4];
      EXPECT_EQ(pSchema->type, TSDB_DATA_TYPE_BINARY);
      EXPECT_EQ(pSchema->colId, 5);
      EXPECT_EQ(pSchema->bytes, 16);
      EXPECT_STREQ(pSchema->name, "tag3");
    }
S
Shengliang Guan 已提交
203
  }
S
Shengliang Guan 已提交
204 205

  // restart
S
Shengliang Guan 已提交
206
  test.Restart();
S
Shengliang Guan 已提交
207

S
Shengliang Guan 已提交
208
  test.SendShowMetaReq(TSDB_MGMT_TABLE_STB, "1.d1");
S
Shengliang Guan 已提交
209
  CHECK_META("show stables", 4);
S
Shengliang Guan 已提交
210
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
211
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang Guan 已提交
212

S
Shengliang Guan 已提交
213
  CheckBinary("stb", TSDB_TABLE_NAME_LEN);
S
Shengliang Guan 已提交
214
  CheckTimestamp();
S
Shengliang Guan 已提交
215 216
  CheckInt32(2);
  CheckInt32(3);
S
Shengliang Guan 已提交
217 218

  {
S
Shengliang Guan 已提交
219
    int32_t contLen = sizeof(SMDropStbReq);
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221
    SMDropStbReq* pReq = (SMDropStbReq*)rpcMallocCont(contLen);
S
Shengliang Guan 已提交
222
    strcpy(pReq->name, "1.d1.stb");
S
Shengliang Guan 已提交
223

S
Shengliang Guan 已提交
224 225 226
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_STB, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
S
Shengliang Guan 已提交
227 228
  }

S
Shengliang Guan 已提交
229
  test.SendShowMetaReq(TSDB_MGMT_TABLE_STB, "1.d1");
S
Shengliang Guan 已提交
230
  CHECK_META("show stables", 4);
S
Shengliang Guan 已提交
231
  test.SendShowRetrieveReq();
S
Shengliang Guan 已提交
232
  EXPECT_EQ(test.GetShowRows(), 0);
S
Shengliang Guan 已提交
233
}