procTest.cpp 8.4 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
  ASSERT_EQ(taosCreateShm(&shm, 1234, 1024 * 1024 * 2), 0);

  shm.size = 1023;
wafwerar's avatar
wafwerar 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
  SProcCfg  cfg = {(ProcConsumeFp)NULL,
                   (ProcMallocFp)taosAllocateQitem,
                   (ProcFreeFp)taosFreeQitem,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryMalloc,
                   (ProcConsumeFp)NULL,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryMalloc,
                   shm,
                   &shm,
                   "1234"};
S
Shengliang Guan 已提交
79 80 81 82 83 84 85 86
  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
  ASSERT_EQ(taosProcRun(proc), 0);
  taosProcCleanup(proc);
  taosDropShm(&shm);
}

S
Shengliang Guan 已提交
92
void ConsumeChild1(void *parent, void *pHead, int16_t headLen, void *pBody, int32_t bodyLen, EProcFuncType 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
  taosFreeQitem(pHead);
}

TEST_F(UtilTesProc, 01_Push_Pop_Child) {
  shm.size = 3000;
  ASSERT_EQ(taosCreateShm(&shm, 1235, shm.size), 0);
wafwerar's avatar
wafwerar 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
  SProcCfg  cfg = {(ProcConsumeFp)ConsumeChild1,
                   (ProcMallocFp)taosAllocateQitem,
                   (ProcFreeFp)taosFreeQitem,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcConsumeFp)NULL,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   shm,
                   (void *)((int64_t)1235),
                   "1235_c"};
S
Shengliang Guan 已提交
120 121 122
  SProcObj *cproc = taosProcInit(&cfg);
  ASSERT_NE(cproc, nullptr);

123 124 125 126 127 128 129
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, 0, PROC_FUNC_RSP), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, 0, PROC_FUNC_REGIST), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, 0, PROC_FUNC_RELEASE), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, NULL, 12, body, 0, 0, 0, PROC_FUNC_REQ), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, 0, body, 0, 0, 0, PROC_FUNC_REQ), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, shm.size, body, 0, 0, 0, PROC_FUNC_REQ), 0);
  ASSERT_NE(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, shm.size, 0, 0, PROC_FUNC_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) {
134
      ASSERT_EQ(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, i, 0, 0, PROC_FUNC_REQ), 0);
S
Shengliang Guan 已提交
135
    }
136
    ASSERT_NE(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, i, 0, 0, PROC_FUNC_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
    SProcObj *pproc = taosProcInit(&cfg);
    ASSERT_NE(pproc, nullptr);
    taosProcRun(pproc);
    taosProcCleanup(pproc);
  }

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

S
Shengliang Guan 已提交
150
void ConsumeParent1(void *parent, void *pHead, int16_t headLen, void *pBody, int32_t bodyLen, EProcFuncType 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
  taosMemoryFree(pHead);
}

TEST_F(UtilTesProc, 02_Push_Pop_Parent) {
  shm.size = 3000;
  ASSERT_EQ(taosCreateShm(&shm, 1236, shm.size), 0);
wafwerar's avatar
wafwerar 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
  SProcCfg  cfg = {(ProcConsumeFp)NULL,
                   (ProcMallocFp)taosAllocateQitem,
                   (ProcFreeFp)taosFreeQitem,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcConsumeFp)ConsumeParent1,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   shm,
                   (void *)((int64_t)1236),
                   "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_FUNC_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

S
Shengliang Guan 已提交
201
void ConsumeChild3(void *parent, void *pHead, int16_t headLen, void *pBody, int32_t bodyLen, EProcFuncType 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
  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);
wafwerar's avatar
wafwerar 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231
  SProcCfg  cfg = {(ProcConsumeFp)ConsumeChild3,
                   (ProcMallocFp)taosAllocateQitem,
                   (ProcFreeFp)taosFreeQitem,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcConsumeFp)NULL,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   (ProcMallocFp)taosMemoryMalloc,
                   (ProcFreeFp)taosMemoryFree,
                   shm,
                   (void *)((int64_t)1235),
                   "1237_p"};
S
Shengliang Guan 已提交
232 233 234 235 236 237 238
  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);
239
      ASSERT_EQ(taosProcPutToChildQ(cproc, &head, sizeof(STestMsg), body, i, (void *)((int64_t)i), i, PROC_FUNC_REQ), 0);
S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248
    }

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

249 250 251 252 253 254 255 256
    int64_t ref = 0;
    
    ref = taosProcRemoveHandle(cproc, (void *)((int64_t)3));
    EXPECT_EQ(ref, 3);
    ref = taosProcRemoveHandle(cproc, (void *)((int64_t)5));
    EXPECT_EQ(ref, 5);
    ref = taosProcRemoveHandle(cproc, (void *)((int64_t)6));
    EXPECT_EQ(ref, 6);
S
Shengliang Guan 已提交
257 258 259 260 261 262
    taosProcCloseHandles(cproc, processHandle);
  }

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