procTest.cpp 8.3 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10
/**
 * @file queue.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief UTIL module queue tests
 * @version 1.0
 * @date 2022-01-27
 *
 * @copyright Copyright (c) 2022
 *
 */
11
#if 0
S
Shengliang Guan 已提交
12
#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
    taosRemoveDir(TD_TMP_DIR_PATH "td");
    taosMkDir(TD_TMP_DIR_PATH "td");
    tstrncpy(tsLogDir, TD_TMP_DIR_PATH "td", PATH_MAX);
S
Shengliang Guan 已提交
44 45 46
    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
  SProc *proc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
80 81 82 83
  ASSERT_EQ(proc, nullptr);

  shm.size = 2468;
  cfg.shm = shm;
S
Shengliang Guan 已提交
84
  proc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
85 86
  ASSERT_NE(proc, nullptr);

S
Shengliang Guan 已提交
87 88
  ASSERT_EQ(dmRunProc(proc), 0);
  dmCleanupProc(proc);
S
Shengliang Guan 已提交
89 90 91
  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
  SProc *cproc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
121 122
  ASSERT_NE(cproc, nullptr);

S
Shengliang Guan 已提交
123 124 125 126 127 128 129
  ASSERT_NE(dmPutToProcCQueue(cproc, &head, 0, body, 0, 0, 0, DND_FUNC_RSP), 0);
  ASSERT_NE(dmPutToProcCQueue(cproc, &head, 0, body, 0, 0, 0, DND_FUNC_REGIST), 0);
  ASSERT_NE(dmPutToProcCQueue(cproc, &head, 0, body, 0, 0, 0, DND_FUNC_RELEASE), 0);
  ASSERT_NE(dmPutToProcCQueue(cproc, NULL, 12, body, 0, 0, 0, DND_FUNC_REQ), 0);
  ASSERT_NE(dmPutToProcCQueue(cproc, &head, 0, body, 0, 0, 0, DND_FUNC_REQ), 0);
  ASSERT_NE(dmPutToProcCQueue(cproc, &head, shm.size, body, 0, 0, 0, DND_FUNC_REQ), 0);
  ASSERT_NE(dmPutToProcCQueue(cproc, &head, sizeof(STestMsg), body, shm.size, 0, 0, DND_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) {
S
Shengliang Guan 已提交
134
      ASSERT_EQ(dmPutToProcCQueue(cproc, &head, sizeof(STestMsg), body, i, 0, 0, DND_FUNC_REQ), 0);
S
Shengliang Guan 已提交
135
    }
S
Shengliang Guan 已提交
136
    ASSERT_NE(dmPutToProcCQueue(cproc, &head, sizeof(STestMsg), body, i, 0, 0, DND_FUNC_REQ), 0);
S
Shengliang Guan 已提交
137 138

    cfg.isChild = true;
S
Shengliang Guan 已提交
139
    cfg.name = "1235_p";
S
Shengliang Guan 已提交
140
    SProc *pproc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
141
    ASSERT_NE(pproc, nullptr);
S
Shengliang Guan 已提交
142 143
    dmRunProc(pproc);
    dmCleanupProc(pproc);
S
Shengliang Guan 已提交
144 145
  }

S
Shengliang Guan 已提交
146
  dmCleanupProc(cproc);
S
Shengliang Guan 已提交
147 148 149
  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
  SProc *cproc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
179 180
  ASSERT_NE(cproc, nullptr);

S
Shengliang Guan 已提交
181
  cfg.name = "1236_p";
S
Shengliang Guan 已提交
182
  cfg.isChild = true;
S
Shengliang Guan 已提交
183
  SProc *pproc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
184 185 186 187 188
  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
      dmPutToProcPQueue(pproc, &head, sizeof(STestMsg), body, i, DND_FUNC_REQ);
S
Shengliang Guan 已提交
190 191
    }

S
Shengliang Guan 已提交
192 193
    dmRunProc(cproc);
    dmStopProc(cproc);
S
Shengliang Guan 已提交
194 195
  }

S
Shengliang Guan 已提交
196 197
  dmCleanupProc(pproc);
  dmCleanupProc(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
  SProc *cproc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
233 234 235 236 237 238
  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(dmPutToProcCQueue(cproc, &head, sizeof(STestMsg), body, i, (void *)((int64_t)i), i, DND_FUNC_REQ), 0);
S
Shengliang Guan 已提交
240 241 242 243
    }

    cfg.isChild = true;
    cfg.name = "child_queue";
S
Shengliang Guan 已提交
244
    SProc *pproc = dmInitProc(&cfg);
S
Shengliang Guan 已提交
245
    ASSERT_NE(pproc, nullptr);
S
Shengliang Guan 已提交
246 247
    dmRunProc(pproc);
    dmCleanupProc(pproc);
S
Shengliang Guan 已提交
248

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

S
Shengliang Guan 已提交
260
  dmCleanupProc(cproc);
S
Shengliang Guan 已提交
261 262
  taosDropShm(&shm);
}
263 264

#endif