qworkerTests.cpp 32.1 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>
#include <iostream>

S
Shengliang Guan 已提交
19 20
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
D
dapan1121 已提交
21 22 23
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
S
Shengliang Guan 已提交
24 25 26 27
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#pragma GCC diagnostic ignored "-Wpointer-arith"
28
#include <addr_any.h>
S
Shengliang Guan 已提交
29

wafwerar's avatar
wafwerar 已提交
30 31 32
#ifdef WINDOWS
#define TD_USE_WINSOCK
#endif
D
dapan1121 已提交
33 34
#include "os.h"

35
#include "tglobal.h"
H
Haojun Liao 已提交
36 37 38
#include "taos.h"
#include "tdef.h"
#include "tvariant.h"
H
Haojun Liao 已提交
39
#include "tdatablock.h"
D
dapan1121 已提交
40
#include "trpc.h"
D
dapan 已提交
41 42
#include "planner.h"
#include "qworker.h"
D
dapan1121 已提交
43
#include "stub.h"
D
dapan1121 已提交
44 45
#include "executor.h"
#include "dataSinkMgt.h"
D
dapan1121 已提交
46 47 48 49


namespace {

D
dapan1121 已提交
50 51 52
#define qwtTestQueryQueueSize 1000000
#define qwtTestFetchQueueSize 1000000

D
dapan1121 已提交
53 54
bool qwtEnableLog = true;

D
dapan1121 已提交
55 56
int32_t qwtTestMaxExecTaskUsec = 2;
int32_t qwtTestReqMaxDelayUsec = 2;
D
dapan1121 已提交
57

wafwerar's avatar
wafwerar 已提交
58
int64_t qwtTestQueryId = 0;
D
dapan1121 已提交
59 60
bool qwtTestEnableSleep = true;
bool qwtTestStop = false;
D
dapan1121 已提交
61
bool qwtTestDeadLoop = false;
D
dapan1121 已提交
62
int32_t qwtTestMTRunSec = 2;
D
dapan1121 已提交
63 64 65
int32_t qwtTestPrintNum = 10000;
uint64_t qwtTestCaseIdx = 0;
uint64_t qwtTestCaseNum = 4;
D
dapan1121 已提交
66 67 68
bool qwtTestCaseFinished = false;
tsem_t qwtTestQuerySem;
tsem_t qwtTestFetchSem;
D
dapan1121 已提交
69 70
int32_t qwtTestQuitThreadNum = 0;

D
dapan1121 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

int32_t qwtTestQueryQueueRIdx = 0;
int32_t qwtTestQueryQueueWIdx = 0;
int32_t qwtTestQueryQueueNum = 0;
SRWLatch qwtTestQueryQueueLock = 0;
struct SRpcMsg *qwtTestQueryQueue[qwtTestQueryQueueSize] = {0};

int32_t qwtTestFetchQueueRIdx = 0;
int32_t qwtTestFetchQueueWIdx = 0;
int32_t qwtTestFetchQueueNum = 0;
SRWLatch qwtTestFetchQueueLock = 0;
struct SRpcMsg *qwtTestFetchQueue[qwtTestFetchQueueSize] = {0};


int32_t qwtTestSinkBlockNum = 0;
int32_t qwtTestSinkMaxBlockNum = 0;
bool qwtTestSinkQueryEnd = false;
SRWLatch qwtTestSinkLock = 0;
D
dapan1121 已提交
89
int32_t qwtTestSinkLastLen = 0;
D
dapan1121 已提交
90 91


D
dapan1121 已提交
92
SSubQueryMsg qwtqueryMsg = {0};
D
dapan1121 已提交
93 94 95 96 97 98
SRpcMsg qwtfetchRpc = {0};
SResFetchReq qwtfetchMsg = {0};
SRpcMsg qwtreadyRpc = {0};
SResReadyReq qwtreadyMsg = {0};
SRpcMsg qwtdropRpc = {0};
STaskDropReq qwtdropMsg = {0};  
D
dapan1121 已提交
99
SSchTasksStatusReq qwtstatusMsg = {0};
D
dapan1121 已提交
100

D
dapan1121 已提交
101 102

void qwtInitLogFile() {
D
dapan1121 已提交
103 104 105
  if (!qwtEnableLog) {
    return;
  }
D
dapan1121 已提交
106 107 108 109 110
  const char    *defaultLogFileNamePrefix = "taosdlog";
  const int32_t  maxLogFileNum = 10;

  tsAsyncLog = 0;
  qDebugFlag = 159;
wafwerar's avatar
wafwerar 已提交
111
  strcpy(tsLogDir, TD_LOG_DIR_PATH);
D
dapan1121 已提交
112

S
Shengliang Guan 已提交
113
  if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) {
S
os env  
Shengliang Guan 已提交
114
    printf("failed to open log file in directory:%s\n", tsLogDir);
D
dapan1121 已提交
115 116 117 118 119
  }

}

void qwtBuildQueryReqMsg(SRpcMsg *queryRpc) {
D
dapan1121 已提交
120 121 122
  qwtqueryMsg.queryId = htobe64(atomic_add_fetch_64(&qwtTestQueryId, 1));
  qwtqueryMsg.sId = htobe64(1);
  qwtqueryMsg.taskId = htobe64(1);
123 124
  qwtqueryMsg.phyLen = htonl(100);
  qwtqueryMsg.sqlLen = 0;
D
dapan1121 已提交
125
  queryRpc->msgType = TDMT_SCH_QUERY;
D
dapan1121 已提交
126
  queryRpc->pCont = &qwtqueryMsg;
D
dapan1121 已提交
127 128 129 130 131
  queryRpc->contLen = sizeof(SSubQueryMsg) + 100;
}

void qwtBuildFetchReqMsg(SResFetchReq *fetchMsg, SRpcMsg *fetchRpc) {
  fetchMsg->sId = htobe64(1);
D
dapan1121 已提交
132
  fetchMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
133
  fetchMsg->taskId = htobe64(1);
D
dapan1121 已提交
134
  fetchRpc->msgType = TDMT_SCH_FETCH;
D
dapan1121 已提交
135 136 137 138 139 140
  fetchRpc->pCont = fetchMsg;
  fetchRpc->contLen = sizeof(SResFetchReq);
}

