예제 #1
0
        public void When_Subscribe_Two_Handler_After_Add_A_Rule_Should_Handlers_Execute()
        {
            var messagebus = new UniRxMessageBus();

            // Define some const
            const string firstHandlerName   = "handler First";
            const string secondHandlerName  = "handler Second";
            const string firstMessageString = firstHandlerName + "_Message";

            // Create Messages
            var firstMessage = new SomeMessage(firstMessageString, string.Empty, DateTime.UtcNow);

            // Create Handlers
            var firstHandler  = new SomeMessageHandler(firstHandlerName);
            var secondHandler = new SomeMessageHandler(secondHandlerName);

            // Add Rules
            messagebus.AddRule(MessageRouteRule.Create <SomeMessage, SomeMessageHandler>(firstHandlerName + "_Rule", false, null, null, null));

            // Subscribe handlers
            messagebus.Subscribe <SomeMessageHandler, SomeMessage>(firstHandler,
                                                                   new MessageHandlerActionExecutor <SomeMessage>(msg => firstHandler.Handle(msg)));

            messagebus.Subscribe <SomeMessageHandler, SomeMessage>(secondHandler,
                                                                   new MessageHandlerActionExecutor <SomeMessage>(msg => secondHandler.Handle(msg)));

            // Raise events
            messagebus.RaiseEvent(firstMessage);

            // Assertions
            Assert.AreEqual(1, firstHandler.HandleCallCount, CreateAssetMessage(firstHandler, 0));
            Assert.AreEqual(1, secondHandler.HandleCallCount, CreateAssetMessage(secondHandler, 1));
        }
예제 #2
0
        public void When_Subscribe_Before_Add_Rule_Should_Handler_Execute()
        {
            var messagebus = new UniRxMessageBus();

            var handler = new SomeMessageHandler(string.Empty);

            messagebus.Subscribe <SomeMessageHandler, SomeMessage>(handler,
                                                                   new MessageHandlerActionExecutor <SomeMessage>(msg => handler.Handle(msg)));

            messagebus.AddRule(MessageRouteRule.Create <SomeMessage, SomeMessageHandler>(string.Empty, false));

            var message = new SomeMessage("First message", string.Empty, DateTime.UtcNow);

            messagebus.RaiseEvent(message);

            Assert.AreEqual(1, handler.HandleCallCount);
        }