qworkerTests.cpp 33.7 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

D
dapan1121 已提交
30 31
#include "os.h"

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


namespace {

D
dapan1121 已提交
47 48 49
#define qwtTestQueryQueueSize 1000000
#define qwtTestFetchQueueSize 1000000

D
dapan1121 已提交
50 51
bool qwtEnableLog = true;

D
dapan1121 已提交
52 53
int32_t qwtTestMaxExecTaskUsec = 2;
int32_t qwtTestReqMaxDelayUsec = 2;
D
dapan1121 已提交
54

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

D
dapan1121 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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 已提交
86
int32_t qwtTestSinkLastLen = 0;
D
dapan1121 已提交
87 88


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

D
dapan1121 已提交
98 99

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

  tsAsyncLog = 0;
  qDebugFlag = 159;
D
dapan1121 已提交
108
  strcpy(tsLogDir, "/var/log/taos");
D
dapan1121 已提交
109

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

}

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

void qwtBuildReadyReqMsg(SResReadyReq *readyMsg, SRpcMsg *readyRpc) {
  readyMsg->sId = htobe64(1);
D
dapan1121 已提交
129
  readyMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
130
  readyMsg->taskId = htobe64(1);
D
dapan1121 已提交
131
  readyRpc->msgType = TDMT_VND_RES_READY;
D
dapan1121 已提交
132 133 134 135 136 137
  readyRpc->pCont = readyMsg;
  readyRpc->contLen = sizeof(SResReadyReq);
}

void qwtBuildFetchReqMsg(SResFetchReq *fetchMsg, SRpcMsg *fetchRpc) {
  fetchMsg->sId = htobe64(1);
D
dapan1121 已提交
138
  fetchMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
139
  fetchMsg->taskId = htobe64(1);
D
dapan1121 已提交
140
  fetchRpc->msgType = TDMT_VND_FETCH;
D
dapan1121 已提交
141 142 143 144 145 146
  fetchRpc->pCont = fetchMsg;
  fetchRpc->contLen = sizeof(SResFetchReq);
}

void qwtBuildDropReqMsg(STaskDropReq *dropMsg, SRpcMsg *dropRpc) {
  dropMsg->sId = htobe64(1);
D
dapan1121 已提交
147
  dropMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
148
  dropMsg->taskId = htobe64(1);
D
dapan1121 已提交
149
  dropRpc->msgType = TDMT_VND_DROP_TASK;
D
dapan1121 已提交
150 151 152 153 154 155 156 157 158 159
  dropRpc->pCont = dropMsg;
  dropRpc->contLen = sizeof(STaskDropReq);
}

void qwtBuildStatusReqMsg(SSchTasksStatusReq *statusMsg, SRpcMsg *statusRpc) {
  statusMsg->sId = htobe64(1);
  statusRpc->pCont = statusMsg;
  statusRpc->contLen = sizeof(SSchTasksStatusReq);
  statusRpc->msgType = TDMT_VND_TASKS_STATUS;
}
D
dapan1121 已提交
160

D
dapan 已提交
161
int32_t qwtStringToPlan(const char* str, SSubplan** subplan) {
D
dapan 已提交
162
  *subplan = (SSubplan *)0x1;
D
dapan 已提交
163
  return 0;
D
dapan1121 已提交
164 165
}

