syncRespMgrTest.cpp 3.9 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "syncRespMgr.h"
//#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"

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 已提交
23 24
    stub.rpcMsg.info.ahandle = (void *)(200 + i);
    stub.rpcMsg.info.handle = (void *)(300 + i);
M
Minghao Li 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    uint64_t ret = syncRespMgrAdd(pMgr, &stub);
    printf("insert %lu \n", ret);
  }
}

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) {
  printf("createTime:%ld, rpcMsg.code:%d rpcMsg.ahandle:%ld rpcMsg.handle:%ld \n", p->createTime, p->rpcMsg.code,
S
Shengliang Guan 已提交
39
         (int64_t)(p->rpcMsg.info.ahandle), (int64_t)(p->rpcMsg.info.handle));
M
Minghao Li 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}
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) {
  printf("------syncRespMgrGetTest------- %lu -- \n", i);
  SRespStub stub;
  int32_t   ret = syncRespMgrGet(pMgr, i, &stub);
  if (ret == 1) {
    printStub(&stub);
  } else if (ret == 0) {
    printf("%ld notFound \n", i);
  }
}

void syncRespMgrGetAndDelTest(uint64_t i) {
  printf("------syncRespMgrGetAndDelTest-------%lu-- \n", i);
  SRespStub stub;
  int32_t   ret = syncRespMgrGetAndDel(pMgr, i, &stub);
  if (ret == 1) {
    printStub(&stub);
  } else if (ret == 0) {
    printf("%ld notFound \n", i);
  }
}

76
SSyncNode *createSyncNode() {
77
  SSyncNode *pSyncNode = (SSyncNode *)taosMemoryMalloc(sizeof(SSyncNode));
78 79 80 81
  memset(pSyncNode, 0, sizeof(SSyncNode));
  return pSyncNode;
}

M
Minghao Li 已提交
82 83
void test1() {
  printf("------- test1 ---------\n");
84
  pMgr = syncRespMgrCreate(createSyncNode(), 0);
M
Minghao Li 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  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");
109
  pMgr = syncRespMgrCreate(createSyncNode(), 0);
M
Minghao Li 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  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");
126
  pMgr = syncRespMgrCreate(createSyncNode(), 0);
M
Minghao Li 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  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);
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
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 已提交
161 162
int main() {
  tsAsyncLog = 0;
163
  sDebugFlag = DEBUG_DEBUG + DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
164 165 166 167
  logTest();
  test1();
  test2();
  test3();
168
  test4();
M
Minghao Li 已提交
169 170 171

  return 0;
}