public async Task RequestSucceeds()
        {
            IBus bus = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m => bus.Publish(new TheEvent {
                CorrelationId = m.CorrelationId
            })));
            var response = await bus.RequestAsync <Message1, TheEvent>(new Message1 { CorrelationId = "12344321" });

            Assert.AreEqual("12344321", response.CorrelationId);
        }
        public void RequestWithNullMessageThrows()
        {
            IBus     bus       = new Bus();
            Message1 message1  = null;
            var      exception = Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await bus.RequestAsync <Message1, TheEvent>(message1);
            }
                                                                            );

            Assert.That(exception.ParamName, Is.EqualTo("message"));
        }
        public async Task RequestWithErrorWithExceptionDuringErrorEventThrows()
        {
            IBus bus       = new Bus();
            var  mockEvent = new Mock <IEvent>();

            mockEvent.SetupGet(m => m.CorrelationId).Throws <InvalidOperationException>();
            bus.AddHandler(new ActionConsumer <Message1>(m => bus.Publish(mockEvent.Object)));
            Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await bus.RequestAsync <Message1, TheEvent, IEvent>(new Message1 {
                    CorrelationId = "12344321"
                });
            });
        }
        public async Task RequestWithErrorEventWithSuccessEventSucceedsWithCorrectEvent()
        {
            IBus bus = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m =>
            {
                bus.Publish(new TheEvent {
                    CorrelationId = m.CorrelationId
                });
            }));

            var @event = await bus.RequestAsync <Message1, TheEvent, TheErrorEvent>(
                new Message1 { CorrelationId = "12344321" });

            Assert.IsInstanceOf <TheEvent>(@event);
        }
        public async Task RequestWithDifferentCorrelationIdDoesNotSucceed()
        {
            IBus bus = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m => bus.Publish(new TheEvent {
                CorrelationId = "0"
            })));
            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));

            Assert.ThrowsAsync <TaskCanceledException>(async() =>
            {
                var response = await bus.RequestAsync <Message1, TheEvent>(
                    new Message1 {
                    CorrelationId = "12344321"
                },
                    cancellationTokenSource.Token);
            });
        }
        public void RequestWithErrorEventThrows()
        {
            IBus bus = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m =>
            {
                bus.Publish(new TheErrorEvent {
                    CorrelationId = m.CorrelationId
                });
            }));

            Assert.ThrowsAsync <ReceivedErrorEventException <TheErrorEvent> >(async() =>
            {
                await bus.RequestAsync <Message1, TheEvent, TheErrorEvent>(
                    new Message1 {
                    CorrelationId = "12344321"
                });
            }
                                                                              );
        }
        public void RequestTimesOut()
        {
            IBus bus = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m =>
            {
                /* do nothing */
            }));

            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(10));

            Assert.ThrowsAsync <TaskCanceledException>(async() =>
            {
                await bus.RequestAsync <Message1, TheEvent>(
                    new Message1 {
                    CorrelationId = "12344321"
                },
                    cancellationTokenSource.Token);
            }
                                                       );
        }
        public void MessageAfterRequestWithErrorEventCancellationDoesNotThrow()
        {
            IBus bus = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m =>
            {
                /* do nothing */
            }));

            var cancellationTokenSource = new CancellationTokenSource();
            var requestAsync            = bus.RequestAsync <Message1, TheEvent, TheErrorEvent>(
                new Message1 {
                CorrelationId = "12344321"
            },
                cancellationTokenSource.Token);

            cancellationTokenSource.Cancel();
            //await requestAsync;
            bus.Handle(new TheEvent {
                CorrelationId = "12344321"
            });
        }
        public void RequestWithErrorEventThrowsWithExceptionWithCorrectEvent()
        {
            var  correlationId = "12344321";
            IBus bus           = new Bus();

            bus.AddHandler(new ActionConsumer <Message1>(m =>
            {
                bus.Publish(new TheErrorEvent {
                    CorrelationId = m.CorrelationId
                });
            }));

            var exception = Assert.ThrowsAsync <ReceivedErrorEventException <TheErrorEvent> >(async() =>
            {
                await bus.RequestAsync <Message1, TheEvent, TheErrorEvent>(
                    new Message1 {
                    CorrelationId = correlationId
                });
            }
                                                                                              );

            Assert.AreEqual(correlationId, exception.ErrorEvent.CorrelationId);
        }