D
dapan1121 已提交
166 167
int32_t qwtPutReqToFetchQueue(void *node, struct SRpcMsg *pMsg) {
  taosWLockLatch(&qwtTestFetchQueueLock);
wafwerar's avatar
wafwerar 已提交
168
  struct SRpcMsg *newMsg = (struct SRpcMsg *)taosMemoryCalloc(1, sizeof(struct SRpcMsg));
D
dapan1121 已提交
169 170
  memcpy(newMsg, pMsg, sizeof(struct SRpcMsg));  
  qwtTestFetchQueue[qwtTestFetchQueueWIdx++] = newMsg;
D
dapan1121 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  if (qwtTestFetchQueueWIdx >= qwtTestFetchQueueSize) {
    qwtTestFetchQueueWIdx = 0;
  }
  
  qwtTestFetchQueueNum++;

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


D
dapan1121 已提交
189
int32_t qwtPutReqToQueue(void *node, struct SRpcMsg *pMsg) {
D
dapan1121 已提交
190
  taosWLockLatch(&qwtTestQueryQueueLock);
wafwerar's avatar
wafwerar 已提交
191
  struct SRpcMsg *newMsg = (struct SRpcMsg *)taosMemoryCalloc(1, sizeof(struct SRpcMsg));
D
dapan1121 已提交
192 193
  memcpy(newMsg, pMsg, sizeof(struct SRpcMsg));
  qwtTestQueryQueue[qwtTestQueryQueueWIdx++] = newMsg;
D
dapan1121 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207
  if (qwtTestQueryQueueWIdx >= qwtTestQueryQueueSize) {
    qwtTestQueryQueueWIdx = 0;
  }
  
  qwtTestQueryQueueNum++;

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

D
dapan1121 已提交
211 212 213
void qwtSendReqToDnode(void* pVnode, struct SEpSet* epSet, struct SRpcMsg* pReq) {
  
}
D
dapan1121 已提交
214

D
dapan1121 已提交
215

D
dapan1121 已提交
216
void qwtRpcSendResponse(const SRpcMsg *pRsp) {
D
dapan1121 已提交
217 218 219 220 221 222 223

  switch (pRsp->msgType) {
    case TDMT_VND_QUERY_RSP: {
      SQueryTableRsp *rsp = (SQueryTableRsp *)pRsp->pCont;

      if (0 == pRsp->code) {
        qwtBuildReadyReqMsg(&qwtreadyMsg, &qwtreadyRpc);    
D
dapan 已提交
224
        qwtPutReqToFetchQueue((void *)0x1, &qwtreadyRpc);
D
dapan1121 已提交
225 226
      } else {
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
227
        qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
228 229
      }
      
D
dapan1121 已提交
230
      rpcFreeCont(rsp);
D
dapan1121 已提交
231 232 233 234 235 236 237
      break;
    }
    case TDMT_VND_RES_READY_RSP: {
      SResReadyRsp *rsp = (SResReadyRsp *)pRsp->pCont;
      
      if (0 == pRsp->code) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
D
dapan 已提交
238
        qwtPutReqToFetchQueue((void *)0x1, &qwtfetchRpc);
D
dapan1121 已提交
239 240
      } else {
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
241
        qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
242
      }
D
dapan1121 已提交
243
      rpcFreeCont(rsp);
D
dapan1121 已提交
244 245 246 247 248 249 250
      break;
    }
    case TDMT_VND_FETCH_RSP: {
      SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)pRsp->pCont;
  
      if (0 == pRsp->code && 0 == rsp->completed) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
D
dapan 已提交
251
        qwtPutReqToFetchQueue((void *)0x1, &qwtfetchRpc);
D
dapan1121 已提交
252
        rpcFreeCont(rsp);
D
dapan1121 已提交
253 254 255 256
        return;
      }

      qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
257
      qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
258
      rpcFreeCont(rsp);
D
dapan1121 已提交
259 260 261
      
      break;
    }
D
dapan1121 已提交
262
    case TDMT_VND_DROP_TASK_RSP: {
D
dapan1121 已提交
263
      STaskDropRsp *rsp = (STaskDropRsp *)pRsp->pCont;
D
dapan1121 已提交
264
      rpcFreeCont(rsp);
D
dapan1121 已提交
265 266 267

      qwtTestCaseFinished = true;
      break;
D
dapan1121 已提交
268 269
    }
  }
D
dapan1121 已提交
270

D
dapan1121 已提交
271
  
D
dapan1121 已提交
272 273 274
  return;
}

D
dapan1121 已提交
275
int32_t qwtCreateExecTask(void* tsdb, int32_t vgId, uint64_t taskId, struct SSubplan* pPlan, qTaskInfo_t* pTaskInfo, DataSinkHandle* handle) {
D
dapan1121 已提交
276
  qwtTestSinkBlockNum = 0;
wafwerar's avatar
wafwerar 已提交
277
  qwtTestSinkMaxBlockNum = taosRand() % 100 + 1;
D
dapan1121 已提交
278
  qwtTestSinkQueryEnd = false;
D
dapan1121 已提交
279
  
D
dapan1121 已提交
280 281 282 283
  *pTaskInfo = (qTaskInfo_t)qwtTestCaseIdx+1;
  *handle = (DataSinkHandle)qwtTestCaseIdx+2;

  ++qwtTestCaseIdx;
D
dapan1121 已提交
284 285 286 287 288
  
  return 0;
}

