transportTests.cpp 5.6 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
    // formate
  }
  STransCtx *ctx;
};

dengyihao's avatar
dengyihao 已提交
153 154 155 156 157 158
int32_t cloneVal(void *src, void **dst) {
  int sz = (int)strlen((char *)src);
  *dst = taosMemoryCalloc(1, sz + 1);
  memcpy(*dst, src, sz);
  return 0;
}
dengyihao's avatar
dengyihao 已提交
159 160 161
TEST_F(TransCtxEnv, mergeTest) {
  int key = 1;
  {
wafwerar's avatar
wafwerar 已提交
162
    STransCtx *src = (STransCtx *)taosMemoryCalloc(1, sizeof(STransCtx));
dengyihao's avatar
dengyihao 已提交
163 164
    transCtxInit(src);
    {
dengyihao's avatar
dengyihao 已提交
165
      STransCtxVal val1 = {NULL, NULL, (void (*)(const void *))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
166
      val1.val = taosMemoryMalloc(12);
dengyihao's avatar
dengyihao 已提交
167 168 169 170 171

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

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

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
dengyihao's avatar
dengyihao 已提交
216
      STransCtxVal val1 = {NULL, NULL, (void (*)(const void *))taosMemoryFree};
wafwerar's avatar
wafwerar 已提交
217
      val1.val = taosMemoryCalloc(1, 11);
dengyihao's avatar
dengyihao 已提交
218
      val1.clone = cloneVal;
dengyihao's avatar
dengyihao 已提交
219 220 221 222 223
      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 已提交
224
    taosMemoryFree(src);
dengyihao's avatar
dengyihao 已提交
225 226 227 228 229
  }
  EXPECT_EQ(4, taosHashGetSize(ctx->args));

  char *skey = (char *)transCtxDumpVal(ctx, 1);
  EXPECT_EQ(0, strcmp(skey, val.c_str()));
wafwerar's avatar
wafwerar 已提交
230
  taosMemoryFree(skey);
dengyihao's avatar
dengyihao 已提交
231 232 233 234

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