void qwtBuildDropReqMsg(STaskDropReq *dropMsg, SRpcMsg *dropRpc) {
  dropMsg->sId = htobe64(1);
D
dapan1121 已提交
141
  dropMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
142
  dropMsg->taskId = htobe64(1);
D
dapan1121 已提交
143
  dropRpc->msgType = TDMT_SCH_DROP_TASK;
D
dapan1121 已提交
144 145 146 147
  dropRpc->pCont = dropMsg;
  dropRpc->contLen = sizeof(STaskDropReq);
}

D
dapan 已提交
148
int32_t qwtStringToPlan(const char* str, SSubplan** subplan) {
D
dapan 已提交
149
  *subplan = (SSubplan *)0x1;
D
dapan 已提交
150
  return 0;
D
dapan1121 已提交
151 152
}

D
dapan1121 已提交
153 154
int32_t qwtPutReqToFetchQueue(void *node, struct SRpcMsg *pMsg) {
  taosWLockLatch(&qwtTestFetchQueueLock);
wafwerar's avatar
wafwerar 已提交
155
  struct SRpcMsg *newMsg = (struct SRpcMsg *)taosMemoryCalloc(1, sizeof(struct SRpcMsg));
D
dapan1121 已提交
156 157
  memcpy(newMsg, pMsg, sizeof(struct SRpcMsg));  
  qwtTestFetchQueue[qwtTestFetchQueueWIdx++] = newMsg;
D
dapan1121 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  if (qwtTestFetchQueueWIdx >= qwtTestFetchQueueSize) {
    qwtTestFetchQueueWIdx = 0;
  }
  
  qwtTestFetchQueueNum++;

  if (qwtTestFetchQueueWIdx == qwtTestFetchQueueRIdx) {
    printf("Fetch queue is full");
    assert(0);
  }
  taosWUnLockLatch(&qwtTestFetchQueueLock);
  
  tsem_post(&qwtTestFetchSem);
  
  return 0;
}

S
Shengliang Guan 已提交
175
int32_t qwtPutReqToQueue(void *node, EQueueType qtype, struct SRpcMsg *pMsg) {
D
dapan1121 已提交
176
  taosWLockLatch(&qwtTestQueryQueueLock);
wafwerar's avatar
wafwerar 已提交
177
  struct SRpcMsg *newMsg = (struct SRpcMsg *)taosMemoryCalloc(1, sizeof(struct SRpcMsg));
D
dapan1121 已提交
178 179
  memcpy(newMsg, pMsg, sizeof(struct SRpcMsg));
  qwtTestQueryQueue[qwtTestQueryQueueWIdx++] = newMsg;
D
dapan1121 已提交
180 181 182
  if (qwtTestQueryQueueWIdx >= qwtTestQueryQueueSize) {
    qwtTestQueryQueueWIdx = 0;
  }
S
Shengliang Guan 已提交
183

D
dapan1121 已提交
184 185 186 187 188 189 190 191 192 193
  qwtTestQueryQueueNum++;

  if (qwtTestQueryQueueWIdx == qwtTestQueryQueueRIdx) {
    printf("query queue is full");
    assert(0);
  }
  taosWUnLockLatch(&qwtTestQueryQueueLock);
  
  tsem_post(&qwtTestQuerySem);
  
D
dapan1121 已提交
194 195 196
  return 0;
}

D
dapan1121 已提交
197 198 199
void qwtSendReqToDnode(void* pVnode, struct SEpSet* epSet, struct SRpcMsg* pReq) {
  
}
D
dapan1121 已提交
200

D
dapan1121 已提交
201

D
dapan1121 已提交
202
void qwtRpcSendResponse(const SRpcMsg *pRsp) {
D
dapan1121 已提交
203 204

  switch (pRsp->msgType) {
D
dapan1121 已提交
205 206
    case TDMT_SCH_QUERY_RSP:
    case TDMT_SCH_MERGE_QUERY_RSP: {
D
dapan1121 已提交
207 208
      SQueryTableRsp *rsp = (SQueryTableRsp *)pRsp->pCont;

D
dapan1121 已提交
209
      if (pRsp->code) {
D
dapan1121 已提交
210
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
211
        qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
212 213
      }
      
D
dapan1121 已提交
214
      rpcFreeCont(rsp);
D
dapan1121 已提交
215 216
      break;
    }
D
dapan1121 已提交
217 218
    case TDMT_SCH_FETCH_RSP:
    case TDMT_SCH_MERGE_FETCH_RSP: {
D
dapan1121 已提交
219 220 221 222
      SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)pRsp->pCont;
  
      if (0 == pRsp->code && 0 == rsp->completed) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
D
dapan 已提交
223
        qwtPutReqToFetchQueue((void *)0x1, &qwtfetchRpc);
D
dapan1121 已提交
224
        rpcFreeCont(rsp);
D
dapan1121 已提交
225 226 227 228
        return;
      }

      qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
229
      qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
230
      rpcFreeCont(rsp);
D
dapan1121 已提交
231 232 233
      
      break;
    }
D
dapan1121 已提交
234
    case TDMT_SCH_DROP_TASK_RSP: {
D
dapan1121 已提交
235
      STaskDropRsp *rsp = (STaskDropRsp *)pRsp->pCont;
D
dapan1121 已提交
236
      rpcFreeCont(rsp);
D
dapan1121 已提交
237 238 239

      qwtTestCaseFinished = true;
      break;
D
dapan1121 已提交
240 241
    }
  }
D
dapan1121 已提交
242

D
dapan1121 已提交
243
  
D
dapan1121 已提交
244 245 246
  return;
}

D
dapan1121 已提交
247
int32_t qwtCreateExecTask(void* tsdb, int32_t vgId, uint64_t taskId, struct SSubplan* pPlan, qTaskInfo_t* pTaskInfo, DataSinkHandle* handle) {
D
dapan1121 已提交
248
  qwtTestSinkBlockNum = 0;
wafwerar's avatar
wafwerar 已提交
249
  qwtTestSinkMaxBlockNum = taosRand() % 100 + 1;
D
dapan1121 已提交
250
  qwtTestSinkQueryEnd = false;
D
dapan1121 已提交
251
  
wafwerar's avatar
wafwerar 已提交
252 253
  *pTaskInfo = (qTaskInfo_t)((char*)qwtTestCaseIdx+1);
  *handle = (DataSinkHandle)((char*)qwtTestCaseIdx+2);
D
dapan1121 已提交
254 255

  ++qwtTestCaseIdx;
D
dapan1121 已提交
256 257 258 259 260
  
  return 0;
}

