resultBufferTest.cpp 3.1 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, 1000, 64, 1024, 4, 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 50 51 52 53 54 55 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 97
  ASSERT_TRUE(t4 == pBufPage5);

  destroyResultBuf(pResultBuf, NULL);
}

void writeDownTest() {
  SDiskbasedResultBuf* pResultBuf = NULL;
  int32_t ret = createDiskbasedResultBuffer(&pResultBuf, 1000, 64, 1024, 4, NULL);

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

H
hjxilinx 已提交
99
  destroyResultBuf(pResultBuf, NULL);
H
hjxilinx 已提交
100 101 102
}
} // namespace

H
Haojun Liao 已提交
103

H
hjxilinx 已提交
104
TEST(testCase, resultBufferTest) {
H
Haojun Liao 已提交
105
  srand(time(NULL));
H
hjxilinx 已提交
106
  simpleTest();
H
Haojun Liao 已提交
107
  writeDownTest();
H
hjxilinx 已提交
108
}