コード例 #1
0
        public void should_not_send_messages_to_persistence_twice_if_persistence_goes_up_and_down()
        {
            using (MessageId.PauseIdGeneration())
            {
                // Stopping persistence
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new MemoryStream(), PersistencePeer));
                InnerTransport.Messages.Clear();

                var ackedMessage = new FakeCommand(456).ToTransportMessage();
                Transport.AckMessage(ackedMessage);
                InnerTransport.AckedMessages.ShouldBeEmpty();

                // starting persistence - should send enqueued messages
                Transport.OnPeerUpdated(PersistencePeer.Id, PeerUpdateAction.Started);

                InnerTransport.ExpectExactly(new[]
                {
                    new TransportMessageSent(new MessageHandled(ackedMessage.Id).ToTransportMessage(Self), PersistencePeer)
                });

                // Stopping persistence again
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new MemoryStream(), PersistencePeer));
                InnerTransport.Messages.Clear();

                // starting persistence again - should not have anything to send
                Transport.OnPeerUpdated(PersistencePeer.Id, PeerUpdateAction.Started);

                InnerTransport.ExpectNothing();
            }
        }
コード例 #2
0
        public void should_not_send_messages_to_persistence_twice_if_persistence_goes_up_and_down()
        {
            using (MessageId.PauseIdGeneration())
            {
                PeerDirectory.Setup(directory => directory.GetPeersHandlingMessage(new MessageBinding(new MessageTypeId(typeof(PersistMessageCommand)), BindingKey.Empty)))
                .Returns(new List <Peer> {
                    PersistencePeer
                });

                // Stopping persistence
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new byte[0], PersistencePeer));
                InnerTransport.Messages.Clear();

                var ackedMessage = new FakeCommand(456).ToTransportMessage();
                Transport.AckMessage(ackedMessage);
                InnerTransport.AckedMessages.ShouldBeEmpty();

                // starting persistence - should send enqueued messages
                Transport.OnPeerUpdated(PersistencePeer.Id, PeerUpdateAction.Started);

                InnerTransport.ExpectExactly(new[]
                {
                    new TransportMessageSent(new MessageHandled(ackedMessage.Id).ToTransportMessage(Self), PersistencePeer)
                });

                // Stopping persistence again
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new byte[0], PersistencePeer));
                InnerTransport.Messages.Clear();

                // starting persistence again - should not have anything to send
                Transport.OnPeerUpdated(PersistencePeer.Id, PeerUpdateAction.Started);

                InnerTransport.ExpectNothing();
            }
        }
コード例 #3
0
        public void should_not_crash_during_safety_phase()
        {
            Transport.Start();

            var failingMessage = new FakeCommand(666).ToTransportMessage();
            var otherMessage   = new FakeCommand(123).ToTransportMessage();
            var successfullyReceivedMessages = new List <TransportMessage>();

            Transport.MessageReceived += msg =>
            {
                if (msg == failingMessage)
                {
                    throw new Exception("Failure");
                }
                successfullyReceivedMessages.Add(msg);
            };

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());
            InnerTransport.RaiseMessageReceived(failingMessage);
            InnerTransport.RaiseMessageReceived(otherMessage);

            Wait.Until(() => successfullyReceivedMessages.Count >= 1, 150.Milliseconds());

            successfullyReceivedMessages.Single().ShouldEqual(otherMessage);
        }
コード例 #4
0
        public void should_receive_messages_immediately_after_start()
        {
            Transport.Start();

            var message = new FakeCommand(123).ToTransportMessage();
            InnerTransport.RaiseMessageReceived(message);

            Wait.Until(() => MessagesForwardedToBus.Count == 1, 500.Milliseconds());
        }
コード例 #5
0
        public void should_not_publish_a_MessageHandled_event_after_a_persistent_message_is_processed_by_a_non_persistent_host()
        {
            Transport.Start();

            var command = new FakeCommand(123).ToTransportMessage();
            InnerTransport.RaiseMessageReceived(command);
            Thread.Sleep(10);

            InnerTransport.ExpectNothing();
        }
コード例 #6
0
        public void should_handle_execution_completion_received()
        {
            var messageExecutionCompletedTransportMessage = new MessageExecutionCompleted(MessageId.NextId(), 0, null).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(messageExecutionCompletedTransportMessage);

            var forwardedMessage = MessagesForwardedToBus.Single(x => x.MessageTypeId == MessageExecutionCompleted.TypeId);

            forwardedMessage.ShouldEqualDeeply(messageExecutionCompletedTransportMessage);
        }
