Beispiel #1
0
        public void can_handle_two_different_messages()
        {
            var msg2 = new TestMessage2();

            Bus.Publish(msg2);
            Assert.IsOrBecomesTrue(
                () => BusMessages.Count == 2,
                null,
                $"Expected 2 Messages on bus, found {BusMessages.Count}");

            Message deQdMsg;

            if (BusMessages.TryDequeue(out deQdMsg))
            {
                Assert.IsType <TestMessage>(deQdMsg);
            }
            if (BusMessages.TryDequeue(out deQdMsg))
            {
                Assert.IsType <TestMessage2>(deQdMsg);
            }

            Assert.False(BusMessages.TryDequeue(out deQdMsg));

            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref MessageSubscriber.TimesTestMessageHandled) == 1,
                1000,
                $"Expected 1 TestMessage, found {MessageSubscriber.TimesTestMessageHandled}");
            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref MessageSubscriber.TimesTestMessage2Handled) == 1,
                100,
                $"Expected 1 TestMessage2, found {MessageSubscriber.TimesTestMessage2Handled}");
        }
Beispiel #2
0
        public static void Bullshit2()
        {
            TestMessage2Deserializer deser = new TestMessage2Deserializer();

            var readOnlyBuffer = new ReadOnlySpan <byte>();
            var header         = new StagedBodyMessageHeader();
            var message        = new TestMessage2();

            deser.DeserializeOnto(readOnlyBuffer, header, ref message);
        }
Beispiel #3
0
        public void can_handle_multiple_subscribers()
        {
            TestQueuedSubscriber secondSubscriber = new TestQueuedSubscriber(Bus);

            var msg2 = new TestMessage2();

            Bus.Publish(msg2);
            Assert.Equal(BusMessages.Count, 2);

            var msg3 = new TestMessage2();

            Bus.Publish(msg3);
            Assert.Equal(BusMessages.Count, 3);

            Message deQdMsg;

            if (BusMessages.TryDequeue(out deQdMsg))
            {
                Assert.IsType <TestMessage>(deQdMsg);
            }
            if (BusMessages.TryDequeue(out deQdMsg))
            {
                Assert.IsType <TestMessage2>(deQdMsg);
            }

            if (BusMessages.TryDequeue(out deQdMsg))
            {
                Assert.IsType <TestMessage2>(deQdMsg);
            }

            Assert.False(BusMessages.TryDequeue(out deQdMsg));

            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref MessageSubscriber.TimesTestMessageHandled) == 1,
                1000,
                $"Expected 1 TestMessage, handled by first subscriber {MessageSubscriber.TimesTestMessageHandled}");

            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref secondSubscriber.TimesTestMessageHandled) == 0,
                1000,
                $"Expected 0 TestMessage handled by second subscriber, found {secondSubscriber.TimesTestMessageHandled}");

            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref MessageSubscriber.TimesTestMessage2Handled) == 2,
                1000,
                $"Expected 2 TimesTestMessage2Handled by secondSubscriber, found {MessageSubscriber.TimesTestMessage2Handled}");

            Assert.IsOrBecomesTrue(
                () => Interlocked.Read(ref secondSubscriber.TimesTestMessage2Handled) == 2,
                1000,
                $"Expected 2 TimesTestMessage2Handled by second subscriber, found {secondSubscriber.TimesTestMessage2Handled}");
        }
Beispiel #4
0
        public async Task MessageBusRecursiveScopeTest()
        {
            var rootCoreSessionProvider = Resolve <ICoreSessionProvider>();

            rootCoreSessionProvider.ShouldBeOfType <UnitTestCoreSessionProvider>();

            using ((rootCoreSessionProvider as UnitTestCoreSessionProvider).Use(TestConsts.CITY_ID, Guid.Parse(TestConsts.COMPANY_ID), TestConsts.COMPANY_NAME, Guid.Parse(TestConsts.STORE_ID), TestConsts.STORE_NAME, TestConsts.BROKER_ID, TestConsts.BROKER_NAME))
            {
                var message = new TestMessage2();

                await MessageBus.PublishAsync(message);

                //No exception(s) means test was passed.
            }
        }
