public void When_multiple_messages_written_then_they_are_consumed_in_order_leaving_nothing()
        {
            var guidMessages = new GuidMessageReaderWriter();
            var store = new SegmentChainMessageStore(guidMessages, _pool);

            var lowerBoundEstimationOfMessagesPerSegment = SingleThreadSegmentPool.SegmentSize/sizeof (Guid);
            var atLeastTwoSegmentsOfMessages = lowerBoundEstimationOfMessagesPerSegment*2;

            var guids = Enumerable.Range(0, atLeastTwoSegmentsOfMessages).Select(i => Guid.NewGuid()).ToArray();
            var env = new Envelope();
            var head = default(Segment*);

            fixed (Guid* g = guids)
            {
                for (var i = 0; i < guids.Length; i++)
                {
                    store.Write(ref env, ref *(g + i), ref head);
                }
            }

            store.Consume(guidMessages.MessageHandler, ref head);
            CollectionAssert.AreEquivalent(guids, guidMessages.Received);

            // nothing is left
            guidMessages.Received.Clear();

            store.Consume(guidMessages.MessageHandler, ref head);
            CollectionAssert.IsEmpty(guidMessages.Received);
        }
Ejemplo n.º 2
0
        public unsafe void Test()
        {
            var counter = Substitute.For<IStructSizeCounter>();
            const int aSize = 64;
            counter.GetSize(typeof (A)).Returns(aSize);
            const int envelopeSize = 4;
            counter.GetSize(typeof (Envelope)).Returns(envelopeSize);

            var buffer = Substitute.For<IRingBuffer>();
            buffer.Write(0, new ByteChunk()).ReturnsForAnyArgs(true);
            const int messageId = 5;

            var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("AnythingForTests"),
                AssemblyBuilderAccess.Run);
            var main = asm.DefineDynamicModule("main");
            var writer = MessageWriterBuilder.Build(counter, l => messageId, new[] {typeof (A)}, main);

            var e = new Envelope(new ActorId(1));
            var a = new A();

            Assert.True(writer.Write(ref e, ref a, buffer));

            var call = buffer.ReceivedCalls().Single();
            var args = call.GetArguments();
            Assert.AreEqual("Write", call.GetMethodInfo().Name);
            Assert.AreEqual(messageId, args[0]);
            Assert.AreEqual(new ByteChunk((byte*) &a, aSize), args[1]);
        }
Ejemplo n.º 3
0
        public void WhenWriteFailsRetryCountMinusOneTimes_ThenFinalSucceeds()
        {
            A any;
            var e = new Envelope();
            _writer.Write(ref e, ref any, null).ReturnsForAnyArgs(call => false, call => false, call => true);
            var a = new A();

            _bus.Send(ref a, AId);
        }
Ejemplo n.º 4
0
        public void WhenWriteFailsRetryCountTimes_ThenExceptionIsThrown()
        {
            A any;
            var e = new Envelope();
            _writer.Write(ref e, ref any, null)
                .ReturnsForAnyArgs(call => false, call => false, call => false, call => true);
            var a = new A();

            Assert.Throws(Is.AssignableTo<Exception>(), () => _bus.Send(ref a, AId));
        }
Ejemplo n.º 5
0
        public void WhenPublish_ThenAllReceiversAreIncluded()
        {
            ArraySegment<IRingBuffer> buffers;
            Registry.GetBuffers(typeof (A), out buffers);
            var expectedCount = buffers.Count;

            A any;
            var e = new Envelope();
            _writer.Write(ref e, ref any, null).ReturnsForAnyArgs(true);
            var a = new A();

            _bus.Publish(ref a);

            _writer.ReceivedWithAnyArgs(expectedCount).Write(ref e, ref a, null);
        }