Esempio n. 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);
        }
Esempio n. 2
0
        public async Task Unsubscribe_SampleMessage_NoMessageReceived()
        {
            // Arrange
            IServiceCollection services = new ServiceCollection();

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

            IMessageBus          messageBus           = provider.GetRequiredService <IMessageBus>();
            SampleMessageHandler sampleMessageHandler = provider.GetRequiredService <SampleMessageHandler>();
            var subscription = messageBus.Subscribe <SampleMessageHandler, SampleMessage>();

            await messageBus.PublishAsync(new SampleMessage("Hello Test"));

            Assert.AreEqual("Hello Test", sampleMessageHandler.MyMessage);

            // Act
            messageBus.Unsubscribe(subscription);

            // Assert
            await messageBus.PublishAsync(new SampleMessage("Hello Echo"));

            Assert.AreEqual("Hello Test", sampleMessageHandler.MyMessage);
        }