public void MeasureThroughput()
        {
            const int sendMessageCount = 2000000;

            var senderTransport = CreateAndStartZmqTransport("Abc.Testing.Sender");

            var receivedMessageCount = 0;
            var receiverTransport = CreateAndStartZmqTransport("Abc.Testing.Receiver", _ => ++receivedMessageCount);
            var receivers = new[] { new Peer(receiverTransport.PeerId, receiverTransport.InboundEndPoint) };

            var transportMessage = new FakeCommand(42).ToTransportMessage();
            senderTransport.Send(transportMessage, receivers);

            var spinWait = new SpinWait();
            while (receivedMessageCount != 1)
                spinWait.SpinOnce();

            using (Measure.Throughput(sendMessageCount))
            {
                for (var i = 0; i < sendMessageCount; ++i)
                {
                    senderTransport.Send(transportMessage, receivers);
                }

                while (receivedMessageCount != sendMessageCount + 1)
                    spinWait.SpinOnce();
            }

            senderTransport.Stop();
            receiverTransport.Stop();
        }
        public void should_send_MessageProcessingFailed_if_unable_to_deserialize_message()
        {
            SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);

            _bus.Start();

            var command = new FakeCommand(123);
            _messageSerializer.AddSerializationExceptionFor(command.TypeId(), "Serialization error");

            using (SystemDateTime.PauseTime())
            using (MessageId.PauseIdGeneration())
            {
                var transportMessage = command.ToTransportMessage();

                var messageProcessingFailedBytes = new MessageProcessingFailed(null, null, null, DateTime.UtcNow, null).ToTransportMessage().MessageBytes;
                _messageSerializer.AddSerializationFuncFor<MessageProcessingFailed>(x =>
                {
                    x.FailingMessage.ShouldEqual(transportMessage);
                    x.ExceptionUtcTime.ShouldEqual(SystemDateTime.UtcNow);
                    x.ExceptionMessage.ShouldContain("Unable to deserialize message");
                    return messageProcessingFailedBytes;
                });
                
                _transport.RaiseMessageReceived(transportMessage);

                var processingFailedTransportMessage = new TransportMessage(MessageUtil.TypeId<MessageProcessingFailed>(), messageProcessingFailedBytes, _self);
                _transport.ExpectExactly(new TransportMessageSent(processingFailedTransportMessage, _peerUp));
            }
        }
Example #3
0
        public void should_not_block_when_hitting_high_water_mark()
        {
            var senderTransport = CreateAndStartZmqTransport();
            senderTransport.SocketOptions.SendHighWaterMark = 3;
            senderTransport.SocketOptions.SendTimeout = 50.Milliseconds();
            senderTransport.SocketOptions.SendRetriesBeforeSwitchingToClosedState = 2;

            var receviedMessages = new List<TransportMessage>();
            var upReceiverTransport = CreateAndStartZmqTransport(onMessageReceived: receviedMessages.Add);
            var upReceiver = new Peer(new PeerId("Abc.Testing.Receiver.Up"), upReceiverTransport.InboundEndPoint);

            var downReceiverTransport = CreateAndStartZmqTransport();
            var downReceiver = new Peer(new PeerId("Abc.Testing.Receiver.Down"), downReceiverTransport.InboundEndPoint);

            downReceiverTransport.Stop();

            for (var i = 1; i <= 10; ++i)
            {
                var message = new FakeCommand(i).ToTransportMessage();
                senderTransport.Send(message, new[] { upReceiver, downReceiver });

                var expectedMessageCount = i;
                Wait.Until(() => receviedMessages.Count == expectedMessageCount, 500.Milliseconds(), "Failed to send message after " + i + " successful sent");
            }
        }
        public void should_resolve_peer_using_basic_round_robin_for_different_commands()
        {
            // Arrange
            var resolver = new RoundRobinPeerSelector();
            
            var command1 = new FakeCommand(42);
            var command1Peer1 = new Peer(new PeerId("command1peer1"), "endpoint1");
            var command1Peer2 = new Peer(new PeerId("command1peer2"), "endpoint2");
            var command1HandlingPeer = new[] { command1Peer1, command1Peer2 };

            var command2 = new FakeInfrastructureCommand();
            var command2Peer1 = new Peer(new PeerId("command2peer1"), "endpoint1");
            var command2Peer2 = new Peer(new PeerId("command2peer2"), "endpoint2");
            var command2HandlingPeer = new[] { command2Peer1, command2Peer2 };

            // Act - Assert
            var resolvedPeer = resolver.GetTargetPeer(command1, command1HandlingPeer);
            resolvedPeer.ShouldEqual(command1Peer1);

            resolvedPeer = resolver.GetTargetPeer(command1, command1HandlingPeer);
            resolvedPeer.ShouldEqual(command1Peer2);

            resolvedPeer = resolver.GetTargetPeer(command2, command2HandlingPeer);
            resolvedPeer.ShouldEqual(command2Peer1);

            resolvedPeer = resolver.GetTargetPeer(command1, command1HandlingPeer);
            resolvedPeer.ShouldEqual(command1Peer1);

            resolvedPeer = resolver.GetTargetPeer(command2, command2HandlingPeer);
            resolvedPeer.ShouldEqual(command2Peer2);

            resolvedPeer = resolver.GetTargetPeer(command2, command2HandlingPeer);
            resolvedPeer.ShouldEqual(command2Peer1);
        }
        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());
        }
        public void should_ack_transport_when_handling_undeserializable_message()
        {
            var command = new FakeCommand(123);
            _messageSerializer.AddSerializationExceptionFor(command.TypeId());

            var transportMessage = command.ToTransportMessage();
            _transport.RaiseMessageReceived(transportMessage);

            _transport.AckedMessages.ShouldContain(transportMessage);
        }
