syncEncodeTest.cpp 4.5 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.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");
}

#define PING_MSG_LEN 20

M
Minghao Li 已提交
18
void test1() {
M
Minghao Li 已提交
19
  sTrace("test1: ---- syncPingSerialize, syncPingDeserialize");
M
Minghao Li 已提交
20 21 22

  char msg[PING_MSG_LEN];
  snprintf(msg, sizeof(msg), "%s", "test ping");
M
Minghao Li 已提交
23 24 25 26 27 28
  SyncPing* pMsg = syncPingBuild(PING_MSG_LEN);
  pMsg->srcId.addr = 1;
  pMsg->srcId.vgId = 2;
  pMsg->destId.addr = 3;
  pMsg->destId.vgId = 4;
  memcpy(pMsg->data, msg, PING_MSG_LEN);
M
Minghao Li 已提交
29 30

  {
M
Minghao Li 已提交
31
    cJSON* pJson = syncPing2Json(pMsg);
M
Minghao Li 已提交
32 33 34 35 36 37
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPing: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

M
Minghao Li 已提交
38
  uint32_t bufLen = pMsg->bytes;
M
Minghao Li 已提交
39
  char*    buf = (char*)malloc(bufLen);
M
Minghao Li 已提交
40
  syncPingSerialize(pMsg, buf, bufLen);
M
Minghao Li 已提交
41

M
Minghao Li 已提交
42 43
  SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes);
  syncPingDeserialize(buf, bufLen, pMsg2);
M
Minghao Li 已提交
44 45

  {
M
Minghao Li 已提交
46
    cJSON* pJson = syncPing2Json(pMsg2);
M
Minghao Li 已提交
47 48 49 50 51 52
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPing2: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

M
Minghao Li 已提交
53 54
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
M
Minghao Li 已提交
55
  free(buf);
M
Minghao Li 已提交
56 57 58
}

void test2() {
M
Minghao Li 已提交
59
  sTrace("test2: ---- syncPing2RpcMsg, syncPingFromRpcMsg");
M
Minghao Li 已提交
60 61 62

  char msg[PING_MSG_LEN];
  snprintf(msg, sizeof(msg), "%s", "hello raft");
M
Minghao Li 已提交
63 64 65 66 67 68
  SyncPing* pMsg = syncPingBuild(PING_MSG_LEN);
  pMsg->srcId.addr = 100;
  pMsg->srcId.vgId = 200;
  pMsg->destId.addr = 300;
  pMsg->destId.vgId = 400;
  memcpy(pMsg->data, msg, PING_MSG_LEN);
M
Minghao Li 已提交
69 70

  {
M
Minghao Li 已提交
71
    cJSON* pJson = syncPing2Json(pMsg);
M
Minghao Li 已提交
72 73 74 75 76 77 78
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPing: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

  SRpcMsg rpcMsg;
M
Minghao Li 已提交
79 80 81
  syncPing2RpcMsg(pMsg, &rpcMsg);
  SyncPing* pMsg2 = (SyncPing*)malloc(pMsg->bytes);
  syncPingFromRpcMsg(&rpcMsg, pMsg2);
M
Minghao Li 已提交
82 83 84
  rpcFreeCont(rpcMsg.pCont);

  {
M
Minghao Li 已提交
85
    cJSON* pJson = syncPing2Json(pMsg2);
M
Minghao Li 已提交
86 87 88 89 90 91
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPing2: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

M
Minghao Li 已提交
92 93
  syncPingDestroy(pMsg);
  syncPingDestroy(pMsg2);
M
Minghao Li 已提交
94 95
}

M
Minghao Li 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
void test3() {
  sTrace("test3: ---- syncPingReplySerialize, syncPingReplyDeserialize");

  char msg[PING_MSG_LEN];
  snprintf(msg, sizeof(msg), "%s", "test ping");
  SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN);
  pMsg->srcId.addr = 19;
  pMsg->srcId.vgId = 29;
  pMsg->destId.addr = 39;
  pMsg->destId.vgId = 49;
  memcpy(pMsg->data, msg, PING_MSG_LEN);

  {
    cJSON* pJson = syncPingReply2Json(pMsg);
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPingReply: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

  uint32_t bufLen = pMsg->bytes;
  char*    buf = (char*)malloc(bufLen);
  syncPingReplySerialize(pMsg, buf, bufLen);

  SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes);
  syncPingReplyDeserialize(buf, bufLen, pMsg2);

  {
    cJSON* pJson = syncPingReply2Json(pMsg2);
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPingReply2: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

  syncPingReplyDestroy(pMsg);
  syncPingReplyDestroy(pMsg2);
  free(buf);
}

void test4() {
  sTrace("test4: ---- syncPingReply2RpcMsg, syncPingReplyFromRpcMsg");

  char msg[PING_MSG_LEN];
  snprintf(msg, sizeof(msg), "%s", "hello raft");
  SyncPingReply* pMsg = syncPingReplyBuild(PING_MSG_LEN);
  pMsg->srcId.addr = 66;
  pMsg->srcId.vgId = 77;
  pMsg->destId.addr = 88;
  pMsg->destId.vgId = 99;
  memcpy(pMsg->data, msg, PING_MSG_LEN);

  {
    cJSON* pJson = syncPingReply2Json(pMsg);
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPingReply: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

  SRpcMsg rpcMsg;
  syncPingReply2RpcMsg(pMsg, &rpcMsg);
  SyncPingReply* pMsg2 = (SyncPingReply*)malloc(pMsg->bytes);
  syncPingReplyFromRpcMsg(&rpcMsg, pMsg2);
  rpcFreeCont(rpcMsg.pCont);

  {
    cJSON* pJson = syncPingReply2Json(pMsg2);
    char*  serialized = cJSON_Print(pJson);
    printf("SyncPingReply2: \n%s\n\n", serialized);
    free(serialized);
    cJSON_Delete(pJson);
  }

  syncPingReplyDestroy(pMsg);
  syncPingReplyDestroy(pMsg2);
}
M
Minghao Li 已提交
173 174 175 176 177 178 179
int main() {
  // taosInitLog((char*)"syncPingTest.log", 100000, 10);
  tsAsyncLog = 0;
  sDebugFlag = 143 + 64;

  test1();
  test2();
M
Minghao Li 已提交
180 181
  test3();
  test4();
M
Minghao Li 已提交
182 183 184

  return 0;
}