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

#include <gtest/gtest.h>
S
Shengliang Guan 已提交
13
#include "tlog.h"
S
Shengliang Guan 已提交
14
#include "tprocess.h"
S
Shengliang Guan 已提交
15
#include "tqueue.h"
S
Shengliang Guan 已提交
16 17 18 19 20 21 22 23 24 25 26

typedef struct STestMsg {
  uint16_t msgType;
  void    *pCont;
  int      contLen;
  int32_t  code;
  void    *handle;         // rpc handle returned to app
  void    *ahandle;        // app handle set by client
  int      noResp;         // has response or not(default 0, 0: resp, 1: no resp);
  int      persistHandle;  // persist handle or not
} STestMsg;
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28
class UtilTesProc : public ::testing::Test {
S
Shengliang Guan 已提交
29
 public:
S
Shengliang Guan 已提交
30 31
  void SetUp() override {
    shm.id = -1;
S
Shengliang Guan 已提交
32 33 34 35 36 37 38 39
    for (int32_t i = 0; i < 4000; ++i) {
      body[i] = i % 26 + 'a';
    }
    head.pCont = body;
    head.code = 1;
    head.msgType = 2;
    head.noResp = 3;
    head.persistHandle = 4;
S
Shengliang Guan 已提交
40 41 42 43 44 45 46

    taosRemoveDir("/tmp/td");
    taosMkDir("/tmp/td");
    tstrncpy(tsLogDir, "/tmp/td", PATH_MAX);
    if (taosInitLog("taosdlog", 1) != 0) {
      printf("failed to init log file\n");
    }
S
Shengliang Guan 已提交
47
  }
S
Shengliang Guan 已提交
48
  void TearDown() override { taosDropShm(&shm); }
S
Shengliang Guan 已提交
49 50

 public:
S
Shengliang Guan 已提交
51
  static STestMsg head;
S
Shengliang Guan 已提交
52
  static char     body[4000];
S
Shengliang Guan 已提交
53 54 55
  static SShm     shm;
  static void     SetUpTestSuite() {}
  static void     TearDownTestSuite() {}
S
Shengliang Guan 已提交
56
};
S
Shengliang Guan 已提交
57 58

SShm     UtilTesProc::shm;
S
Shengliang Guan 已提交
59
char     UtilTesProc::body[4000];
S
Shengliang Guan 已提交
60
STestMsg UtilTesProc::head;
S
Shengliang Guan 已提交
61

S
Shengliang Guan 已提交
62
TEST_F(UtilTesProc, 00_Init_Cleanup) {
S
Shengliang Guan 已提交
63 64 65 66 67 68
  ASSERT_EQ(taosCreateShm(&shm, 1234, 1024 * 1024 * 2), 0);

  shm.size = 1023;
  SProcCfg  cfg = {.childConsumeFp = (ProcConsumeFp)NULL,
                   .childMallocHeadFp = (ProcMallocFp)taosAllocateQitem,
                   .childFreeHeadFp = (ProcFreeFp)taosFreeQitem,
S
Shengliang Guan 已提交
69 70
                   .childMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .childFreeBodyFp = (ProcFreeFp)taosMemoryMalloc,
S
Shengliang Guan 已提交
71 72 73
                   .parentConsumeFp = (ProcConsumeFp)NULL,
                   .parentMallocHeadFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeHeadFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
74 75
                   .parentMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeBodyFp = (ProcFreeFp)taosMemoryMalloc,
S
Shengliang Guan 已提交
76 77 78 79 80 81 82 83 84 85 86
                   .shm = shm,
                   .parent = &shm,
                   .name = "1234"};
  SProcObj *proc = taosProcInit(&cfg);
  ASSERT_EQ(proc, nullptr);

  shm.size = 2468;
  cfg.shm = shm;
  proc = taosProcInit(&cfg);
  ASSERT_NE(proc, nullptr);

S
Shengliang Guan 已提交
87 88 89 90 91 92
  ASSERT_EQ(taosProcRun(proc), 0);
  taosProcCleanup(proc);
  taosDropShm(&shm);
}