Example #7
0
        public void should_throw_exception_if_no_peer_is_setup_to_handle_a_command()
        {
            var command = new FakeCommand(456);

            SetupPeersHandlingMessage<FakeCommand>(new Peer[0]);

            _bus.Start();

            Assert.Throws<InvalidOperationException>(() => _bus.Send(command));
        }
        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();
        }
Example #9
0
        public void should_dispatch_received_message()
        {
            var command = new FakeCommand(123);
            var invokerCalled = false;
            SetupDispatch(command, _ => invokerCalled = true);

            var transportMessageReceived = command.ToTransportMessage(_peerUp);
            _transport.RaiseMessageReceived(transportMessageReceived);

            invokerCalled.ShouldBeTrue();
        }
Example #10
0
        public void should_not_throw_when_handling_a_persistent_command_even_if_peer_is_not_responding()
        {
            var command = new FakeCommand(456);
            var peer = new Peer(new PeerId("Abc.Testing.Peer1"), "Peer1Endpoint", true, isResponding: false);
            SetupPeersHandlingMessage<FakeCommand>(new[] { peer });

            _bus.Start();
            _bus.Send(command);

            var sentMessage = _transport.MessagesSent.ExpectedSingle();
            sentMessage.ShouldHaveSamePropertiesAs(command);
        }
        public void should_send_command_with_message_context()
        {
            MessageContext.Current.ShouldBeNull();

            var message = new FakeCommand(1);
            MessageContext context = null;
            _busMock.Setup(x => x.Send(message)).Callback(() => context = MessageContext.Current);

            _bus.Send(message);

            MessageContext.Current.ShouldBeNull();
            context.ShouldEqual(_context);
        }
        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);
        }
Example #13
0
        public void should_throw_exception_if_several_peers_can_handle_a_command()
        {
            var command = new FakeCommand(456);

            var peer1 = new Peer(new PeerId("Abc.Testing.Peer1"), "Peer1Endpoint");
            var peer2 = new Peer(new PeerId("Abc.Testing.Peer2"), "Peer2Endpoint");

            SetupPeersHandlingMessage<FakeCommand>(peer1, peer2);

            _bus.Start();

            Assert.Throws<InvalidOperationException>(() => _bus.Send(command));
        }
