Exemplo n.º 1
0
        public void ShouldLimitReadOfMessages()
        {
            const int  msgLength           = 16;
            var        recordLength        = RingBufferDescriptor.HeaderLength + msgLength;
            var        alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);
            const long head      = 0L;
            const int  headIndex = (int)head;

            _atomicLong.Read(Head).Returns(head);
            _atomicLong.VolatileRead(new IntPtr(headIndex))
            .Returns(RingBufferDescriptor.MakeHeader(recordLength, MessageTypeId));

            var            counter      = 0;
            MessageHandler h            = (id, chunk) => counter++;
            var            messagesRead = _ringBuffer.Read(h, 1);

            Assert.AreEqual(1, messagesRead);
            Assert.AreEqual(1, counter);

            Received.InOrder(() =>
            {
                _buffer.ZeroMemory(headIndex, alignedRecordLength);
                _atomicLong.VolatileWrite(Head, head + alignedRecordLength);
            });
        }
Exemplo n.º 2
0
        public void ShouldWriteToEmptyBuffer()
        {
            const int length              = 8;
            var       recordLength        = length + RingBufferDescriptor.HeaderLength;
            var       alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);

            const int headValue = 0;

            _atomicLong.VolatileRead(Arg.Is(Head)).Returns(headValue);
            const int tailValue = 0;

            _atomicLong.VolatileRead(Arg.Is(Tail)).Returns(tailValue);

            _atomicLong.CompareExchange(Arg.Is(Tail), Arg.Is(tailValue), Arg.Is(tailValue + alignedRecordLength))
            .Returns(tailValue);

            var block = stackalloc byte[100];

            var chunk = new ByteChunk(block, length);

            Assert.IsTrue(_ringBuffer.Write(MessageTypeId, chunk));

            Received.InOrder(() =>
            {
                _atomicLong.VolatileWrite(CurrentSlot, RingBufferDescriptor.MakeHeader(-recordLength, MessageTypeId));
                _buffer.Write(RingBufferDescriptor.EncodedMsgOffset(tailValue), chunk);
                _buffer.GetAtomicInt(tailValue);
                _atomicInt.VolatileWrite(CurrentSlot, recordLength);
            });
        }
Exemplo n.º 3
0
        public void ShouldInsertPaddingRecordPlusMessageOnBufferWrapWithHeadEqualToTail()
        {
            const int length              = 200;
            var       recordLength        = length + RingBufferDescriptor.HeaderLength;
            var       alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);
            var       tail = Capacity - RingBufferDescriptor.HeaderLength;
            var       head = tail;

            _atomicLong.VolatileRead(Head).Returns(head);
            _atomicLong.VolatileRead(Tail).Returns(tail);
            _atomicLong.CompareExchange(Tail, tail + alignedRecordLength + RingBufferDescriptor.RecordAlignment, tail)
            .Returns(tail);

            var chunk = new ByteChunk(null, length);

            Assert.IsTrue(_ringBuffer.Write(MessageTypeId, chunk));

            Received.InOrder(() =>
            {
                // padding first
                _atomicLong.VolatileWrite(new IntPtr(tail),
                                          RingBufferDescriptor.MakeHeader(RingBufferDescriptor.HeaderLength,
                                                                          ManyToOneRingBuffer.PaddingMsgTypeId));

                // message then
                _atomicLong.VolatileWrite(new IntPtr(0), RingBufferDescriptor.MakeHeader(-recordLength, MessageTypeId));
                _buffer.Write(RingBufferDescriptor.EncodedMsgOffset(0), chunk);
                _atomicInt.VolatileWrite(new IntPtr(0), recordLength);
            });
        }
Exemplo n.º 4
0
        public void ShouldInsertPaddingRecordPlusMessageOnBufferWrap()
        {
            const int length              = 200;
            var       recordLength        = length + RingBufferDescriptor.HeaderLength;
            var       alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);
            long      tail = Capacity - RingBufferDescriptor.HeaderLength;
            var       head = tail - RingBufferDescriptor.RecordAlignment * 4;

            _atomicLong.VolatileRead(Head).Returns(head);
            _atomicLong.Read(Tail).Returns(tail);

            var chunk = new ByteChunk(null, length);

            Assert.True(_ringBuffer.Write(MessageTypeId, chunk));

            Received.InOrder(() =>
            {
                // padding first
                _atomicLong.VolatileWrite(new IntPtr(tail),
                                          RingBufferDescriptor.MakeHeader(RingBufferDescriptor.HeaderLength,
                                                                          ManyToOneRingBuffer.PaddingMsgTypeId));

                // then write from the start
                _buffer.Write(RingBufferDescriptor.EncodedMsgOffset(0), chunk);
                _buffer.GetAtomicLong(0);
                _atomicLong.VolatileWrite(new IntPtr(0), RingBufferDescriptor.MakeHeader(recordLength, MessageTypeId));
            });
        }
Exemplo n.º 5
0
        public void ShouldCopeWithExceptionFromHandler()
        {
            const int  msgLength           = 16;
            var        recordLength        = RingBufferDescriptor.HeaderLength + msgLength;
            var        alignedRecordLength = recordLength.AlignToMultipleOf(RingBufferDescriptor.RecordAlignment);
            var        tail      = alignedRecordLength * 2;
            const long head      = 0L;
            const int  headIndex = (int)head;

            _atomicLong.Read(Head).Returns(head);
            _atomicLong.VolatileRead(new IntPtr(headIndex))
            .Returns(RingBufferDescriptor.MakeHeader(recordLength, MessageTypeId));
            _atomicLong.VolatileRead(new IntPtr(headIndex + alignedRecordLength))
            .Returns(RingBufferDescriptor.MakeHeader(recordLength, MessageTypeId));

            var            counter = 0;
            MessageHandler h       = (id, chunk) =>
            {
                counter++;
                if (counter == 2)
                {
                    throw new Exception();
                }
            };

            try
            {
                _ringBuffer.Read(h, 2);
            }
            catch
            {
                Assert.AreEqual(2, counter);

                Received.InOrder(() =>
                {
                    _buffer.Received(1).ZeroMemory(Arg.Is(headIndex), Arg.Is(alignedRecordLength * 2));
                    _atomicLong.Received(1).VolatileWrite(Head, tail);
                });
                return;
            }

            Assert.Fail("This should not go to this point");
        }