Ejemplo n.º 1
0
        public void Setup()
        {
            _busMock = new Mock <IBus>();
            _context = MessageContext.CreateTest("u.name");

            _bus = new MessageContextAwareBus(_busMock.Object, _context);
        }
Ejemplo n.º 2
0
        private static void RunLocalDispatch()
        {
            var bus = new BusFactory()
                      .WithHandlers(typeof(BusPerformanceTests.PerfHandler))
                      .CreateAndStartInMemoryBus();

            Console.WriteLine("Press any key to start");
            Console.ReadKey();

            var running = true;

            var runTask = Task.Run(() =>
            {
                using (DispatchQueue.SetCurrentDispatchQueueName(DispatchQueueNameScanner.DefaultQueueName))
                    using (MessageContext.SetCurrent(MessageContext.CreateTest()))
                    {
                        while (running)
                        {
                            bus.Send(new BusPerformanceTests.PerfCommand(42));
                        }
                    }
            });

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            running = false;
            runTask.Wait();

            bus.Stop();
        }
Ejemplo n.º 3
0
 public void Setup()
 {
     _invoker           = new TestMessageHandlerInvoker <FakeCommand>();
     _message           = new FakeCommand(123);
     _messageContext    = MessageContext.CreateTest("u.name");
     _pipes             = new List <IPipe>();
     _invocation        = new PipeInvocation(_invoker, _message, _messageContext, _pipes);
     _handlerInvocation = _invocation;
 }
Ejemplo n.º 4
0
        private Task <DispatchResult> Dispatch(IMessage message)
        {
            var taskCompletionSource = new TaskCompletionSource <DispatchResult>();

            var dispatch = new MessageDispatch(MessageContext.CreateTest("u.name"), message, (x, r) => taskCompletionSource.SetResult(r));

            _messageDispatcher.Dispatch(dispatch);

            return(taskCompletionSource.Task);
        }
Ejemplo n.º 5
0
 public void Setup()
 {
     _invoker        = new TestAsyncMessageHandlerInvoker <ExecutableEvent>();
     _message        = new ExecutableEvent();
     _messageContext = MessageContext.CreateTest();
     _pipes          = new List <IPipe>();
     _invocation     = new PipeInvocation(_invoker, new List <IMessage> {
         _message
     }, _messageContext, _pipes);
 }
        public void should_throw_if_a_blacklisted_peer_tries_to_register()
        {
            var blacklistedPeer = TestDataBuilder.CreatePersistentPeerDescriptor("tcp://blacklistedpeer:123", typeof(FakeCommand));
            var registerCommand = new RegisterPeerCommand(blacklistedPeer);

            _handler.Context = MessageContext.CreateTest(new OriginatorInfo(blacklistedPeer.Peer.Id, blacklistedPeer.Peer.EndPoint, "BLACKLISTEDMACHINE", "initiator"));

            var exception = typeof(InvalidOperationException).ShouldBeThrownBy(() => _handler.Handle(registerCommand));

            exception.Message.ShouldEqual("Peer Abc.Testing.0 on host BLACKLISTEDMACHINE is not allowed to register on this directory");
        }
Ejemplo n.º 7
0
        public void should_get_reply_code()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var context  = MessageContext.CreateTest("u.name");
            var dispatch = new MessageDispatch(context.WithDispatchQueueName(DispatchQueueNameScanner.DefaultQueueName), new ReplyCommand(), (x, r) => { });

            _messageDispatcher.Dispatch(dispatch);

            context.ReplyCode.ShouldEqual(ReplyCommand.ReplyCode);
        }
Ejemplo n.º 8
0
        public void should_get_reply_code()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            int?replyCode = null;
            var context   = MessageContext.CreateTest();
            var dispatch  = new MessageDispatch(context, new ReplyCommand(), (x, r) => replyCode = context.ReplyCode);

            _messageDispatcher.Dispatch(dispatch);

            Wait.Until(() => replyCode == ReplyCommand.ReplyCode, 500.Milliseconds());
        }
Ejemplo n.º 9
0
        public void should_get_reply_code()
        {
            LoadAndStartDispatcher();

            int?replyCode = null;
            var context   = MessageContext.CreateTest();
            var dispatch  = new MessageDispatch(context, new ReplyCommand(), new TestMessageSerializer(), (x, r) => replyCode = context.ReplyCode);

            _messageDispatcher.Dispatch(dispatch);

            Wait.Until(() => replyCode == ReplyCommand.ReplyCode, 2.Seconds());
        }