Example #14
0
        public void should_not_consider_if_peer_is_up_to_send_commands([Values(true, false)]bool isTargetPeerUp)
        {
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(456);
                SetupPeersHandlingMessage<FakeCommand>(isTargetPeerUp ? _peerUp : _peerDown);

                _bus.Start();
                _bus.Send(command);

                _transport.ExpectExactly(new TransportMessageSent(command.ToTransportMessage(_self), new[] { isTargetPeerUp ? _peerUp : _peerDown }));
            }
        }
        public void should_return_null_when_no_peer_can_handle_the_command()
        {
            // Arrange
            var resolver = new RoundRobinPeerSelector();
            var command = new FakeCommand(42);
            var handlingPeers = new Peer[0];

            // Act
            var resolvedPeer = resolver.GetTargetPeer(command, handlingPeers);

            // Assert
            resolvedPeer.ShouldBeNull();
        }
        public void should_send_command_to_peer_with_message_context()
        {
            MessageContext.Current.ShouldBeNull();

            var message = new FakeCommand(1);
            var peer = new Peer(new PeerId("Abc.Foo.0"), "tcp://dtc:1234");
            MessageContext context = null;
            _busMock.Setup(x => x.Send(message, peer)).Callback(() => context = MessageContext.Current);

            _bus.Send(message, peer);

            MessageContext.Current.ShouldBeNull();
            context.ShouldEqual(_context);
        }
        public void shoud_send_a_completion_message_with_error_code_on_exception()
        {
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(123);
                SetupDispatch(command, error: new Exception());
                var transportMessageReceived = command.ToTransportMessage(_peerUp);

                _transport.RaiseMessageReceived(transportMessageReceived);

                var expectedTransportMessage = new MessageExecutionCompleted(transportMessageReceived.Id, 1, null).ToTransportMessage(_self);
                _transport.Expect(new TransportMessageSent(expectedTransportMessage, _peerUp));
            }
        }
        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);
        }
        public void should_return_the_only_handling_peer_when_there_is_only_one_handling_peer()
        {
            // Arrange
            var resolver = new RoundRobinPeerSelector();
            var command = new FakeCommand(42);
            var peer1 = new Peer(new PeerId("peer1"), "endpoint1");
            var handlingPeers = new []{ peer1 };

            // Act
            var resolvedPeer = resolver.GetTargetPeer(command, handlingPeers);

            // Assert
            resolvedPeer.ShouldEqual(peer1);
        }
        public void shoud_send_a_completion_message_without_error_code()
        {
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(123);
                SetupDispatch(command);

                var transportMessageReceived = command.ToTransportMessage(_peerUp);
                _transport.RaiseMessageReceived(transportMessageReceived);

                var messageExecutionCompleted = new MessageExecutionCompleted(transportMessageReceived.Id, 0, null).ToTransportMessage(_self);
                _transport.ExpectExactly(new TransportMessageSent(messageExecutionCompleted, _peerUp));
            }
        }
Example #21
0
        public void should_not_filter_received_messages_when_environment_is_not_specified()
        {
            var transport1 = CreateAndStartZmqTransport(environment: null);

            var transport2ReceivedMessages = new ConcurrentBag<TransportMessage>();
            var transport2 = CreateAndStartZmqTransport(onMessageReceived: transport2ReceivedMessages.Add, environment: "NotTest");
            var transport2Peer = new Peer(new PeerId("Abc.Testing.2"), transport2.InboundEndPoint);

            var message = new FakeCommand(1).ToTransportMessage();
            transport1.Send(message, new[] { transport2Peer });

            Wait.Until(() => transport2ReceivedMessages.Count >= 1, 500.Milliseconds());
            transport2ReceivedMessages.Single().Id.ShouldEqual(message.Id);
        }
Example #22
0
        public void should_forward_error_when_handling_command_locally()
        {
            var command = new FakeCommand(1);
            SetupDispatch(command, error: new Exception("Test error"));
            SetupPeersHandlingMessage<FakeCommand>(_self);

            _bus.Start();

            var task = _bus.Send(command);
            var completed = task.Wait(500);

            completed.ShouldBeTrue();
            task.Result.IsSuccess.ShouldBeFalse();
        }
        public void shoud_send_a_completion_message_with_domain_exception_error_code()
        {
            using (MessageId.PauseIdGeneration())
            {
                const int domainExceptionValue = 5000;
                var command = new FakeCommand(123);
                SetupDispatch(command, error: new DomainException(domainExceptionValue, "Domain Exception"));
                var transportMessageReceived = command.ToTransportMessage(_peerUp);

                _transport.RaiseMessageReceived(transportMessageReceived);

                var expectedTransportMessage = new MessageExecutionCompleted(transportMessageReceived.Id, domainExceptionValue).ToTransportMessage(_self);
                _transport.ExpectExactly(new TransportMessageSent(expectedTransportMessage, _peerUp));
            }
        }