int32_t qwtExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t *useconds) {
D
dapan1121 已提交
289 290 291 292 293 294
  int32_t endExec = 0;
  
  if (NULL == tinfo) {
    *pRes = NULL;
    *useconds = 0;
  } else {
D
dapan1121 已提交
295 296
    if (qwtTestSinkQueryEnd) {
      *pRes = NULL;
wafwerar's avatar
wafwerar 已提交
297
      *useconds = taosRand() % 10;
D
dapan1121 已提交
298 299 300
      return 0;
    }
    
wafwerar's avatar
wafwerar 已提交
301
    endExec = taosRand() % 5;
D
dapan1121 已提交
302
    
D
dapan1121 已提交
303 304
    int32_t runTime = 0;
    if (qwtTestEnableSleep && qwtTestMaxExecTaskUsec > 0) {
wafwerar's avatar
wafwerar 已提交
305
      runTime = taosRand() % qwtTestMaxExecTaskUsec;
D
dapan1121 已提交
306 307 308 309
    }

    if (qwtTestEnableSleep) {
      if (runTime) {
wafwerar's avatar
wafwerar 已提交
310
        taosUsleep(runTime);
D
dapan1121 已提交
311 312
      }
    }
D
dapan1121 已提交
313
      
D
dapan1121 已提交
314
    if (endExec) {
wafwerar's avatar
wafwerar 已提交
315
      *pRes = (SSDataBlock*)taosMemoryCalloc(1, sizeof(SSDataBlock));
D
dapan1121 已提交
316
      (*pRes)->info.rows = taosRand() % 1000 + 1;
D
dapan1121 已提交
317 318
    } else {
      *pRes = NULL;
wafwerar's avatar
wafwerar 已提交
319
      *useconds = taosRand() % 10;
D
dapan1121 已提交
320 321 322
    }
  }
  
D
dapan1121 已提交
323 324 325 326 327 328 329 330 331 332 333 334
  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 已提交
335 336 337 338
  if (NULL == handle || NULL == pInput || NULL == pContinue) {
    assert(0);
  }

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

D
dapan1121 已提交
341 342 343 344 345 346
  taosWLockLatch(&qwtTestSinkLock);

  qwtTestSinkBlockNum++;

  if (qwtTestSinkBlockNum >= qwtTestSinkMaxBlockNum) {
    *pContinue = false;
D
dapan1121 已提交
347 348
  } else {
    *pContinue = true;
D
dapan1121 已提交
349 350 351
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
352 353 354 355
  return 0;
}

void qwtEndPut(DataSinkHandle handle, uint64_t useconds) {
D
dapan1121 已提交
356 357 358 359 360
  if (NULL == handle) {
    assert(0);
  }

  qwtTestSinkQueryEnd = true;
D
dapan1121 已提交
361 362 363
}

void qwtGetDataLength(DataSinkHandle handle, int32_t* pLen, bool* pQueryEnd) {
D
dapan1121 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377
  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 已提交
378
    *pLen = taosRand() % 100 + 1;
D
dapan1121 已提交
379 380 381 382
    qwtTestSinkBlockNum--;
  } else {
    *pLen = 0;
  }
D
dapan1121 已提交
383
  qwtTestSinkLastLen = *pLen;
D
dapan1121 已提交
384 385 386 387 388
  taosWUnLockLatch(&qwtTestSinkLock);

  *pQueryEnd = qwtTestSinkQueryEnd;

  atomic_sub_fetch_32(&in, 1);
D
dapan1121 已提交
389 390 391
}