Ejemplo n.º 10
0
        private Task <DispatchResult> Dispatch(IMessage message, bool isLocal = false)
        {
            var taskCompletionSource = new TaskCompletionSource <DispatchResult>();

            var dispatch = new MessageDispatch(MessageContext.CreateTest("u.name"), message, new TestMessageSerializer(), (x, r) => taskCompletionSource.SetResult(r))
            {
                IsLocal = isLocal
            };

            _messageDispatcher.Dispatch(dispatch);

            return(taskCompletionSource.Task);
        }
Ejemplo n.º 11
0
        private Task <DispatchResult> EnqueueBatchedInvocation(ExecutableEvent message)
        {
            var tcs = new TaskCompletionSource <DispatchResult>();

            var dispatch = new MessageDispatch(MessageContext.CreateTest(), message, (d, r) => tcs.SetResult(r));

            dispatch.SetHandlerCount(1);

            var invoker = new TestBatchedMessageHandlerInvoker <FakeEvent>();

            _dispatchQueue.Enqueue(dispatch, invoker);

            return(tcs.Task);
        }
Ejemplo n.º 12
0
        public void should_override_username()
        {
            var context = MessageContext.CreateTest("original");

            using (MessageContext.SetCurrent(context))
            {
                MessageContext.Current.InitiatorUserName.ShouldEqual("original");

                using (MessageContext.OverrideInitiatorUsername("override"))
                    MessageContext.Current.InitiatorUserName.ShouldEqual("override");

                MessageContext.Current.InitiatorUserName.ShouldEqual("original");
            }
        }
        public void Setup()
        {
            _contextScope = MessageContext.SetCurrent(MessageContext.CreateTest());

            _configurationMock = new Mock <IDirectoryConfiguration>();
            _configurationMock.SetupGet(conf => conf.BlacklistedMachines).Returns(new[] { "ANOTHER_BLACKLISTEDMACHINE", "BLACKlistedMACHINE" });
            _repositoryMock = new Mock <IPeerRepository>();
            _bus            = new TestBus();
            _speedReporter  = new Mock <IDirectorySpeedReporter>();
            _handler        = new DirectoryCommandsHandler(_bus, _repositoryMock.Object, _configurationMock.Object, _speedReporter.Object)
            {
                Context = MessageContext.CreateOverride(_sender.Id, _sender.EndPoint)
            };
        }
Ejemplo n.º 14
0
        public void should_build_invocation_with_pipe()
        {
            var pipe = new FakePipe {
                IsAutoEnabled = true
            };

            _pipeSource.Pipes.Add(pipe);

            var message        = new FakeCommand(123);
            var messageContext = MessageContext.CreateTest("u.name");
            var invoker        = new TestMessageHandlerInvoker(typeof(FakeCommandHandler), typeof(FakeCommand));
            var invocation     = _pipeManager.BuildPipeInvocation(invoker, message, messageContext);

            invocation.Pipes.Single().ShouldEqual(pipe);
        }
Ejemplo n.º 15
0
        public void MeasureLocalDispatch()
        {
            // 25/11/2013 - PC CAO: 190k/s

            var bus = new BusFactory()
                      .WithHandlers(typeof(PerfCommandHandler), typeof(PerfEventHandler))
                      .CreateAndStartInMemoryBus();

            using (MessageContext.SetCurrent(MessageContext.CreateTest().WithDispatchQueueName(DispatchQueueNameScanner.DefaultQueueName)))
            {
                Measure.Execution(500000, () => bus.Send(new PerfCommand(42)));
            }

            bus.Stop();
        }
Ejemplo n.º 16
0
        public void should_continue_processing_messages_after_continuation_error()
        {
            _dispatchQueue.Start();

            var message1 = new ExecutableEvent {
                Callback = x => throw new Exception("Processing error")
            };
            var dispatch = new MessageDispatch(MessageContext.CreateTest(), message1, new TestMessageSerializer(), (d, r) => throw new Exception("Continuation error"));

            dispatch.SetHandlerCount(1);

            _dispatchQueue.Enqueue(dispatch, new TestMessageHandlerInvoker <ExecutableEvent>());

            var message2 = new ExecutableEvent();
            var task     = EnqueueInvocation(message2);

            task.Wait(5.Seconds()).ShouldBeTrue();
        }
Ejemplo n.º 17
0
        public void should_forward_initiator_id()
        {
            using (MessageId.PauseIdGeneration())
            {
                var receivedCommand = new FakeCommand(123);
                var eventToPublish  = new FakeEvent(456);
                SetupDispatch(receivedCommand, _ => _bus.Publish(eventToPublish));
                SetupPeersHandlingMessage <FakeEvent>(_peerDown);

                using (MessageContext.SetCurrent(MessageContext.CreateTest(new OriginatorInfo(_peerUp.Id, _peerUp.EndPoint, null, "x.initiator"))))
                {
                    var transportMessageReceived = receivedCommand.ToTransportMessage(_peerUp);
                    transportMessageReceived.Originator.InitiatorUserName.ShouldEqual("x.initiator");
                    _transport.RaiseMessageReceived(transportMessageReceived);
                }

                var sentMessage = _transport.Messages.Single(x => x.TransportMessage.MessageTypeId == eventToPublish.TypeId());
                sentMessage.TransportMessage.Originator.InitiatorUserName.ShouldEqual("x.initiator");
            }
        }
