Ejemplo n.º 1
0
 /**
  * Gets a view of the given {@code segment} and verifies the ref count and the data obtained from
  * the view against {@code expectedRefCount} and {@code dataInSegment} respectively.
  *
  * @param segment the {@link LogSegment} to get a view from.
  * @param writeStartOffset the offset at which write was started on the segment.
  * @param offset the offset for which a view is required.
  * @param dataInSegment the entire data in the {@link LogSegment}.
  * @param expectedRefCount the expected return value of {@link LogSegment#refCount()} once the
  *     view is obtained from the {@code segment}
  * @throws IOException
  */
 private void getAndVerifyView(
     LogSegment segment,
     long writeStartOffset,
     int offset,
     byte[] dataInSegment,
     long expectedRefCount)
     throws IOException {
   Random random = new Random();
   Pair<File, FileChannel> view = segment.getView();
   assertNotNull("File object received in view is null", view.getFirst());
   assertNotNull("FileChannel object received in view is null", view.getSecond());
   assertEquals("Ref count is not as expected", expectedRefCount, segment.refCount());
   int sizeToRead = random.nextInt(dataInSegment.length - offset + 1);
   ByteBuffer buffer = ByteBuffer.wrap(new byte[sizeToRead]);
   view.getSecond().read(buffer, writeStartOffset + offset);
   assertArrayEquals(
       "Data read from file does not match data written",
       Arrays.copyOfRange(dataInSegment, offset, offset + sizeToRead),
       buffer.array());
 }
Ejemplo n.º 2
0
  /**
   * Verifies getting and closing views and makes sure that data and ref counts are consistent.
   *
   * @throws IOException
   */
  @Test
  public void viewAndRefCountTest() throws IOException {
    String segmentName = "log_current";
    LogSegment segment = getSegment(segmentName, STANDARD_SEGMENT_SIZE, true);
    try {
      long startOffset = segment.getStartOffset();
      int readSize = 100;
      int viewCount = 5;
      byte[] data = appendRandomData(segment, readSize * viewCount);

      for (int i = 0; i < viewCount; i++) {
        getAndVerifyView(segment, startOffset, i * readSize, data, i + 1);
      }

      for (int i = 0; i < viewCount; i++) {
        segment.closeView();
        assertEquals("Ref count is not as expected", viewCount - i - 1, segment.refCount());
      }
    } finally {
      closeSegmentAndDeleteFile(segment);
    }
  }