int32_t qwtGetDataBlock(DataSinkHandle handle, SOutputData* pOutput) {
D
dapan1121 已提交
392
  taosWLockLatch(&qwtTestSinkLock);
D
dapan1121 已提交
393
  if (qwtTestSinkLastLen > 0) {
wafwerar's avatar
wafwerar 已提交
394
    pOutput->numOfRows = taosRand() % 10 + 1;
D
dapan1121 已提交
395
    pOutput->compressed = 1;
D
dapan1121 已提交
396 397 398 399 400 401 402 403
    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 已提交
404
    pOutput->useconds = taosRand() % 10 + 1;
D
dapan1121 已提交
405 406 407 408 409
    pOutput->precision = 1;
  } else if (qwtTestSinkLastLen == 0) {
    pOutput->numOfRows = 0;
    pOutput->compressed = 1;
    pOutput->pData = NULL;
D
dapan1121 已提交
410 411 412 413 414 415 416 417
    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 已提交
418
    pOutput->useconds = taosRand() % 10 + 1;
D
dapan1121 已提交
419 420 421 422 423 424
    pOutput->precision = 1;
  } else {
    assert(0);
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
425 426 427 428 429 430 431
  return 0;
}

void qwtDestroyDataSinker(DataSinkHandle handle) {

}

D
dapan1121 已提交
432

D
dapan1121 已提交
433

D
dapan 已提交
434
void stubSetStringToPlan() {
D
dapan1121 已提交
435
  static Stub stub;
D
dapan 已提交
436
  stub.set(qStringToSubplan, qwtStringToPlan);
D
dapan1121 已提交
437
  {
D
dapan 已提交
438
    AddrAny any("libplanner.so");
D
dapan1121 已提交
439
    std::map<std::string,void*> result;
D
dapan 已提交
440
    any.get_global_func_addr_dynsym("^qStringToSubplan$", result);
D
dapan1121 已提交
441
    for (const auto& f : result) {
D
dapan 已提交
442
      stub.set(f.second, qwtStringToPlan);
D
dapan1121 已提交
443 444 445 446
    }
  }
}

D
dapan1121 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
void stubSetExecTask() {
  static Stub stub;
  stub.set(qExecTask, qwtExecTask);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qExecTask$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtExecTask);
    }
  }
}



void stubSetCreateExecTask() {
  static Stub stub;
  stub.set(qCreateExecTask, qwtCreateExecTask);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qCreateExecTask$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtCreateExecTask);
    }
  }
}

void stubSetAsyncKillTask() {
  static Stub stub;
  stub.set(qAsyncKillTask, qwtKillTask);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qAsyncKillTask$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtKillTask);
    }
  }
}

void stubSetDestroyTask() {
  static Stub stub;
  stub.set(qDestroyTask, qwtDestroyTask);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^qDestroyTask$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtDestroyTask);
    }
  }
}


void stubSetDestroyDataSinker() {
  static Stub stub;
  stub.set(dsDestroyDataSinker, qwtDestroyDataSinker);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsDestroyDataSinker$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtDestroyDataSinker);
    }
  }
}

void stubSetGetDataLength() {
  static Stub stub;
  stub.set(dsGetDataLength, qwtGetDataLength);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsGetDataLength$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtGetDataLength);
    }
  }
}

void stubSetEndPut() {
  static Stub stub;
  stub.set(dsEndPut, qwtEndPut);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsEndPut$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtEndPut);
    }
  }
}

void stubSetPutDataBlock() {
  static Stub stub;
  stub.set(dsPutDataBlock, qwtPutDataBlock);
  {
    AddrAny any("libexecutor.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsPutDataBlock$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtPutDataBlock);
    }
  }
}

D
dapan1121 已提交
554 555 556 557
void stubSetRpcSendResponse() {
  static Stub stub;
  stub.set(rpcSendResponse, qwtRpcSendResponse);
  {
D
dapan1121 已提交
558
    AddrAny any("libtransport.so");
D
dapan1121 已提交
559 560 561 562 563 564 565 566
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^rpcSendResponse$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtRpcSendResponse);
    }
  }
}

D
dapan1121 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580
void stubSetGetDataBlock() {
  static Stub stub;
  stub.set(dsGetDataBlock, qwtGetDataBlock);
  {
    AddrAny any("libtransport.so");
    std::map<std::string,void*> result;
    any.get_global_func_addr_dynsym("^dsGetDataBlock$", result);
    for (const auto& f : result) {
      stub.set(f.second, qwtGetDataBlock);
    }
  }
}


D
dapan1121 已提交
581 582 583 584 585 586 587
void *queryThread(void *param) {
  SRpcMsg queryRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;

D
dapan1121 已提交
588 589 590 591
  while (!qwtTestStop) {
    qwtBuildQueryReqMsg(&queryRpc);
    qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);    
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
592
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
593 594
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607
      printf("query:%d\n", n);
    }
  }

  return NULL;
}