int32_t qwtExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t *useconds) {
D
dapan1121 已提交
261 262 263 264 265 266
  int32_t endExec = 0;
  
  if (NULL == tinfo) {
    *pRes = NULL;
    *useconds = 0;
  } else {
D
dapan1121 已提交
267 268
    if (qwtTestSinkQueryEnd) {
      *pRes = NULL;
wafwerar's avatar
wafwerar 已提交
269
      *useconds = taosRand() % 10;
D
dapan1121 已提交
270 271 272
      return 0;
    }
    
wafwerar's avatar
wafwerar 已提交
273
    endExec = taosRand() % 5;
D
dapan1121 已提交
274
    
D
dapan1121 已提交
275 276
    int32_t runTime = 0;
    if (qwtTestEnableSleep && qwtTestMaxExecTaskUsec > 0) {
wafwerar's avatar
wafwerar 已提交
277
      runTime = taosRand() % qwtTestMaxExecTaskUsec;
D
dapan1121 已提交
278 279 280 281
    }

    if (qwtTestEnableSleep) {
      if (runTime) {
wafwerar's avatar
wafwerar 已提交
282
        taosUsleep(runTime);
D
dapan1121 已提交
283 284
      }
    }
D
dapan1121 已提交
285
      
D
dapan1121 已提交
286
    if (endExec) {
wafwerar's avatar
wafwerar 已提交
287
      *pRes = (SSDataBlock*)taosMemoryCalloc(1, sizeof(SSDataBlock));
D
dapan1121 已提交
288
      (*pRes)->info.rows = taosRand() % 1000 + 1;
D
dapan1121 已提交
289 290
    } else {
      *pRes = NULL;
wafwerar's avatar
wafwerar 已提交
291
      *useconds = taosRand() % 10;
D
dapan1121 已提交
292 293 294
    }
  }
  
D
dapan1121 已提交
295 296 297 298 299 300 301 302 303 304 305 306
  return 0;
}

int32_t qwtKillTask(qTaskInfo_t qinfo) {
  return 0;
}

void qwtDestroyTask(qTaskInfo_t qHandle) {
}


