tqMetaTest.cpp 6.9 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11
#include <gtest/gtest.h>
#include <cstring>
#include <iostream>
#include <queue>

#include "tqMetaStore.h"

struct Foo {
  int32_t a;
};

L
Liu Jicong 已提交
12
int FooSerializer(const void* pObj, STqSerializedHead** ppHead) {
L
Liu Jicong 已提交
13 14
  Foo* foo = (Foo*)pObj;
  if ((*ppHead) == NULL || (*ppHead)->ssize < sizeof(STqSerializedHead) + sizeof(int32_t)) {
L
Liu Jicong 已提交
15 16
    *ppHead = (STqSerializedHead*)realloc(*ppHead, sizeof(STqSerializedHead) + sizeof(int32_t));
    (*ppHead)->ssize = sizeof(STqSerializedHead) + sizeof(int32_t);
L
Liu Jicong 已提交
17 18 19
  }
  *(int32_t*)(*ppHead)->content = foo->a;
  return (*ppHead)->ssize;
L
Liu Jicong 已提交
20 21
}

L
Liu Jicong 已提交
22
const void* FooDeserializer(const STqSerializedHead* pHead, void** ppObj) {
L
Liu Jicong 已提交
23
  if (*ppObj == NULL) {
L
Liu Jicong 已提交
24 25 26
    *ppObj = realloc(*ppObj, sizeof(int32_t));
  }
  Foo* pFoo = *(Foo**)ppObj;
L
Liu Jicong 已提交
27
  pFoo->a = *(int32_t*)pHead->content;
L
Liu Jicong 已提交
28 29 30
  return NULL;
}

L
Liu Jicong 已提交
31
void FooDeleter(void* pObj) { free(pObj); }
L
Liu Jicong 已提交
32

L
Liu Jicong 已提交
33
class TqMetaUpdateAppendTest : public ::testing::Test {
L
Liu Jicong 已提交
34 35 36 37 38 39 40 41 42 43 44
 protected:
  void SetUp() override {
    taosRemoveDir(pathName);
    pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
    ASSERT(pMeta);
  }

  void TearDown() override { tqStoreClose(pMeta); }

  STqMetaStore* pMeta;
  const char*   pathName = "/tmp/tq_test";
L
Liu Jicong 已提交
45 46
};

L
Liu Jicong 已提交
47
TEST_F(TqMetaUpdateAppendTest, copyPutTest) {
L
Liu Jicong 已提交
48 49 50 51
  Foo foo;
  foo.a = 3;
  tqHandleCopyPut(pMeta, 1, &foo, sizeof(Foo));

L
Liu Jicong 已提交
52
  Foo* pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
53
  EXPECT_EQ(pFoo == NULL, true);
L
Liu Jicong 已提交
54 55

  tqHandleCommit(pMeta, 1);
L
Liu Jicong 已提交
56
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
57
  EXPECT_EQ(pFoo->a, 3);
L
Liu Jicong 已提交
58 59
}

L
Liu Jicong 已提交
60
TEST_F(TqMetaUpdateAppendTest, persistTest) {
L
Liu Jicong 已提交
61 62
  Foo* pFoo = (Foo*)malloc(sizeof(Foo));
  pFoo->a = 2;
L
Liu Jicong 已提交
63
  tqHandleMovePut(pMeta, 1, pFoo);
L
Liu Jicong 已提交
64 65 66 67 68 69 70 71 72
  Foo* pBar = (Foo*)tqHandleGet(pMeta, 1);
  EXPECT_EQ(pBar == NULL, true);
  tqHandleCommit(pMeta, 1);
  pBar = (Foo*)tqHandleGet(pMeta, 1);
  EXPECT_EQ(pBar->a, pFoo->a);
  pBar = (Foo*)tqHandleGet(pMeta, 2);
  EXPECT_EQ(pBar == NULL, true);

  tqStoreClose(pMeta);
L
Liu Jicong 已提交
73
  pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
L
Liu Jicong 已提交
74 75 76 77 78 79 80 81 82 83
  ASSERT(pMeta);

  pBar = (Foo*)tqHandleGet(pMeta, 1);
  ASSERT_EQ(pBar != NULL, true);
  EXPECT_EQ(pBar->a, 2);

  pBar = (Foo*)tqHandleGet(pMeta, 2);
  EXPECT_EQ(pBar == NULL, true);
}

