pageBufferTest.cpp 5.4 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <gtest/gtest.h>
#include <cassert>
#include <iostream>

#include "taos.h"
#include "tpagedbuf.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"

namespace {
// simple test
void simpleTest() {
15
  SDiskbasedBuf* pBuf = NULL;
H
Hongze Cheng 已提交
16 17
  int32_t        ret = createDiskbasedBuf(&pBuf, 1024, 4096, "", TD_TMP_DIR_PATH);

H
Haojun Liao 已提交
18 19
  int32_t pageId = 0;
  int32_t groupId = 0;
H
Hongze Cheng 已提交
20

21
  SFilePage* pBufPage = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
H
Haojun Liao 已提交
22
  ASSERT_TRUE(pBufPage != NULL);
H
Hongze Cheng 已提交
23

24
  ASSERT_EQ(getTotalBufSize(pBuf), 1024);
H
Hongze Cheng 已提交
25

26
  SArray* list = getDataBufPagesIdList(pBuf);
H
Haojun Liao 已提交
27
  ASSERT_EQ(taosArrayGetSize(list), 1);
H
Hongze Cheng 已提交
28
  // ASSERT_EQ(getNumOfBufGroupId(pBuf), 1);
H
Haojun Liao 已提交
29

30
  releaseBufPage(pBuf, pBufPage);
H
Haojun Liao 已提交
31

32
  SFilePage* pBufPage1 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
H
Haojun Liao 已提交
33

34
  SFilePage* t = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
35 36
  ASSERT_TRUE(t == pBufPage1);

37
  SFilePage* pBufPage2 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
38
  SFilePage* t1 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
39 40
  ASSERT_TRUE(t1 == pBufPage2);

H
Hongze Cheng 已提交
41
  SFilePage* pBufPage3 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
42
  SFilePage* t2 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
43 44
  ASSERT_TRUE(t2 == pBufPage3);

H
Hongze Cheng 已提交
45
  SFilePage* pBufPage4 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
46
  SFilePage* t3 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
47 48
  ASSERT_TRUE(t3 == pBufPage4);

49 50
  releaseBufPage(pBuf, pBufPage2);

H
Hongze Cheng 已提交
51
  SFilePage* pBufPage5 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
52
  SFilePage* t4 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
53 54
  ASSERT_TRUE(t4 == pBufPage5);

55
  destroyDiskbasedBuf(pBuf);
H
Haojun Liao 已提交
56 57 58
}

void writeDownTest() {
59
  SDiskbasedBuf* pBuf = NULL;
H
Hongze Cheng 已提交
60
  int32_t        ret = createDiskbasedBuf(&pBuf, 1024, 4 * 1024, "1", TD_TMP_DIR_PATH);
H
Haojun Liao 已提交
61 62 63 64 65 66

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

H
Hongze Cheng 已提交
67
  SFilePage* pBufPage = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
H
Haojun Liao 已提交
68 69 70 71
  ASSERT_TRUE(pBufPage != NULL);

  *(int32_t*)(pBufPage->data) = nx;
  writePageId = pageId;
H
Hongze Cheng 已提交
72

73 74
  setBufPageDirty(pBufPage, true);
  releaseBufPage(pBuf, pBufPage);
H
Haojun Liao 已提交
75

76
  SFilePage* pBufPage1 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
77
  SFilePage* t1 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
78 79 80
  ASSERT_TRUE(t1 == pBufPage1);
  ASSERT_TRUE(pageId == 1);

81
  SFilePage* pBufPage2 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
82
  SFilePage* t2 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
83 84 85
  ASSERT_TRUE(t2 == pBufPage2);
  ASSERT_TRUE(pageId == 2);

H
Hongze Cheng 已提交
86
  SFilePage* pBufPage3 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
87
  SFilePage* t3 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
88 89 90
  ASSERT_TRUE(t3 == pBufPage3);
  ASSERT_TRUE(pageId == 3);

H
Hongze Cheng 已提交
91
  SFilePage* pBufPage4 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
92
  SFilePage* t4 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
93 94
  ASSERT_TRUE(t4 == pBufPage4);
  ASSERT_TRUE(pageId == 4);
95
  releaseBufPage(pBuf, t4);
H
Haojun Liao 已提交
96 97

  // flush the written page to disk, and read it out again
98
  SFilePage* pBufPagex = static_cast<SFilePage*>(getBufPage(pBuf, writePageId));
H
Haojun Liao 已提交
99 100
  ASSERT_EQ(*(int32_t*)pBufPagex->data, nx);

D
dapan1121 已提交
101
  SArray* pa = getDataBufPagesIdList(pBuf);
H
Haojun Liao 已提交
102 103
  ASSERT_EQ(taosArrayGetSize(pa), 5);

104
  destroyDiskbasedBuf(pBuf);
H
Haojun Liao 已提交
105 106 107
}

void recyclePageTest() {
108
  SDiskbasedBuf* pBuf = NULL;
H
Hongze Cheng 已提交
109
  int32_t        ret = createDiskbasedBuf(&pBuf, 1024, 4 * 1024, "1", TD_TMP_DIR_PATH);
H
Haojun Liao 已提交
110 111 112 113 114 115

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

H
Hongze Cheng 已提交
116
  SFilePage* pBufPage = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
H
Haojun Liao 已提交
117
  ASSERT_TRUE(pBufPage != NULL);
118
  releaseBufPage(pBuf, pBufPage);
H
Haojun Liao 已提交
119

H
Hongze Cheng 已提交
120
  SFilePage* pBufPage1 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
121
  SFilePage* t1 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
122 123 124
  ASSERT_TRUE(t1 == pBufPage1);
  ASSERT_TRUE(pageId == 1);

H
Hongze Cheng 已提交
125
  SFilePage* pBufPage2 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
126
  SFilePage* t2 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
127 128 129
  ASSERT_TRUE(t2 == pBufPage2);
  ASSERT_TRUE(pageId == 2);

H
Hongze Cheng 已提交
130
  SFilePage* pBufPage3 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
131
  SFilePage* t3 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
132 133 134
  ASSERT_TRUE(t3 == pBufPage3);
  ASSERT_TRUE(pageId == 3);

H
Hongze Cheng 已提交
135
  SFilePage* pBufPage4 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
136
  SFilePage* t4 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
137 138
  ASSERT_TRUE(t4 == pBufPage4);
  ASSERT_TRUE(pageId == 4);
139
  releaseBufPage(pBuf, t4);
H
Haojun Liao 已提交
140

H
Hongze Cheng 已提交
141
  SFilePage* pBufPage5 = static_cast<SFilePage*>(getNewBufPage(pBuf, &pageId));
142
  SFilePage* t5 = static_cast<SFilePage*>(getBufPage(pBuf, pageId));
H
Haojun Liao 已提交
143 144
  ASSERT_TRUE(t5 == pBufPage5);
  ASSERT_TRUE(pageId == 5);
145
  releaseBufPage(pBuf, t5);
H
Haojun Liao 已提交
146 147

  // flush the written page to disk, and read it out again
148
  SFilePage* pBufPagex = static_cast<SFilePage*>(getBufPage(pBuf, writePageId));
H
Haojun Liao 已提交
149
  *(int32_t*)(pBufPagex->data) = nx;
H
Hongze Cheng 已提交
150
  writePageId = pageId;  // update the data
151
  releaseBufPage(pBuf, pBufPagex);
H
Haojun Liao 已提交
152

153
  SFilePage* pBufPagex1 = static_cast<SFilePage*>(getBufPage(pBuf, 1));
H
Haojun Liao 已提交
154

D
dapan1121 已提交
155
  SArray* pa = getDataBufPagesIdList(pBuf);
H
Haojun Liao 已提交
156 157
  ASSERT_EQ(taosArrayGetSize(pa), 6);

158
  destroyDiskbasedBuf(pBuf);
H
Haojun Liao 已提交
159
}
H
Hongze Cheng 已提交
160
}  // namespace
H
Haojun Liao 已提交
161 162

TEST(testCase, resultBufferTest) {
wafwerar's avatar
wafwerar 已提交
163
  taosSeedRand(taosGetTimestampSec());
H
Haojun Liao 已提交
164 165 166 167 168 169
  simpleTest();
  writeDownTest();
  recyclePageTest();
}

#pragma GCC diagnostic pop