int32_t qwtPutDataBlock(DataSinkHandle handle, const SInputData* pInput, bool* pContinue) {
D
dapan1121 已提交
307 308 309 310
  if (NULL == handle || NULL == pInput || NULL == pContinue) {
    assert(0);
  }

wafwerar's avatar
wafwerar 已提交
311
  taosMemoryFree((void *)pInput->pData);
D
dapan1121 已提交
312

D
dapan1121 已提交
313 314 315 316 317 318
  taosWLockLatch(&qwtTestSinkLock);

  qwtTestSinkBlockNum++;

  if (qwtTestSinkBlockNum >= qwtTestSinkMaxBlockNum) {
    *pContinue = false;
D
dapan1121 已提交
319 320
  } else {
    *pContinue = true;
D
dapan1121 已提交
321 322 323
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
324 325 326 327
  return 0;
}

void qwtEndPut(DataSinkHandle handle, uint64_t useconds) {
D
dapan1121 已提交
328 329 330 331 332
  if (NULL == handle) {
    assert(0);
  }

  qwtTestSinkQueryEnd = true;
D
dapan1121 已提交
333 334 335
}

void qwtGetDataLength(DataSinkHandle handle, int32_t* pLen, bool* pQueryEnd) {
D
dapan1121 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349
  static int32_t in = 0;

  if (in > 0) {
    assert(0);
  }

  atomic_add_fetch_32(&in, 1);
  
  if (NULL == handle) {
    assert(0);
  }

  taosWLockLatch(&qwtTestSinkLock);
  if (qwtTestSinkBlockNum > 0) {
wafwerar's avatar
wafwerar 已提交
350
    *pLen = taosRand() % 100 + 1;
D
dapan1121 已提交
351 352 353 354
    qwtTestSinkBlockNum--;
  } else {
    *pLen = 0;
  }
D
dapan1121 已提交
355
  qwtTestSinkLastLen = *pLen;
D
dapan1121 已提交
356 357 358 359 360
  taosWUnLockLatch(&qwtTestSinkLock);

  *pQueryEnd = qwtTestSinkQueryEnd;

  atomic_sub_fetch_32(&in, 1);
D
dapan1121 已提交
361 362 363
}

int32_t qwtGetDataBlock(DataSinkHandle handle, SOutputData* pOutput) {
D
dapan1121 已提交
364
  taosWLockLatch(&qwtTestSinkLock);
D
dapan1121 已提交
365
  if (qwtTestSinkLastLen > 0) {
wafwerar's avatar
wafwerar 已提交
366
    pOutput->numOfRows = taosRand() % 10 + 1;
D
dapan1121 已提交
367
    pOutput->compressed = 1;
D
dapan1121 已提交
368 369 370 371 372 373 374 375
    pOutput->queryEnd = qwtTestSinkQueryEnd;
    if (qwtTestSinkBlockNum == 0) {
      pOutput->bufStatus = DS_BUF_EMPTY;
    } else if (qwtTestSinkBlockNum <= qwtTestSinkMaxBlockNum*0.5) {
      pOutput->bufStatus = DS_BUF_LOW;
    } else {
      pOutput->bufStatus = DS_BUF_FULL;
    }
wafwerar's avatar
wafwerar 已提交
376
    pOutput->useconds = taosRand() % 10 + 1;
D
dapan1121 已提交
377 378 379 380 381
    pOutput->precision = 1;
  } else if (qwtTestSinkLastLen == 0) {
    pOutput->numOfRows = 0;
    pOutput->compressed = 1;
    pOutput->pData = NULL;
D
dapan1121 已提交
382 383 384 385 386 387 388 389
    pOutput->queryEnd = qwtTestSinkQueryEnd;
    if (qwtTestSinkBlockNum == 0) {
      pOutput->bufStatus = DS_BUF_EMPTY;
    } else if (qwtTestSinkBlockNum <= qwtTestSinkMaxBlockNum*0.5) {
      pOutput->bufStatus = DS_BUF_LOW;
    } else {
      pOutput->bufStatus = DS_BUF_FULL;
    }
wafwerar's avatar
wafwerar 已提交
390
    pOutput->useconds = taosRand() % 10 + 1;
D
dapan1121 已提交
391 392 393 394 395 396
    pOutput->precision = 1;
  } else {
    assert(0);
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
397 398 399 400 401 402 403
  return 0;
}

void qwtDestroyDataSinker(DataSinkHandle handle) {

}

D
dapan1121 已提交
404

D
dapan1121 已提交
405

D
dapan 已提交
406
void stubSetStringToPlan() {
D
dapan1121 已提交
407
  static Stub stub;
D
dapan 已提交
408
  stub.set(qStringToSubplan, qwtStringToPlan);
D
dapan1121 已提交
409
  {
wafwerar's avatar
wafwerar 已提交
410 411 412 413 414 415
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("qStringToSubplan", result);
#endif
#ifdef LINUX
D
dapan 已提交
416
    AddrAny any("libplanner.so");
D
dapan1121 已提交
417
    std::map<std::string,void*> result;
D
dapan 已提交
418
    any.get_global_func_addr_dynsym("^qStringToSubplan$", result);
wafwerar's avatar
wafwerar 已提交
419
#endif
D
dapan1121 已提交
420
    for (const auto& f : result) {
D
dapan 已提交
421
      stub.set(f.second, qwtStringToPlan);
D
dapan1121 已提交
422 423 424 425
    }
  }
}

D
dapan1121 已提交
426 427 428 429
void stubSetExecTask() {
  static Stub stub;
  stub.set(qExecTask, qwtExecTask);
  {
wafwerar's avatar
wafwerar 已提交
430 431 432 433 434 435
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("qExecTask", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
436 437 438
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qExecTask$", result);
wafwerar's avatar
wafwerar 已提交
439
#endif
D
dapan1121 已提交
440 441 442 443 444 445 446 447 448 449 450 451
    for (const auto& f : result) {
      stub.set(f.second, qwtExecTask);
    }
  }
}



void stubSetCreateExecTask() {
  static Stub stub;
  stub.set(qCreateExecTask, qwtCreateExecTask);
  {
wafwerar's avatar
wafwerar 已提交
452 453 454 455 456 457
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("qCreateExecTask", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
458 459 460
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qCreateExecTask$", result);
wafwerar's avatar
wafwerar 已提交
461
#endif
D
dapan1121 已提交
462 463 464 465 466 467 468 469 470 471
    for (const auto& f : result) {
      stub.set(f.second, qwtCreateExecTask);
    }
  }
}

void stubSetAsyncKillTask() {
  static Stub stub;
  stub.set(qAsyncKillTask, qwtKillTask);
  {
wafwerar's avatar
wafwerar 已提交
472 473 474 475 476 477
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("qAsyncKillTask", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
478 479 480
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qAsyncKillTask$", result);
wafwerar's avatar
wafwerar 已提交
481
#endif
D
dapan1121 已提交
482 483 484 485 486 487 488 489 490 491
    for (const auto& f : result) {
      stub.set(f.second, qwtKillTask);
    }
  }
}

void stubSetDestroyTask() {
  static Stub stub;
  stub.set(qDestroyTask, qwtDestroyTask);
  {
wafwerar's avatar
wafwerar 已提交
492 493 494 495 496 497
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("qDestroyTask", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
498 499 500
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qDestroyTask$", result);
wafwerar's avatar
wafwerar 已提交
501
#endif
D
dapan1121 已提交
502 503 504 505 506 507 508 509 510 511 512
    for (const auto& f : result) {
      stub.set(f.second, qwtDestroyTask);
    }
  }
}


void stubSetDestroyDataSinker() {
  static Stub stub;
  stub.set(dsDestroyDataSinker, qwtDestroyDataSinker);
  {
wafwerar's avatar
wafwerar 已提交
513 514 515 516 517 518
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("dsDestroyDataSinker", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
519 520 521
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsDestroyDataSinker$", result);
wafwerar's avatar
wafwerar 已提交
522
#endif
D
dapan1121 已提交
523 524 525 526 527 528 529 530 531 532
    for (const auto& f : result) {
      stub.set(f.second, qwtDestroyDataSinker);
    }
  }
}

void stubSetGetDataLength() {
  static Stub stub;
  stub.set(dsGetDataLength, qwtGetDataLength);
  {
wafwerar's avatar
wafwerar 已提交
533 534 535 536 537 538
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("dsGetDataLength", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
539 540 541
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsGetDataLength$", result);
wafwerar's avatar
wafwerar 已提交
542
#endif
D
dapan1121 已提交
543 544 545 546 547 548 549 550 551 552
    for (const auto& f : result) {
      stub.set(f.second, qwtGetDataLength);
    }
  }
}

void stubSetEndPut() {
  static Stub stub;
  stub.set(dsEndPut, qwtEndPut);
  {
wafwerar's avatar
wafwerar 已提交
553 554 555 556 557 558
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("dsEndPut", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
559 560 561
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsEndPut$", result);
wafwerar's avatar
wafwerar 已提交
562
#endif
D
dapan1121 已提交
563 564 565 566 567 568 569 570 571 572
    for (const auto& f : result) {
      stub.set(f.second, qwtEndPut);
    }
  }
}

void stubSetPutDataBlock() {
  static Stub stub;
  stub.set(dsPutDataBlock, qwtPutDataBlock);
  {
wafwerar's avatar
wafwerar 已提交
573 574 575 576 577 578
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("dsPutDataBlock", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
579 580 581
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsPutDataBlock$", result);
wafwerar's avatar
wafwerar 已提交
582
#endif
D
dapan1121 已提交
583 584 585 586 587 588
    for (const auto& f : result) {
      stub.set(f.second, qwtPutDataBlock);
    }
  }
}

D
dapan1121 已提交
589 590 591 592
void stubSetRpcSendResponse() {
  static Stub stub;
  stub.set(rpcSendResponse, qwtRpcSendResponse);
  {
wafwerar's avatar
wafwerar 已提交
593 594 595 596 597 598
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("rpcSendResponse", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
599
    AddrAny any("libtransport.so");
D
dapan1121 已提交
600 601
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendResponse$", result);
wafwerar's avatar
wafwerar 已提交
602
#endif
D
dapan1121 已提交
603 604 605 606 607 608
    for (const auto& f : result) {
      stub.set(f.second, qwtRpcSendResponse);
    }
  }
}

D
dapan1121 已提交
609 610 611 612
void stubSetGetDataBlock() {
  static Stub stub;
  stub.set(dsGetDataBlock, qwtGetDataBlock);
  {
wafwerar's avatar
wafwerar 已提交
613 614 615 616 617 618
#ifdef WINDOWS
    AddrAny any;
    std::map<std::string,void*> result;
    any.get_func_addr("dsGetDataBlock", result);
#endif
#ifdef LINUX
D
dapan1121 已提交
619 620 621
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsGetDataBlock$", result);
wafwerar's avatar
wafwerar 已提交
622
#endif
D
dapan1121 已提交
623 624 625 626 627 628 629
    for (const auto& f : result) {
      stub.set(f.second, qwtGetDataBlock);
    }
  }
}


D
dapan1121 已提交
630 631 632 633 634 635 636
void *queryThread(void *param) {
  SRpcMsg queryRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;

D
dapan1121 已提交
637 638
  while (!qwtTestStop) {
    qwtBuildQueryReqMsg(&queryRpc);
D
dapan1121 已提交
639
    qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc, 0);    
D
dapan1121 已提交
640
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
641
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
642 643
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656
      printf("query:%d\n", n);
    }
  }

  return NULL;
}

void *fetchThread(void *param) {
  SRpcMsg fetchRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
657
  SResFetchReq fetchMsg = {0};
D
dapan1121 已提交
658

D
dapan1121 已提交
659 660
  while (!qwtTestStop) {
    qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
661
    code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc, 0);
D
dapan1121 已提交
662
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
663
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
664 665
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678
      printf("fetch:%d\n", n);
    }    
  }

  return NULL;
}

void *dropThread(void *param) {
  SRpcMsg dropRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
679
  STaskDropReq dropMsg = {0};  
D
dapan1121 已提交
680

D
dapan1121 已提交
681 682
  while (!qwtTestStop) {
    qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
683
    code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc, 0);
D
dapan1121 已提交
684
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
685
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
686 687
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
688 689 690 691 692 693 694
      printf("drop:%d\n", n);
    }    
  }

  return NULL;
}

D
dapan1121 已提交
695
void *qwtclientThread(void *param) {
D
dapan1121 已提交
696 697 698
  int32_t code = 0;
  uint32_t n = 0;
  void *mgmt = param;
D
dapan1121 已提交
699 700 701
  void *mockPointer = (void *)0x1;    
  SRpcMsg queryRpc = {0};

wafwerar's avatar
wafwerar 已提交
702
  taosSsleep(1);
D
dapan1121 已提交
703 704

  while (!qwtTestStop) {
D
dapan1121 已提交
705
    qwtTestCaseFinished = false;
S
Shengliang Guan 已提交
706

D
dapan1121 已提交
707
    qwtBuildQueryReqMsg(&queryRpc);
S
Shengliang Guan 已提交
708
    qwtPutReqToQueue((void *)0x1, QUERY_QUEUE, &queryRpc);
D
dapan1121 已提交
709 710

    while (!qwtTestCaseFinished) {
wafwerar's avatar
wafwerar 已提交
711
      taosUsleep(1);
D
dapan1121 已提交
712 713 714
    }
    
    
D
dapan1121 已提交
715
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
716
      printf("case run:%d\n", n);
D
dapan1121 已提交
717 718 719
    }
  }

D
dapan1121 已提交
720 721
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan1121 已提交
722 723
  return NULL;
}
D
dapan1121 已提交
724

D
dapan1121 已提交
725
void *queryQueueThread(void *param) {
D
dapan1121 已提交
726 727 728 729
  void *mockPointer = (void *)0x1;   
  SRpcMsg *queryRpc = NULL;
  void *mgmt = param;

D
dapan1121 已提交
730
  while (true) {
D
dapan1121 已提交
731 732
    tsem_wait(&qwtTestQuerySem);

D
dapan1121 已提交
733
    if (qwtTestStop && qwtTestQueryQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
734 735 736
      break;
    }

D
dapan1121 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    taosWLockLatch(&qwtTestQueryQueueLock);
    if (qwtTestQueryQueueNum <= 0 || qwtTestQueryQueueRIdx == qwtTestQueryQueueWIdx) {
      printf("query queue is empty\n");
      assert(0);
    }
    
    queryRpc = qwtTestQueryQueue[qwtTestQueryQueueRIdx++];
    
    if (qwtTestQueryQueueRIdx >= qwtTestQueryQueueSize) {
      qwtTestQueryQueueRIdx = 0;
    }
    
    qwtTestQueryQueueNum--;
    taosWUnLockLatch(&qwtTestQueryQueueLock);

D
dapan1121 已提交
752 753

    if (qwtTestEnableSleep && qwtTestReqMaxDelayUsec > 0) {
wafwerar's avatar
wafwerar 已提交
754
      int32_t delay = taosRand() % qwtTestReqMaxDelayUsec;
D
dapan1121 已提交
755 756

      if (delay) {
wafwerar's avatar
wafwerar 已提交
757
        taosUsleep(delay);
D
dapan1121 已提交
758 759 760
      }
    }
    
D
dapan1121 已提交
761
    if (TDMT_SCH_QUERY == queryRpc->msgType) {
D
dapan1121 已提交
762
      qWorkerProcessQueryMsg(mockPointer, mgmt, queryRpc, 0);
D
dapan1121 已提交
763
    } else if (TDMT_SCH_QUERY_CONTINUE == queryRpc->msgType) {
D
dapan1121 已提交
764
      qWorkerProcessCQueryMsg(mockPointer, mgmt, queryRpc, 0);
D
dapan1121 已提交
765 766 767 768
    } else {
      printf("unknown msg in query queue, type:%d\n", queryRpc->msgType);
      assert(0);
    }
D
dapan1121 已提交
769

wafwerar's avatar
wafwerar 已提交
770
    taosMemoryFree(queryRpc);
D
dapan1121 已提交
771

D
dapan1121 已提交
772
    if (qwtTestStop && qwtTestQueryQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
773 774
      break;
    }
D
dapan1121 已提交
775
  }
D
dapan1121 已提交
776

D
dapan1121 已提交
777 778
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan 已提交
779
  return NULL;
D
dapan1121 已提交
780 781 782
}

void *fetchQueueThread(void *param) {
D
dapan1121 已提交
783 784 785 786
  void *mockPointer = (void *)0x1;   
  SRpcMsg *fetchRpc = NULL;
  void *mgmt = param;

D
dapan1121 已提交
787
  while (true) {
D
dapan1121 已提交
788 789
    tsem_wait(&qwtTestFetchSem);

D
dapan1121 已提交
790 791 792 793
    if (qwtTestStop && qwtTestFetchQueueNum <= 0 && qwtTestCaseFinished) {
      break;
    }    

D
dapan1121 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    taosWLockLatch(&qwtTestFetchQueueLock);
    if (qwtTestFetchQueueNum <= 0 || qwtTestFetchQueueRIdx == qwtTestFetchQueueWIdx) {
      printf("Fetch queue is empty\n");
      assert(0);
    }
    
    fetchRpc = qwtTestFetchQueue[qwtTestFetchQueueRIdx++];
    
    if (qwtTestFetchQueueRIdx >= qwtTestFetchQueueSize) {
      qwtTestFetchQueueRIdx = 0;
    }
    
    qwtTestFetchQueueNum--;
    taosWUnLockLatch(&qwtTestFetchQueueLock);

D
dapan1121 已提交
809
    if (qwtTestEnableSleep && qwtTestReqMaxDelayUsec > 0) {
wafwerar's avatar
wafwerar 已提交
810
      int32_t delay = taosRand() % qwtTestReqMaxDelayUsec;
D
dapan1121 已提交
811 812

      if (delay) {
wafwerar's avatar
wafwerar 已提交
813
        taosUsleep(delay);
D
dapan1121 已提交
814 815 816
      }
    }

D
dapan1121 已提交
817
    switch (fetchRpc->msgType) {
D
dapan1121 已提交
818
      case TDMT_SCH_FETCH:
D
dapan1121 已提交
819
      case TDMT_SCH_MERGE_FETCH:
D
dapan1121 已提交
820
        qWorkerProcessFetchMsg(mockPointer, mgmt, fetchRpc, 0);
D
dapan1121 已提交
821
        break;
D
dapan1121 已提交
822
      case TDMT_SCH_CANCEL_TASK:
D
dapan1121 已提交
823
        qWorkerProcessCancelMsg(mockPointer, mgmt, fetchRpc, 0);
D
dapan1121 已提交
824
        break;
D
dapan1121 已提交
825
      case TDMT_SCH_DROP_TASK:
D
dapan1121 已提交
826
        qWorkerProcessDropMsg(mockPointer, mgmt, fetchRpc, 0);
D
dapan1121 已提交
827
        break;
D
dapan1121 已提交
828 829 830
      default:
        printf("unknown msg type:%d in fetch queue", fetchRpc->msgType);
        assert(0);
D
dapan1121 已提交
831
        break;
D
dapan1121 已提交
832
    }
D
dapan1121 已提交
833

wafwerar's avatar
wafwerar 已提交
834
    taosMemoryFree(fetchRpc);
D
dapan1121 已提交
835

D
dapan1121 已提交
836
    if (qwtTestStop && qwtTestFetchQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
837 838
      break;
    }    
D
dapan1121 已提交
839
  }
D
dapan1121 已提交
840

D
dapan1121 已提交
841 842
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan 已提交
843
  return NULL;
D
dapan1121 已提交
844 845
}

D
dapan1121 已提交
846

D
dapan1121 已提交
847 848 849 850

}


D
dapan1121 已提交
851
TEST(seqTest, normalCase) {
D
dapan 已提交
852 853 854 855 856 857
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SRpcMsg queryRpc = {0};
  SRpcMsg fetchRpc = {0};
  SRpcMsg dropRpc = {0};
D
dapan1121 已提交
858 859

  qwtInitLogFile();
D
dapan 已提交
860

D
dapan1121 已提交
861 862 863
  qwtBuildQueryReqMsg(&queryRpc);
  qwtBuildFetchReqMsg(&qwtfetchMsg, &fetchRpc);
  qwtBuildDropReqMsg(&qwtdropMsg, &dropRpc);
D
dapan1121 已提交
864
  
D
dapan 已提交
865
  stubSetStringToPlan();
D
dapan1121 已提交
866
  stubSetRpcSendResponse();
D
dapan1121 已提交
867 868 869 870 871 872 873 874 875
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();
D
dapan 已提交
876
  
X
Xiaoyu Wang 已提交
877
  SMsgCb msgCb = {0};
878
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
879
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
X
Xiaoyu Wang 已提交
880
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan 已提交
881 882
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
883
  code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc, 0);
D
dapan 已提交
884 885
  ASSERT_EQ(code, 0);

D
dapan 已提交
886 887
  //code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
  //ASSERT_EQ(code, 0);
D
dapan 已提交
888

D
dapan1121 已提交
889
  code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc, 0);
D
dapan 已提交
890 891
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
892
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc, 0);
D
dapan1121 已提交
893 894 895 896 897 898 899 900 901 902 903
  ASSERT_EQ(code, 0);

  qWorkerDestroy(&mgmt);
}

