sut.cpp 5.1 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#include "sut.h"
S
Shengliang Guan 已提交
17 18

void Testbase::InitLog(const char* path) {
S
Shengliang Guan 已提交
19
  dDebugFlag = 143;
S
Shengliang Guan 已提交
20
  vDebugFlag = 0;
S
Shengliang Guan 已提交
21
  mDebugFlag = 143;
S
Shengliang Guan 已提交
22 23
  cDebugFlag = 0;
  jniDebugFlag = 0;
dengyihao's avatar
fix bug  
dengyihao 已提交
24 25 26
  tmrDebugFlag = 143;
  uDebugFlag = 143;
  rpcDebugFlag = 143;
S
Shengliang Guan 已提交
27 28 29 30 31
  qDebugFlag = 0;
  wDebugFlag = 0;
  sDebugFlag = 0;
  tsdbDebugFlag = 0;
  tscEmbeddedInUtil = 1;
S
Shengliang Guan 已提交
32
  tsAsyncLog = 0;
S
Shengliang Guan 已提交
33 34 35

  taosRemoveDir(path);
  taosMkDir(path);
S
Shengliang Guan 已提交
36
  tstrncpy(tsLogDir, path, PATH_MAX);
S
Shengliang Guan 已提交
37
  if (taosInitLog("taosdlog", 1) != 0) {
S
Shengliang Guan 已提交
38 39 40 41 42
    printf("failed to init log file\n");
  }
}

void Testbase::Init(const char* path, int16_t port) {
S
Shengliang Guan 已提交
43
  SDnodeEnvCfg cfg = {0};
S
Shengliang Guan 已提交
44
  cfg.numOfCommitThreads = 1;
S
Shengliang Guan 已提交
45
  cfg.numOfCores = 1;
S
Shengliang Guan 已提交
46 47
  cfg.rpcMaxTime = 600;
  cfg.rpcTimer = 300;
S
Shengliang Guan 已提交
48 49
  dndInit(&cfg);

S
Shengliang Guan 已提交
50 51 52 53
  char fqdn[] = "localhost";
  char firstEp[TSDB_EP_LEN] = {0};
  snprintf(firstEp, TSDB_EP_LEN, "%s:%u", fqdn, port);

S
Shengliang Guan 已提交
54
  InitLog("/tmp/td");
S
Shengliang Guan 已提交
55 56 57
  server.Start(path, fqdn, port, firstEp);
  client.Init("root", "taosdata", fqdn, port);
  taosMsleep(1100);
S
Shengliang Guan 已提交
58 59 60 61 62 63

  tFreeSTableMetaRsp(&metaRsp);
  showId = 0;
  pData = 0;
  pos = 0;
  pRetrieveRsp = NULL;
S
Shengliang Guan 已提交
64 65 66
}

void Testbase::Cleanup() {
S
Shengliang Guan 已提交
67
  tFreeSTableMetaRsp(&metaRsp);
S
Shengliang Guan 已提交
68
  client.Cleanup();
dengyihao's avatar
fix bug  
dengyihao 已提交
69 70
  taosMsleep(10);
  server.Stop();
S
Shengliang Guan 已提交
71
  dndCleanup();
S
Shengliang Guan 已提交
72 73
}

dengyihao's avatar
fix bug  
dengyihao 已提交
74 75 76 77
void Testbase::Restart() {
  server.Restart();
  client.Restart();
}
S
Shengliang Guan 已提交
78

S
Shengliang Guan 已提交
79 80 81
void Testbase::ServerStop() { server.Stop(); }

void Testbase::ServerStart() { server.DoStart(); }
dengyihao's avatar
fix bug  
dengyihao 已提交
82
void Testbase::ClientRestart() { client.Restart(); }
S
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84
SRpcMsg* Testbase::SendReq(tmsg_t msgType, void* pCont, int32_t contLen) {
S
Shengliang Guan 已提交
85 86
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = pCont;
S
Shengliang Guan 已提交
87
  rpcMsg.contLen = contLen;
S
Shengliang Guan 已提交
88 89
  rpcMsg.msgType = msgType;

S
Shengliang Guan 已提交
90
  return client.SendReq(&rpcMsg);
S
Shengliang Guan 已提交
91 92
}

S
Shengliang Guan 已提交
93
void Testbase::SendShowMetaReq(int8_t showType, const char* db) {
S
Shengliang Guan 已提交
94 95
  SShowReq showReq = {0};
  showReq.type = showType;
S
Shengliang Guan 已提交
96
  strcpy(showReq.db, db);
S
Shengliang Guan 已提交
97

S
Shengliang Guan 已提交
98
  int32_t contLen = tSerializeSShowReq(NULL, 0, &showReq);
S
Shengliang Guan 已提交
99
  void*   pReq = rpcMallocCont(contLen);
S
Shengliang Guan 已提交
100 101 102
  tSerializeSShowReq(pReq, contLen, &showReq);
  tFreeSShowReq(&showReq);

S
Shengliang Guan 已提交
103 104
  SRpcMsg* pRsp = SendReq(TDMT_MND_SHOW, pReq, contLen);
  ASSERT(pRsp->pCont != nullptr);
S
Shengliang Guan 已提交
105

S
Shengliang Guan 已提交
106 107
  if (pRsp->contLen == 0) return;

S
Shengliang Guan 已提交
108 109 110 111 112
  SShowRsp showRsp = {0};
  tDeserializeSShowRsp(pRsp->pCont, pRsp->contLen, &showRsp);
  tFreeSTableMetaRsp(&metaRsp);
  metaRsp = showRsp.tableMeta;
  showId = showRsp.showId;
S
Shengliang Guan 已提交
113 114 115
}

