public void when_executed_with_few_handlers_it_should_execute_them_in_parallel()
        {
            // TODO: On AppVeyor this test is blinking. I thing because of shared resources. This test shouldn't use time.
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
            container.AddLogging(_testOutput);
            var strategy = new ParallelHandlersExecutionStrategy(container.GetInstance <ILogger <ParallelHandlersExecutionStrategy> >());
            var handlers = VentureContextTools.CreateHandlerDescriptors(
                new DelayedMessageHandler(TimeSpan.FromMilliseconds(value: 100)),
                new DelayedMessageHandler(TimeSpan.FromMilliseconds(value: 100)),
                new DelayedMessageHandler(TimeSpan.FromMilliseconds(value: 100)),
                new DelayedMessageHandler(TimeSpan.FromMilliseconds(value: 100)),
                new DelayedMessageHandler(TimeSpan.FromMilliseconds(value: 100)),
                new DelayedMessageHandler(TimeSpan.FromMilliseconds(value: 100))).ToArray();
            var message = new Message02();
            var context = VentureContextTools.CreateFilledVentureContext(message);

            Func <Task> sut = () => strategy.ExecuteHandlers(
                handlers,
                message,
                context);
            var singleHandlerExecutionTime = TimeSpan.FromMilliseconds(value: 100);

            sut.ExecutionTime().Should()
            .BeCloseTo(
                singleHandlerExecutionTime,
                TimeSpan.FromMilliseconds(value: 50),
                "should run simultaneously");
        }
        public void when_executed_with_failing_handler_it_should_throw()
        {
            var handlers = VentureContextTools.CreateHandlerDescriptors(
                new MessageHandler(),
                new ThrowingHandler(),
                new ThrowingHandler()).ToArray();
            var message  = new Message02();
            var context  = VentureContextTools.CreateFilledVentureContext(message);
            var strategy = new ParallelHandlersExecutionStrategy(NullLogger <ParallelHandlersExecutionStrategy> .Instance);

            Func <Task> sut = () => strategy.ExecuteHandlers(
                handlers,
                message,
                context);

            sut.Should().ThrowExactly <AggregateException>()
            .Where(exception => exception.InnerExceptions.Count == 2, "two handlers did throw an exception");
        }
        public void when_executed_without_context_it_should_throw()
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
            container.AddLogging(_testOutput);
            var strategy = new ParallelHandlersExecutionStrategy(container.GetInstance <ILogger <ParallelHandlersExecutionStrategy> >());
            var handlers = VentureContextTools.CreateHandlerDescriptors(new MessageHandler()).ToArray();
            var message  = new Message02();

            // ReSharper disable once AssignNullToNotNullAttribute - it's a test against null.
            Func <Task> sut = () => strategy.ExecuteHandlers(
                handlers,
                message,
                context: null);

            sut.Should().Throw <ArgumentNullException>().Where(exception => exception.ParamName.Equals("context"));
        }
        public async Task when_executed_with_few_handlers_it_should_execute_all_of_them()
        {
            var handler1 = new MessageHandler();
            var handler2 = new MessageHandler();
            var handler3 = new MessageHandler();
            var handlers = VentureContextTools.CreateHandlerDescriptors(
                handler1,
                handler2,
                handler3).ToArray();
            var message = new Message02();
            var context = VentureContextTools.CreateFilledVentureContext(message);
            var sut     = new ParallelHandlersExecutionStrategy(NullLogger <ParallelHandlersExecutionStrategy> .Instance);

            await sut.ExecuteHandlers(
                handlers,
                message,
                context);

            handler1.IsExecuted.Should().BeTrue("handler #1 should be called");
            handler2.IsExecuted.Should().BeTrue("handler #2 should be called");
            handler3.IsExecuted.Should().BeTrue("handler #3 should be called");
        }
        public void when_executed_with_handlers_it_should_log_execution_progress()
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
            container.AddLogging(_testOutput);
            var handlers = VentureContextTools.CreateHandlerDescriptors(
                new MessageHandler(),
                new ThrowingHandler(),
                new ThrowingHandler()).ToArray();
            var message  = new Message02();
            var context  = VentureContextTools.CreateFilledVentureContext(message);
            var strategy = new ParallelHandlersExecutionStrategy(container.GetInstance <ILogger <ParallelHandlersExecutionStrategy> >());

            Func <Task> sut = () => strategy.ExecuteHandlers(
                handlers,
                message,
                context);

            sut.Should().ThrowExactly <AggregateException>();
            InMemorySink.Instance.LogEvents
            .Where(log => log.Level == LogEventLevel.Debug)
            .Select(log => log.RenderMessage())
            .Count(log => log.StartsWith("Executing a message handler of type"))
            .Should().Be(handlers.Length, "for each handler should be logged its start");
            InMemorySink.Instance.LogEvents
            .Where(log => log.Level == LogEventLevel.Debug)
            .Select(log => log.RenderMessage())
            .Count(log => log.StartsWith("Executed a message handler of type"))
            .Should().Be(handlers.Length, "for each handler should be logged its finish");
            InMemorySink.Instance.LogEvents
            .Where(log => log.Level == LogEventLevel.Error)
            .Select(log => log.RenderMessage())
            .Count(log => log.StartsWith("An error occurred during a message handler execution of type"))
            .Should().Be(expected: 2, "for each failed handler should be logged an error");
        }