TEST(seqTest, cancelFirst) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SRpcMsg queryRpc = {0};
  SRpcMsg dropRpc = {0};
D
dapan1121 已提交
904 905

  qwtInitLogFile();
D
dapan1121 已提交
906
  
D
dapan1121 已提交
907 908
  qwtBuildQueryReqMsg(&queryRpc);
  qwtBuildDropReqMsg(&qwtdropMsg, &dropRpc);
D
dapan1121 已提交
909 910 911

  stubSetStringToPlan();
  stubSetRpcSendResponse();
S
Shengliang Guan 已提交
912

X
Xiaoyu Wang 已提交
913
  SMsgCb msgCb = {0};
914
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
915
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
X
Xiaoyu Wang 已提交
916
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
917 918
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
919
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc, 0);
D
dapan 已提交
920
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
921

D
dapan1121 已提交
922
  code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc, 0);
D
dapan1121 已提交
923
  ASSERT_TRUE(0 != code);
D
dapan1121 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936

  qWorkerDestroy(&mgmt);
}

TEST(seqTest, randCase) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
  SRpcMsg queryRpc = {0};
  SRpcMsg readyRpc = {0};
  SRpcMsg fetchRpc = {0};
  SRpcMsg dropRpc = {0};
  SRpcMsg statusRpc = {0};