int32_t Testbase::GetMetaColId(int32_t index) {
S
Shengliang Guan 已提交
116
  SSchema* pSchema = &metaRsp.pSchemas[index];
S
Shengliang Guan 已提交
117 118 119 120
  return pSchema->colId;
}

int8_t Testbase::GetMetaType(int32_t index) {
S
Shengliang Guan 已提交
121
  SSchema* pSchema = &metaRsp.pSchemas[index];
S
Shengliang Guan 已提交
122 123 124 125
  return pSchema->type;
}

int32_t Testbase::GetMetaBytes(int32_t index) {
S
Shengliang Guan 已提交
126
  SSchema* pSchema = &metaRsp.pSchemas[index];
S
Shengliang Guan 已提交
127 128 129 130
  return pSchema->bytes;
}

const char* Testbase::GetMetaName(int32_t index) {
S
Shengliang Guan 已提交
131
  SSchema* pSchema = &metaRsp.pSchemas[index];
S
Shengliang Guan 已提交
132 133 134
  return pSchema->name;
}

S
Shengliang Guan 已提交
135
int32_t Testbase::GetMetaNum() { return metaRsp.numOfColumns; }
S
Shengliang Guan 已提交
136

S
Shengliang Guan 已提交
137
const char* Testbase::GetMetaTbName() { return metaRsp.tbName; }
S
Shengliang Guan 已提交
138

S
Shengliang Guan 已提交
139
void Testbase::SendShowRetrieveReq() {
S
Shengliang Guan 已提交
140 141 142
  SRetrieveTableReq retrieveReq = {0};
  retrieveReq.showId = showId;
  retrieveReq.free = 0;
S
Shengliang Guan 已提交
143

S
Shengliang Guan 已提交
144 145 146
  int32_t contLen = tSerializeSRetrieveTableReq(NULL, 0, &retrieveReq);
  void*   pReq = rpcMallocCont(contLen);
  tSerializeSRetrieveTableReq(pReq, contLen, &retrieveReq);
S
Shengliang Guan 已提交
147

S
Shengliang Guan 已提交
148
  SRpcMsg* pRsp = SendReq(TDMT_MND_SHOW_RETRIEVE, pReq, contLen);
S
Shengliang Guan 已提交
149
  pRetrieveRsp = (SRetrieveTableRsp*)pRsp->pCont;
S
Shengliang Guan 已提交
150 151 152
  pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows);
  pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds);
  pRetrieveRsp->compLen = htonl(pRetrieveRsp->compLen);
S
Shengliang Guan 已提交
153 154 155 156 157

  pData = pRetrieveRsp->data;
  pos = 0;
}

S
Shengliang Guan 已提交
158
const char* Testbase::GetShowName() { return metaRsp.tbName; }
S
Shengliang Guan 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

int8_t Testbase::GetShowInt8() {
  int8_t data = *((int8_t*)(pData + pos));
  pos += sizeof(int8_t);
  return data;
}

int16_t Testbase::GetShowInt16() {
  int16_t data = *((int16_t*)(pData + pos));
  pos += sizeof(int16_t);
  return data;
}

int32_t Testbase::GetShowInt32() {
  int32_t data = *((int32_t*)(pData + pos));
  pos += sizeof(int32_t);
  return data;
}

int64_t Testbase::GetShowInt64() {
  int64_t data = *((int64_t*)(pData + pos));
  pos += sizeof(int64_t);
  return data;
}

int64_t Testbase::GetShowTimestamp() {
  int64_t data = *((int64_t*)(pData + pos));
  pos += sizeof(int64_t);
  return data;
}

const char* Testbase::GetShowBinary(int32_t len) {
  pos += sizeof(VarDataLenT);
  char* data = (char*)(pData + pos);
  pos += len;
  return data;
S
Shengliang Guan 已提交
195 196 197 198
}

int32_t Testbase::GetShowRows() { return pRetrieveRsp->numOfRows; }

S
Shengliang Guan 已提交
199
STableMetaRsp* Testbase::GetShowMeta() { return &metaRsp; }
S
Shengliang Guan 已提交
200

dengyihao's avatar
fix bug  
dengyihao 已提交
201
SRetrieveTableRsp* Testbase::GetRetrieveRsp() { return pRetrieveRsp; }