transportTests.cpp 5.4 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
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/>.
 */

dengyihao's avatar
dengyihao 已提交
16 17
#ifdef USE_UV

dengyihao's avatar
dengyihao 已提交
18 19 20 21 22
#include <gtest/gtest.h>
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
dengyihao's avatar
dengyihao 已提交
23
#include <vector>
dengyihao's avatar
dengyihao 已提交
24

dengyihao's avatar
dengyihao 已提交
25
#include "transComm.h"
dengyihao's avatar
dengyihao 已提交
26 27 28 29 30
#include "transportInt.h"
#include "trpc.h"

using namespace std;

dengyihao's avatar
dengyihao 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
struct QueueElem {
  queue q;
  int   val;
};
class QueueObj {
 public:
  QueueObj() {
    // avoid formate
    QUEUE_INIT(&head);
  }
  void Push(QueueElem *el) {
    // avoid formate
    QUEUE_PUSH(&head, &el->q);
  }
  QueueElem *Pop() {
    QueueElem *el = NULL;
    if (!IsEmpty()) {
      queue *h = QUEUE_HEAD(&head);
      el = QUEUE_DATA(h, QueueElem, q);
dengyihao's avatar
dengyihao 已提交
50
      QUEUE_REMOVE(h);
dengyihao's avatar
dengyihao 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    }
    return el;
  }
  bool IsEmpty() {
    // avoid formate
    return QUEUE_IS_EMPTY(&head);
  }
  void RmElem(QueueElem *el) {
    // impl
    QUEUE_REMOVE(&el->q);
  }
  void ForEach(std::vector<int> &result) {
    queue *h;
    QUEUE_FOREACH(h, &head) {
      // add more
      QueueElem *el = QUEUE_DATA(h, QueueElem, q);
      result.push_back(el->val);
    }
  }

 private:
  queue head;
};
dengyihao's avatar
dengyihao 已提交
74

dengyihao's avatar
dengyihao 已提交
75 76 77 78 79 80 81 82 83
class QueueEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    // TODO
    q = new QueueObj();
  }
  virtual void TearDown() {
    delete q;
    // formate
dengyihao's avatar
dengyihao 已提交
84
  }
dengyihao's avatar
dengyihao 已提交
85 86 87 88 89 90 91 92
  QueueObj *q;
};

TEST_F(QueueEnv, testPushAndPop) {
  // add more test
  assert(q->IsEmpty());

  for (int i = 0; i < 100; i++) {
wafwerar's avatar
wafwerar 已提交
93
    QueueElem *el = (QueueElem *)taosMemoryMalloc(sizeof(QueueElem));
dengyihao's avatar
dengyihao 已提交
94 95 96 97 98 99 100
    el->val = i;
    q->Push(el);
  }
  int i = 0;
  while (!q->IsEmpty()) {
    QueueElem *el = q->Pop();
    assert(el->val == i++);
wafwerar's avatar
wafwerar 已提交
101
    taosMemoryFree(el);
dengyihao's avatar
dengyihao 已提交
102 103 104 105 106 107 108 109 110 111
  }
  assert(q->IsEmpty());
}
TEST_F(QueueEnv, testRm) {
  // add more test

  std::vector<QueueElem *> set;
  assert(q->IsEmpty());

  for (int i = 0; i < 100; i++) {
wafwerar's avatar
wafwerar 已提交
112
    QueueElem *el = (QueueElem *)taosMemoryMalloc(sizeof(QueueElem));
dengyihao's avatar
dengyihao 已提交
113 114 115 116 117 118 119
    el->val = i;
    q->Push(el);
    set.push_back(el);
  }
  for (int i = set.size() - 1; i >= 0; i--) {
    QueueElem *el = set[i];
    q->RmElem(el);
wafwerar's avatar
wafwerar 已提交
120
    taosMemoryFree(el);
dengyihao's avatar
dengyihao 已提交
121 122
  }
  assert(q->IsEmpty());
dengyihao's avatar
dengyihao 已提交
123
}
dengyihao's avatar
dengyihao 已提交
124 125 126 127 128
TEST_F(QueueEnv, testIter) {
  // add more test
  assert(q->IsEmpty());
  std::vector<int> vals;
  for (int i = 0; i < 100; i++) {
wafwerar's avatar
wafwerar 已提交
129
    QueueElem *el = (QueueElem *)taosMemoryMalloc(sizeof(QueueElem));
dengyihao's avatar
dengyihao 已提交
130 131 132 133 134 135 136 137 138
    el->val = i;
    q->Push(el);
    vals.push_back(i);
  }
  std::vector<int> result;
  q->ForEach(result);
  assert(result.size() == vals.size());
}

