syncRespMgrTest.cpp 3.9 KB
Newer Older
1
#include "syncTest.h"
M
Minghao Li 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

void logTest() {
  sTrace("--- sync log test: trace");
  sDebug("--- sync log test: debug");
  sInfo("--- sync log test: info");
  sWarn("--- sync log test: warn");
  sError("--- sync log test: error");
  sFatal("--- sync log test: fatal");
}
SSyncRespMgr *pMgr = NULL;

void syncRespMgrInsert(uint64_t count) {
  for (uint64_t i = 0; i < count; ++i) {
    SRespStub stub;
    memset(&stub, 0, sizeof(SRespStub));
    stub.createTime = taosGetTimestampMs();
    stub.rpcMsg.code = (pMgr->seqNum + 1);
S
Shengliang Guan 已提交
19 20
    stub.rpcMsg.info.ahandle = (void *)(200 + i);
    stub.rpcMsg.info.handle = (void *)(300 + i);
M
Minghao Li 已提交
21
    uint64_t ret = syncRespMgrAdd(pMgr, &stub);
S
Shengliang Guan 已提交
22
    printf("insert: %" PRIu64 " \n", ret);
M
Minghao Li 已提交
23 24 25 26 27 28 29 30 31 32 33
  }
}

void syncRespMgrDelTest(uint64_t begin, uint64_t end) {
  for (uint64_t i = begin; i <= end; ++i) {
    int32_t ret = syncRespMgrDel(pMgr, i);
    assert(ret == 0);
  }
}

void printStub(SRespStub *p) {
34 35
  printf("createTime:%" PRId64 ", rpcMsg.code:%d rpcMsg.ahandle:%" PRId64 " rpcMsg.handle:%" PRId64 " \n",
         p->createTime, p->rpcMsg.code, (int64_t)(p->rpcMsg.info.ahandle), (int64_t)(p->rpcMsg.info.handle));
M
Minghao Li 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
}
void syncRespMgrPrint() {
  printf("\n----------------syncRespMgrPrint--------------\n");
  taosThreadMutexLock(&(pMgr->mutex));

  SRespStub *p = (SRespStub *)taosHashIterate(pMgr->pRespHash, NULL);
  while (p) {
    printStub(p);
    p = (SRespStub *)taosHashIterate(pMgr->pRespHash, p);
  }

  taosThreadMutexUnlock(&(pMgr->mutex));
}

void syncRespMgrGetTest(uint64_t i) {
S
Shengliang Guan 已提交
51
  printf("------syncRespMgrGetTest-------: %" PRIu64 " -- \n", i);
M
Minghao Li 已提交
52 53 54 55 56
  SRespStub stub;
  int32_t   ret = syncRespMgrGet(pMgr, i, &stub);
  if (ret == 1) {
    printStub(&stub);
  } else if (ret == 0) {
M
Minghao Li 已提交
57
    printf("%" PRId64 " notFound \n", i);
M
Minghao Li 已提交
58 59 60 61
  }
}

void syncRespMgrGetAndDelTest(uint64_t i) {
M
Minghao Li 已提交
62
  printf("------syncRespMgrGetAndDelTest-------%" PRIu64 "-- \n", i);
63
  SRpcHandleInfo stub;
M
Minghao Li 已提交
64 65
  int32_t   ret = syncRespMgrGetAndDel(pMgr, i, &stub);
  if (ret == 1) {
66
    // printStub(&stub);
M
Minghao Li 已提交
67
  } else if (ret == 0) {
M
Minghao Li 已提交
68
    printf("%" PRId64 " notFound \n", i);
M
Minghao Li 已提交
69 70 71
  }
}

72
SSyncNode *createSyncNode() {
73
  SSyncNode *pSyncNode = (SSyncNode *)taosMemoryMalloc(sizeof(SSyncNode));
74 75 76 77
  memset(pSyncNode, 0, sizeof(SSyncNode));
  return pSyncNode;
}

M
Minghao Li 已提交
78 79
void test1() {
  printf("------- test1 ---------\n");
80
  pMgr = syncRespMgrCreate(createSyncNode(), 0);
M
Minghao Li 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  assert(pMgr != NULL);

  syncRespMgrInsert(10);
  syncRespMgrPrint();

  printf("====== get print \n");
  for (uint64_t i = 1; i <= 10; ++i) {
    syncRespMgrGetTest(i);
  }

  printf("===== delete 5 - 7 \n");
  syncRespMgrDelTest(5, 7);
  syncRespMgrPrint();

  printf("====== get print \n");
  for (uint64_t i = 1; i <= 10; ++i) {
    syncRespMgrGetTest(i);
  }

  syncRespMgrDestroy(pMgr);
}

void test2() {
  printf("------- test2 ---------\n");
105
  pMgr = syncRespMgrCreate(createSyncNode(), 0);
M
Minghao Li 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  assert(pMgr != NULL);

  syncRespMgrInsert(10);
  syncRespMgrPrint();

  printf("====== get and delete 3 - 7 \n");
  for (uint64_t i = 3; i <= 7; ++i) {
    syncRespMgrGetAndDelTest(i);
  }

  syncRespMgrPrint();
  syncRespMgrDestroy(pMgr);
}

void test3() {
  printf("------- test3 ---------\n");
122
  pMgr = syncRespMgrCreate(createSyncNode(), 0);
M
Minghao Li 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
  assert(pMgr != NULL);

  syncRespMgrInsert(10);
  syncRespMgrPrint();

  printf("====== get and delete 0 - 20 \n");
  for (uint64_t i = 0; i <= 20; ++i) {
    syncRespMgrGetAndDelTest(i);
  }

  syncRespMgrPrint();
  syncRespMgrDestroy(pMgr);
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
void test4() {
  printf("------- test4 ---------\n");
  pMgr = syncRespMgrCreate(createSyncNode(), 2);
  assert(pMgr != NULL);

  syncRespMgrInsert(5);
  syncRespMgrPrint();

  taosMsleep(3000);

  syncRespMgrInsert(3);
  syncRespMgrPrint();

  printf("====== after clean ttl \n");
  syncRespClean(pMgr);
  syncRespMgrPrint();

  syncRespMgrDestroy(pMgr);
}

M
Minghao Li 已提交
157 158
int main() {
  tsAsyncLog = 0;
159
  sDebugFlag = DEBUG_DEBUG + DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
160 161 162 163
  logTest();
  test1();
  test2();
  test3();
164
  test4();
M
Minghao Li 已提交
165 166 167

  return 0;
}