qworkerTests.cpp 31.8 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * 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 <tglobal.h>
#include <iostream>
#pragma GCC diagnostic ignored "-Wwrite-strings"

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "os.h"

#include "taos.h"
#include "tdef.h"
#include "tvariant.h"
#include "tep.h"
#include "trpc.h"
D
dapan 已提交
31 32
#include "planner.h"
#include "qworker.h"
D
dapan1121 已提交
33 34
#include "stub.h"
#include "addr_any.h"
D
dapan1121 已提交
35 36
#include "executor.h"
#include "dataSinkMgt.h"
D
dapan1121 已提交
37 38 39 40


namespace {

D
dapan1121 已提交
41 42 43 44 45
#define qwtTestQueryQueueSize 1000000
#define qwtTestFetchQueueSize 1000000

int32_t qwtTestMaxExecTaskUsec = 2;
int32_t qwtTestReqMaxDelayUsec = 2;
D
dapan1121 已提交
46

D
dapan1121 已提交
47
uint64_t qwtTestQueryId = 0;
D
dapan1121 已提交
48 49
bool qwtTestEnableSleep = true;
bool qwtTestStop = false;
D
dapan1121 已提交
50 51
bool qwtTestDeadLoop = false;
int32_t qwtTestMTRunSec = 60;
D
dapan1121 已提交
52 53 54
int32_t qwtTestPrintNum = 100000;
int32_t qwtTestCaseIdx = 0;
int32_t qwtTestCaseNum = 4;
D
dapan1121 已提交
55 56 57
bool qwtTestCaseFinished = false;
tsem_t qwtTestQuerySem;
tsem_t qwtTestFetchSem;
D
dapan1121 已提交
58 59
int32_t qwtTestQuitThreadNum = 0;

D
dapan1121 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

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 已提交
78
int32_t qwtTestSinkLastLen = 0;
D
dapan1121 已提交
79 80


D
dapan1121 已提交
81
SSubQueryMsg qwtqueryMsg = {0};
D
dapan1121 已提交
82 83 84 85 86 87
SRpcMsg qwtfetchRpc = {0};
SResFetchReq qwtfetchMsg = {0};
SRpcMsg qwtreadyRpc = {0};
SResReadyReq qwtreadyMsg = {0};
SRpcMsg qwtdropRpc = {0};
STaskDropReq qwtdropMsg = {0};  
D
dapan1121 已提交
88
SSchTasksStatusReq qwtstatusMsg = {0};
D
dapan1121 已提交
89

D
dapan1121 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

void qwtInitLogFile() {
  const char    *defaultLogFileNamePrefix = "taosdlog";
  const int32_t  maxLogFileNum = 10;

  tsAsyncLog = 0;
  qDebugFlag = 159;

  char temp[128] = {0};
  sprintf(temp, "%s/%s", tsLogDir, defaultLogFileNamePrefix);
  if (taosInitLog(temp, tsNumOfLogLines, maxLogFileNum) < 0) {
    printf("failed to open log file in directory:%s\n", tsLogDir);
  }

}

void qwtBuildQueryReqMsg(SRpcMsg *queryRpc) {
D
dapan1121 已提交
107 108 109 110
  qwtqueryMsg.queryId = htobe64(atomic_add_fetch_64(&qwtTestQueryId, 1));
  qwtqueryMsg.sId = htobe64(1);
  qwtqueryMsg.taskId = htobe64(1);
  qwtqueryMsg.contentLen = htonl(100);
D
dapan1121 已提交
111
  queryRpc->msgType = TDMT_VND_QUERY;
D
dapan1121 已提交
112
  queryRpc->pCont = &qwtqueryMsg;
D
dapan1121 已提交
113 114 115 116 117
  queryRpc->contLen = sizeof(SSubQueryMsg) + 100;
}

void qwtBuildReadyReqMsg(SResReadyReq *readyMsg, SRpcMsg *readyRpc) {
  readyMsg->sId = htobe64(1);
D
dapan1121 已提交
118
  readyMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
119
  readyMsg->taskId = htobe64(1);
D
dapan1121 已提交
120
  readyRpc->msgType = TDMT_VND_RES_READY;
D
dapan1121 已提交
121 122 123 124 125 126
  readyRpc->pCont = readyMsg;
  readyRpc->contLen = sizeof(SResReadyReq);
}

void qwtBuildFetchReqMsg(SResFetchReq *fetchMsg, SRpcMsg *fetchRpc) {
  fetchMsg->sId = htobe64(1);
D
dapan1121 已提交
127
  fetchMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
128
  fetchMsg->taskId = htobe64(1);
D
dapan1121 已提交
129
  fetchRpc->msgType = TDMT_VND_FETCH;
D
dapan1121 已提交
130 131 132 133 134 135
  fetchRpc->pCont = fetchMsg;
  fetchRpc->contLen = sizeof(SResFetchReq);
}

void qwtBuildDropReqMsg(STaskDropReq *dropMsg, SRpcMsg *dropRpc) {
  dropMsg->sId = htobe64(1);
D
dapan1121 已提交
136
  dropMsg->queryId = htobe64(atomic_load_64(&qwtTestQueryId));
D
dapan1121 已提交
137
  dropMsg->taskId = htobe64(1);
D
dapan1121 已提交
138
  dropRpc->msgType = TDMT_VND_DROP_TASK;
D
dapan1121 已提交
139 140 141 142 143 144 145 146 147 148
  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 已提交
149

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

D
dapan1121 已提交
155 156
int32_t qwtPutReqToFetchQueue(void *node, struct SRpcMsg *pMsg) {
  taosWLockLatch(&qwtTestFetchQueueLock);
D
dapan1121 已提交
157 158 159
  struct SRpcMsg *newMsg = (struct SRpcMsg *)calloc(1, sizeof(struct SRpcMsg));
  memcpy(newMsg, pMsg, sizeof(struct SRpcMsg));  
  qwtTestFetchQueue[qwtTestFetchQueueWIdx++] = newMsg;
D
dapan1121 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  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 已提交
178
int32_t qwtPutReqToQueue(void *node, struct SRpcMsg *pMsg) {
D
dapan1121 已提交
179
  taosWLockLatch(&qwtTestQueryQueueLock);
D
dapan1121 已提交
180 181 182
  struct SRpcMsg *newMsg = (struct SRpcMsg *)calloc(1, sizeof(struct SRpcMsg));
  memcpy(newMsg, pMsg, sizeof(struct SRpcMsg));
  qwtTestQueryQueue[qwtTestQueryQueueWIdx++] = newMsg;
D
dapan1121 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196
  if (qwtTestQueryQueueWIdx >= qwtTestQueryQueueSize) {
    qwtTestQueryQueueWIdx = 0;
  }
  
  qwtTestQueryQueueNum++;

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


D
dapan1121 已提交
201

D
dapan1121 已提交
202
void qwtRpcSendResponse(const SRpcMsg *pRsp) {
D
dapan1121 已提交
203 204 205 206 207 208 209

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

      if (0 == pRsp->code) {
        qwtBuildReadyReqMsg(&qwtreadyMsg, &qwtreadyRpc);    
D
dapan 已提交
210
        qwtPutReqToFetchQueue((void *)0x1, &qwtreadyRpc);
D
dapan1121 已提交
211 212
      } else {
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
213
        qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
214 215
      }
      
D
dapan1121 已提交
216
      rpcFreeCont(rsp);
D
dapan1121 已提交
217 218 219 220 221 222 223
      break;
    }
    case TDMT_VND_RES_READY_RSP: {
      SResReadyRsp *rsp = (SResReadyRsp *)pRsp->pCont;
      
      if (0 == pRsp->code) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
D
dapan 已提交
224
        qwtPutReqToFetchQueue((void *)0x1, &qwtfetchRpc);
D
dapan1121 已提交
225 226
      } else {
        qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
227
        qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
228
      }
D
dapan1121 已提交
229
      rpcFreeCont(rsp);
D
dapan1121 已提交
230 231 232 233 234 235 236
      break;
    }
    case TDMT_VND_FETCH_RSP: {
      SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)pRsp->pCont;
  
      if (0 == pRsp->code && 0 == rsp->completed) {
        qwtBuildFetchReqMsg(&qwtfetchMsg, &qwtfetchRpc);
D
dapan 已提交
237
        qwtPutReqToFetchQueue((void *)0x1, &qwtfetchRpc);
D
dapan1121 已提交
238 239 240 241
        return;
      }

      qwtBuildDropReqMsg(&qwtdropMsg, &qwtdropRpc);
D
dapan 已提交
242
      qwtPutReqToFetchQueue((void *)0x1, &qwtdropRpc);
D
dapan1121 已提交
243
      rpcFreeCont(rsp);
D
dapan1121 已提交
244 245 246
      
      break;
    }
D
dapan1121 已提交
247
    case TDMT_VND_DROP_TASK_RSP: {
D
dapan1121 已提交
248
      STaskDropRsp *rsp = (STaskDropRsp *)pRsp->pCont;
D
dapan1121 已提交
249
      rpcFreeCont(rsp);
D
dapan1121 已提交
250 251 252

      qwtTestCaseFinished = true;
      break;
D
dapan1121 已提交
253 254
    }
  }
D
dapan1121 已提交
255

D
dapan1121 已提交
256
  
D
dapan1121 已提交
257 258 259
  return;
}