Beispiel #5
0
        // Scenario2 tests
        // - Call by interface method
        // - Call by interface method calling another delegate
        // - Call by method-info
        // - Call by compiled expression that calls method by method-info
        private static void RunScenario2()
        {
            const int TestCount   = 100000000;
            var       testMessage = new TestMessage2 {
                a = 1, b = 2
            };

            Console.WriteLine("***** PerfRequestTest-2 *****\n");

            RunTest("Interface", TestCount, testMessage, (TestActor2 actor, object message) =>
            {
                var msg = (TestMessage2)message;
                return(msg.Invoke(actor));
            });

            var minLambda = (Func <TestActor2, int, int, int>)Delegate.CreateDelegate(typeof(Func <TestActor2, int, int, int>), typeof(TestActor2).GetMethod("Min"));

            RunTest("InterfaceIndirect", TestCount, testMessage, (TestActor2 actor, object message) =>
            {
                var msg = (TestMessage2)message;
                return(msg.Invoke(actor, minLambda));
            });

            RunTest("Injection", TestCount, testMessage, (TestActor2 actor, object message) =>
            {
                var msg    = (TestMessage2)message;
                var result = minLambda(actor, msg.a, msg.b);
                return(new Temp__Result {
                    v = result
                });
            });

            var minMethod = typeof(TestActor2).GetMethod("Min");
            var handler   = DelegateBuilderHandlerExtendedFunc.Build <TestActor2>(typeof(TestMessage2), typeof(Temp__Result), minMethod);

            RunTest("InjectionCompiled", TestCount, testMessage, (TestActor2 actor, object message) =>
            {
                return(handler(actor, message));
            });

            Console.WriteLine("");
        }
Beispiel #6
0
        public void Test_FlushChannelMessages()
        {
            TestMessage1 msg1 = new TestMessage1();

            msg1.Data.Add(100);
            msg1.Data.Add(200);
            string channel1Name = "TestChannel1";

            TestMessage2 msg2 = new TestMessage2();

            msg2.Data.Add(1000);
            msg2.Data.Add(2000);
            string channel2Name = "TestChannel2";

            // Setup the mocks
            MockDistributedMemoryManager distributedMemoryManagerMock = new MockDistributedMemoryManager();

            DeferredChannelMessageContextEntry entry = new DeferredChannelMessageContextEntry(distributedMemoryManagerMock);

            entry.AddOrUpdateMessage(channel1Name, msg1, null);
            entry.AddOrUpdateMessage(channel2Name, msg2, null);
            entry.FlushMessages();

            TestMessage1 msg1Out;
            TestMessage2 msg2Out;

            Assert.IsFalse(entry.TryGetMessage(channel1Name, out msg1Out), "Channel1 should have no messages");
            Assert.IsFalse(entry.TryGetMessage(channel2Name, out msg2Out), "Channel2 should have no messages");

            // Validation
            MockChannel <TestMessage1> channel1 = distributedMemoryManagerMock.CreatedChannels[channel1Name] as MockChannel <TestMessage1>;

            Assert.IsNotNull(channel1, "{0} was not found", channel1Name);
            Assert.AreEqual(1, channel1.PublishedMessages.Count);
            Assert.AreSame(msg1, channel1.PublishedMessages[0]);

            MockChannel <TestMessage2> channel2 = distributedMemoryManagerMock.CreatedChannels[channel2Name] as MockChannel <TestMessage2>;

            Assert.IsNotNull(channel2, "{0} was not found", channel2Name);
            Assert.AreEqual(1, channel2.PublishedMessages.Count);
            Assert.AreSame(msg2, channel2.PublishedMessages[0]);
        }
        public async Task Given_multiple_are_configured_when_receiving_invalid_message_it_should_select_appropriate_strategy()
        {
            string       messageId1       = null;
            string       messageId2       = null;
            TestMessage1 receivedMessage1 = null;
            TestMessage2 receivedMessage2 = null;
            var          sync             = new AutoResetEvent(false);

            var activator = new BuiltinHandlerActivator();

            activator
            .Handle <TestMessage1>((bus, context, message) =>
            {
                // This should happen.
                receivedMessage1 = message;
                messageId1       = context.Message.GetMessageId();
                sync.Set();
                return(Task.CompletedTask);
            })
            .Handle <TestMessage2>((bus, context, message) =>
            {
                // This should not happen.
                receivedMessage2 = message;
                messageId2       = context.Message.GetMessageId();
                sync.Set();
                return(Task.CompletedTask);
            });

            using (IBus bus = CreateBus(activator,
                                        o =>
            {
                o.ValidateIncomingMessages(_validatorFactoryMock.Object,
                                           v => v
                                           .PassThrough <TestMessage1>()
                                           .Drop <TestMessage2>()
                                           );

                o.OnPipelineCompletion <TestMessage2>(failed =>
                {
                    receivedMessage2 = failed;
                    messageId2 = MessageContext.Current.Message.GetMessageId();
                    sync.Set();
                });
            }))
            {
                // Act
                await bus.Send(new TestMessage1
                {
                    ShouldPassValidation = false
                });

                await bus.Send(new TestMessage2
                {
                    ShouldPassValidation = false
                });

                // Assert
                sync.WaitOne(Debugger.IsAttached ? -1 : 5000);
                sync.WaitOne(Debugger.IsAttached ? -1 : 5000);

                receivedMessage1.Should().NotBeNull();
                receivedMessage2.Should().NotBeNull();
            }

            var logMessages = _loggerFactory.LogEvents
                              .Select(le => le.ToString())
                              .ToList();

            logMessages
            .Should()
            .ContainMatch($"*\"{messageId1}\" is configured to pass through.*");
            logMessages
            .Should()
            .ContainMatch($"*\"{messageId2}\" is configured to be dropped.*");
        }