void *readyThread(void *param) {
  SRpcMsg readyRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
608
  SResReadyReq readyMsg = {0};
D
dapan1121 已提交
609

D
dapan1121 已提交
610 611
  while (!qwtTestStop) {
    qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
D
dapan1121 已提交
612
    code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
D
dapan1121 已提交
613
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
614
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
615 616
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629
      printf("ready:%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 已提交
630
  SResFetchReq fetchMsg = {0};
D
dapan1121 已提交
631

D
dapan1121 已提交
632 633
  while (!qwtTestStop) {
    qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
634
    code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
D
dapan1121 已提交
635
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
636
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
637 638
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651
      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 已提交
652
  STaskDropReq dropMsg = {0};  
D
dapan1121 已提交
653

D
dapan1121 已提交
654 655
  while (!qwtTestStop) {
    qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
656
    code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
D
dapan1121 已提交
657
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
658
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
659 660
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673
      printf("drop:%d\n", n);
    }    
  }

  return NULL;
}

void *statusThread(void *param) {
  SRpcMsg statusRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;  
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;
S
Shengliang Guan 已提交
674
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
675

D
dapan1121 已提交
676 677
  while (!qwtTestStop) {
    qwtBuildStatusReqMsg(&statusMsg, &statusRpc);
D
dapan1121 已提交
678
    code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
D
dapan1121 已提交
679
    if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
680
      taosUsleep(taosRand()%5);
D
dapan1121 已提交
681 682
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
683 684 685 686 687 688 689 690
      printf("status:%d\n", n);
    }    
  }

  return NULL;
}


D
dapan1121 已提交
691
void *qwtclientThread(void *param) {
D
dapan1121 已提交
692 693 694
  int32_t code = 0;
  uint32_t n = 0;
  void *mgmt = param;
D
dapan1121 已提交
695 696 697
  void *mockPointer = (void *)0x1;    
  SRpcMsg queryRpc = {0};

wafwerar's avatar
wafwerar 已提交
698
  taosSsleep(1);
D
dapan1121 已提交
699 700

  while (!qwtTestStop) {
D
dapan1121 已提交
701 702
    qwtTestCaseFinished = false;
    
D
dapan1121 已提交
703
    qwtBuildQueryReqMsg(&queryRpc);
D
dapan 已提交
704
    qwtPutReqToQueue((void *)0x1, &queryRpc);
D
dapan1121 已提交
705 706

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

D
dapan1121 已提交
716 717
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan1121 已提交
718 719
  return NULL;
}
D
dapan1121 已提交
720

D
dapan1121 已提交
721
void *queryQueueThread(void *param) {
D
dapan1121 已提交
722 723 724 725
  void *mockPointer = (void *)0x1;   
  SRpcMsg *queryRpc = NULL;
  void *mgmt = param;

D
dapan1121 已提交
726
  while (true) {
D
dapan1121 已提交
727 728
    tsem_wait(&qwtTestQuerySem);

D
dapan1121 已提交
729
    if (qwtTestStop && qwtTestQueryQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
730 731 732
      break;
    }

D
dapan1121 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    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 已提交
748 749

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

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

wafwerar's avatar
wafwerar 已提交
766
    taosMemoryFree(queryRpc);
D
dapan1121 已提交
767

D
dapan1121 已提交
768
    if (qwtTestStop && qwtTestQueryQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
769 770
      break;
    }
D
dapan1121 已提交
771
  }
D
dapan1121 已提交
772

D
dapan1121 已提交
773 774
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan 已提交
775
  return NULL;
D
dapan1121 已提交
776 777 778
}

void *fetchQueueThread(void *param) {
D
dapan1121 已提交
779 780 781 782
  void *mockPointer = (void *)0x1;   
  SRpcMsg *fetchRpc = NULL;
  void *mgmt = param;

D
dapan1121 已提交
783
  while (true) {
D
dapan1121 已提交
784 785
    tsem_wait(&qwtTestFetchSem);

D
dapan1121 已提交
786 787 788 789
    if (qwtTestStop && qwtTestFetchQueueNum <= 0 && qwtTestCaseFinished) {
      break;
    }    

D
dapan1121 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
    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 已提交
805
    if (qwtTestEnableSleep && qwtTestReqMaxDelayUsec > 0) {
wafwerar's avatar
wafwerar 已提交
806
      int32_t delay = taosRand() % qwtTestReqMaxDelayUsec;
D
dapan1121 已提交
807 808

      if (delay) {
wafwerar's avatar
wafwerar 已提交
809
        taosUsleep(delay);
D
dapan1121 已提交
810 811 812
      }
    }

D
dapan1121 已提交
813 814 815
    switch (fetchRpc->msgType) {
      case TDMT_VND_FETCH:
        qWorkerProcessFetchMsg(mockPointer, mgmt, fetchRpc);
D
dapan1121 已提交
816
        break;
D
dapan1121 已提交
817 818
      case TDMT_VND_RES_READY:
        qWorkerProcessReadyMsg(mockPointer, mgmt, fetchRpc);
D
dapan1121 已提交
819
        break;
D
dapan1121 已提交
820 821
      case TDMT_VND_TASKS_STATUS:
        qWorkerProcessStatusMsg(mockPointer, mgmt, fetchRpc);
D
dapan1121 已提交
822
        break;
D
dapan1121 已提交
823 824
      case TDMT_VND_CANCEL_TASK:
        qWorkerProcessCancelMsg(mockPointer, mgmt, fetchRpc);
D
dapan1121 已提交
825
        break;
D
dapan1121 已提交
826 827
      case TDMT_VND_DROP_TASK:
        qWorkerProcessDropMsg(mockPointer, mgmt, fetchRpc);
D
dapan1121 已提交
828
        break;
D
dapan1121 已提交
829 830 831
      default:
        printf("unknown msg type:%d in fetch queue", fetchRpc->msgType);
        assert(0);
D
dapan1121 已提交
832
        break;
D
dapan1121 已提交
833
    }
D
dapan1121 已提交
834

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

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

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

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

D
dapan1121 已提交
847

D
dapan1121 已提交
848 849 850 851

}


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

  qwtInitLogFile();
D
dapan 已提交
863

D
dapan1121 已提交
864 865 866 867
  qwtBuildQueryReqMsg(&queryRpc);
  qwtBuildReadyReqMsg(&qwtreadyMsg, &readyRpc);
  qwtBuildFetchReqMsg(&qwtfetchMsg, &fetchRpc);
  qwtBuildDropReqMsg(&qwtdropMsg, &dropRpc);
D
dapan1121 已提交
868
  
D
dapan 已提交
869
  stubSetStringToPlan();
D
dapan1121 已提交
870
  stubSetRpcSendResponse();
D
dapan1121 已提交
871 872 873 874 875 876 877 878 879
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();
D
dapan 已提交
880
  
X
Xiaoyu Wang 已提交
881 882 883 884
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan 已提交
885 886 887 888 889
  ASSERT_EQ(code, 0);

  code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
890 891
  //code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
  //ASSERT_EQ(code, 0);
D
dapan 已提交
892 893 894 895

  code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
896 897 898
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
899
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  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};
  SRpcMsg statusRpc = {0};
D
dapan1121 已提交
913 914

  qwtInitLogFile();
D
dapan1121 已提交
915
  
D
dapan1121 已提交
916 917 918
  qwtBuildQueryReqMsg(&queryRpc);
  qwtBuildDropReqMsg(&qwtdropMsg, &dropRpc);
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
919 920 921 922

  stubSetStringToPlan();
  stubSetRpcSendResponse();
  
X
Xiaoyu Wang 已提交
923 924 925 926
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
927 928
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
929
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
930 931 932
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
933 934
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
935

D
dapan1121 已提交
936
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
937 938 939 940
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

  code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
D
dapan1121 已提交
941
  ASSERT_TRUE(0 != code);
D
dapan1121 已提交
942

D
dapan1121 已提交
943
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

  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 已提交
959 960 961 962
  SResReadyReq readyMsg = {0};
  SResFetchReq fetchMsg = {0};
  STaskDropReq dropMsg = {0};  
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
963 964

  qwtInitLogFile();
D
dapan1121 已提交
965 966 967
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
D
dapan1121 已提交
968
  stubSetCreateExecTask();
D
dapan1121 已提交
969

wafwerar's avatar
wafwerar 已提交
970
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
971
  
X
Xiaoyu Wang 已提交
972 973 974 975
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
976 977 978 979 980
  ASSERT_EQ(code, 0);

  int32_t t = 0;
  int32_t maxr = 10001;
  while (true) {
wafwerar's avatar
wafwerar 已提交
981
    int32_t r = taosRand() % maxr;
D
dapan1121 已提交
982 983
    
    if (r >= 0 && r < maxr/5) {
D
dapan1121 已提交
984 985
      printf("Query,%d\n", t++);      
      qwtBuildQueryReqMsg(&queryRpc);
D
dapan1121 已提交
986 987
      code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
    } else if (r >= maxr/5 && r < maxr * 2/5) {
D
dapan 已提交
988 989 990 991 992 993
      //printf("Ready,%d\n", t++);
      //qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
      //code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
      //if (qwtTestEnableSleep) {
      //  taosUsleep(1);
      //}
D
dapan1121 已提交
994 995
    } else if (r >= maxr * 2/5 && r < maxr* 3/5) {
      printf("Fetch,%d\n", t++);
D
dapan1121 已提交
996
      qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
997
      code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
D
dapan1121 已提交
998
      if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
999
        taosUsleep(1);
D
dapan1121 已提交
1000
      }
D
dapan1121 已提交
1001 1002
    } else if (r >= maxr * 3/5 && r < maxr * 4/5) {
      printf("Drop,%d\n", t++);
D
dapan1121 已提交
1003
      qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
1004
      code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
D
dapan1121 已提交
1005
      if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1006
        taosUsleep(1);
D
dapan1121 已提交
1007
      }
D
dapan1121 已提交
1008 1009
    } else if (r >= maxr * 4/5 && r < maxr-1) {
      printf("Status,%d\n", t++);
D
dapan1121 已提交
1010
      qwtBuildStatusReqMsg(&statusMsg, &statusRpc);
D
dapan1121 已提交
1011 1012
      code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
      ASSERT_EQ(code, 0);
D
dapan1121 已提交
1013
      if (qwtTestEnableSleep) {
wafwerar's avatar
wafwerar 已提交
1014
        taosUsleep(1);
D
dapan1121 已提交
1015
      }      
D
dapan1121 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    } else {
      printf("QUIT RAND NOW");
      break;
    }
  }

  qWorkerDestroy(&mgmt);
}

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

  qwtInitLogFile();
