func.cpp 17.8 KB
Newer Older
S
Shengliang 已提交
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 36 37 38 39 40 41 42 43 44 45
/**
 * @file func.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief MNODE module func tests
 * @version 1.0
 * @date 2022-01-24
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "sut.h"

class MndTestFunc : public ::testing::Test {
 protected:
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_func", 9038); }
  static void TearDownTestSuite() { test.Cleanup(); }

  static Testbase test;

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

Testbase MndTestFunc::test;

TEST_F(MndTestFunc, 01_Show_Func) {
  test.SendShowMetaReq(TSDB_MGMT_TABLE_FUNC, "");
  CHECK_META("show functions", 7);

  CHECK_SCHEMA(0, TSDB_DATA_TYPE_BINARY, TSDB_FUNC_NAME_LEN + VARSTR_HEADER_SIZE, "name");
  CHECK_SCHEMA(1, TSDB_DATA_TYPE_BINARY, PATH_MAX + VARSTR_HEADER_SIZE, "comment");
  CHECK_SCHEMA(2, TSDB_DATA_TYPE_INT, 4, "aggregate");
  CHECK_SCHEMA(3, TSDB_DATA_TYPE_BINARY, TSDB_TYPE_STR_MAX_LEN + VARSTR_HEADER_SIZE, "outputtype");
  CHECK_SCHEMA(4, TSDB_DATA_TYPE_TIMESTAMP, 8, "create_time");
  CHECK_SCHEMA(5, TSDB_DATA_TYPE_INT, 4, "code_len");
  CHECK_SCHEMA(6, TSDB_DATA_TYPE_INT, 4, "bufsize");

  test.SendShowRetrieveReq();
  EXPECT_EQ(test.GetShowRows(), 0);
}

TEST_F(MndTestFunc, 02_Create_Func) {
  {
S
Shengliang Guan 已提交
46 47 48 49 50 51
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "");

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
52 53 54 55 56 57 58

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME);
  }

  {
S
Shengliang Guan 已提交
59 60 61 62 63 64
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
65 66 67 68 69 70 71

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_COMMENT);
  }

  {
S
Shengliang Guan 已提交
72 73 74 75 76 77 78
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN + 1;

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
79 80 81 82 83 84 85

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_COMMENT);
  }

  {
S
Shengliang Guan 已提交
86 87 88 89 90 91 92
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN;

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
93 94 95 96 97 98 99

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
  }

  {
S
Shengliang Guan 已提交
100 101 102 103 104 105 106 107
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
    createReq.codeSize = TSDB_FUNC_CODE_LEN + 1;

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
108 109 110 111 112 113 114

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
  }

  {
S
Shengliang Guan 已提交
115 116 117 118 119 120 121 122
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
    createReq.codeSize = TSDB_FUNC_CODE_LEN;

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
123 124 125 126 127 128 129

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
  }

  {
S
Shengliang Guan 已提交
130 131 132 133 134 135 136 137 138
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
    createReq.codeSize = TSDB_FUNC_CODE_LEN;
    createReq.pCode[0] = 'a';

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
S
Shengliang 已提交
139 140 141 142 143 144 145

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE);
  }

  {
S
Shengliang Guan 已提交
146 147 148 149 150 151 152 153 154 155 156
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
    createReq.codeSize = TSDB_FUNC_CODE_LEN;
    createReq.pCode[0] = 'a';
    createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;

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

S
Shengliang 已提交
157 158 159 160 161 162
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE);
  }

  for (int32_t i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
    createReq.commentSize = TSDB_FUNC_COMMENT_LEN;
    createReq.codeSize = TSDB_FUNC_CODE_LEN;
    createReq.pCode[0] = 'a';
    createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;
    createReq.igExists = 0;
    if (i == 2) createReq.igExists = 1;
    createReq.funcType = 1;
    createReq.scriptType = 2;
    createReq.outputType = TSDB_DATA_TYPE_SMALLINT;
    createReq.outputLen = 12;
    createReq.bufSize = 4;
    createReq.signature = 5;
    for (int32_t i = 0; i < createReq.commentSize - 1; ++i) {
      createReq.pComment[i] = 'm';
S
Shengliang 已提交
179
    }
S
Shengliang Guan 已提交
180 181
    for (int32_t i = 0; i < createReq.codeSize - 1; ++i) {
      createReq.pCode[i] = 'd';
S
Shengliang 已提交
182 183
    }

S
Shengliang Guan 已提交
184 185 186 187
    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);

S
Shengliang 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    if (i == 0 || i == 2) {
      ASSERT_EQ(pRsp->code, 0);
    } else {
      ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_ALREADY_EXIST);
    }
  }

  test.SendShowMetaReq(TSDB_MGMT_TABLE_FUNC, "");
  CHECK_META("show functions", 7);

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

  CheckBinary("f1", TSDB_FUNC_NAME_LEN);
  CheckBinaryByte('m', TSDB_FUNC_COMMENT_LEN);
  CheckInt32(0);
  CheckBinary("SMALLINT", TSDB_TYPE_STR_MAX_LEN);
  CheckTimestamp();
  CheckInt32(TSDB_FUNC_CODE_LEN);
  CheckInt32(4);
}

TEST_F(MndTestFunc, 03_Retrieve_Func) {
  {
S
Shengliang Guan 已提交
214 215 216 217
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f1");
S
Shengliang 已提交
218

S
Shengliang Guan 已提交
219 220 221 222
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
223 224 225 226 227

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
228 229 230 231
    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
S
Shengliang 已提交
232

S
Shengliang Guan 已提交
233
    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
234 235 236 237 238 239 240 241 242 243 244

    EXPECT_STREQ(pFuncInfo->name, "f1");
    EXPECT_EQ(pFuncInfo->funcType, 1);
    EXPECT_EQ(pFuncInfo->scriptType, 2);
    EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_SMALLINT);
    EXPECT_EQ(pFuncInfo->outputLen, 12);
    EXPECT_EQ(pFuncInfo->bufSize, 4);
    EXPECT_EQ(pFuncInfo->signature, 5);
    EXPECT_EQ(pFuncInfo->commentSize, TSDB_FUNC_COMMENT_LEN);
    EXPECT_EQ(pFuncInfo->codeSize, TSDB_FUNC_CODE_LEN);

S
Shengliang Guan 已提交
245
    char comments[TSDB_FUNC_COMMENT_LEN] = {0};
S
Shengliang 已提交
246 247 248 249 250 251 252
    for (int32_t i = 0; i < TSDB_FUNC_COMMENT_LEN - 1; ++i) {
      comments[i] = 'm';
    }
    char codes[TSDB_FUNC_CODE_LEN] = {0};
    for (int32_t i = 0; i < TSDB_FUNC_CODE_LEN - 1; ++i) {
      codes[i] = 'd';
    }
S
Shengliang Guan 已提交
253 254 255
    EXPECT_STREQ(comments, pFuncInfo->pComment);
    EXPECT_STREQ(codes, pFuncInfo->pCode);
    taosArrayDestroy(retrieveRsp.pFuncInfos);
S
Shengliang 已提交
256 257 258
  }

  {
S
Shengliang Guan 已提交
259 260 261
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 0;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
S
Shengliang 已提交
262

S
Shengliang Guan 已提交
263 264 265 266
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
267 268 269 270 271 272 273

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_RETRIEVE);
  }

  {
S
Shengliang Guan 已提交
274 275 276 277 278 279
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = TSDB_FUNC_MAX_RETRIEVE + 1;
    retrieveReq.pFuncNames = taosArrayInit(TSDB_FUNC_MAX_RETRIEVE + 1, TSDB_FUNC_NAME_LEN);
    for (int32_t i = 0; i < TSDB_FUNC_MAX_RETRIEVE + 1; ++i) {
      taosArrayPush(retrieveReq.pFuncNames, "1");
    }
S
Shengliang 已提交
280

S
Shengliang Guan 已提交
281 282 283 284
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
285 286 287 288 289 290 291

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_RETRIEVE);
  }

  {
S
Shengliang Guan 已提交
292 293 294 295
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
296

S
Shengliang Guan 已提交
297 298 299 300
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
301 302 303 304 305 306 307

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC);
  }

  {
S
Shengliang Guan 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f2");
    createReq.commentSize = 1024;
    createReq.codeSize = 9527;
    createReq.igExists = 1;
    createReq.funcType = 2;
    createReq.scriptType = 3;
    createReq.outputType = TSDB_DATA_TYPE_BINARY;
    createReq.outputLen = 24;
    createReq.bufSize = 6;
    createReq.signature = 18;
    for (int32_t i = 0; i < createReq.commentSize - 1; ++i) {
      createReq.pComment[i] = 'p';
S
Shengliang 已提交
321
    }
S
Shengliang Guan 已提交
322 323
    for (int32_t i = 0; i < createReq.codeSize - 1; ++i) {
      createReq.pCode[i] = 'q';
S
Shengliang 已提交
324 325
    }

S
Shengliang Guan 已提交
326 327 328 329
    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);

S
Shengliang 已提交
330 331 332 333 334 335 336 337 338 339 340 341
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    test.SendShowMetaReq(TSDB_MGMT_TABLE_FUNC, "");
    CHECK_META("show functions", 7);

    test.SendShowRetrieveReq();
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
342 343 344 345
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
346

S
Shengliang Guan 已提交
347 348 349 350
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
351 352 353 354 355

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
356 357 358 359
    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
S
Shengliang 已提交
360

S
Shengliang Guan 已提交
361
    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
362 363 364 365 366 367 368 369 370 371 372

    EXPECT_STREQ(pFuncInfo->name, "f2");
    EXPECT_EQ(pFuncInfo->funcType, 2);
    EXPECT_EQ(pFuncInfo->scriptType, 3);
    EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_BINARY);
    EXPECT_EQ(pFuncInfo->outputLen, 24);
    EXPECT_EQ(pFuncInfo->bufSize, 6);
    EXPECT_EQ(pFuncInfo->signature, 18);
    EXPECT_EQ(pFuncInfo->commentSize, 1024);
    EXPECT_EQ(pFuncInfo->codeSize, 9527);

S
Shengliang Guan 已提交
373
    char comments[TSDB_FUNC_COMMENT_LEN] = {0};
S
Shengliang 已提交
374 375 376
    for (int32_t i = 0; i < 1024 - 1; ++i) {
      comments[i] = 'p';
    }
S
Shengliang Guan 已提交
377
    char codes[TSDB_FUNC_CODE_LEN] = {0};
S
Shengliang 已提交
378 379 380
    for (int32_t i = 0; i < 9527 - 1; ++i) {
      codes[i] = 'q';
    }
S
Shengliang Guan 已提交
381 382 383 384

    EXPECT_STREQ(comments, pFuncInfo->pComment);
    EXPECT_STREQ(codes, pFuncInfo->pCode);
    taosArrayDestroy(retrieveRsp.pFuncInfos);
S
Shengliang 已提交
385 386 387
  }

  {
S
Shengliang Guan 已提交
388 389 390 391 392
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 2;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
    taosArrayPush(retrieveReq.pFuncNames, "f1");
S
Shengliang 已提交
393

S
Shengliang Guan 已提交
394 395 396 397
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
398 399 400 401 402

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
403 404 405 406
    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 2);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
S
Shengliang 已提交
407 408

    {
S
Shengliang Guan 已提交
409
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
410 411 412 413 414 415 416 417 418 419
      EXPECT_STREQ(pFuncInfo->name, "f2");
      EXPECT_EQ(pFuncInfo->funcType, 2);
      EXPECT_EQ(pFuncInfo->scriptType, 3);
      EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_BINARY);
      EXPECT_EQ(pFuncInfo->outputLen, 24);
      EXPECT_EQ(pFuncInfo->bufSize, 6);
      EXPECT_EQ(pFuncInfo->signature, 18);
      EXPECT_EQ(pFuncInfo->commentSize, 1024);
      EXPECT_EQ(pFuncInfo->codeSize, 9527);

S
Shengliang Guan 已提交
420
      char comments[TSDB_FUNC_COMMENT_LEN] = {0};
S
Shengliang 已提交
421 422 423
      for (int32_t i = 0; i < 1024 - 1; ++i) {
        comments[i] = 'p';
      }
S
Shengliang Guan 已提交
424
      char codes[TSDB_FUNC_CODE_LEN] = {0};
S
Shengliang 已提交
425 426 427
      for (int32_t i = 0; i < 9527 - 1; ++i) {
        codes[i] = 'q';
      }
S
Shengliang Guan 已提交
428 429 430

      EXPECT_STREQ(comments, pFuncInfo->pComment);
      EXPECT_STREQ(codes, pFuncInfo->pCode);
S
Shengliang 已提交
431 432 433
    }

    {
S
Shengliang Guan 已提交
434
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 1);
S
Shengliang 已提交
435 436 437 438 439 440 441 442 443 444
      EXPECT_STREQ(pFuncInfo->name, "f1");
      EXPECT_EQ(pFuncInfo->funcType, 1);
      EXPECT_EQ(pFuncInfo->scriptType, 2);
      EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_SMALLINT);
      EXPECT_EQ(pFuncInfo->outputLen, 12);
      EXPECT_EQ(pFuncInfo->bufSize, 4);
      EXPECT_EQ(pFuncInfo->signature, 5);
      EXPECT_EQ(pFuncInfo->commentSize, TSDB_FUNC_COMMENT_LEN);
      EXPECT_EQ(pFuncInfo->codeSize, TSDB_FUNC_CODE_LEN);

S
Shengliang Guan 已提交
445
      char comments[TSDB_FUNC_COMMENT_LEN] = {0};
S
Shengliang 已提交
446 447 448 449 450 451 452
      for (int32_t i = 0; i < TSDB_FUNC_COMMENT_LEN - 1; ++i) {
        comments[i] = 'm';
      }
      char codes[TSDB_FUNC_CODE_LEN] = {0};
      for (int32_t i = 0; i < TSDB_FUNC_CODE_LEN - 1; ++i) {
        codes[i] = 'd';
      }
S
Shengliang Guan 已提交
453 454
      EXPECT_STREQ(comments, pFuncInfo->pComment);
      EXPECT_STREQ(codes, pFuncInfo->pCode);
S
Shengliang 已提交
455
    }
S
Shengliang Guan 已提交
456
    taosArrayDestroy(retrieveRsp.pFuncInfos);
S
Shengliang 已提交
457 458
  }

S
Shengliang Guan 已提交
459 460 461 462 463 464
  {
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 2;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
    taosArrayPush(retrieveReq.pFuncNames, "f3");
S
Shengliang 已提交
465

S
Shengliang Guan 已提交
466 467 468 469
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    taosArrayDestroy(retrieveReq.pFuncNames);
S
Shengliang 已提交
470 471 472 473

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC);
S
Shengliang Guan 已提交
474
  }
S
Shengliang 已提交
475
}
S
Shengliang 已提交
476 477 478

TEST_F(MndTestFunc, 04_Drop_Func) {
  {
S
Shengliang Guan 已提交
479 480
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "");
S
Shengliang 已提交
481

S
Shengliang Guan 已提交
482 483 484
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
485 486 487

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
S
Shengliang 已提交
488
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME);
S
Shengliang 已提交
489 490 491
  }

  {
S
Shengliang Guan 已提交
492 493
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
S
Shengliang 已提交
494

S
Shengliang Guan 已提交
495 496 497
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
498 499 500 501 502 503 504

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_NOT_EXIST);
  }

  {
S
Shengliang Guan 已提交
505 506 507
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
508

S
Shengliang Guan 已提交
509 510 511
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
512 513 514 515 516 517 518

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
S
Shengliang Guan 已提交
519 520 521
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f1");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
522

S
Shengliang Guan 已提交
523 524 525
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
526 527 528 529 530 531 532

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  test.SendShowMetaReq(TSDB_MGMT_TABLE_FUNC, "");
S
Shengliang 已提交
533
  CHECK_META("show functions", 7);
S
Shengliang 已提交
534 535

  test.SendShowRetrieveReq();
S
Shengliang 已提交
536
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang 已提交
537 538 539 540 541

  // restart
  test.Restart();

  test.SendShowMetaReq(TSDB_MGMT_TABLE_FUNC, "");
S
Shengliang 已提交
542
  CHECK_META("show functions", 7);
S
Shengliang 已提交
543 544

  test.SendShowRetrieveReq();
S
Shengliang 已提交
545
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang 已提交
546

S
Shengliang Guan 已提交
547
  CheckBinary("f2", TSDB_FUNC_NAME_LEN);
S
Shengliang 已提交
548
}