コード例 #7
0
        public void should_not_handle_replayed_message_with_unknown_replay_id()
        {
            var otherReplayId = Guid.NewGuid();

            var message = new FakeCommand(123).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(message.ToReplayedTransportMessage(otherReplayId));

            Thread.Sleep(10);
            MessagesForwardedToBus.Count.ShouldEqual(0);
        }
コード例 #8
0
        public void should_forward_normal_message_after_replay_phase()
        {
            Transport.Start();

            var message = new FakeCommand(123).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());
            Thread.Sleep(10);
            InnerTransport.RaiseMessageReceived(message);
            Thread.Sleep(10);

            MessagesForwardedToBus.Count.ShouldEqual(1);
            MessagesForwardedToBus.Single().Id.ShouldEqual(message.Id);
        }
コード例 #9
0
        public void should_only_forward_replayed_messages_during_replay_phase()
        {
            Transport.Start();
            var transportMessageToForward = new FakeCommand(123).ToTransportMessage();
            var normalTransportMessage    = new FakeEvent(123).ToTransportMessage();

            var replayedTransportMessage = transportMessageToForward.ToReplayedTransportMessage(ReplayId);

            InnerTransport.RaiseMessageReceived(replayedTransportMessage);
            InnerTransport.RaiseMessageReceived(normalTransportMessage);

            MessagesForwardedToBus.Count.ShouldEqual(1);
            MessagesForwardedToBus.Single().Id.ShouldEqual(transportMessageToForward.Id);
        }
コード例 #10
0
        public void should_stop_to_deduplicate_after_safety_phase()
        {
            Transport.Start();
            var duplicatedMessage = new FakeCommand(123).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());
            InnerTransport.RaiseMessageReceived(new SafetyPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());
            InnerTransport.RaiseMessageReceived(duplicatedMessage);
            InnerTransport.RaiseMessageReceived(duplicatedMessage);

            Wait.Until(() => MessagesForwardedToBus.Count == 2, 100.Milliseconds());

            MessagesForwardedToBus.ForEach(msg => msg.Id.ShouldEqual(duplicatedMessage.Id));
        }
コード例 #11
0
        public void should_not_handle_twice_duplicate_messages()
        {
            Transport.Start();

            var duplicatedMessage = new FakeCommand(123).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());
            InnerTransport.RaiseMessageReceived(duplicatedMessage);
            InnerTransport.RaiseMessageReceived(duplicatedMessage.ToReplayedTransportMessage(StartMessageReplayCommand.ReplayId));

            Wait.Until(() => MessagesForwardedToBus.Count == 1, 150.Milliseconds());

            MessagesForwardedToBus.Single().Id.ShouldEqual(duplicatedMessage.Id);
        }
コード例 #12
0
        public void should_publish_a_MessageHandled_event_after_a_persistent_message_is_processed_by_the_bus()
        {
            Transport.Start();
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(123).ToTransportMessage();
                InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());

                InnerTransport.RaiseMessageReceived(command);
                Transport.AckMessage(command);

                InnerTransport.ExpectExactly(new TransportMessageSent(new MessageHandled(command.Id).ToTransportMessage(Self), new[] { PersistencePeer }));
            }
        }
コード例 #13
0
        public void should_not_lose_messages_when_persistent_transport_goes_down_and_comes_back_up()
        {
            using (MessageId.PauseIdGeneration())
            {
                PeerDirectory.Setup(directory => directory.GetPeersHandlingMessage(new MessageBinding(new MessageTypeId(typeof(PersistMessageCommand)), BindingKey.Empty)))
                .Returns(new List <Peer> {
                    PersistencePeer
                });

                // Stopping persistence
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new byte[0], PersistencePeer));

                InnerTransport.ExpectExactly(new TransportMessageSent(new TransportMessage(MessageTypeId.PersistenceStoppingAck, new byte[0], Self), PersistencePeer));
                InnerTransport.Messages.Clear();

                // should enqueue messages to persistence
                var message      = new FakeCommand(123).ToTransportMessage();
                var ackedMessage = new FakeCommand(456).ToTransportMessage();
                Transport.Send(message, new[] { AnotherPersistentPeer });
                Transport.AckMessage(ackedMessage);

                InnerTransport.AckedMessages.ShouldBeEmpty();
                InnerTransport.ExpectExactly(new TransportMessageSent(message, AnotherPersistentPeer));
                InnerTransport.Messages.Clear();

                // starting persistence - should send enqueued messages
                Transport.OnPeerUpdated(PersistencePeer.Id, PeerUpdateAction.Started);

                InnerTransport.ExpectExactly(new[]
                {
                    new TransportMessageSent(new PersistMessageCommand(message, new[] { AnotherPersistentPeer.Id }).ToTransportMessage(Self), PersistencePeer),
                    new TransportMessageSent(new MessageHandled(ackedMessage.Id).ToTransportMessage(Self), PersistencePeer)
                });


                InnerTransport.Messages.Clear();
                InnerTransport.AckedMessages.Clear();

                // should send messages without going through the queue
                Transport.Send(message, new[] { AnotherPersistentPeer });
                Transport.AckMessage(ackedMessage);

                InnerTransport.ExpectExactly(new[]
                {
                    new TransportMessageSent(message, AnotherPersistentPeer),
                    new TransportMessageSent(new PersistMessageCommand(message, new[] { AnotherPersistentPeer.Id }).ToTransportMessage(Self), PersistencePeer),
                    new TransportMessageSent(new MessageHandled(ackedMessage.Id).ToTransportMessage(Self), PersistencePeer),
                });
            }
        }
