Exemplo n.º 1
0
        private static void TestMessageHandler(MessageHandlingMode handlingMode)
        {
            Console.WriteLine($"===================================================");
            Console.WriteLine($"Testing handling mode '{handlingMode}'");
            Console.WriteLine($"===================================================");
            Console.WriteLine();

            try
            {
                var messageHandler = new MessageHandler(handlingMode, MyHandlerMethod);
                var tasks          = new List <Task>();
                for (int i = 0; i < 10; i++)
                {
                    tasks.Add(
                        messageHandler.HandleAsync(new Message()
                    {
                        MessageText = $"Message {i}"
                    })
                        );
                }

                Task.WaitAll(tasks.ToArray());
            }
            catch (Exception exception)
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine($"Uncaught exception: {exception.Message}");
                Console.WriteLine();
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine();
        }
Exemplo n.º 2
0
        private static List <Task> CreateAndWaitMessages(MessageHandlingMode mode, out List <TestMessage> completionOrder)
        {
            completionOrder = new List <TestMessage>();
            var completionOrder2 = completionOrder;
            var messageHandler   = new MessageHandler(
                mode,
                async(message) =>
            {
                if (message is BadMessage)
                {
                    throw new BadMessageException();
                }
                if (message is TestMessage testMessage)
                {
                    await Task.Delay(testMessage.TestDelay);
                    completionOrder2.Add(testMessage);
                }
            });
            var tasks = new List <Task>()
            {
                messageHandler.HandleAsync(new TestMessage(1, 3000)),
                messageHandler.HandleAsync(new TestMessage(2, 0)),
                messageHandler.HandleAsync(new TestMessage(3, 2000)),
                messageHandler.HandleAsync(new BadMessage(4, 0)),
                messageHandler.HandleAsync(new TestMessage(5, 1000)),
            };

            //just wait for the tasks but don't throw exceptions
            foreach (var task in tasks)
            {
                try
                {
                    Task.WaitAll(task);
                }
                catch { }
            }

            return(tasks);
        }
Exemplo n.º 3
0
 public MessageHandler(MessageHandlingMode handlingMode, Func <Message, Task> handlerMethod)
 {
     HandlingMode  = handlingMode;
     HandlerMethod = handlerMethod;
 }