void ConsumeChild1(void *parent, void *pHead, int16_t headLen, void *pBody, int32_t bodyLen, ProcFuncType ftype) {
S
Shengliang Guan 已提交
93
  STestMsg msg;
S
Shengliang Guan 已提交
94 95 96 97 98 99
  memcpy(&msg, pHead, headLen);
  char body[2000] = {0};
  memcpy(body, pBody, bodyLen);

  uDebug("====> parent:%" PRId64 " ftype:%d, headLen:%d bodyLen:%d head:%d:%d:%d:%d body:%s <====", (int64_t)parent,
         ftype, headLen, bodyLen, msg.code, msg.msgType, msg.noResp, msg.persistHandle, body);
S
Shengliang Guan 已提交
100
  taosMemoryFree(pBody);
S
Shengliang Guan 已提交
101 102 103 104 105 106 107 108 109
  taosFreeQitem(pHead);
}

TEST_F(UtilTesProc, 01_Push_Pop_Child) {
  shm.size = 3000;
  ASSERT_EQ(taosCreateShm(&shm, 1235, shm.size), 0);
  SProcCfg  cfg = {.childConsumeFp = (ProcConsumeFp)ConsumeChild1,
                   .childMallocHeadFp = (ProcMallocFp)taosAllocateQitem,
                   .childFreeHeadFp = (ProcFreeFp)taosFreeQitem,
S
Shengliang Guan 已提交
110 111
                   .childMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .childFreeBodyFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
112 113 114
                   .parentConsumeFp = (ProcConsumeFp)NULL,
                   .parentMallocHeadFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeHeadFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
115 116
                   .parentMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeBodyFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
117 118
                   .shm = shm,
                   .parent = (void *)((int64_t)1235),
S
Shengliang Guan 已提交
119
                   .name = "1235_c"};
S
Shengliang Guan 已提交
120 121 122 123 124 125 126 127 128
  SProcObj *cproc = taosProcInit(&cfg);
  ASSERT_NE(cproc, nullptr);

  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, PROC_RSP), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, PROC_REGIST), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, PROC_RELEASE), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, NULL, 12, body, 0, 0, PROC_REQ), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, PROC_REQ), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, shm.size, body, 0, 0, PROC_REQ), 0);
S
Shengliang Guan 已提交
129
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, shm.size, 0, PROC_REQ), 0);
S
Shengliang Guan 已提交
130 131 132 133

  for (int32_t j = 0; j < 1000; j++) {
    int32_t i = 0;
    for (i = 0; i < 20; ++i) {
S
Shengliang Guan 已提交
134
      ASSERT_EQ(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, i, 0, PROC_REQ), 0);
S
Shengliang Guan 已提交
135
    }
S
Shengliang Guan 已提交
136
    ASSERT_NE(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, i, 0, PROC_REQ), 0);
S
Shengliang Guan 已提交
137 138

    cfg.isChild = true;
S
Shengliang Guan 已提交
139
    cfg.name = "1235_p";
S
Shengliang Guan 已提交
140 141 142 143 144 145 146 147 148 149 150
    SProcObj *pproc = taosProcInit(&cfg);
    ASSERT_NE(pproc, nullptr);
    taosProcRun(pproc);
    taosProcCleanup(pproc);
  }

  taosProcCleanup(cproc);
  taosDropShm(&shm);
}

void ConsumeParent1(void *parent, void *pHead, int16_t headLen, void *pBody, int32_t bodyLen, ProcFuncType ftype) {
S
Shengliang Guan 已提交
151
  STestMsg msg;
S
Shengliang Guan 已提交
152 153 154 155 156 157
  memcpy(&msg, pHead, headLen);
  char body[2000] = {0};
  memcpy(body, pBody, bodyLen);

  uDebug("----> parent:%" PRId64 " ftype:%d, headLen:%d bodyLen:%d head:%d:%d:%d:%d body:%s <----", (int64_t)parent,
         ftype, headLen, bodyLen, msg.code, msg.msgType, msg.noResp, msg.persistHandle, body);
S
Shengliang Guan 已提交
158
  taosMemoryFree(pBody);
S
Shengliang Guan 已提交
159 160 161 162 163 164 165 166 167
  taosMemoryFree(pHead);
}

