public void ShouldUpdatePositionOnRethrownExceptionInPoll() { long initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(0)); A.CallTo(() => MockFragmentHandler(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).Throws(new Exception()); A.CallTo(ErrorHandler).Throws(new Exception()); bool thrown = false; try { image.Poll(MockFragmentHandler, int.MaxValue); } catch (Exception) { thrown = true; } Assert.True(thrown); Assert.AreEqual(initialPosition + ALIGNED_FRAME_LENGTH, image.Position()); A.CallTo(() => MockFragmentHandler(A <UnsafeBuffer> ._, DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened(); }
public void ShouldNotReadLastMessageWhenPadding() { const int msgLength = 1; int frameLength = HEADER_LENGTH + msgLength; int alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT); int frameOffset = TERM_BUFFER_CAPACITY - alignedFrameLength; long startingPosition = LogBufferDescriptor.ComputePosition( INITIAL_TERM_ID, frameOffset, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).Returns(frameLength); A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(frameOffset))) .Returns((short)FrameDescriptor.PADDING_FRAME_TYPE); A.CallTo(() => subscriberPosition.GetVolatile()).Returns(startingPosition); int readOutcome = TermReader.Read( termBuffer, frameOffset, handler, int.MaxValue, header, errorHandler, startingPosition, subscriberPosition); Assert.AreEqual(0, readOutcome); A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).MustHaveHappened() .Then(A.CallTo(() => subscriberPosition.SetOrdered(TERM_BUFFER_CAPACITY)).MustHaveHappened()); A.CallTo(() => handler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)) .MustNotHaveHappened(); }
public void ShouldPollFragmentsToControlledFragmentHandlerOnContinue() { var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(0)); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(1)); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)) .Returns(ControlledFragmentHandlerAction.CONTINUE); var fragmentsRead = image.ControlledPoll(MockControlledFragmentHandler, int.MaxValue); Assert.AreEqual(2, fragmentsRead); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened() .Then(A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, ALIGNED_FRAME_LENGTH + DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)) .MustHaveHappened()) .Then(A.CallTo(() => Position.SetOrdered(initialPosition + ALIGNED_FRAME_LENGTH * 2)) .MustHaveHappened()); }
public void ShouldNotPollOneFragmentToControlledFragmentHandlerOnAbort() { var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(0)); A.CallTo(() => MockControlledFragmentHandler.Invoke(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).Returns(ControlledFragmentHandlerAction.ABORT); var fragmentsRead = image.ControlledPoll(MockControlledFragmentHandler, int.MaxValue); Assert.AreEqual(0, fragmentsRead); Assert.AreEqual(initialPosition, image.Position()); A.CallTo(() => MockControlledFragmentHandler.Invoke(A <UnsafeBuffer> ._, DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened(); }
public void ShouldReportCorrectPositionOnReception() { var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(0)); var messages = image.Poll(MockFragmentHandler, int.MaxValue); Assert.AreEqual(messages, 1); A.CallTo(() => MockFragmentHandler(A <UnsafeBuffer> ._, DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened(); A.CallTo(() => Position.SetOrdered(initialPosition)).MustHaveHappened().Then( A.CallTo(() => Position.SetOrdered(initialPosition + ALIGNED_FRAME_LENGTH)).MustHaveHappened() ); }
public void ShouldPollFragmentsToBoundedControlledFragmentHandlerWithInitialOffsetNotZero() { var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, OffsetForFrame(1), POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); var maxPosition = initialPosition + ALIGNED_FRAME_LENGTH; Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(1)); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(2)); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).Returns(ControlledFragmentHandlerAction.CONTINUE); var fragmentsRead = image.BoundedControlledPoll(MockControlledFragmentHandler, maxPosition, int.MaxValue); Assert.That(fragmentsRead, Is.EqualTo(1)); Assert.That(Position.Get(), Is.EqualTo(maxPosition)); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).MustHaveHappened(); }
public void ShouldPollNoFragmentsToBoundedControlledFragmentHandlerWithMaxPositionBeforeInitialPosition() { var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); var maxPosition = initialPosition - DataHeaderFlyweight.HEADER_LENGTH; Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(0)); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(1)); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).Returns(ControlledFragmentHandlerAction.CONTINUE); var fragmentsRead = image.BoundedControlledPoll(MockControlledFragmentHandler, maxPosition, int.MaxValue); Assert.That(fragmentsRead, Is.EqualTo(0)); Assert.That(Position.Get(), Is.EqualTo(initialPosition)); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustNotHaveHappened(); }
public void ShouldPollFragmentsToBoundedControlledFragmentHandlerWithMaxPositionAfterEndOfTerm() { var initialOffset = TERM_BUFFER_LENGTH - (ALIGNED_FRAME_LENGTH * 2); var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, initialOffset, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); var maxPosition = initialPosition + TERM_BUFFER_LENGTH; Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, initialOffset); InsertPaddingFrame(INITIAL_TERM_ID, initialOffset + ALIGNED_FRAME_LENGTH); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).Returns(ControlledFragmentHandlerAction.CONTINUE); var fragmentsRead = image.BoundedControlledPoll(MockControlledFragmentHandler, maxPosition, int.MaxValue); Assert.That(fragmentsRead, Is.EqualTo(1)); A.CallTo(() => MockControlledFragmentHandler.OnFragment(A <UnsafeBuffer> ._, initialOffset + DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened() .Then(A.CallTo(() => Position.SetOrdered(TERM_BUFFER_LENGTH)).MustHaveHappened()); }
public void ShouldPollFragmentsToBoundedFragmentHandlerWithMaxPositionBeforeNextMessage() { var initialPosition = LogBufferDescriptor.ComputePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); var maxPosition = initialPosition + ALIGNED_FRAME_LENGTH; Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(0)); InsertDataFrame(INITIAL_TERM_ID, OffsetForFrame(1)); var fragmentsRead = image.BoundedPoll(MockFragmentHandler, maxPosition, int.MaxValue); Assert.That(fragmentsRead, Is.EqualTo(1)); A.CallTo(() => MockFragmentHandler.OnFragment(A <UnsafeBuffer> ._, DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened() .Then(A.CallTo(() => Position.SetOrdered(initialPosition + ALIGNED_FRAME_LENGTH)).MustHaveHappened()); }
public void ShouldReportCorrectPositionOnReceptionWithNonZeroPositionInNonInitialTermId() { var activeTermId = INITIAL_TERM_ID + 1; const int initialMessageIndex = 5; var initialTermOffset = OffsetForFrame(initialMessageIndex); var initialPosition = LogBufferDescriptor.ComputePosition(activeTermId, initialTermOffset, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID); Position.SetOrdered(initialPosition); var image = CreateImage(); InsertDataFrame(activeTermId, OffsetForFrame(initialMessageIndex)); var messages = image.Poll(MockFragmentHandler, int.MaxValue); Assert.AreEqual(1, messages); A.CallTo(() => MockFragmentHandler(A <UnsafeBuffer> ._, initialTermOffset + DataHeaderFlyweight.HEADER_LENGTH, DATA.Length, A <Header> ._)).MustHaveHappened(); A.CallTo(() => Position.SetOrdered(initialPosition)).MustHaveHappened().Then( A.CallTo(() => Position.SetOrdered(initialPosition + ALIGNED_FRAME_LENGTH)).MustHaveHappened() ); }