D
dapan1121 已提交
1031 1032 1033
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
D
dapan1121 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();
D
dapan1121 已提交
1043

wafwerar's avatar
wafwerar 已提交
1044
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1045
  
X
Xiaoyu Wang 已提交
1046 1047 1048 1049
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1050 1051
  ASSERT_EQ(code, 0);

wafwerar's avatar
wafwerar 已提交
1052 1053
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1054

X
Xiaoyu Wang 已提交
1055
  TdThread t1,t2,t3,t4,t5,t6;
wafwerar's avatar
wafwerar 已提交
1056
  taosThreadCreate(&(t1), &thattr, queryThread, mgmt);
1057
  //taosThreadCreate(&(t2), &thattr, readyThread, NULL);
wafwerar's avatar
wafwerar 已提交
1058 1059 1060
  taosThreadCreate(&(t3), &thattr, fetchThread, NULL);
  taosThreadCreate(&(t4), &thattr, dropThread, NULL);
  taosThreadCreate(&(t5), &thattr, statusThread, NULL);
X
Xiaoyu Wang 已提交
1061
  taosThreadCreate(&(t6), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1062

D
dapan1121 已提交
1063 1064
  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1065
      taosSsleep(1);
D
dapan1121 已提交
1066
    } else {
wafwerar's avatar
wafwerar 已提交
1067
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1068 1069 1070 1071 1072
      break;
    }
  }
  
  qwtTestStop = true;
