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"

9 10
#pragma GCC diagnostic ignored "-Werror=unused-function"

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

  releaseResBufPage(pResultBuf, pBufPage);

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

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

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

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

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

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

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

void writeDownTest() {
  SDiskbasedResultBuf* pResultBuf = NULL;
H
Haojun Liao 已提交
57
  int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1024, 4*1024, 1);
H
Haojun Liao 已提交
58 59 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

  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 已提交
99
  destroyResultBuf(pResultBuf);
H
Haojun Liao 已提交
100 101 102 103
}

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

  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 已提交
114

H
Haojun Liao 已提交
115 116 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
  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 已提交
151

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

H
Haojun Liao 已提交
156

H
hjxilinx 已提交
157
TEST(testCase, resultBufferTest) {
H
Haojun Liao 已提交
158
  srand(time(NULL));
H
hjxilinx 已提交
159
  simpleTest();
H
Haojun Liao 已提交
160
  writeDownTest();
H
Haojun Liao 已提交
161
  recyclePageTest();
H
hjxilinx 已提交
162
}