L
Liu Jicong 已提交
84
TEST_F(TqMetaUpdateAppendTest, uncommittedTest) {
L
Liu Jicong 已提交
85 86
  Foo* pFoo = (Foo*)malloc(sizeof(Foo));
  pFoo->a = 3;
L
Liu Jicong 已提交
87
  tqHandleMovePut(pMeta, 1, pFoo);
L
Liu Jicong 已提交
88

L
Liu Jicong 已提交
89
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
90 91 92
  EXPECT_EQ(pFoo == NULL, true);
}

L
Liu Jicong 已提交
93
TEST_F(TqMetaUpdateAppendTest, abortTest) {
L
Liu Jicong 已提交
94 95
  Foo* pFoo = (Foo*)malloc(sizeof(Foo));
  pFoo->a = 3;
L
Liu Jicong 已提交
96
  tqHandleMovePut(pMeta, 1, pFoo);
L
Liu Jicong 已提交
97

L
Liu Jicong 已提交
98
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
99 100 101
  EXPECT_EQ(pFoo == NULL, true);

  tqHandleAbort(pMeta, 1);
L
Liu Jicong 已提交
102
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
103 104
  EXPECT_EQ(pFoo == NULL, true);
}
L
Liu Jicong 已提交
105

L
Liu Jicong 已提交
106
TEST_F(TqMetaUpdateAppendTest, deleteTest) {
L
Liu Jicong 已提交
107 108 109 110
  Foo* pFoo = (Foo*)malloc(sizeof(Foo));
  pFoo->a = 3;
  tqHandleMovePut(pMeta, 1, pFoo);

L
Liu Jicong 已提交
111
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
112 113 114 115
  EXPECT_EQ(pFoo == NULL, true);

  tqHandleCommit(pMeta, 1);

L
Liu Jicong 已提交
116
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
117 118 119 120
  ASSERT_EQ(pFoo != NULL, true);
  EXPECT_EQ(pFoo->a, 3);

  tqHandleDel(pMeta, 1);
L
Liu Jicong 已提交
121
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
122 123 124 125
  ASSERT_EQ(pFoo != NULL, true);
  EXPECT_EQ(pFoo->a, 3);

  tqHandleCommit(pMeta, 1);
L
Liu Jicong 已提交
126
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
127
  EXPECT_EQ(pFoo == NULL, true);
L
Liu Jicong 已提交
128 129

  tqStoreClose(pMeta);
L
Liu Jicong 已提交
130
  pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
L
Liu Jicong 已提交
131
  ASSERT(pMeta);
L
Liu Jicong 已提交
132

L
Liu Jicong 已提交
133
  pFoo = (Foo*)tqHandleGet(pMeta, 1);
L
Liu Jicong 已提交
134
  EXPECT_EQ(pFoo == NULL, true);
L
Liu Jicong 已提交
135
}
L
Liu Jicong 已提交
136

L
Liu Jicong 已提交
137
TEST_F(TqMetaUpdateAppendTest, intxnPersist) {
L
Liu Jicong 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150
  Foo* pFoo = (Foo*)malloc(sizeof(Foo));
  pFoo->a = 3;
  tqHandleMovePut(pMeta, 1, pFoo);
  tqHandleCommit(pMeta, 1);

  Foo* pBar = (Foo*)malloc(sizeof(Foo));
  pBar->a = 4;
  tqHandleMovePut(pMeta, 1, pBar);

  Foo* pFoo1 = (Foo*)tqHandleGet(pMeta, 1);
  EXPECT_EQ(pFoo1->a, 3);

  tqStoreClose(pMeta);
L
Liu Jicong 已提交
151
  pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
L
Liu Jicong 已提交
152 153 154 155 156 157 158 159 160 161 162
  ASSERT(pMeta);

  pFoo1 = (Foo*)tqHandleGet(pMeta, 1);
  EXPECT_EQ(pFoo1->a, 3);

  tqHandleCommit(pMeta, 1);

  pFoo1 = (Foo*)tqHandleGet(pMeta, 1);
  EXPECT_EQ(pFoo1->a, 4);

  tqStoreClose(pMeta);
L
Liu Jicong 已提交
163
  pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
L
Liu Jicong 已提交
164 165 166 167 168
  ASSERT(pMeta);

  pFoo1 = (Foo*)tqHandleGet(pMeta, 1);
  EXPECT_EQ(pFoo1->a, 4);
}
L
Liu Jicong 已提交
169

L
Liu Jicong 已提交
170
TEST_F(TqMetaUpdateAppendTest, multiplePage) {
L
Liu Jicong 已提交
171 172
  srand(0);
  std::vector<int> v;
L
Liu Jicong 已提交
173
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
174 175 176 177 178
    v.push_back(rand());
    Foo foo;
    foo.a = v[i];
    tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo));
  }
