pageBufferTest.cpp 5.5 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 16
  SDiskbasedBuf* pBuf = NULL;
  int32_t ret = createDiskbasedBuf(&pBuf, 1024, 4096, "", "/tmp/");
H
Haojun Liao 已提交
17 18 19 20
  
  int32_t pageId = 0;
  int32_t groupId = 0;
  
21
  SFilePage* pBufPage = static_cast<SFilePage*>(getNewBufPage(pBuf, groupId, &pageId));
H
Haojun Liao 已提交
22 23
  ASSERT_TRUE(pBufPage != NULL);
  
24
  ASSERT_EQ(getTotalBufSize(pBuf), 1024);
H
Haojun Liao 已提交
25
  
26
  SIDList list = getDataBufPagesIdList(pBuf, groupId);
H
Haojun Liao 已提交
27
  ASSERT_EQ(taosArrayGetSize(list), 1);
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, groupId, &pageId));
H
Haojun Liao 已提交
33

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

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

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

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

49 50 51 52
  releaseBufPage(pBuf, pBufPage2);

  SFilePage* pBufPage5 = static_cast<SFilePage*>(getNewBufPage(pBuf, groupId, &pageId));
  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 60
  SDiskbasedBuf* pBuf = NULL;
  int32_t ret = createDiskbasedBuf(&pBuf, 1024, 4*1024, "1", "/tmp/");
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;

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

  *(int32_t*)(pBufPage->data) = nx;
  writePageId = pageId;
72 73 74
  
  setBufPageDirty(pBufPage, true);
  releaseBufPage(pBuf, pBufPage);
H
Haojun Liao 已提交
75

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

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

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

91 92
  SFilePage* pBufPage4 = static_cast<SFilePage*>(getNewBufPage(pBuf, groupId, &pageId));
  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);

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

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

void recyclePageTest() {
108 109
  SDiskbasedBuf* pBuf = NULL;
  int32_t ret = createDiskbasedBuf(&pBuf, 1024, 4*1024, "1", "/tmp/");
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;

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

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

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

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

135 136
  SFilePage* pBufPage4 = static_cast<SFilePage*>(getNewBufPage(pBuf, groupId, &pageId));
  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

141 142
  SFilePage* pBufPage5 = static_cast<SFilePage*>(getNewBufPage(pBuf, groupId, &pageId));
  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 150
  *(int32_t*)(pBufPagex->data) = nx;
  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

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

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


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

#pragma GCC diagnostic pop