cfs/test/fsproc/testBlockBuffer_utest2.cc
// split out one block, install to another
split out one block, install to another
split out one block install to another
TEST(SplitBufferItem, T3) {
  uint32_t blockNum = 4;
  int blockSize = 32;
  char *memPtrSrc = (char *)malloc(blockNum * blockSize);
  BlockBuffer bufferSrc(blockNum, blockSize, memPtrSrc);
  char *memPtrDst = (char *)malloc(blockNum * blockSize);
  BlockBuffer bufferDst(blockNum, blockSize, memPtrDst);
  int index = 1;
  int lockGrabbed;
  bool hasDirty = true;
  BlockBufferItem *item;
  int outBlockIdx;
  item = bufferSrc.getBlock(1000, lockGrabbed, hasDirty, index);
  outBlockIdx = item->getIdx();
  bufferSrc.releaseBlock(1000);
  std::unordered_set<BlockBufferItem *> itemSet;
  bufferSrc.splitBufferItemsByIndex(index, itemSet);
  EXPECT_EQ(itemSet.size(), 1);
  std::unordered_set<int> dstBlockIdxSet;
  std::unordered_set<BlockBufferItem *> items;
  for (uint i = 0; i < blockNum; i++) {
    item = bufferDst.getBlock(2000 + i, lockGrabbed, hasDirty);
    EXPECT_NE(item, nullptr);
    EXPECT_EQ(lockGrabbed, BLOCK_BUF_LOCK_GRAB_YES);
    dstBlockIdxSet.emplace(item->getIdx());
    items.emplace(item);
  }
  EXPECT_EQ(dstBlockIdxSet.size(), blockNum);
  EXPECT_NE(dstBlockIdxSet.find(outBlockIdx), dstBlockIdxSet.end());
  item = bufferDst.getBlock(1000, lockGrabbed, hasDirty);
  EXPECT_EQ(item, nullptr);
  bufferDst.installBufferItemsOfIndex(index, itemSet);
  item = bufferDst.getBlock(1000, lockGrabbed, hasDirty);
  EXPECT_EQ(lockGrabbed, BLOCK_BUF_LOCK_GRAB_YES);
  EXPECT_NE(item, nullptr);
  EXPECT_EQ(item->getIdx(), outBlockIdx);
  EXPECT_EQ(items.find(item), items.end());
  bufferDst.releaseBlock(item);
  int rt;
  for (uint i = 0; i < blockNum; i++) {
    rt = bufferDst.releaseBlock(2000 + i);
    EXPECT_TRUE(rt > 0);
  }
  std::unordered_set<BlockBufferItem *> dstItems;
  for (uint i = 0; i < 1000; i++) {
    item = bufferDst.getBlock(3000 + i, lockGrabbed, hasDirty);
    EXPECT_NE(item, nullptr);
    bufferDst.releaseBlock(item);
    dstItems.emplace(item);
  }
  EXPECT_EQ(dstItems.size(), blockNum + 1);
  free(memPtrSrc);
  free(memPtrDst);
}
TEST ( SplitBufferItem , T3 ) { uint32_t blockNum = 4 ; int blockSize = 32 ; char * memPtrSrc = ( char * ) malloc ( blockNum * blockSize ) ; BlockBuffer bufferSrc ( blockNum , blockSize , memPtrSrc ) ; char * memPtrDst = ( char * ) malloc ( blockNum * blockSize ) ; BlockBuffer bufferDst ( blockNum , blockSize , memPtrDst ) ; int index = 1 ; int lockGrabbed ; bool hasDirty = true ; BlockBufferItem * item ; int outBlockIdx ; item = bufferSrc . getBlock ( 1000 , lockGrabbed , hasDirty , index ) ; outBlockIdx = item -> getIdx ( ) ; bufferSrc . releaseBlock ( 1000 ) ; std :: unordered_set < BlockBufferItem * > itemSet ; bufferSrc . splitBufferItemsByIndex ( index , itemSet ) ; EXPECT_EQ ( itemSet . size ( ) , 1 ) ; std :: unordered_set < int > dstBlockIdxSet ; std :: unordered_set < BlockBufferItem * > items ; for ( uint i = 0 ; i < blockNum ; i ++ ) { item = bufferDst . getBlock ( 2000 + i , lockGrabbed , hasDirty ) ; EXPECT_NE ( item , nullptr ) ; EXPECT_EQ ( lockGrabbed , BLOCK_BUF_LOCK_GRAB_YES ) ; dstBlockIdxSet . emplace ( item -> getIdx ( ) ) ; items . emplace ( item ) ; } EXPECT_EQ ( dstBlockIdxSet . size ( ) , blockNum ) ; EXPECT_NE ( dstBlockIdxSet . find ( outBlockIdx ) , dstBlockIdxSet . end ( ) ) ; item = bufferDst . getBlock ( 1000 , lockGrabbed , hasDirty ) ; EXPECT_EQ ( item , nullptr ) ; bufferDst . installBufferItemsOfIndex ( index , itemSet ) ; item = bufferDst . getBlock ( 1000 , lockGrabbed , hasDirty ) ; EXPECT_EQ ( lockGrabbed , BLOCK_BUF_LOCK_GRAB_YES ) ; EXPECT_NE ( item , nullptr ) ; EXPECT_EQ ( item -> getIdx ( ) , outBlockIdx ) ; EXPECT_EQ ( items . find ( item ) , items . end ( ) ) ; bufferDst . releaseBlock ( item ) ; int rt ; for ( uint i = 0 ; i < blockNum ; i ++ ) { rt = bufferDst . releaseBlock ( 2000 + i ) ; EXPECT_TRUE ( rt > 0 ) ; } std :: unordered_set < BlockBufferItem * > dstItems ; for ( uint i = 0 ; i < 1000 ; i ++ ) { item = bufferDst . getBlock ( 3000 + i , lockGrabbed , hasDirty ) ; EXPECT_NE ( item , nullptr ) ; bufferDst . releaseBlock ( item ) ; dstItems . emplace ( item ) ; } EXPECT_EQ ( dstItems . size ( ) , blockNum + 1 ) ; free ( memPtrSrc ) ; free ( memPtrDst ) ; }
split out one block, install to another
split out one block install to another
// bufferSrc.updateBlockIndex(item, index); // do not release block here // now we cannot allocate buffer slot anymore // now we install // release blocks and see if the cache still works // at this time, dstBuffer should contains 5 items