Ejemplo n.º 6
0
        public void TestPubsub()
        {
            var           exchangeName    = "test.pubsub";
            var           subscriberNames = new string[] { "wms", "erp" };
            IPubsubSender publisher       = new PubsubSender(_conn, exchangeName, subscriberNames, debugEnabled: true);
            var           sentMessageDict = new ConcurrentDictionary <string, Tuple <bool, IMessageTransportationContext> >();

            publisher.OnMessageSent += (sender, e) =>
            {
                sentMessageDict.TryAdd(e.Context.GetMessageId(), new Tuple <bool, IMessageTransportationContext>(false, e.Context));
            };
            publisher.OnMessageSendingSucceeded += (sender, e) =>
            {
                sentMessageDict.AddOrUpdate(e.Context.GetMessageId(), new Tuple <bool, IMessageTransportationContext>(true, e.Context), (key, originVal) => new Tuple <bool, IMessageTransportationContext>(true, originVal.Item2));
            };
            int sendSequence = 0;

            while (true)
            {
                try
                {
                    sendSequence++;
                    if (sendSequence % 4 != 0)
                    {
                        var message = new Message01(new Guid("58437EDC-87B7-4995-A5C0-BB5FD0FE49E0"))
                        {
                            Sequence = random.Next(1, 20)
                        };
                        Envelope envelopedMessage = Envelope.Create(message, $"{message.HostingFilialeId}_{Guid.NewGuid()}");
                        Task.Run(() => publisher.SendMessage(envelopedMessage));
                        Console.WriteLine($"{sendSequence}. send Message01 sequence={message.Sequence} ");
                    }
                    else
                    {
                        var message = new Message02(new Guid("7AE62AF0-EB1F-49C6-8FD1-128D77C84698"));
                        Task.Run(() => publisher.SendMessage(Envelope.Create(message, $"{message.SaleFilialeId}")));
                        Console.WriteLine($"{sendSequence}. send Message02 no sequence");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                }
                if (sendSequence == 1000000)
                {
                    Thread.Sleep(3000);
                    Task.Run(() =>
                    {
                        publisher.Dispose();
                    });
                    break;
                }
            }
            var timeout        = sendSequence / 10;
            var executeSeconds = 0;

            while (sentMessageDict.Count != sendSequence || sentMessageDict.Values.Any(w => !w.Item1 && w.Item2.LastException == null))
            {
                if (executeSeconds > timeout)
                {
                    break;
                }
                Thread.Sleep(1000);
                executeSeconds++;
            }
            var failList = sentMessageDict.Values.Where(w => !w.Item1).ToList();
        }
Ejemplo n.º 7
0
        public void TestRpcClient()
        {
            IRpcClient publisher       = new RpcClient(_conn, "rpc-test", confirmEnabled: true, debugEnabled: true);
            var        sentMessageDict = new ConcurrentDictionary <string, Tuple <bool, IMessageTransportationContext> > ();

            publisher.OnMessageSent += (sender, e) =>
            {
                sentMessageDict.TryAdd(e.Context.GetMessageId(), new Tuple <bool, IMessageTransportationContext>(false, e.Context));
            };
            publisher.OnMessageSendingSucceeded += (sender, e) =>
            {
                sentMessageDict.AddOrUpdate(e.Context.GetMessageId(), new Tuple <bool, IMessageTransportationContext>(true, e.Context), (key, originVal) => new Tuple <bool, IMessageTransportationContext>(true, originVal.Item2));
            };
            int sendSequence = 0;

            while (true)
            {
                try
                {
                    sendSequence++;
                    if (sendSequence % 4 != 0)
                    {
                        var message = new Message01(new Guid("58437EDC-87B7-4995-A5C0-BB5FD0FE49E0"))
                        {
                            Sequence = random.Next(1, 20)
                        };
                        var envelopedMessage = Envelope.Create(message);
                        publisher.SendRequest(envelopedMessage, "method1", envelopedMessage.MessageId);
                        Console.WriteLine($"{sendSequence}. send Message01 sequence={message.Sequence} ");
                    }
                    else
                    {
                        var message = new Message02(new Guid("7AE62AF0-EB1F-49C6-8FD1-128D77C84698"));
                        publisher.SendRequest(Envelope.Create(message), "method2", Guid.NewGuid().ToString());
                        Console.WriteLine($"{sendSequence}. send Message02 no sequence");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{ex.Message}");
                }
                if (sendSequence == 1000000)
                {
                    Thread.Sleep(3000);
                    Task.Run(() =>
                    {
                        publisher.Dispose();
                    });
                    break;
                }
            }
            var timeout        = sendSequence / 10;
            var executeSeconds = 0;

            while (sentMessageDict.Count != sendSequence || sentMessageDict.Values.Any(w => !w.Item1 && w.Item2.LastException == null))
            {
                if (executeSeconds > timeout)
                {
                    break;
                }
                Thread.Sleep(1000);
                executeSeconds++;
            }
            var failList = sentMessageDict.Values.Where(w => !w.Item1).ToList();
        }
Ejemplo n.º 8
0
 public void Handle(Message02 message, IMessageTransportationContext context)
 {
     Thread.Sleep(500);
     Console.WriteLine($"Message02 from queue {context.QueueName}");
 }