resultBufferTest.cpp 4.8 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 9 10 11 12
#include "taos.h"
#include "tsdb.h"

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

  releaseResBufPage(pResultBuf, pBufPage);

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

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

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

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

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

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

H
Haojun Liao 已提交
50
  destroyResultBuf(pResultBuf);
H
Haojun Liao 已提交
51 52 53 54
}

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

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

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

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

H
Haojun Liao 已提交
113 114 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
  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 已提交
149

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

H
Haojun Liao 已提交
154

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