S
Shengliang Guan 已提交
937 938 939 940
  SResReadyReq readyMsg = {0};
  SResFetchReq fetchMsg = {0};
  STaskDropReq dropMsg = {0};  
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
941 942

  qwtInitLogFile();
D
dapan1121 已提交
943 944 945
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
D
dapan1121 已提交
946
  stubSetCreateExecTask();
D
dapan1121 已提交
947

wafwerar's avatar
wafwerar 已提交
948
  taosSeedRand(taosGetTimestampSec());
S
Shengliang Guan 已提交
949

X
Xiaoyu Wang 已提交
950
  SMsgCb msgCb = {0};
951
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
952
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
X
Xiaoyu Wang 已提交
953
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
954 955 956 957 958
  ASSERT_EQ(code, 0);

  int32_t t = 0;
  int32_t maxr = 10001;
  while (true) {
wafwerar's avatar
wafwerar 已提交
959
    int32_t r = taosRand() % maxr;
D
dapan1121 已提交
960 961
    
    if (r >= 0 && r < maxr/5) {
D
dapan1121 已提交
962 963
      printf("Query,%d\n", t++);      
      qwtBuildQueryReqMsg(&queryRpc);
D
dapan1121 已提交
964
      code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc, 0);
D
dapan1121 已提交
965
    } else if (r >= maxr/5 && r < maxr * 2/5) {
D
dapan 已提交
966 967 968 969 970 971
      //printf("Ready,%d\n", t++);
      //qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
      //code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
      //if (qwtTestEnableSleep) {
      //  taosUsleep(1);
      //}
D
dapan1121 已提交
972 973
    } else if (r >= maxr * 2/5 && r < maxr* 3/5) {
      printf("Fetch,%d\n", t++);
D
dapan1121 已提交
974
      qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
975
      code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc, 0);