D
dapan1121 已提交
260
int32_t qwtCreateExecTask(void* tsdb, int32_t vgId, struct SSubplan* pPlan, qTaskInfo_t* pTaskInfo, DataSinkHandle* handle) {
D
dapan1121 已提交
261 262 263 264 265
  int32_t idx = abs((++qwtTestCaseIdx) % qwtTestCaseNum);

  qwtTestSinkBlockNum = 0;
  qwtTestSinkMaxBlockNum = rand() % 100 + 1;
  qwtTestSinkQueryEnd = false;
D
dapan1121 已提交
266 267
  
  if (0 == idx) {
D
dapan1121 已提交
268 269
    *pTaskInfo = (qTaskInfo_t)qwtTestCaseIdx;
    *handle = (DataSinkHandle)qwtTestCaseIdx+1;
D
dapan1121 已提交
270 271 272 273
  } else if (1 == idx) {
    *pTaskInfo = NULL;
    *handle = NULL;
  } else if (2 == idx) {
D
dapan1121 已提交
274
    *pTaskInfo = (qTaskInfo_t)qwtTestCaseIdx;
D
dapan1121 已提交
275 276 277
    *handle = NULL;
  } else if (3 == idx) {
    *pTaskInfo = NULL;
D
dapan1121 已提交
278
    *handle = (DataSinkHandle)qwtTestCaseIdx;
D
dapan1121 已提交
279 280 281 282 283 284
  }
  
  return 0;
}

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

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

