@Test
  public void endOffsetTest() throws IOException, StoreException {
    LogSegmentName segmentName = LogSegmentName.generateFirstSegmentName(false);
    LogSegment segment = getSegment(segmentName, STANDARD_SEGMENT_SIZE, true);
    try {
      long writeStartOffset = segment.getStartOffset();
      int segmentSize = 500;
      appendRandomData(segment, segmentSize);
      assertEquals("End offset is not as expected", writeStartOffset + segmentSize, segment.getEndOffset());

      // should be able to set end offset to something >= initial offset and <= file size
      int[] offsetsToSet = {(int) (writeStartOffset), segmentSize / 2, segmentSize};
      for (int offset : offsetsToSet) {
        segment.setEndOffset(offset);
        assertEquals("End offset is not equal to what was set", offset, segment.getEndOffset());
        assertEquals("File channel positioning is incorrect", offset, segment.getView().getSecond().position());
      }

      // cannot set end offset to illegal values (< initial offset or > file size)
      int[] invalidOffsets = {(int) (writeStartOffset - 1), (int) (segment.sizeInBytes() + 1)};
      for (int offset : invalidOffsets) {
        try {
          segment.setEndOffset(offset);
          fail("Setting log end offset an invalid offset [" + offset + "] should have failed");
        } catch (StoreException e) {
          // expected. Nothing to do.
          Assert.assertEquals(StoreErrorCodes.LogEndOffsetError, e.getErrorCode());
        }
      }
    } finally {
      closeSegmentAndDeleteFile(segment);
    }
  }
Tests setting end offset - makes sure legal values are set correctly and illegal values are rejected.
@throws IOException
ambry-store/src/test/java/com/github/ambry/store/LogSegmentTest.java