transportTests.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
/*
 * 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 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
  QueueObj *q;
};

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

  for (int i = 0; i < 100; i++) {
    QueueElem *el = (QueueElem *)malloc(sizeof(QueueElem));
    el->val = i;
    q->Push(el);
  }
  int i = 0;
  while (!q->IsEmpty()) {
    QueueElem *el = q->Pop();
    assert(el->val == i++);
    free(el);
  }
  assert(q->IsEmpty());
}
TEST_F(QueueEnv, testRm) {
  // add more test

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

  for (int i = 0; i < 100; i++) {
    QueueElem *el = (QueueElem *)malloc(sizeof(QueueElem));
    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);
    free(el);
  }
  assert(q->IsEmpty());
dengyihao's avatar
dengyihao 已提交
123
}
dengyihao's avatar
dengyihao 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
TEST_F(QueueEnv, testIter) {
  // add more test
  assert(q->IsEmpty());
  std::vector<int> vals;
  for (int i = 0; i < 100; i++) {
    QueueElem *el = (QueueElem *)malloc(sizeof(QueueElem));
    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 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 223 224 225 226 227 228 229 230 231 232
class TransCtxEnv : public ::testing::Test {
 protected:
  virtual void SetUp() {
    ctx = (STransCtx *)calloc(1, sizeof(STransCtx));
    transCtxInit(ctx);
    // TODO
  }
  virtual void TearDown() {
    transCtxDestroy(ctx);
    // formate
  }
  STransCtx *ctx;
};

TEST_F(TransCtxEnv, mergeTest) {
  int key = 1;
  {
    STransCtx *src = (STransCtx *)calloc(1, sizeof(STransCtx));
    transCtxInit(src);
    {
      STransCtxVal val1 = {.val = NULL, .len = 0, .free = free};
      val1.val = malloc(12);
      val1.len = 12;

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
      STransCtxVal val1 = {.val = NULL, .len = 0, .free = free};
      val1.val = malloc(12);
      val1.len = 12;
      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    transCtxMerge(ctx, src);
    free(src);
  }
  EXPECT_EQ(2, taosHashGetSize(ctx->args));
  {
    STransCtx *src = (STransCtx *)calloc(1, sizeof(STransCtx));
    transCtxInit(src);
    {
      STransCtxVal val1 = {.val = NULL, .len = 0, .free = free};
      val1.val = malloc(12);
      val1.len = 12;

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
      STransCtxVal val1 = {.val = NULL, .len = 0, .free = free};
      val1.val = malloc(12);
      val1.len = 12;
      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    transCtxMerge(ctx, src);
    free(src);
  }
  std::string val("Hello");
  EXPECT_EQ(4, taosHashGetSize(ctx->args));
  {
    key = 1;
    STransCtx *src = (STransCtx *)calloc(1, sizeof(STransCtx));
    transCtxInit(src);
    {
      STransCtxVal val1 = {.val = NULL, .len = 0, .free = free};
      val1.val = calloc(1, 11);
      memcpy(val1.val, val.c_str(), val.size());
      val1.len = 11;

      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    {
      STransCtxVal val1 = {.val = NULL, .len = 0, .free = free};
      val1.val = calloc(1, 11);
      memcpy(val1.val, val.c_str(), val.size());
      val1.len = 11;
      taosHashPut(src->args, &key, sizeof(key), &val1, sizeof(val1));
      key++;
    }
    transCtxMerge(ctx, src);
    free(src);
  }
  EXPECT_EQ(4, taosHashGetSize(ctx->args));

  char *skey = (char *)transCtxDumpVal(ctx, 1);
  EXPECT_EQ(0, strcmp(skey, val.c_str()));
  free(skey);

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