D
dapan1121 已提交
335 336
  free((void *)pInput->pData);

D
dapan1121 已提交
337 338 339 340 341 342
  taosWLockLatch(&qwtTestSinkLock);

  qwtTestSinkBlockNum++;

  if (qwtTestSinkBlockNum >= qwtTestSinkMaxBlockNum) {
    *pContinue = false;
D
dapan1121 已提交
343 344
  } else {
    *pContinue = true;
D
dapan1121 已提交
345 346 347
  }
  taosWUnLockLatch(&qwtTestSinkLock);
  
D
dapan1121 已提交
348 349 350 351
  return 0;
}

void qwtEndPut(DataSinkHandle handle, uint64_t useconds) {
D
dapan1121 已提交
352 353 354 355 356
  if (NULL == handle) {
    assert(0);
  }

  qwtTestSinkQueryEnd = true;
D
dapan1121 已提交
357 358 359
}

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

  *pQueryEnd = qwtTestSinkQueryEnd;

  atomic_sub_fetch_32(&in, 1);
D
dapan1121 已提交
385 386 387
}

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

void qwtDestroyDataSinker(DataSinkHandle handle) {

}

D
dapan1121 已提交
428

D
dapan1121 已提交
429

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

D
dapan1121 已提交
443 444 445 446 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
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 已提交
550 551 552 553
void stubSetRpcSendResponse() {
  static Stub stub;
  stub.set(rpcSendResponse, qwtRpcSendResponse);
  {
D
dapan1121 已提交
554
    AddrAny any("libtransport.so");
D
dapan1121 已提交
555 556 557 558 559 560 561 562
    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 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576
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 已提交
577 578 579 580 581 582 583
void *queryThread(void *param) {
  SRpcMsg queryRpc = {0};
  int32_t code = 0;
  uint32_t n = 0;
  void *mockPointer = (void *)0x1;    
  void *mgmt = param;

D
dapan1121 已提交
584 585 586 587 588 589 590
  while (!qwtTestStop) {
    qwtBuildQueryReqMsg(&queryRpc);
    qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);    
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603
      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 已提交
604
  SResReadyReq readyMsg = {0};
D
dapan1121 已提交
605

D
dapan1121 已提交
606 607
  while (!qwtTestStop) {
    qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
D
dapan1121 已提交
608
    code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
D
dapan1121 已提交
609 610 611 612
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625
      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 已提交
626
  SResFetchReq fetchMsg = {0};
D
dapan1121 已提交
627

D
dapan1121 已提交
628 629
  while (!qwtTestStop) {
    qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
630
    code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
D
dapan1121 已提交
631 632 633 634
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647
      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 已提交
648
  STaskDropReq dropMsg = {0};  
D
dapan1121 已提交
649

D
dapan1121 已提交
650 651
  while (!qwtTestStop) {
    qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
652
    code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
D
dapan1121 已提交
653 654 655 656
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669
      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 已提交
670
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
671

D
dapan1121 已提交
672 673
  while (!qwtTestStop) {
    qwtBuildStatusReqMsg(&statusMsg, &statusRpc);
D
dapan1121 已提交
674
    code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
D
dapan1121 已提交
675 676 677 678
    if (qwtTestEnableSleep) {
      usleep(rand()%5);
    }
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
679 680 681 682 683 684 685 686
      printf("status:%d\n", n);
    }    
  }

  return NULL;
}


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

  sleep(1);
D
dapan1121 已提交
695 696

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

    while (!qwtTestCaseFinished) {
      usleep(1);
    }
    
    
D
dapan1121 已提交
707
    if (++n % qwtTestPrintNum == 0) {
D
dapan1121 已提交
708
      printf("case run:%d\n", n);
D
dapan1121 已提交
709 710 711
    }
  }

D
dapan1121 已提交
712 713
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan1121 已提交
714 715
  return NULL;
}
D
dapan1121 已提交
716

D
dapan1121 已提交
717
void *queryQueueThread(void *param) {
D
dapan1121 已提交
718 719 720 721
  void *mockPointer = (void *)0x1;   
  SRpcMsg *queryRpc = NULL;
  void *mgmt = param;

D
dapan1121 已提交
722
  while (true) {
D
dapan1121 已提交
723 724
    tsem_wait(&qwtTestQuerySem);

D
dapan1121 已提交
725
    if (qwtTestStop && qwtTestQueryQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
726 727 728
      break;
    }

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

    if (qwtTestEnableSleep && qwtTestReqMaxDelayUsec > 0) {
      int32_t delay = rand() % qwtTestReqMaxDelayUsec;

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

    free(queryRpc);

D
dapan1121 已提交
764
    if (qwtTestStop && qwtTestQueryQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
765 766
      break;
    }
D
dapan1121 已提交
767
  }
D
dapan1121 已提交
768

D
dapan1121 已提交
769 770
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan 已提交
771
  return NULL;
D
dapan1121 已提交
772 773 774
}

void *fetchQueueThread(void *param) {
D
dapan1121 已提交
775 776 777 778
  void *mockPointer = (void *)0x1;   
  SRpcMsg *fetchRpc = NULL;
  void *mgmt = param;

D
dapan1121 已提交
779
  while (true) {
D
dapan1121 已提交
780 781
    tsem_wait(&qwtTestFetchSem);

D
dapan1121 已提交
782 783 784 785
    if (qwtTestStop && qwtTestFetchQueueNum <= 0 && qwtTestCaseFinished) {
      break;
    }    

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

      if (delay) {
        usleep(delay);
      }
    }

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

    free(fetchRpc);

D
dapan1121 已提交
833
    if (qwtTestStop && qwtTestFetchQueueNum <= 0 && qwtTestCaseFinished) {
D
dapan1121 已提交
834 835
      break;
    }    
D
dapan1121 已提交
836
  }
D
dapan1121 已提交
837

D
dapan1121 已提交
838 839
  atomic_add_fetch_32(&qwtTestQuitThreadNum, 1);

D
dapan 已提交
840
  return NULL;
D
dapan1121 已提交
841 842
}

D
dapan1121 已提交
843

D
dapan1121 已提交
844 845 846

}

D
dapan1121 已提交
847
#if 0
D
dapan1121 已提交
848

D
dapan1121 已提交
849
TEST(seqTest, normalCase) {
D
dapan 已提交
850 851 852 853 854 855 856
  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 已提交
857
  SRpcMsg statusRpc = {0};
D
dapan1121 已提交
858 859

  qwtInitLogFile();
D
dapan 已提交
860

D
dapan1121 已提交
861 862 863 864
  qwtBuildQueryReqMsg(&queryRpc);
  qwtBuildReadyReqMsg(&qwtreadyMsg, &readyRpc);
  qwtBuildFetchReqMsg(&qwtfetchMsg, &fetchRpc);
  qwtBuildDropReqMsg(&qwtdropMsg, &dropRpc);
D
dapan1121 已提交
865
  
D
dapan 已提交
866
  stubSetStringToPlan();
D
dapan1121 已提交
867
  stubSetRpcSendResponse();
D
dapan1121 已提交
868 869 870 871 872 873 874 875 876
  stubSetExecTask();
  stubSetCreateExecTask();
  stubSetAsyncKillTask();
  stubSetDestroyTask();
  stubSetDestroyDataSinker();
  stubSetGetDataLength();
  stubSetEndPut();
  stubSetPutDataBlock();
  stubSetGetDataBlock();
D
dapan 已提交
877
  
D
dapan1121 已提交
878
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan 已提交
879 880 881 882 883 884 885 886 887 888 889
  ASSERT_EQ(code, 0);

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

  code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
  ASSERT_EQ(code, 0);

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

D
dapan1121 已提交
890 891 892
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
893
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906
  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 已提交
907 908

  qwtInitLogFile();
D
dapan1121 已提交
909
  
D
dapan1121 已提交
910 911 912
  qwtBuildQueryReqMsg(&queryRpc);
  qwtBuildDropReqMsg(&qwtdropMsg, &dropRpc);
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
913 914 915 916

  stubSetStringToPlan();
  stubSetRpcSendResponse();
  
D
dapan1121 已提交
917
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan1121 已提交
918 919
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
920
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
921 922 923
  code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
  ASSERT_EQ(code, 0);

D
dapan 已提交
924 925
  code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
  ASSERT_EQ(code, 0);
D
dapan1121 已提交
926

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

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

D
dapan1121 已提交
934
  qwtBuildStatusReqMsg(&qwtstatusMsg, &statusRpc);
D
dapan1121 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  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 已提交
950 951 952 953
  SResReadyReq readyMsg = {0};
  SResFetchReq fetchMsg = {0};
  STaskDropReq dropMsg = {0};  
  SSchTasksStatusReq statusMsg = {0};
D
dapan1121 已提交
954 955

  qwtInitLogFile();
D
dapan1121 已提交
956 957 958
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();
D
dapan1121 已提交
959
  stubSetCreateExecTask();
D
dapan1121 已提交
960 961 962

  srand(time(NULL));
  
D
dapan1121 已提交
963
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan1121 已提交
964 965 966 967 968 969 970 971
  ASSERT_EQ(code, 0);

  int32_t t = 0;
  int32_t maxr = 10001;
  while (true) {
    int32_t r = rand() % maxr;
    
    if (r >= 0 && r < maxr/5) {
D
dapan1121 已提交
972 973
      printf("Query,%d\n", t++);      
      qwtBuildQueryReqMsg(&queryRpc);
D
dapan1121 已提交
974 975 976
      code = qWorkerProcessQueryMsg(mockPointer, mgmt, &queryRpc);
    } else if (r >= maxr/5 && r < maxr * 2/5) {
      printf("Ready,%d\n", t++);
D
dapan1121 已提交
977
      qwtBuildReadyReqMsg(&readyMsg, &readyRpc);
D
dapan1121 已提交
978
      code = qWorkerProcessReadyMsg(mockPointer, mgmt, &readyRpc);
D
dapan1121 已提交
979 980 981
      if (qwtTestEnableSleep) {
        usleep(1);
      }
D
dapan1121 已提交
982 983
    } else if (r >= maxr * 2/5 && r < maxr* 3/5) {
      printf("Fetch,%d\n", t++);
D
dapan1121 已提交
984
      qwtBuildFetchReqMsg(&fetchMsg, &fetchRpc);
D
dapan1121 已提交
985
      code = qWorkerProcessFetchMsg(mockPointer, mgmt, &fetchRpc);
D
dapan1121 已提交
986 987 988
      if (qwtTestEnableSleep) {
        usleep(1);
      }
D
dapan1121 已提交
989 990
    } else if (r >= maxr * 3/5 && r < maxr * 4/5) {
      printf("Drop,%d\n", t++);
D
dapan1121 已提交
991
      qwtBuildDropReqMsg(&dropMsg, &dropRpc);
D
dapan1121 已提交
992
      code = qWorkerProcessDropMsg(mockPointer, mgmt, &dropRpc);
D
dapan1121 已提交
993 994 995
      if (qwtTestEnableSleep) {
        usleep(1);
      }
D
dapan1121 已提交
996 997
    } else if (r >= maxr * 4/5 && r < maxr-1) {
      printf("Status,%d\n", t++);
D
dapan1121 已提交
998
      qwtBuildStatusReqMsg(&statusMsg, &statusRpc);
D
dapan1121 已提交
999 1000
      code = qWorkerProcessStatusMsg(mockPointer, mgmt, &statusRpc);
      ASSERT_EQ(code, 0);
D
dapan1121 已提交
1001 1002 1003
      if (qwtTestEnableSleep) {
        usleep(1);
      }      
D
dapan1121 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    } else {
      printf("QUIT RAND NOW");
      break;
    }
  }

  qWorkerDestroy(&mgmt);
}

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

  qwtInitLogFile();
D
dapan1121 已提交
1019 1020 1021 1022 1023 1024
  
  stubSetStringToPlan();
  stubSetRpcSendResponse();

  srand(time(NULL));
  
D
dapan1121 已提交
1025
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
D
dapan1121 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  ASSERT_EQ(code, 0);

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
  pthread_create(&(t1), &thattr, queryThread, mgmt);
  pthread_create(&(t2), &thattr, readyThread, NULL);
  pthread_create(&(t3), &thattr, fetchThread, NULL);
  pthread_create(&(t4), &thattr, dropThread, NULL);
  pthread_create(&(t5), &thattr, statusThread, NULL);

D
dapan1121 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;
  sleep(3);
  
  qWorkerDestroy(&mgmt);
}