コード例 #14
0
        public void should_forward_a_normal_message_after_a_back_to_live_event()
        {
            Transport.Start();

            var transportMessageToForward = new FakeCommand(123).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(transportMessageToForward);
            MessagesForwardedToBus.ShouldBeEmpty();

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());

            Thread.Sleep(50);
            MessagesForwardedToBus.Count.ShouldEqual(1);
            MessagesForwardedToBus.Single().ShouldEqualDeeply(transportMessageToForward);
        }
コード例 #15
0
        public void should_not_publish_a_MessageHandled_event_after_a_non_persistent_message_is_processed_by_the_bus()
        {
            Transport.Start();

            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeNonPersistentCommand(123).ToTransportMessage();
                InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());

                InnerTransport.RaiseMessageReceived(command);
                Transport.AckMessage(command);

                InnerTransport.ExpectNothing();
            }
        }
コード例 #16
0
        public void should_force_WasPersisted_for_replayed_messages()
        {
            Transport.Start();

            var sourceTransportMessage = new FakeCommand(123).ToTransportMessage();

            sourceTransportMessage.WasPersisted = null;

            var replayTransportMessage = sourceTransportMessage.ToReplayedTransportMessage(ReplayId);

            InnerTransport.RaiseMessageReceived(replayTransportMessage);

            var forwardedTransportMessage = MessagesForwardedToBus.ExpectedSingle();

            forwardedTransportMessage.WasPersisted.ShouldEqual(true);
        }
コード例 #17
0
        public void should_not_lose_messages_when_switching_to_safety_phase()
        {
            Transport.Start();

            var liveMessageToStack = new FakeCommand(123).ToTransportMessage();
            var replayedMessageToPlayAfterStack = new FakeCommand(456).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(liveMessageToStack);
            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());
            InnerTransport.RaiseMessageReceived(replayedMessageToPlayAfterStack.ToReplayedTransportMessage(StartMessageReplayCommand.ReplayId));

            Wait.Until(() => MessagesForwardedToBus.Count >= 2, 150.Milliseconds());

            MessagesForwardedToBus.Count.ShouldEqual(2);
            MessagesForwardedToBus.First().Id.ShouldEqual(liveMessageToStack.Id);
            MessagesForwardedToBus.Last().Id.ShouldEqual(replayedMessageToPlayAfterStack.Id);
        }
コード例 #18
0
        public void should_forward_a_normal_message_after_a_back_to_live_event()
        {
            Transport.Start();

            var transportMessageToForward = new FakeCommand(123).ToTransportMessage();

            InnerTransport.RaiseMessageReceived(transportMessageToForward);
            MessagesForwardedToBus.ShouldBeEmpty();

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());

            Wait.Until(() => MessagesForwardedToBus.Count == 1, 150.Milliseconds());

            var transportMessage = MessagesForwardedToBus.Single();

            transportMessage.ShouldEqualDeeply(transportMessageToForward);
        }
コード例 #19
0
        public void should_prioritize_infrastructure_messages()
        {
            Transport.Start();

            Transport.MessageReceived += x =>
            {
                Thread.Sleep(200);
            };

            InnerTransport.RaiseMessageReceived(new FakeCommand(1).ToTransportMessage());
            InnerTransport.RaiseMessageReceived(new FakeInfrastructureCommand().ToTransportMessage());
            Thread.Sleep(20);

            MessagesForwardedToBus.ShouldContain(x => x.MessageTypeId == new MessageTypeId(typeof(FakeInfrastructureCommand)));

            Transport.Stop();
        }