D
dapan1121 已提交
976
      if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
977
        taosUsleep(1);
D
dapan1121 已提交
978
      }
D
dapan1121 已提交
979 980
    } else if (r >= maxr * 3/5 && r < maxr * 4/5) {
      printf("Drop,%d\n", t++);
D
dapan1121 已提交
981
      qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
982
      code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc, 0);
D
dapan1121 已提交
983
      if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
984
        taosUsleep(1);
D
dapan1121 已提交
985
      }
D
dapan1121 已提交
986 987
    } else if (r >= maxr * 4/5 && r < maxr-1) {
      printf("Status,%d\n", t++);
D
dapan1121 已提交
988
      if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
989
        taosUsleep(1);
D
dapan1121 已提交
990
      }      
D
dapan1121 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    } else {
      printf("QUIT RAND NOW");
      break;
    }
  }

  qWorkerDestroy(&mgmt);
}

TEST(seqTest, multithreadRand) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;
D
dapan1121 已提交
1004 1005

  qwtInitLogFile();
D
dapan1121 已提交
1006 1007 1008
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
D
dapan1121 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();
D
dapan1121 已提交
1018

wafwerar's avatar
wafwerar 已提交
1019
  taosSeedRand(taosGetTimestampSec());
S
Shengliang Guan 已提交
1020

X
Xiaoyu Wang 已提交
1021
  SMsgCb msgCb = {0};
1022
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
1023
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
X
Xiaoyu Wang 已提交
1024
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1025 1026
  ASSERT_EQ(code, 0);

wafwerar's avatar
wafwerar 已提交
1027 1028
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1029

X
Xiaoyu Wang 已提交
1030
  TdThread t1,t2,t3,t4,t5,t6;
wafwerar's avatar
wafwerar 已提交
1031
  taosThreadCreate(&(t1), &thattr, queryThread, mgmt);
1032
  //taosThreadCreate(&(t2), &thattr, readyThread, NULL);
wafwerar's avatar
wafwerar 已提交
1033 1034
  taosThreadCreate(&(t3), &thattr, fetchThread, NULL);
  taosThreadCreate(&(t4), &thattr, dropThread, NULL);
X
Xiaoyu Wang 已提交
1035
  taosThreadCreate(&(t6), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1036

D
dapan1121 已提交
1037 1038
  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1039
      taosSsleep(1);
D
dapan1121 已提交
1040
    } else {
wafwerar's avatar
wafwerar 已提交
1041
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1042 1043 1044 1045 1046
      break;
    }
  }
  
  qwtTestStop = true;
wafwerar's avatar
wafwerar 已提交
1047
  taosSsleep(3);
D
dapan1121 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056

  qwtTestQueryQueueNum = 0;
  qwtTestQueryQueueRIdx = 0;
  qwtTestQueryQueueWIdx = 0;
  qwtTestQueryQueueLock = 0;
  qwtTestFetchQueueNum = 0;
  qwtTestFetchQueueRIdx = 0;
  qwtTestFetchQueueWIdx = 0;
  qwtTestFetchQueueLock = 0;
D
dapan1121 已提交
1057 1058 1059 1060
  
  qWorkerDestroy(&mgmt);
}

D
dapan1121 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
TEST(rcTest, shortExecshortDelay) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;

  qwtInitLogFile();
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();

wafwerar's avatar
wafwerar 已提交
1080
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1081 1082 1083
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

S
Shengliang Guan 已提交
1084
  SMsgCb msgCb = {0};
1085
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
1086
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
S
Shengliang Guan 已提交
1087
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1088 1089 1090 1091 1092 1093 1094 1095
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 0;
  qwtTestReqMaxDelayUsec = 0;

  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

wafwerar's avatar
wafwerar 已提交
1096 1097
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1098

wafwerar's avatar
wafwerar 已提交
1099 1100 1101 1102
  TdThread t1,t2,t3,t4,t5;
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1103 1104 1105

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1106
      taosSsleep(1);
