transUT.cc 5.3 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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 <cstdio>
#include <cstring>
dengyihao's avatar
dengyihao 已提交
18
#include "tep.h"
dengyihao's avatar
dengyihao 已提交
19
#include "tglobal.h"
dengyihao's avatar
dengyihao 已提交
20
#include "trpc.h"
dengyihao's avatar
dengyihao 已提交
21
#include "ulog.h"
dengyihao's avatar
dengyihao 已提交
22 23
using namespace std;

dengyihao's avatar
dengyihao 已提交
24 25 26 27
const char *label = "APP";
const char *secret = "secret";
const char *user = "user";
const char *ckey = "ckey";
dengyihao's avatar
dengyihao 已提交
28

dengyihao's avatar
dengyihao 已提交
29 30 31 32 33 34 35 36 37
class Server;
int port = 7000;
// server process
static void processReq(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
// client process;
static void processResp(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet);
class Client {
 public:
  void Init(int nThread) {
dengyihao's avatar
dengyihao 已提交
38 39 40
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localPort = 0;
    rpcInit.label = (char *)label;
dengyihao's avatar
dengyihao 已提交
41 42
    rpcInit.numOfThreads = nThread;
    rpcInit.cfp = processResp;
dengyihao's avatar
dengyihao 已提交
43 44 45 46
    rpcInit.user = (char *)user;
    rpcInit.secret = (char *)secret;
    rpcInit.ckey = (char *)ckey;
    rpcInit.spi = 1;
dengyihao's avatar
dengyihao 已提交
47
    rpcInit.parent = this;
dengyihao's avatar
dengyihao 已提交
48
    rpcInit.connType = TAOS_CONN_CLIENT;
dengyihao's avatar
dengyihao 已提交
49 50
    this->transCli = rpcOpen(&rpcInit);
    tsem_init(&this->sem, 0, 0);
dengyihao's avatar
dengyihao 已提交
51
  }
dengyihao's avatar
dengyihao 已提交
52 53 54 55 56 57 58 59 60
  void SetResp(SRpcMsg *pMsg) {
    // set up resp;
    this->resp = *pMsg;
  }
  SRpcMsg *Resp() { return &this->resp; }

  void Restart() {
    rpcClose(this->transCli);
    this->transCli = rpcOpen(&rpcInit);
dengyihao's avatar
dengyihao 已提交
61
  }
dengyihao's avatar
dengyihao 已提交
62

dengyihao's avatar
dengyihao 已提交
63
  void SendAndRecv(SRpcMsg *req, SRpcMsg *resp) {
dengyihao's avatar
dengyihao 已提交
64 65
    SEpSet epSet = {0};
    epSet.inUse = 0;
dengyihao's avatar
dengyihao 已提交
66
    addEpIntoEpSet(&epSet, "127.0.0.1", 7000);
dengyihao's avatar
dengyihao 已提交
67

dengyihao's avatar
dengyihao 已提交
68 69 70
    rpcSendRequest(this->transCli, &epSet, req, NULL);
    SemWait();
    *resp = this->resp;
dengyihao's avatar
dengyihao 已提交
71
  }
dengyihao's avatar
dengyihao 已提交
72 73 74 75 76 77
  void SemWait() { tsem_wait(&this->sem); }
  void SemPost() { tsem_post(&this->sem); }
  void Reset() {}

  ~Client() {
    if (this->transCli) rpcClose(this->transCli);
dengyihao's avatar
dengyihao 已提交
78 79 80
  }

 private:
dengyihao's avatar
dengyihao 已提交
81
  tsem_t   sem;
dengyihao's avatar
dengyihao 已提交
82
  SRpcInit rpcInit;
dengyihao's avatar
dengyihao 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 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
  void *   transCli;
  SRpcMsg  resp;
};
class Server {
 public:
  Server() {
    memset(&rpcInit, 0, sizeof(rpcInit));
    rpcInit.localPort = port;
    rpcInit.label = (char *)label;
    rpcInit.numOfThreads = 5;
    rpcInit.cfp = processReq;
    rpcInit.user = (char *)user;
    rpcInit.secret = (char *)secret;
    rpcInit.ckey = (char *)ckey;
    rpcInit.spi = 1;
    rpcInit.connType = TAOS_CONN_SERVER;
  }
  void Start() {
    this->transSrv = rpcOpen(&this->rpcInit);
    taosMsleep(1000);
  }
  void Stop() {
    if (this->transSrv == NULL) return;
    rpcClose(this->transSrv);
    this->transSrv = NULL;
  }
  void Restart() {
    this->Stop();
    this->Start();
  }
  ~Server() {
    if (this->transSrv) rpcClose(this->transSrv);
    this->transSrv = NULL;
  }

 private:
  SRpcInit rpcInit;
  void *   transSrv;
};
static void processReq(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
  SRpcMsg rpcMsg = {0};
  rpcMsg.pCont = rpcMallocCont(100);
  rpcMsg.contLen = 100;
  rpcMsg.handle = pMsg->handle;
  rpcMsg.code = 0;
  rpcSendResponse(&rpcMsg);
}
// client process;
static void processResp(void *parent, SRpcMsg *pMsg, SEpSet *pEpSet) {
  Client *client = (Client *)parent;
  client->SetResp(pMsg);
  client->SemPost();
}
class TransObj {
 public:
  TransObj() {
    dDebugFlag = 143;
    vDebugFlag = 0;
    mDebugFlag = 143;
    cDebugFlag = 0;
    jniDebugFlag = 0;
    tmrDebugFlag = 143;
    uDebugFlag = 143;
    rpcDebugFlag = 143;
    qDebugFlag = 0;
    wDebugFlag = 0;
    sDebugFlag = 0;
    tsdbDebugFlag = 0;
    tscEmbeddedInUtil = 1;
    tsAsyncLog = 0;

    std::string path = "/tmp/transport";
    taosRemoveDir(path.c_str());
    taosMkDir(path.c_str());

S
os env  
Shengliang Guan 已提交
158
    tstrncpy(osLogDir(), path.c_str(), PATH_MAX);
S
Shengliang Guan 已提交
159
    if (taosInitLog("taosdlog", 1) != 0) {
dengyihao's avatar
dengyihao 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      printf("failed to init log file\n");
    }
    cli = new Client;
    cli->Init(1);
    srv = new Server;
    srv->Start();
  }
  void RestartCli() { cli->Restart(); }
  void StopSrv() { srv->Stop(); }
  void RestartSrv() { srv->Restart(); }
  void cliSendAndRecv(SRpcMsg *req, SRpcMsg *resp) { cli->SendAndRecv(req, resp); }
  ~TransObj() {
    delete cli;
    delete srv;
  }

 private:
  Client *cli;
  Server *srv;
dengyihao's avatar
dengyihao 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192
};
class TransEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    // set up trans obj
    tr = new TransObj();
  }
  virtual void TearDown() {
    // tear down
    delete tr;
  }

  TransObj *tr = NULL;
};
dengyihao's avatar
dengyihao 已提交
193

dengyihao's avatar
dengyihao 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
// TEST_F(TransEnv, 01sendAndRec) {
//  for (int i = 0; i < 1; i++) {
//    SRpcMsg req = {0}, resp = {0};
//    req.msgType = 0;
//    req.pCont = rpcMallocCont(10);
//    req.contLen = 10;
//    tr->cliSendAndRecv(&req, &resp);
//    assert(resp.code == 0);
//  }
//}

TEST_F(TransEnv, 02StopServer) {
  for (int i = 0; i < 1; i++) {
    SRpcMsg req = {0}, resp = {0};
    req.msgType = 0;
    req.pCont = rpcMallocCont(10);
    req.contLen = 10;
    tr->cliSendAndRecv(&req, &resp);
    assert(resp.code == 0);
  }
  SRpcMsg req = {0}, resp = {0};
  req.msgType = 1;
  req.pCont = rpcMallocCont(10);
  req.contLen = 10;
  tr->StopSrv();
  // tr->RestartSrv();
  tr->cliSendAndRecv(&req, &resp);

  assert(resp.code != 0);
dengyihao's avatar
dengyihao 已提交
223
}