resultBufferTest.cpp 4.9 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
namespace {
// simple test
void simpleTest() {
16
  SDiskbasedBuf* 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(getTotalBufSize(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

H
Haojun Liao 已提交
31
  releaseBufPage(pResultBuf, pBufPage);
H
Haojun Liao 已提交
32 33 34

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

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

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

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

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

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

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

void writeDownTest() {
58
  SDiskbasedBuf* 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

  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;
H
Haojun Liao 已提交
71
  releaseBufPage(pResultBuf, pBufPage);
H
Haojun Liao 已提交
72 73

  tFilePage* pBufPage1 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
74
  tFilePage* t1 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
75 76 77 78
  ASSERT_TRUE(t1 == pBufPage1);
  ASSERT_TRUE(pageId == 1);

  tFilePage* pBufPage2 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
79
  tFilePage* t2 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
80 81 82 83
  ASSERT_TRUE(t2 == pBufPage2);
  ASSERT_TRUE(pageId == 2);

  tFilePage* pBufPage3 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
84
  tFilePage* t3 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
85 86 87 88
  ASSERT_TRUE(t3 == pBufPage3);
  ASSERT_TRUE(pageId == 3);

  tFilePage* pBufPage4 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
89
  tFilePage* t4 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
90 91
  ASSERT_TRUE(t4 == pBufPage4);
  ASSERT_TRUE(pageId == 4);
H
Haojun Liao 已提交
92
  releaseBufPage(pResultBuf, t4);
H
Haojun Liao 已提交
93 94

  // flush the written page to disk, and read it out again
H
Haojun Liao 已提交
95
  tFilePage* pBufPagex = getBufPage(pResultBuf, writePageId);
H
Haojun Liao 已提交
96 97 98 99 100
  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
}

void recyclePageTest() {
105
  SDiskbasedBuf* 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

  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);
H
Haojun Liao 已提交
115
  releaseBufPage(pResultBuf, pBufPage);
H
Haojun Liao 已提交
116

H
Haojun Liao 已提交
117
  tFilePage* pBufPage1 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
118
  tFilePage* t1 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
119 120 121 122
  ASSERT_TRUE(t1 == pBufPage1);
  ASSERT_TRUE(pageId == 1);

  tFilePage* pBufPage2 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
123
  tFilePage* t2 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
124 125 126 127
  ASSERT_TRUE(t2 == pBufPage2);
  ASSERT_TRUE(pageId == 2);

  tFilePage* pBufPage3 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
128
  tFilePage* t3 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
129 130 131 132
  ASSERT_TRUE(t3 == pBufPage3);
  ASSERT_TRUE(pageId == 3);

  tFilePage* pBufPage4 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
133
  tFilePage* t4 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
134 135
  ASSERT_TRUE(t4 == pBufPage4);
  ASSERT_TRUE(pageId == 4);
H
Haojun Liao 已提交
136
  releaseBufPage(pResultBuf, t4);
H
Haojun Liao 已提交
137 138

  tFilePage* pBufPage5 = getNewDataBuf(pResultBuf, groupId, &pageId);
H
Haojun Liao 已提交
139
  tFilePage* t5 = getBufPage(pResultBuf, pageId);
H
Haojun Liao 已提交
140 141 142 143
  ASSERT_TRUE(t5 == pBufPage5);
  ASSERT_TRUE(pageId == 5);

  // flush the written page to disk, and read it out again
H
Haojun Liao 已提交
144
  tFilePage* pBufPagex = getBufPage(pResultBuf, writePageId);
H
Haojun Liao 已提交
145 146
  *(int32_t*)(pBufPagex->data) = nx;
  writePageId = pageId;   // update the data
H
Haojun Liao 已提交
147
  releaseBufPage(pResultBuf, pBufPagex);
H
Haojun Liao 已提交
148

H
Haojun Liao 已提交
149
  tFilePage* pBufPagex1 = getBufPage(pResultBuf, 1);
H
Haojun Liao 已提交
150 151 152

  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