D
dapan1121 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
#endif

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();

  srand(time(NULL));
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 0;
  qwtTestReqMaxDelayUsec = 0;

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

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
  pthread_create(&(t1), &thattr, qwtclientThread, mgmt);
  pthread_create(&(t2), &thattr, queryQueueThread, mgmt);
  pthread_create(&(t3), &thattr, fetchQueueThread, mgmt);

  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;

  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
D
dapan1121 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    sleep(1);

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

        usleep(10);
      }
    }
D
dapan1121 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 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 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 1190 1191 1192
    
  }

  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();

  srand(time(NULL));
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 1000000;
  qwtTestReqMaxDelayUsec = 0;

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

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
  pthread_create(&(t1), &thattr, qwtclientThread, mgmt);
  pthread_create(&(t2), &thattr, queryQueueThread, mgmt);
  pthread_create(&(t3), &thattr, fetchQueueThread, mgmt);

  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;

 
  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
D
dapan1121 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    sleep(1);

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);
        
        usleep(10);
      }
    }
D
dapan1121 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 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 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();

  srand(time(NULL));
  qwtTestStop = false;
  qwtTestQuitThreadNum = 0;

  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
  ASSERT_EQ(code, 0);

  qwtTestMaxExecTaskUsec = 0;
  qwtTestReqMaxDelayUsec = 1000000;

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

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
  pthread_create(&(t1), &thattr, qwtclientThread, mgmt);
  pthread_create(&(t2), &thattr, queryQueueThread, mgmt);
  pthread_create(&(t3), &thattr, fetchQueueThread, mgmt);

  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;


  while (true) {
    if (qwtTestQuitThreadNum == 3) {
      break;
    }
    
D
dapan1121 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    sleep(1);

    if (qwtTestCaseFinished) {
      if (qwtTestQuitThreadNum < 3) { 
        tsem_post(&qwtTestQuerySem);
        tsem_post(&qwtTestFetchSem);
        
        usleep(10);
      }
    }
D
dapan1121 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    
  }

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