wafwerar's avatar
wafwerar 已提交
1073
  taosSsleep(3);
D
dapan1121 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082

  qwtTestQueryQueueNum = 0;
  qwtTestQueryQueueRIdx = 0;
  qwtTestQueryQueueWIdx = 0;
  qwtTestQueryQueueLock = 0;
  qwtTestFetchQueueNum = 0;
  qwtTestFetchQueueRIdx = 0;
  qwtTestFetchQueueWIdx = 0;
  qwtTestFetchQueueLock = 0;
D
dapan1121 已提交
1083 1084 1085 1086
  
  qWorkerDestroy(&mgmt);
}

D
dapan1121 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
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 已提交
1106
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1107 1108 1109
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

S
Shengliang Guan 已提交
1110 1111 1112 1113
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1114 1115 1116 1117 1118 1119 1120 1121
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 0;
  qwtTestReqMaxDelayUsec = 0;

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

wafwerar's avatar
wafwerar 已提交
1122 1123
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1124

wafwerar's avatar
wafwerar 已提交
1125 1126 1127 1128
  TdThread t1,t2,t3,t4,t5;
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1129 1130 1131

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1132
      taosSsleep(1);
D
dapan1121 已提交
1133
    } else {
wafwerar's avatar
wafwerar 已提交
1134
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
      break;
    }
  }
  
  qwtTestStop = true;

  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
wafwerar's avatar
wafwerar 已提交
1146
    taosSsleep(1);
D
dapan1121 已提交
1147 1148 1149 1150 1151 1152

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

wafwerar's avatar
wafwerar 已提交
1153
        taosUsleep(10);