L
Liu Jicong 已提交
179
  for (int i = 0; i < 500; i++) {
L
Liu Jicong 已提交
180 181 182 183 184 185 186
    tqHandleCommit(pMeta, i);
    Foo* pFoo = (Foo*)tqHandleGet(pMeta, i);
    ASSERT_EQ(pFoo != NULL, true) << " at idx " << i << "\n";
    EXPECT_EQ(pFoo->a, v[i]);
  }

  tqStoreClose(pMeta);
L
Liu Jicong 已提交
187
  pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
L
Liu Jicong 已提交
188
  ASSERT(pMeta);
L
Liu Jicong 已提交
189 190

  for (int i = 500; i < 1000; i++) {
L
Liu Jicong 已提交
191 192 193 194 195 196
    tqHandleCommit(pMeta, i);
    Foo* pFoo = (Foo*)tqHandleGet(pMeta, i);
    ASSERT_EQ(pFoo != NULL, true) << " at idx " << i << "\n";
    EXPECT_EQ(pFoo->a, v[i]);
  }

L
Liu Jicong 已提交
197
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
198 199 200 201 202 203
    Foo* pFoo = (Foo*)tqHandleGet(pMeta, i);
    ASSERT_EQ(pFoo != NULL, true) << " at idx " << i << "\n";
    EXPECT_EQ(pFoo->a, v[i]);
  }
}

L
Liu Jicong 已提交
204
TEST_F(TqMetaUpdateAppendTest, multipleRewrite) {
L
Liu Jicong 已提交
205 206
  srand(0);
  std::vector<int> v;
L
Liu Jicong 已提交
207
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
208 209 210 211 212 213
    v.push_back(rand());
    Foo foo;
    foo.a = v[i];
    tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo));
  }

L
Liu Jicong 已提交
214
  for (int i = 0; i < 500; i++) {
L
Liu Jicong 已提交
215 216 217 218 219 220 221
    tqHandleCommit(pMeta, i);
    v[i] = rand();
    Foo foo;
    foo.a = v[i];
    tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo));
  }

L
Liu Jicong 已提交
222
  for (int i = 500; i < 1000; i++) {
L
Liu Jicong 已提交
223 224 225 226 227 228
    v[i] = rand();
    Foo foo;
    foo.a = v[i];
    tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo));
  }

L
Liu Jicong 已提交
229
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
230 231 232 233
    tqHandleCommit(pMeta, i);
  }

  tqStoreClose(pMeta);
L
Liu Jicong 已提交
234
  pMeta = tqStoreOpen(pathName, FooSerializer, FooDeserializer, FooDeleter, TQ_UPDATE_APPEND);
L
Liu Jicong 已提交
235
  ASSERT(pMeta);
L
Liu Jicong 已提交
236 237

  for (int i = 500; i < 1000; i++) {
L
Liu Jicong 已提交
238 239 240 241 242 243 244
    v[i] = rand();
    Foo foo;
    foo.a = v[i];
    tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo));
    tqHandleCommit(pMeta, i);
  }

L
Liu Jicong 已提交
245
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
246 247 248 249 250 251
    Foo* pFoo = (Foo*)tqHandleGet(pMeta, i);
    ASSERT_EQ(pFoo != NULL, true) << " at idx " << i << "\n";
    EXPECT_EQ(pFoo->a, v[i]);
  }
}

L
Liu Jicong 已提交
252
TEST_F(TqMetaUpdateAppendTest, dupCommit) {
L
Liu Jicong 已提交
253 254
  srand(0);
  std::vector<int> v;
L
Liu Jicong 已提交
255
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
256 257 258 259 260 261
    v.push_back(rand());
    Foo foo;
    foo.a = v[i];
    tqHandleCopyPut(pMeta, i, &foo, sizeof(Foo));
  }

L
Liu Jicong 已提交
262
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
263 264 265 266 267 268
    int ret = tqHandleCommit(pMeta, i);
    EXPECT_EQ(ret, 0);
    ret = tqHandleCommit(pMeta, i);
    EXPECT_EQ(ret, -1);
  }

L
Liu Jicong 已提交
269
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
270 271 272 273
    int ret = tqHandleCommit(pMeta, i);
    EXPECT_EQ(ret, -1);
  }

L
Liu Jicong 已提交
274
  for (int i = 0; i < 1000; i++) {
L
Liu Jicong 已提交
275 276 277 278 279
    Foo* pFoo = (Foo*)tqHandleGet(pMeta, i);
    ASSERT_EQ(pFoo != NULL, true) << " at idx " << i << "\n";
    EXPECT_EQ(pFoo->a, v[i]);
  }
}