Ejemplo n.º 18
0
        public void should_invoke_handler()
        {
            var handler   = new Handler();
            var container = new Container(x => x.ForSingletonOf <Handler>().Use(handler));
            var messages  = new List <IMessage>
            {
                new Message {
                    Id = 1
                },
                new Message {
                    Id = 2
                },
            };

            var invoker    = new BatchedMessageHandlerInvoker(container, typeof(Handler), typeof(Message));
            var invocation = new PipeInvocation(invoker, messages, MessageContext.CreateTest(), new IPipe[0]);

            invocation.Run();

            handler.Messages.ShouldEqual(messages.Cast <Message>().ToList());
        }
Ejemplo n.º 19
0
        public void should_hide_task_scheduler()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var syncHandler = new CapturingTaskSchedulerSyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(CapturingTaskSchedulerSyncCommandHandler))).Returns(syncHandler);

            var asyncHandler = new CapturingTaskSchedulerAsyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(CapturingTaskSchedulerAsyncCommandHandler))).Returns(asyncHandler);

            var command = new DispatchCommand();

            Dispatch(command, MessageContext.CreateTest("u.name").WithDispatchQueueName("some queue")); // make sure we go through a dispatch queue!

            syncHandler.Signal.WaitOne(1.Second()).ShouldBeTrue();
            asyncHandler.Signal.WaitOne(1.Second()).ShouldBeTrue();

            syncHandler.TaskScheduler.ShouldEqual(TaskScheduler.Default);
            asyncHandler.TaskScheduler.ShouldEqual(TaskScheduler.Default);
        }
Ejemplo n.º 20
0
        public void should_run_invoker_synchronously_if_dispatch_queue_name_equals_current_queue_name()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler = new SyncCommandHandlerWithQueueName1()
            {
                WaitForSignal = true
            };

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler);

            var task = Task.Run(() =>
            {
                var context = MessageContext.CreateTest("u.name").WithDispatchQueueName("DispatchQueue1");
                Dispatch(new DispatchCommand(), context);
            });

            Thread.Sleep(150);
            task.IsCompleted.ShouldBeFalse("Dispatch should run synchronously");

            handler.CalledSignal.Set();
            Wait.Until(() => task.IsCompleted, 150.Milliseconds());
        }
Ejemplo n.º 21
0
        public void should_filter_invoker()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var asyncHandler = new AsyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(AsyncCommandHandler))).Returns(asyncHandler);

            var syncHandler = new SyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var context    = MessageContext.CreateTest("u.name");
            var command    = new DispatchCommand();
            var dispatched = new ManualResetEvent(false);
            var dispatch   = new MessageDispatch(context, command, (x, r) => dispatched.Set());

            _messageDispatcher.Dispatch(dispatch, x => x == typeof(AsyncCommandHandler));

            dispatched.WaitOne(500.Milliseconds()).ShouldBeTrue();

            syncHandler.Called.ShouldBeFalse();
            asyncHandler.CalledSignal.IsSet.ShouldBeTrue();
        }
Ejemplo n.º 22
0
 public static IMessageHandlerInvocation ToInvocation(this IMessage message)
 {
     return(ToInvocation(message, MessageContext.CreateTest("u.name")));
 }
Ejemplo n.º 23
0
 protected virtual MessageContext CreateMessageContext()
 {
     return(MessageContext.CreateTest());
 }
Ejemplo n.º 24
0
        private void Dispatch(IMessage message, MessageDispatcher dispatcher = null)
        {
            var messageContext = MessageContext.CreateTest("u.name");

            Dispatch(message, messageContext.WithDispatchQueueName(DispatchQueueNameScanner.DefaultQueueName), dispatcher);
        }
Ejemplo n.º 25
0
 public static TestPipeInvocation Create <TMessage>(TMessage message) where TMessage : class, IMessage
 {
     return(new TestPipeInvocation(message, MessageContext.CreateTest("u.name"), new TestMessageHandlerInvoker <TMessage>()));
 }
Ejemplo n.º 26
0
 public TestPipeInvocation(IMessage message, Type handlerType, Exception exception = null) : base(new TestMessageHandlerInvoker(handlerType, message.GetType()), message, MessageContext.CreateTest("u.name"), new List <IPipe>())
 {
     AddExceptionCallback(exception);
 }