Example #24
0
        public void should_handle_command_locally()
        {
            var command = new FakeCommand(1);
            var handled = false;
            SetupDispatch(command, _ => handled = true);
            SetupPeersHandlingMessage<FakeCommand>(_self);

            _bus.Start();

            var completed = _bus.Send(command).Wait(500);

            handled.ShouldBeTrue();
            completed.ShouldBeTrue();
            _transport.Messages.ShouldBeEmpty();
        }
Example #25
0
        public void should_not_let_the_outbound_thread_die_if_a_peer_cannot_be_resolved()
        {
            var senderTransport = CreateAndStartZmqTransport(environment: null);

            var receivedMessages = new ConcurrentBag<TransportMessage>();
            var destinationTransport = CreateAndStartZmqTransport(onMessageReceived: receivedMessages.Add, environment: "NotTest");
            var destinationPeer = new Peer(new PeerId("Abc.Testing.2"), destinationTransport.InboundEndPoint);
            var nonExistingPeer = new Peer(new PeerId("Abc.NonExistingPeer.2"), "tcp://non_existing_peer:1234");

            var message = new FakeCommand(1).ToTransportMessage();
            senderTransport.Send(message, new[] { nonExistingPeer });
            senderTransport.Send(message, new[] { destinationPeer });

            Wait.Until(() => receivedMessages.Count >= 1, 1000.Milliseconds(), "The outbound thread was killed and couldn't connect to the next peer");
        }
        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);
        }
        public void should_not_send_a_MessageProcessingFailed_on_domain_exception_with_local_processing()
        {
            SetupPeersHandlingMessage<MessageProcessingFailed>(_peerUp);

            using (SystemDateTime.PauseTime())
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(123);
                SetupDispatch(command, error: new DomainException(123, "Exception message"));
                SetupPeersHandlingMessage<FakeCommand>(_self);

                _bus.Start();
                _bus.Send(command);

                _transport.ExpectNothing();
            }
        }
Example #28
0
        public void should_send_message()
        {
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(456);
                SetupPeersHandlingMessage<FakeCommand>(_peerUp);

                _bus.Start();
                _bus.Send(command);

                var sentMessage = _transport.Messages.Single();

                var expectedTransportMessage = command.ToTransportMessage(_self);
                sentMessage.TransportMessage.ShouldHaveSamePropertiesAs(expectedTransportMessage);
                var destination = sentMessage.Targets.Single();
                destination.ShouldHaveSamePropertiesAs(_peerUp);
            }
        }
        public void should_release_task_when_completion_message_is_sent()
        {
            using (MessageId.PauseIdGeneration())
            {
                var command = new FakeCommand(123);
                SetupPeersHandlingMessage<FakeCommand>(_peerUp);
                _bus.Start();

                var task = _bus.Send(command);
                var commandCompleted = new MessageExecutionCompleted(MessageId.NextId(), 1);
                _transport.RaiseMessageReceived(commandCompleted.ToTransportMessage());

                var receivedAck = task.Wait(10);

                receivedAck.ShouldBeTrue();
                task.Result.ErrorCode.ShouldEqual(1);
            }
        }
Example #30
0
        public void handlers_reply_with_an_int()
        {
            using (MessageId.PauseIdGeneration())
            {
                const int commandReply = 456;
                var command = new FakeCommand(123);
                SetupDispatch(command, _ => _bus.Reply(commandReply));

                var transportMessageReceived = command.ToTransportMessage(_peerUp);
                var expectedTransportMessage = new MessageExecutionCompleted(transportMessageReceived.Id, commandReply, null).ToTransportMessage(_self);

                _transport.RaiseMessageReceived(transportMessageReceived);

                var sentMessage = _transport.Messages.Single();
                expectedTransportMessage.ShouldHaveSamePropertiesAs(sentMessage.TransportMessage);
                var destination = sentMessage.Targets.Single();
                destination.ShouldHaveSamePropertiesAs(_peerUp);
            }
        }