TEST_F(UtilTesProc, 02_Push_Pop_Parent) {
  shm.size = 3000;
  ASSERT_EQ(taosCreateShm(&shm, 1236, shm.size), 0);
  SProcCfg  cfg = {.childConsumeFp = (ProcConsumeFp)NULL,
                   .childMallocHeadFp = (ProcMallocFp)taosAllocateQitem,
                   .childFreeHeadFp = (ProcFreeFp)taosFreeQitem,
S
Shengliang Guan 已提交
168 169
                   .childMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .childFreeBodyFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
170 171 172
                   .parentConsumeFp = (ProcConsumeFp)ConsumeParent1,
                   .parentMallocHeadFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeHeadFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
173 174
                   .parentMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeBodyFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
175
                   .shm = shm,
S
Shengliang Guan 已提交
176 177
                   .parent = (void *)((int64_t)1236),
                   .name = "1236_c"};
S
Shengliang Guan 已提交
178 179 180
  SProcObj *cproc = taosProcInit(&cfg);
  ASSERT_NE(cproc, nullptr);

S
Shengliang Guan 已提交
181
  cfg.name = "1236_p";
S
Shengliang Guan 已提交
182 183 184 185 186 187 188
  cfg.isChild = true;
  SProcObj *pproc = taosProcInit(&cfg);
  ASSERT_NE(pproc, nullptr);

  for (int32_t j = 0; j < 1000; j++) {
    int32_t i = 0;
    for (i = 0; i < 20; ++i) {
S
Shengliang Guan 已提交
189
      taosProcPutToParentQ(pproc, &head, sizeof(STestMsg), body, i, PROC_REQ);
S
Shengliang Guan 已提交
190 191 192 193 194 195 196 197
    }

    taosProcRun(cproc);
    taosProcStop(cproc);
  }

  taosProcCleanup(pproc);
  taosProcCleanup(cproc);
S
Shengliang Guan 已提交
198
  taosDropShm(&shm);
S
Shengliang Guan 已提交
199
}
S
Shengliang Guan 已提交
200 201

void ConsumeChild3(void *parent, void *pHead, int16_t headLen, void *pBody, int32_t bodyLen, ProcFuncType ftype) {
S
Shengliang Guan 已提交
202
  STestMsg msg;
S
Shengliang Guan 已提交
203 204 205 206 207 208
  memcpy(&msg, pHead, headLen);
  char body[2000] = {0};
  memcpy(body, pBody, bodyLen);

  uDebug("====> parent:%" PRId64 " ftype:%d, headLen:%d bodyLen:%d handle:%" PRId64 " body:%s <====", (int64_t)parent,
         ftype, headLen, bodyLen, (int64_t)msg.handle, body);
S
Shengliang Guan 已提交
209
  taosMemoryFree(pBody);
S
Shengliang Guan 已提交
210 211 212 213 214 215 216 217 218 219 220 221
  taosFreeQitem(pHead);
}

void processHandle(void *handle) { uDebug("----> remove handle:%" PRId64 " <----", (int64_t)handle); }

TEST_F(UtilTesProc, 03_Handle) {
  // uDebugFlag = 207;
  shm.size = 3000;
  ASSERT_EQ(taosCreateShm(&shm, 1237, shm.size), 0);
  SProcCfg  cfg = {.childConsumeFp = (ProcConsumeFp)ConsumeChild3,
                   .childMallocHeadFp = (ProcMallocFp)taosAllocateQitem,
                   .childFreeHeadFp = (ProcFreeFp)taosFreeQitem,
S
Shengliang Guan 已提交
222 223
                   .childMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .childFreeBodyFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
224 225 226
                   .parentConsumeFp = (ProcConsumeFp)NULL,
                   .parentMallocHeadFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeHeadFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
227 228
                   .parentMallocBodyFp = (ProcMallocFp)taosMemoryMalloc,
                   .parentFreeBodyFp = (ProcFreeFp)taosMemoryFree,
S
Shengliang Guan 已提交
229 230 231 232 233 234 235 236 237 238
                   .shm = shm,
                   .parent = (void *)((int64_t)1235),
                   .name = "1237_p"};
  SProcObj *cproc = taosProcInit(&cfg);
  ASSERT_NE(cproc, nullptr);

  for (int32_t j = 0; j < 1; j++) {
    int32_t i = 0;
    for (i = 0; i < 20; ++i) {
      head.handle = (void *)((int64_t)i);
S
Shengliang Guan 已提交
239
      ASSERT_EQ(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, i, (void *)((int64_t)i), PROC_REQ), 0);
S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    }

    cfg.isChild = true;
    cfg.name = "child_queue";
    SProcObj *pproc = taosProcInit(&cfg);
    ASSERT_NE(pproc, nullptr);
    taosProcRun(pproc);
    taosProcCleanup(pproc);

    taosProcRemoveHandle(cproc, (void *)((int64_t)3));
    taosProcRemoveHandle(cproc, (void *)((int64_t)5));
    taosProcRemoveHandle(cproc, (void *)((int64_t)6));
    taosProcCloseHandles(cproc, processHandle);
  }

  taosProcCleanup(cproc);
  taosDropShm(&shm);
}