#if 0
TEST(rcTest, dropTest) {
D
dapan1121 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
  void *mgmt = NULL;
  int32_t code = 0;
  void *mockPointer = (void *)0x1;

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

  srand(time(NULL));
  
  code = qWorkerInit(NODE_TYPE_VNODE, 1, NULL, &mgmt, mockPointer, qwtPutReqToQueue);
  ASSERT_EQ(code, 0);

D
dapan1121 已提交
1327 1328 1329
  tsem_init(&qwtTestQuerySem, 0, 0);
  tsem_init(&qwtTestFetchSem, 0, 0);

D
dapan1121 已提交
1330 1331 1332 1333
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);

  pthread_t t1,t2,t3,t4,t5;
D
dapan1121 已提交
1334 1335 1336
  pthread_create(&(t1), &thattr, clientThread, mgmt);
  pthread_create(&(t2), &thattr, queryQueueThread, mgmt);
  pthread_create(&(t3), &thattr, fetchQueueThread, mgmt);
D
dapan1121 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

  while (true) {
    if (qwtTestDeadLoop) {
      sleep(1);
    } else {
      sleep(qwtTestMTRunSec);
      break;
    }
  }
  
  qwtTestStop = true;
  sleep(3);
D
dapan1121 已提交
1349
  
D
dapan1121 已提交
1350
  qWorkerDestroy(&mgmt);
D
dapan 已提交
1351
}
D
dapan1121 已提交
1352
#endif
D
dapan 已提交
1353

D
dapan1121 已提交
1354

D
dapan1121 已提交
1355
int main(int argc, char** argv) {
D
dapan1121 已提交
1356
  srand(time(NULL));
D
dapan1121 已提交
1357 1358 1359 1360 1361 1362
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}