@Test public void shouldAppendFrameTwiceToLog() { final int headerLength = DEFAULT_HEADER.capacity(); final UnsafeBuffer buffer = new UnsafeBuffer(new byte[128]); final int msgLength = 20; final int frameLength = msgLength + headerLength; final int alignedFrameLength = align(frameLength, FRAME_ALIGNMENT); int tail = 0; when(metaDataBuffer.getAndAddInt(TERM_TAIL_COUNTER_OFFSET, alignedFrameLength)) .thenReturn(0) .thenReturn(alignedFrameLength); assertThat(termAppender.append(buffer, 0, msgLength), is(alignedFrameLength)); assertThat(termAppender.append(buffer, 0, msgLength), is(alignedFrameLength * 2)); final InOrder inOrder = inOrder(termBuffer, metaDataBuffer); inOrder .verify(metaDataBuffer, times(1)) .getAndAddInt(TERM_TAIL_COUNTER_OFFSET, alignedFrameLength); verifyDefaultHeader(inOrder, termBuffer, tail, frameLength); inOrder.verify(termBuffer, times(1)).putBytes(headerLength, buffer, 0, msgLength); inOrder.verify(termBuffer, times(1)).putInt(termOffsetOffset(tail), tail, LITTLE_ENDIAN); inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength); tail = alignedFrameLength; inOrder .verify(metaDataBuffer, times(1)) .getAndAddInt(TERM_TAIL_COUNTER_OFFSET, alignedFrameLength); verifyDefaultHeader(inOrder, termBuffer, tail, frameLength); inOrder.verify(termBuffer, times(1)).putBytes(tail + headerLength, buffer, 0, msgLength); inOrder.verify(termBuffer, times(1)).putInt(termOffsetOffset(tail), tail, LITTLE_ENDIAN); inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength); }
@Test public void shouldPadLogAndTripWhenAppendingWithInsufficientRemainingCapacityIncludingHeader() { final int headerLength = DEFAULT_HEADER.capacity(); final int msgLength = 120; final int requiredFrameSize = align(headerLength + msgLength, FRAME_ALIGNMENT); final int tailValue = termAppender.termBuffer().capacity() - (requiredFrameSize + (headerLength - FRAME_ALIGNMENT)); final UnsafeBuffer buffer = new UnsafeBuffer(new byte[128]); final int frameLength = TERM_BUFFER_LENGTH - tailValue; when(metaDataBuffer.getAndAddInt(TERM_TAIL_COUNTER_OFFSET, requiredFrameSize)) .thenReturn(tailValue); assertThat(termAppender.append(buffer, 0, msgLength), is(TermAppender.TRIPPED)); final InOrder inOrder = inOrder(termBuffer, metaDataBuffer); inOrder .verify(metaDataBuffer, times(1)) .getAndAddInt(TERM_TAIL_COUNTER_OFFSET, requiredFrameSize); verifyDefaultHeader(inOrder, termBuffer, tailValue, frameLength); inOrder .verify(termBuffer, times(1)) .putShort(typeOffset(tailValue), (short) PADDING_FRAME_TYPE, LITTLE_ENDIAN); inOrder .verify(termBuffer, times(1)) .putInt(termOffsetOffset(tailValue), tailValue, LITTLE_ENDIAN); inOrder.verify(termBuffer, times(1)).putIntOrdered(tailValue, frameLength); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenMaxMessageLengthExceeded() { final int maxMessageLength = termAppender.maxMessageLength(); final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]); termAppender.append(srcBuffer, 0, maxMessageLength + 1); }
@Test public void shouldFragmentMessageOverTwoFrames() { final int msgLength = termAppender.maxPayloadLength() + 1; final int headerLength = DEFAULT_HEADER.capacity(); final int frameLength = headerLength + 1; final int requiredCapacity = align(headerLength + 1, FRAME_ALIGNMENT) + termAppender.maxFrameLength(); final UnsafeBuffer buffer = new UnsafeBuffer(new byte[msgLength]); when(metaDataBuffer.getAndAddInt(TERM_TAIL_COUNTER_OFFSET, requiredCapacity)).thenReturn(0); assertThat(termAppender.append(buffer, 0, msgLength), is(requiredCapacity)); int tail = 0; final InOrder inOrder = inOrder(termBuffer, metaDataBuffer); inOrder .verify(metaDataBuffer, times(1)) .getAndAddInt(TERM_TAIL_COUNTER_OFFSET, requiredCapacity); verifyDefaultHeader(inOrder, termBuffer, tail, termAppender.maxFrameLength()); inOrder .verify(termBuffer, times(1)) .putBytes(tail + headerLength, buffer, 0, termAppender.maxPayloadLength()); inOrder.verify(termBuffer, times(1)).putByte(flagsOffset(tail), BEGIN_FRAG); inOrder.verify(termBuffer, times(1)).putInt(termOffsetOffset(tail), tail, LITTLE_ENDIAN); inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, termAppender.maxFrameLength()); tail = termAppender.maxFrameLength(); verifyDefaultHeader(inOrder, termBuffer, tail, frameLength); inOrder .verify(termBuffer, times(1)) .putBytes(tail + headerLength, buffer, termAppender.maxPayloadLength(), 1); inOrder.verify(termBuffer, times(1)).putByte(flagsOffset(tail), END_FRAG); inOrder.verify(termBuffer, times(1)).putInt(termOffsetOffset(tail), tail, LITTLE_ENDIAN); inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength); }