resultBufferTest.cpp 5.0 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4
#include <gtest/gtest.h>
#include <cassert>
#include <iostream>

H
Haojun Liao 已提交
5
#include "qResultbuf.h"
H
hjxilinx 已提交
6 7 8
#include "taos.h"
#include "tsdb.h"

S
Shengliang Guan 已提交
9
#pragma GCC diagnostic push
S
Shengliang Guan 已提交
10 11
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
12

H
hjxilinx 已提交
13 14 15 16
namespace {
// simple test
void simpleTest() {
  SDiskbasedResultBuf* pResultBuf = NULL;
H
Haojun Liao 已提交
17
  int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4096, 1);
H
hjxilinx 已提交
18 19 20 21 22 23 24
  
  int32_t pageId = 0;
  int32_t groupId = 0;
  
  tFilePage* pBufPage = getNewDataBuf(pResultBuf, groupId, &pageId);
  ASSERT_TRUE(pBufPage != NULL);
  
H
Haojun Liao 已提交
25
  ASSERT_EQ(getResBufSize(pResultBuf), 1024);
H
hjxilinx 已提交
26 27
  
  SIDList list = getDataBufPagesIdList(pResultBuf, groupId);
H
Haojun Liao 已提交
28
  ASSERT_EQ(taosArrayGetSize(list), 1);
H
hjxilinx 已提交
29
  ASSERT_EQ(getNumOfResultBufGroupId(pResultBuf), 1);
H
Haojun Liao 已提交
30 31 32 33 34 35

  releaseResBufPage(pResultBuf, pBufPage);

  tFilePage* pBufPage1 = getNewDataBuf(pResultBuf, groupId, &pageId);

  tFilePage* t = getResBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
36
  ASSERT_TRUE(t == pBufPage1);
H
Haojun Liao 已提交
37 38 39

  tFilePage* pBufPage2 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t1 = getResBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
40
  ASSERT_TRUE(t1 == pBufPage2);
H
Haojun Liao 已提交
41 42 43

  tFilePage* pBufPage3 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t2 = getResBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
44
  ASSERT_TRUE(t2 == pBufPage3);
H
Haojun Liao 已提交
45 46 47

  tFilePage* pBufPage4 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t3 = getResBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
48
  ASSERT_TRUE(t3 == pBufPage4);
H
Haojun Liao 已提交
49 50 51

  tFilePage* pBufPage5 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t4 = getResBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
52 53
  ASSERT_TRUE(t4 == pBufPage5);

H
Haojun Liao 已提交
54
  destroyResultBuf(pResultBuf);
H
Haojun Liao 已提交
55 56 57 58
}

void writeDownTest() {
  SDiskbasedResultBuf* pResultBuf = NULL;
H
Haojun Liao 已提交
59
  int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4*1024, 1);
H
Haojun Liao 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

  int32_t pageId = 0;
  int32_t writePageId = 0;
  int32_t groupId = 0;
  int32_t nx = 12345;

  tFilePage* pBufPage = getNewDataBuf(pResultBuf, groupId, &pageId);
  ASSERT_TRUE(pBufPage != NULL);

  *(int32_t*)(pBufPage->data) = nx;
  writePageId = pageId;
  releaseResBufPage(pResultBuf, pBufPage);

  tFilePage* pBufPage1 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t1 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t1 == pBufPage1);
  ASSERT_TRUE(pageId == 1);

  tFilePage* pBufPage2 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t2 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t2 == pBufPage2);
  ASSERT_TRUE(pageId == 2);

  tFilePage* pBufPage3 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t3 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t3 == pBufPage3);
  ASSERT_TRUE(pageId == 3);

  tFilePage* pBufPage4 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t4 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t4 == pBufPage4);
  ASSERT_TRUE(pageId == 4);
  releaseResBufPage(pResultBuf, t4);

  // flush the written page to disk, and read it out again
  tFilePage* pBufPagex = getResBufPage(pResultBuf, writePageId);
  ASSERT_EQ(*(int32_t*)pBufPagex->data, nx);

  SArray* pa = getDataBufPagesIdList(pResultBuf, groupId);
  ASSERT_EQ(taosArrayGetSize(pa), 5);

H
Haojun Liao 已提交
101
  destroyResultBuf(pResultBuf);
H
Haojun Liao 已提交
102 103 104 105
}

void recyclePageTest() {
  SDiskbasedResultBuf* pResultBuf = NULL;
H
Haojun Liao 已提交
106
  int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4*1024, 1);
H
Haojun Liao 已提交
107 108 109 110 111 112 113 114 115

  int32_t pageId = 0;
  int32_t writePageId = 0;
  int32_t groupId = 0;
  int32_t nx = 12345;

  tFilePage* pBufPage = getNewDataBuf(pResultBuf, groupId, &pageId);
  ASSERT_TRUE(pBufPage != NULL);
  releaseResBufPage(pResultBuf, pBufPage);
H
Haojun Liao 已提交
116

H
Haojun Liao 已提交
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
  tFilePage* pBufPage1 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t1 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t1 == pBufPage1);
  ASSERT_TRUE(pageId == 1);

  tFilePage* pBufPage2 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t2 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t2 == pBufPage2);
  ASSERT_TRUE(pageId == 2);

  tFilePage* pBufPage3 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t3 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t3 == pBufPage3);
  ASSERT_TRUE(pageId == 3);

  tFilePage* pBufPage4 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t4 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t4 == pBufPage4);
  ASSERT_TRUE(pageId == 4);
  releaseResBufPage(pResultBuf, t4);

  tFilePage* pBufPage5 = getNewDataBuf(pResultBuf, groupId, &pageId);
  tFilePage* t5 = getResBufPage(pResultBuf, pageId);
  ASSERT_TRUE(t5 == pBufPage5);
  ASSERT_TRUE(pageId == 5);

  // flush the written page to disk, and read it out again
  tFilePage* pBufPagex = getResBufPage(pResultBuf, writePageId);
  *(int32_t*)(pBufPagex->data) = nx;
  writePageId = pageId;   // update the data
  releaseResBufPage(pResultBuf, pBufPagex);

  tFilePage* pBufPagex1 = getResBufPage(pResultBuf, 1);

  SArray* pa = getDataBufPagesIdList(pResultBuf, groupId);
  ASSERT_EQ(taosArrayGetSize(pa), 6);
H
Haojun Liao 已提交
153

H
Haojun Liao 已提交
154
  destroyResultBuf(pResultBuf);
H
hjxilinx 已提交
155 156 157
}
} // namespace

H
Haojun Liao 已提交
158

H
hjxilinx 已提交
159
TEST(testCase, resultBufferTest) {
H
Haojun Liao 已提交
160
  srand(time(NULL));
H
hjxilinx 已提交
161
  simpleTest();
H
Haojun Liao 已提交
162
  writeDownTest();
H
Haojun Liao 已提交
163
  recyclePageTest();
H
hjxilinx 已提交
164
}
S
Shengliang Guan 已提交
165 166

#pragma GCC diagnostic pop