D
dapan1121 已提交
1107
    } else {
wafwerar's avatar
wafwerar 已提交
1108
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
      break;
    }
  }
  
  qwtTestStop = true;

  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
wafwerar's avatar
wafwerar 已提交
1120
    taosSsleep(1);
D
dapan1121 已提交
1121 1122 1123 1124 1125 1126

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);

wafwerar's avatar
wafwerar 已提交
1127
        taosUsleep(10);
D
dapan1121 已提交
1128 1129
      }
    }
D
dapan1121 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    
  }

  qwtTestQueryQueueNum = 0;
  qwtTestQueryQueueRIdx = 0;
  qwtTestQueryQueueWIdx = 0;
  qwtTestQueryQueueLock = 0;
  qwtTestFetchQueueNum = 0;
  qwtTestFetchQueueRIdx = 0;
  qwtTestFetchQueueWIdx = 0;
  qwtTestFetchQueueLock = 0;
  
  qWorkerDestroy(&mgmt);  
}

TEST(rcTest, longExecshortDelay) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;

  qwtInitLogFile();
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();

wafwerar's avatar
wafwerar 已提交
1164
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1165 1166 1167
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

S
Shengliang Guan 已提交
1168
  SMsgCb msgCb = {0};
1169
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
1170
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
S
Shengliang Guan 已提交
1171
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1172 1173 1174 1175 1176 1177 1178 1179
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 1000000;
  qwtTestReqMaxDelayUsec = 0;

  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

wafwerar's avatar
wafwerar 已提交
1180 1181
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1182

wafwerar's avatar
wafwerar 已提交
1183 1184 1185 1186
  TdThread t1,t2,t3,t4,t5;
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1187 1188 1189

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1190
      taosSsleep(1);
D
dapan1121 已提交
1191
    } else {
wafwerar's avatar
wafwerar 已提交
1192
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
      break;
    }
  }
  
  qwtTestStop = true;

 
  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
wafwerar's avatar
wafwerar 已提交
1205
    taosSsleep(1);
D
dapan1121 已提交
1206 1207 1208 1209 1210 1211

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);
        
wafwerar's avatar
wafwerar 已提交
1212
        taosUsleep(10);
D
dapan1121 已提交
1213 1214
      }
    }
D
dapan1121 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    
  }

  qwtTestQueryQueueNum = 0;
  qwtTestQueryQueueRIdx = 0;
  qwtTestQueryQueueWIdx = 0;
  qwtTestQueryQueueLock = 0;
  qwtTestFetchQueueNum = 0;
  qwtTestFetchQueueRIdx = 0;
  qwtTestFetchQueueWIdx = 0;
  qwtTestFetchQueueLock = 0;
  
  qWorkerDestroy(&mgmt);
}


TEST(rcTest, shortExeclongDelay) {
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;

  qwtInitLogFile();
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();

wafwerar's avatar
wafwerar 已提交
1250
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1251 1252 1253
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

S
Shengliang Guan 已提交
1254
  SMsgCb msgCb = {0};
1255
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
1256
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
S
Shengliang Guan 已提交
1257
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1258 1259 1260 1261 1262 1263 1264 1265
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 0;
  qwtTestReqMaxDelayUsec = 1000000;

  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

wafwerar's avatar
wafwerar 已提交
1266 1267
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1268

wafwerar's avatar
wafwerar 已提交
1269 1270 1271 1272
  TdThread t1,t2,t3,t4,t5;
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1273 1274 1275

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1276
      taosSsleep(1);
D
dapan1121 已提交
1277
    } else {
wafwerar's avatar
wafwerar 已提交
1278
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
      break;
    }
  }
  
  qwtTestStop = true;


  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
wafwerar's avatar
wafwerar 已提交
1291
    taosSsleep(1);
D
dapan1121 已提交
1292 1293 1294 1295 1296 1297

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);
        
wafwerar's avatar
wafwerar 已提交
1298
        taosUsleep(10);
D
dapan1121 已提交
1299 1300
      }
    }
D
dapan1121 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    
  }

  qwtTestQueryQueueNum = 0;
  qwtTestQueryQueueRIdx = 0;
  qwtTestQueryQueueWIdx = 0;
  qwtTestQueryQueueLock = 0;
  qwtTestFetchQueueNum = 0;
  qwtTestFetchQueueRIdx = 0;
  qwtTestFetchQueueWIdx = 0;
  qwtTestFetchQueueLock = 0;
  
  qWorkerDestroy(&mgmt);
}


TEST(rcTest, dropTest) {
D
dapan1121 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;

  qwtInitLogFile();
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();

wafwerar's avatar
wafwerar 已提交
1336
  taosSeedRand(taosGetTimestampSec());
S
Shengliang Guan 已提交
1337

X
Xiaoyu Wang 已提交
1338
  SMsgCb msgCb = {0};
1339
  msgCb.mgmt = (void *)mockPointer;
S
Shengliang Guan 已提交
1340
  msgCb.putToQueueFp = (PutToQueueFp)qwtPutReqToQueue;
X
Xiaoyu Wang 已提交
1341
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1342 1343
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1344 1345 1346
  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

wafwerar's avatar
wafwerar 已提交
1347 1348
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1349

wafwerar's avatar
wafwerar 已提交
1350
  TdThread t1,t2,t3,t4,t5;
X
Xiaoyu Wang 已提交
1351
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
wafwerar's avatar
wafwerar 已提交
1352 1353
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1354 1355 1356

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1357
      taosSsleep(1);
D
dapan1121 已提交
1358
    } else {
wafwerar's avatar
wafwerar 已提交
1359
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1360 1361 1362 1363 1364
      break;
    }
  }
  
  qwtTestStop = true;
wafwerar's avatar
wafwerar 已提交
1365
  taosSsleep(3);
D
dapan1121 已提交
1366
  
D
dapan1121 已提交
1367
  qWorkerDestroy(&mgmt);
D
dapan 已提交
1368 1369
}

D
dapan1121 已提交
1370

D
dapan1121 已提交
1371
int main(int argc, char** argv) {
wafwerar's avatar
wafwerar 已提交
1372
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1373 1374 1375 1376
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

D
dapan1121 已提交
1377
#pragma GCC diagnostic pop