public static int scanForGap(
        final UnsafeBuffer termBuffer,
        final int termId,
        final int termOffset,
        final int limitOffset,
        final GapHandler handler)
    {
        int offset = termOffset;
        do
        {
            final int frameLength = frameLengthVolatile(termBuffer, offset);
            if (frameLength <= 0)
            {
                break;
            }

            offset += align(frameLength, FRAME_ALIGNMENT);
        }
        while (offset < limitOffset);

        final int gapBeginOffset = offset;
        if (offset < limitOffset)
        {
            final int limit = limitOffset - ALIGNED_HEADER_LENGTH;
            while (offset < limit)
            {
                offset += FRAME_ALIGNMENT;

                if (0 != termBuffer.getIntVolatile(offset))
                {
                    offset -= ALIGNED_HEADER_LENGTH;
                    break;
                }
            }

            final int gapLength = (offset - gapBeginOffset) + ALIGNED_HEADER_LENGTH;
            handler.onGap(termId, gapBeginOffset, gapLength);
        }

        return gapBeginOffset;
    }
@Test
    void shouldReportNoGapWhenHwmIsInPadding()
    {
        final int paddingLength = align(HEADER_LENGTH, FRAME_ALIGNMENT) * 2;
        final int tail = LOG_BUFFER_CAPACITY - paddingLength;
        final int highWaterMark = LOG_BUFFER_CAPACITY - paddingLength + HEADER_LENGTH;

        when(termBuffer.getIntVolatile(tail)).thenReturn(paddingLength);
        when(termBuffer.getIntVolatile(tail + HEADER_LENGTH)).thenReturn(0);

        assertEquals(
            LOG_BUFFER_CAPACITY, TermGapScanner.scanForGap(termBuffer, TERM_ID, tail, highWaterMark, gapHandler));

        verifyNoInteractions(gapHandler);
    }