dengyihao's avatar
dengyihao 已提交
139 140 141
class TransCtxEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
wafwerar's avatar
wafwerar 已提交
142
    ctx = (STransCtx *)taosMemoryCalloc(1, sizeof(STransCtx));
dengyihao's avatar
dengyihao 已提交
143 144 145 146
    transCtxInit(ctx);
    // TODO
  }
  virtual void TearDown() {
dengyihao's avatar
dengyihao 已提交
147
    transCtxCleanup(ctx);
dengyihao's avatar
dengyihao 已提交
148 149 150 151 152 153 154 155
    // formate
  }
  STransCtx *ctx;
};

TEST_F(TransCtxEnv, mergeTest) {
  int key = 1;
  {
wafwerar's avatar
wafwerar 已提交
156
    STransCtx *src = (STransCtx *)taosMemoryCalloc(1, sizeof(STransCtx));
dengyihao's avatar
dengyihao 已提交
157 158
    transCtxInit(src);
    {
wafwerar's avatar
wafwerar 已提交
159
      STransCtxVal val1 = { NULL, NULL, (void (*)(const void*))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
160
      val1.val = taosMemoryMalloc(12);
dengyihao's avatar
dengyihao 已提交
161 162 163 164 165

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
wafwerar's avatar
wafwerar 已提交
166
      STransCtxVal val1 = { NULL, NULL, (void (*)(const void*))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
167
      val1.val = taosMemoryMalloc(12);
dengyihao's avatar
dengyihao 已提交
168 169 170 171
      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    transCtxMerge(ctx, src);
wafwerar's avatar
wafwerar 已提交
172
    taosMemoryFree(src);
dengyihao's avatar
dengyihao 已提交
173 174 175
  }
  EXPECT_EQ(2, taosHashGetSize(ctx->args));
  {
wafwerar's avatar
wafwerar 已提交
176
    STransCtx *src = (STransCtx *)taosMemoryCalloc(1, sizeof(STransCtx));
dengyihao's avatar
dengyihao 已提交
177 178
    transCtxInit(src);
    {
wafwerar's avatar
wafwerar 已提交
179
      STransCtxVal val1 = { NULL, NULL, (void (*)(const void*))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
180
      val1.val = taosMemoryMalloc(12);
dengyihao's avatar
dengyihao 已提交
181 182 183 184 185

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
wafwerar's avatar
wafwerar 已提交
186
      STransCtxVal val1 = { NULL, NULL, (void (*)(const void*))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
187
      val1.val = taosMemoryMalloc(12);
dengyihao's avatar
dengyihao 已提交
188 189 190 191
      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    transCtxMerge(ctx, src);
wafwerar's avatar
wafwerar 已提交
192
    taosMemoryFree(src);
dengyihao's avatar
dengyihao 已提交
193 194 195 196 197
  }
  std::string val("Hello");
  EXPECT_EQ(4, taosHashGetSize(ctx->args));
  {
    key = 1;
wafwerar's avatar
wafwerar 已提交
198
    STransCtx *src = (STransCtx *)taosMemoryCalloc(1, sizeof(STransCtx));
dengyihao's avatar
dengyihao 已提交
199 200
    transCtxInit(src);
    {
wafwerar's avatar
wafwerar 已提交
201
      STransCtxVal val1 = { NULL, NULL, (void (*)(const void*))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
202
      val1.val = taosMemoryCalloc(1, 11);
dengyihao's avatar
dengyihao 已提交
203 204 205 206 207 208
      memcpy(val1.val, val.c_str(), val.size());

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
wafwerar's avatar
wafwerar 已提交
209
      STransCtxVal val1 = { NULL, NULL, (void (*)(const void*))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
210
      val1.val = taosMemoryCalloc(1, 11);
dengyihao's avatar
dengyihao 已提交
211 212 213 214 215
      memcpy(val1.val, val.c_str(), val.size());
      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    transCtxMerge(ctx, src);
wafwerar's avatar
wafwerar 已提交
216
    taosMemoryFree(src);
dengyihao's avatar
dengyihao 已提交
217 218 219 220 221
  }
  EXPECT_EQ(4, taosHashGetSize(ctx->args));

  char *skey = (char *)transCtxDumpVal(ctx, 1);
  EXPECT_EQ(0, strcmp(skey, val.c_str()));
wafwerar's avatar
wafwerar 已提交
222
  taosMemoryFree(skey);
dengyihao's avatar
dengyihao 已提交
223 224 225 226

  skey = (char *)transCtxDumpVal(ctx, 2);
  EXPECT_EQ(0, strcmp(skey, val.c_str()));
}
dengyihao's avatar
dengyihao 已提交
227
#endif