コード例 #1
0
        public async Task PublishAsync_SampleMessage_IncreaseMessageHandlerShouldNotReceiveMessage()
        {
            // Arrange
            IServiceCollection services = new ServiceCollection();

            services.AddSingleton <IMessageBus, InMemoryMessageBus>();
            services.AddSingleton <SampleMessageHandler>();
            services.AddSingleton <ToLowerSampleMessageHandler>();
            services.AddSingleton <IncreaseMessageHandler>();
            IServiceProvider provider = services.BuildServiceProvider();

            IMessageBus                 messageBus                  = provider.GetRequiredService <IMessageBus>();
            SampleMessageHandler        sampleMessageHandler        = provider.GetRequiredService <SampleMessageHandler>();
            ToLowerSampleMessageHandler toLowerSampleMessageHandler = provider.GetRequiredService <ToLowerSampleMessageHandler>();
            IncreaseMessageHandler      increaseMessageHandler      = provider.GetRequiredService <IncreaseMessageHandler>();

            messageBus.Subscribe <SampleMessageHandler, SampleMessage>();
            messageBus.Subscribe <ToLowerSampleMessageHandler, SampleMessage>();
            messageBus.Subscribe <IncreaseMessageHandler, IncreaseMessage>();

            var sampleMessage = new SampleMessage("Hello Test");

            // Act
            await messageBus.PublishAsync(sampleMessage);

            // Assert
            Assert.AreEqual("Hello Test", sampleMessageHandler.MyMessage);
            Assert.AreEqual("hello test", toLowerSampleMessageHandler.MyMessage);
            Assert.AreEqual(0, increaseMessageHandler.Value);
        }
コード例 #2
0
        public async Task PublishAsync_IncreaseMessageInitialValueIs0_FinalValueIs42()
        {
            // Arrange
            IServiceCollection services = new ServiceCollection();

            services.AddSingleton <IMessageBus, InMemoryMessageBus>();
            services.AddSingleton <IncreaseMessageHandler>();
            IServiceProvider provider = services.BuildServiceProvider();

            IMessageBus            messageBus = provider.GetRequiredService <IMessageBus>();
            IncreaseMessageHandler handler    = provider.GetRequiredService <IncreaseMessageHandler>();

            messageBus.Subscribe <IncreaseMessageHandler, IncreaseMessage>();

            var firstMessage  = new IncreaseMessage(40);
            var secondMessage = new IncreaseMessage(2);

            // Act
            await messageBus.PublishAsync(firstMessage);

            await messageBus.PublishAsync(secondMessage);

            // Assert
            Assert.AreEqual(42, handler.Value);
        }