Beispiel #8
0
 public SagaFinderTestSaga FindBy(TestMessage2 message)
 {
     return(SagaFinderTestSaga.Instance);
 }
Beispiel #9
0
 public void Consume(TestMessage2 message)
 {
     wait.Set();
 }
Beispiel #10
0
 public void Handle(TestMessage2 message)
 {
     Interlocked.Increment(ref TimesTestMessage2Handled);
 }
Beispiel #11
0
 public void OnPublished(TestMessage2 message)
 {
     Debug.Log($"TestSub message 2 received: {message.IntMessage}");
 }
Beispiel #12
0
 public virtual void Consume(TestMessage2 message)
 {
 }
Beispiel #13
0
        public void Test_AddOrUpdateMessage_Merge_FlushChannelMessages()
        {
            TestMessage1 msg11 = new TestMessage1();

            msg11.Data.Add(100);
            msg11.Data.Add(200);
            TestMessage1 msg12 = new TestMessage1();

            msg12.Data.Add(300);
            msg12.Data.Add(400);
            string channel1Name = "TestChannel1";

            TestMessage2 msg21 = new TestMessage2();

            msg21.Data.Add(1000);
            msg21.Data.Add(2000);
            TestMessage2 msg22 = new TestMessage2();

            msg22.Data.Add(3000);
            msg22.Data.Add(4000);
            msg22.Data.Add(5000);
            string channel2Name = "TestChannel2";

            // Setup the mocks
            MockDistributedMemoryManager distributedMemoryManagerMock = new MockDistributedMemoryManager();

            DeferredChannelMessageContextEntry entry = new DeferredChannelMessageContextEntry(distributedMemoryManagerMock);

            entry.AddOrUpdateMessage(channel1Name, msg11, (e, n) => e.Data.UnionWith(n.Data));
            entry.AddOrUpdateMessage(channel1Name, msg12, (e, n) => e.Data.UnionWith(n.Data)); // This will merge
            entry.AddOrUpdateMessage(channel2Name, msg21, (e, n) => e.Data.UnionWith(n.Data));
            entry.AddOrUpdateMessage(channel2Name, msg22, (e, n) => e.Data.UnionWith(n.Data)); // This will merge
            entry.FlushMessages();

            TestMessage1 msg1Out;
            TestMessage2 msg2Out;

            Assert.IsFalse(entry.TryGetMessage(channel1Name, out msg1Out), "Channel1 should have no messages");
            Assert.IsFalse(entry.TryGetMessage(channel2Name, out msg2Out), "Channel2 should have no messages");

            // Validation
            MockChannel <TestMessage1> channel1 = distributedMemoryManagerMock.CreatedChannels[channel1Name] as MockChannel <TestMessage1>;

            Assert.IsNotNull(channel1, "{0} was not found", channel1Name);
            Assert.AreEqual(1, channel1.PublishedMessages.Count); // Should still have only one message as it has been merged

            SortedSet <int> msg1Data = new SortedSet <int>();

            msg1Data.UnionWith(msg11.Data);
            msg1Data.UnionWith(msg12.Data);

            Assert.AreEqual(msg1Data.Count, channel1.PublishedMessages[0].Data.Count);
            Assert.IsTrue(channel1.PublishedMessages[0].Data.SetEquals(msg1Data));

            MockChannel <TestMessage2> channel2 = distributedMemoryManagerMock.CreatedChannels[channel2Name] as MockChannel <TestMessage2>;

            Assert.IsNotNull(channel2, "{0} was not found", channel2Name);
            Assert.AreEqual(1, channel2.PublishedMessages.Count); // Should still have only one message as it has been merged

            SortedSet <int> msg2Data = new SortedSet <int>();

            msg2Data.UnionWith(msg21.Data);
            msg2Data.UnionWith(msg22.Data);

            Assert.AreEqual(msg2Data.Count, channel2.PublishedMessages[0].Data.Count);
            Assert.IsTrue(channel2.PublishedMessages[0].Data.SetEquals(msg2Data));
        }