コード例 #20
0
        public void should_not_lose_messages_when_persistent_transport_goes_down_and_comes_back_up()
        {
            using (MessageId.PauseIdGeneration())
            {
                // Stopping persistence
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new MemoryStream(), PersistencePeer));
                InnerTransport.ExpectExactly(new TransportMessageSent(new TransportMessage(MessageTypeId.PersistenceStoppingAck, new MemoryStream(), Self), PersistencePeer));

                InnerTransport.Messages.Clear();

                // should enqueue messages to persistence
                var message      = new FakeCommand(123).ToTransportMessage();
                var ackedMessage = new FakeCommand(456).ToTransportMessage();
                Transport.Send(message, new[] { AnotherPersistentPeer });
                Transport.AckMessage(ackedMessage);

                InnerTransport.AckedMessages.ShouldBeEmpty();
                InnerTransport.ExpectExactly(new TransportMessageSent(message, AnotherPersistentPeer, true));
                InnerTransport.Messages.Clear();

                // starting persistence - should send enqueued messages
                Transport.OnPeerUpdated(PersistencePeer.Id, PeerUpdateAction.Started);

                InnerTransport.ExpectExactly(new[]
                {
                    new TransportMessageSent(message.ToPersistTransportMessage(AnotherPersistentPeer.Id), PersistencePeer),
                    new TransportMessageSent(new MessageHandled(ackedMessage.Id).ToTransportMessage(Self), PersistencePeer)
                });

                InnerTransport.Messages.Clear();
                InnerTransport.AckedMessages.Clear();

                // should send messages without going through the queue
                Transport.Send(message, new[] { AnotherPersistentPeer });
                Transport.AckMessage(ackedMessage);

                InnerTransport.ExpectExactly(new[]
                {
                    new TransportMessageSent(message, AnotherPersistentPeer, true).ToPersistence(PersistencePeer),
                    new TransportMessageSent(new MessageHandled(ackedMessage.Id).ToTransportMessage(Self), PersistencePeer),
                });
            }
        }
コード例 #21
0
        public void should_force_WasPersisted_for_replayed_messages_during_safety_phase()
        {
            Transport.Start();

            InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(ReplayId).ToTransportMessage());

            var sourceTransportMessage = new FakeCommand(123).ToTransportMessage();

            sourceTransportMessage.WasPersisted = null;

            var replayTransportMessage = sourceTransportMessage.ToReplayedTransportMessage(ReplayId);

            InnerTransport.RaiseMessageReceived(replayTransportMessage);

            Wait.Until(() => MessagesForwardedToBus.Count == 1, 150.Milliseconds());

            var forwardedTransportMessage = MessagesForwardedToBus.ExpectedSingle();

            forwardedTransportMessage.WasPersisted.ShouldEqual(true);
        }
コード例 #22
0
        public void should_consider_WasPersisted_before_publishing_a_MessageHandled_event(bool wasPersisted)
        {
            Transport.Start();
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(123).ToTransportMessage(wasPersisted: wasPersisted);
                InnerTransport.RaiseMessageReceived(new ReplayPhaseEnded(StartMessageReplayCommand.ReplayId).ToTransportMessage());

                InnerTransport.RaiseMessageReceived(command);
                Transport.AckMessage(command);

                if (wasPersisted)
                {
                    InnerTransport.ExpectExactly(new TransportMessageSent(new MessageHandled(command.Id).ToTransportMessage(Self), PersistencePeer));
                }
                else
                {
                    InnerTransport.ExpectNothing();
                }
            }
        }
コード例 #23
0
        public void should_discard_messages_waiting_for_persistence_on_stop()
        {
            using (MessageId.PauseIdGeneration())
            {
                Transport.Start();

                PeerDirectory.Setup(directory => directory.GetPeersHandlingMessage(new MessageBinding(MessageTypeId.PersistenceStoppingAck, BindingKey.Empty)))
                .Returns(new List <Peer> {
                    PersistencePeer
                });

                // Stopping persistence
                InnerTransport.RaiseMessageReceived(new TransportMessage(MessageTypeId.PersistenceStopping, new byte[0], PersistencePeer));
                InnerTransport.Messages.Clear();

                var ackedMessage = new FakeCommand(456).ToTransportMessage();
                Transport.AckMessage(ackedMessage);

                Transport.Stop();
                Transport.Start();
            }
        }