public void Handle(OrderPlaced orderPlaced, IDocumentSession session, IServiceBus bus)
        {
            session.Store(new PickList {
                Id = orderPlaced.Id
            });
            bus.Send(new ItemOutOfStock {
                OrderId = orderPlaced.Id, ItemName = orderPlaced.ItemName
            });

            // wait here for the test to inspect state.
            TestSynch.ProcessedOrderPlacedEvent.Set();
            TestSynch.WarehouseHandlerShouldContinueEvent.WaitOne(5.Seconds());
        }
        public void Handle(OrderPlaced orderPlaced, IMartenOutbox outbox)
        {
            outbox.DocumentSession.Store(new PickList {
                Id = orderPlaced.Id
            });
            outbox.Send(new ItemOutOfStock {
                OrderId = orderPlaced.Id, ItemName = orderPlaced.ItemName
            });

            // wait here for the test to inspect state.
            TestSynch.ProcessedOrderPlacedEvent.Set();
            TestSynch.WarehouseHandlerShouldContinueEvent.WaitOne(5.Seconds());

            outbox.Complete();
        }
        public object Handle(OrderPlaced orderPlaced, IDocumentSession session)
        {
            session.Store(new PickList {
                Id = orderPlaced.Id
            });
            var outOfStockEvent = new ItemOutOfStock {
                OrderId = orderPlaced.Id, ItemName = orderPlaced.ItemName
            };

            // wait here for the test to inspect state.
            TestSynch.ProcessedOrderPlacedEvent.Set();
            TestSynch.WarehouseHandlerShouldContinueEvent.WaitOne(5.Seconds());

            return(outOfStockEvent);
        }
        public async Task send_and_handle_end_to_end()
        {
            /* Alternative 1 (Preferred) */
            // when this code appears in an MVC controller, the outbox could be injected as a scoped dependency.
            using (var outbox = theSender.Get <MartenOutbox>())
            {
                var order = new Order {
                    Id = Guid.NewGuid(), ItemName = "Hat"
                };
                outbox.DocumentSession.Store(order);

                var eventMessage = new OrderPlaced {
                    Id = order.Id, ItemName = order.ItemName
                };
                outbox.Send(eventMessage);

                //TODO: assert that at this point, the sending agent should _not_ have been told to deliver the message

                // Complete will: save the document session including the envelopes in the outbox,
                // then trigger the sending agent to start delivering those envelopes.
                await outbox.Complete();

                //TODO: assert that at this point, the sending agent should have been told to deliver the message
            }

            TestSynch.ProcessedOrderPlacedEvent.WaitOne(5.Seconds()).ShouldBe(true, "Waited too long for OrderPlaced event to be handled");
            //TODO: at this point, the warehouse app's sending agent should _not_ have been told to deliver the message
            TestSynch.WarehouseHandlerShouldContinueEvent.Set();

            TestSynch.ProcessedItemOutOfStockEvent.WaitOne(5.Seconds()).ShouldBe(true, "Waited too long for ItemOutOfStock event to be handled");



            /* Alternative 2 */
            // when this code appears in an MVC controller, both the session and the outbox could be injected as scoped dependencies.
            using (var session = theSender.Get <IDocumentStore>().OpenSession())
            {
                using (var outboxBus = new MartenOutboxBus(session, theSender.Get <SessionCommitListener>()))
                {
                    var order = new Order {
                        Id = Guid.NewGuid(), ItemName = "Hat"
                    };
                    session.Store(order);

                    var commandMessage = new OrderPlaced {
                        Id = order.Id, ItemName = order.ItemName
                    };
                    outboxBus.Send(commandMessage);

                    //TODO: assert that at this point, there should not have been anything handed to the sending agent.

                    // Complete will: save the document session including the envelopes in the outbox,
                    // then trigger the sending agent to start delivering those envelopes.
                    await session.SaveChangesAsync();

                    //TODO: assert that at this point, the sending agent should have been told to deliver the message
                }
            }

            TestSynch.ProcessedOrderPlacedEvent.WaitOne(5.Seconds()).ShouldBe(true, "Waited too long for OrderPlaced event to be handled");
            //TODO: at this point, the warehouse app's sending agent should _not_ have been told to deliver the message
            TestSynch.WarehouseHandlerShouldContinueEvent.Set();

            TestSynch.ProcessedItemOutOfStockEvent.WaitOne(5.Seconds()).ShouldBe(true, "Waited too long for ItemOutOfStock event to be handled");
        }