Exemplo n.º 1
0
        public async Task PlaceOrder_WithMessageQueuing_EventsArePublished()
        {
            IUnityContainer container = new UnityContainer();

            Mock<ISpy> spy = new Mock<ISpy>();
            container.RegisterInstance(typeof(ISpy), spy.Object);

            using (ProcessorConfiguration config = new ProcessorConfiguration())
            {
                config.EnableInMemoryMessageQueuing();
                config.RegisterContainer(container);
                using (MessageProcessor processor = new MessageProcessor(config))
                {
                    PlaceOrder command = new PlaceOrder(1);
                    await processor.ProcessAsync(command);

                    CancellationTokenSource cts = new CancellationTokenSource(5);
                    await config.CommandBroker.StartAsync(cts.Token);

                    // commands assertions
                    spy.Verify(s => s.Spy("PlaceOrder"), Times.Once());
                    spy.Verify(s => s.Spy("MakePayment"), Times.Once());
                    spy.Verify(s => s.Spy("MakeReservation"), Times.Once());
                    spy.Verify(s => s.Spy("AddSeatsToWaitList"), Times.Never());

                    // events assertions
                    spy.Verify(s => s.Spy("OrderConfirmed"), Times.Exactly(2));
                    spy.Verify(s => s.Spy("OrderCreated"), Times.Once());
                    spy.Verify(s => s.Spy("PaymentAccepted"), Times.Once());
                    spy.Verify(s => s.Spy("SeatsReserved"), Times.Once());
                    spy.Verify(s => s.Spy("SeatsNotReserved"), Times.Never());

                    // ctor assertions
                    spy.Verify(s => s.Spy("OrderProcessManager ctor"), Times.Exactly(3));
                    spy.Verify(s => s.Spy("Order ctor"), Times.Exactly(2));
                    spy.Verify(s => s.Spy("Payment ctor"), Times.Once());
                    spy.Verify(s => s.Spy("Payment ctor"), Times.Once());
                    spy.Verify(s => s.Spy("Payment ctor"), Times.Once());
                    spy.Verify(s => s.Spy("Reservation ctor"), Times.Exactly(2));
                }
            }
        }
Exemplo n.º 2
0
        private static void SequentialTaskProcessing(int maxIterations, MessageProcessor processor)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            Task[] tasks = new Task[maxIterations];
            for (int i = 0; i < maxIterations; i++)
            {
                PlaceOrder command = new PlaceOrder(1);
                tasks[i] = processor.ProcessAsync(command);
            }

            Task.WaitAll(tasks);
            stopwatch.Stop();
            Console.WriteLine("Sequential Tasks, " + maxIterations + " iterations : " + stopwatch.ElapsedMilliseconds + " ms");
        }
Exemplo n.º 3
0
 private static void ParallelProcessing(int maxIterations, MessageProcessor processor)
 {
     Stopwatch stopwatch = Stopwatch.StartNew();
     Parallel.For(
         0, 
         maxIterations, 
         async i =>
     {
         PlaceOrder command = new PlaceOrder(1);
         await processor.ProcessAsync(command);
     });
     stopwatch.Stop();
     System.Console.WriteLine("Parallel for, " + maxIterations + " iterations : " + stopwatch.ElapsedMilliseconds + " ms");
 }
Exemplo n.º 4
0
 private static async void SingleProcessing(MessageProcessor processor)
 {
     PlaceOrder command = new PlaceOrder(10);
     await processor.ProcessAsync(command);
 }
Exemplo n.º 5
0
        private static async void SequentialTaskProcessingV3(int maxIterations, MessageProcessor processor)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            TaskCompletionSource<HandlerResponse> tcs = new TaskCompletionSource<HandlerResponse>();
            tcs.SetResult(null);
            Task task = tcs.Task;
            for (int i = 0; i < maxIterations; i++)
            {
                PlaceOrder command = new PlaceOrder(1);
                task = task.ContinueWith(t => processor.ProcessAsync(command));
            }

            await task;
            stopwatch.Stop();
            Console.WriteLine("Sequential Tasks v3, " + maxIterations + " iterations : " + stopwatch.ElapsedMilliseconds + " ms");
        }
Exemplo n.º 6
0
        private static async void SequentialTaskProcessingV2(int maxIterations, MessageProcessor processor)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < maxIterations; i++)
            {
                PlaceOrder command = new PlaceOrder(1);
                await processor.ProcessAsync(command);
            }

            stopwatch.Stop();
            Console.WriteLine("Sequential Tasks v2, " + maxIterations + " iterations : " + stopwatch.ElapsedMilliseconds + " ms");
        }