D
dapan1121 已提交
1154 1155
      }
    }
D
dapan1121 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    
  }

  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 已提交
1190
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1191 1192 1193
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

S
Shengliang Guan 已提交
1194 1195 1196 1197
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1198 1199 1200 1201 1202 1203 1204 1205
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 1000000;
  qwtTestReqMaxDelayUsec = 0;

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

wafwerar's avatar
wafwerar 已提交
1206 1207
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1208

wafwerar's avatar
wafwerar 已提交
1209 1210 1211 1212
  TdThread t1,t2,t3,t4,t5;
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1213 1214 1215

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1216
      taosSsleep(1);
D
dapan1121 已提交
1217
    } else {
wafwerar's avatar
wafwerar 已提交
1218
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
      break;
    }
  }
  
  qwtTestStop = true;

 
  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
wafwerar's avatar
wafwerar 已提交
1231
    taosSsleep(1);
D
dapan1121 已提交
1232 1233 1234 1235 1236 1237

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);
        
wafwerar's avatar
wafwerar 已提交
1238
        taosUsleep(10);
D
dapan1121 已提交
1239 1240
      }
    }
D
dapan1121 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    
  }

  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 已提交
1276
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1277 1278 1279
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

S
Shengliang Guan 已提交
1280 1281 1282 1283
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1284 1285 1286 1287 1288 1289 1290 1291
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 0;
  qwtTestReqMaxDelayUsec = 1000000;

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

wafwerar's avatar
wafwerar 已提交
1292 1293
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1294

wafwerar's avatar
wafwerar 已提交
1295 1296 1297 1298
  TdThread t1,t2,t3,t4,t5;
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1299 1300 1301

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1302
      taosSsleep(1);
D
dapan1121 已提交
1303
    } else {
wafwerar's avatar
wafwerar 已提交
1304
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
      break;
    }
  }
  
  qwtTestStop = true;


  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
wafwerar's avatar
wafwerar 已提交
1317
    taosSsleep(1);
D
dapan1121 已提交
1318 1319 1320 1321 1322 1323

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);
        
wafwerar's avatar
wafwerar 已提交
1324
        taosUsleep(10);
D
dapan1121 已提交
1325 1326
      }
    }
D
dapan1121 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    
  }

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


TEST(rcTest, dropTest) {
D
dapan1121 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  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 已提交
1362
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1363
  
X
Xiaoyu Wang 已提交
1364 1365 1366 1367
  SMsgCb msgCb = {0};
  msgCb.pWrapper = (struct SMgmtWrapper *)mockPointer;
  msgCb.queueFps[QUERY_QUEUE] = (PutToQueueFp)qwtPutReqToQueue;
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, &msgCb);
D
dapan1121 已提交
1368 1369
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1370 1371 1372
  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

wafwerar's avatar
wafwerar 已提交
1373 1374
  TdThreadAttr thattr;
  taosThreadAttrInit(&thattr);
D
dapan1121 已提交
1375

wafwerar's avatar
wafwerar 已提交
1376
  TdThread t1,t2,t3,t4,t5;
X
Xiaoyu Wang 已提交
1377
  taosThreadCreate(&(t1), &thattr, qwtclientThread, mgmt);
wafwerar's avatar
wafwerar 已提交
1378 1379
  taosThreadCreate(&(t2), &thattr, queryQueueThread, mgmt);
  taosThreadCreate(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1380 1381 1382

  while (true) {
    if (qwtTestDeadLoop) {
wafwerar's avatar
wafwerar 已提交
1383
      taosSsleep(1);
D
dapan1121 已提交
1384
    } else {
wafwerar's avatar
wafwerar 已提交
1385
      taosSsleep(qwtTestMTRunSec);
D
dapan1121 已提交
1386 1387 1388 1389 1390
      break;
    }
  }
  
  qwtTestStop = true;
wafwerar's avatar
wafwerar 已提交
1391
  taosSsleep(3);
D
dapan1121 已提交
1392
  
D
dapan1121 已提交
1393
  qWorkerDestroy(&mgmt);
D
dapan 已提交
1394 1395
}

D
dapan1121 已提交
1396

D
dapan1121 已提交
1397
int main(int argc, char** argv) {
wafwerar's avatar
wafwerar 已提交
1398
  taosSeedRand(taosGetTimestampSec());
D
dapan1121 已提交
1399 1400 1401 1402
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

D
dapan1121 已提交
1403
#pragma GCC diagnostic pop