public async Task Should_capture_all_events_single_saga()
        {
            Guid correlationId = Guid.NewGuid();

            await InputQueueSendEndpoint.Send(new RehersalBegins { CorrelationId = correlationId });

            var saga = await GetSagaRetry(correlationId, TestTimeout);

            Assert.IsNotNull(saga);

            await Task.WhenAll(
                InputQueueSendEndpoint.Send(new Bass {
                CorrelationId = correlationId, Name = "John"
            }),
                InputQueueSendEndpoint.Send(new Baritone {
                CorrelationId = correlationId, Name = "Mark"
            }),
                InputQueueSendEndpoint.Send(new Tenor {
                CorrelationId = correlationId, Name = "Anthony"
            }),
                InputQueueSendEndpoint.Send(new Countertenor {
                CorrelationId = correlationId, Name = "Tom"
            })
                );

            saga = await GetSagaRetry(correlationId, TestTimeout, x => x.CurrentState == _machine.Harmony.Name);

            Assert.IsNotNull(saga);
            Assert.IsTrue(saga.CurrentState == _machine.Harmony.Name);

            ChoirState instance = await GetSaga(correlationId);

            Assert.IsTrue(instance.CurrentState.Equals("Harmony"));
        }
        public async Task Should_not_be_in_final_state()
        {
            Guid correlationId = Guid.NewGuid();

            await InputQueueSendEndpoint.Send(new RehersalBegins { CorrelationId = correlationId });

            Guid?sagaId = await _repository.Value.ShouldContainSaga(correlationId, TestTimeout);

            Assert.IsTrue(sagaId.HasValue);

            await Task.WhenAll(
                InputQueueSendEndpoint.Send(new Bass {
                CorrelationId = correlationId, Name = "John"
            }),
                InputQueueSendEndpoint.Send(new Baritone {
                CorrelationId = correlationId, Name = "Mark"
            }),
                InputQueueSendEndpoint.Send(new Tenor {
                CorrelationId = correlationId, Name = "Anthony"
            }),
                InputQueueSendEndpoint.Send(new Countertenor {
                CorrelationId = correlationId, Name = "Tom"
            })
                );

            // Because concurrency exception's happened without retry middleware configured, we aren't in our final state/
            ChoirState instance = await GetSaga(correlationId);

            Assert.IsTrue(!instance.CurrentState.Equals("Harmony"));
        }
        public async Task Some_should_not_be_in_final_state_all()
        {
            var tasks = new List <Task>();

            Guid[] sagaIds = new Guid[20];
            for (int i = 0; i < 20; i++)
            {
                Guid correlationId = NewId.NextGuid();

                await InputQueueSendEndpoint.Send(new RehersalBegins { CorrelationId = correlationId });

                sagaIds[i] = correlationId;
            }

            for (int i = 0; i < 20; i++)
            {
                Guid?sagaId = await _repository.Value.ShouldContainSaga(sagaIds[i], TestTimeout);

                Assert.IsTrue(sagaId.HasValue);
            }

            for (int i = 0; i < 20; i++)
            {
                tasks.Add(InputQueueSendEndpoint.Send(new Bass {
                    CorrelationId = sagaIds[i], Name = "John"
                }));
                tasks.Add(InputQueueSendEndpoint.Send(new Baritone {
                    CorrelationId = sagaIds[i], Name = "Mark"
                }));
                tasks.Add(InputQueueSendEndpoint.Send(new Tenor {
                    CorrelationId = sagaIds[i], Name = "Anthony"
                }));
                tasks.Add(InputQueueSendEndpoint.Send(new Countertenor {
                    CorrelationId = sagaIds[i], Name = "Tom"
                }));
            }

            await Task.WhenAll(tasks);

            await Task.Delay(2000);

            tasks.Clear();

            var someNotInFinalState = false;

            foreach (var sid in sagaIds)
            {
                ChoirState instance = await GetSaga(sid);

                someNotInFinalState = !instance.CurrentState.Equals("Harmony");
            }

            Assert.IsTrue(someNotInFinalState);
        }
        public async Task Should_capture_all_events_many_sagas()
        {
            var tasks = new List <Task>();

            Guid[] sagaIds = new Guid[20];
            for (int i = 0; i < 20; i++)
            {
                Guid correlationId = NewId.NextGuid();

                await InputQueueSendEndpoint.Send(new RehersalBegins { CorrelationId = correlationId });

                sagaIds[i] = correlationId;
            }

            for (int i = 0; i < 20; i++)
            {
                var saga = await GetSagaRetry(sagaIds[i], TestTimeout);

                Assert.IsNotNull(saga);
            }

            for (int i = 0; i < 20; i++)
            {
                tasks.Add(InputQueueSendEndpoint.Send(new Bass {
                    CorrelationId = sagaIds[i], Name = "John"
                }));
                tasks.Add(InputQueueSendEndpoint.Send(new Baritone {
                    CorrelationId = sagaIds[i], Name = "Mark"
                }));
                tasks.Add(InputQueueSendEndpoint.Send(new Tenor {
                    CorrelationId = sagaIds[i], Name = "Anthony"
                }));
                tasks.Add(InputQueueSendEndpoint.Send(new Countertenor {
                    CorrelationId = sagaIds[i], Name = "Tom"
                }));
            }

            await Task.WhenAll(tasks);

            await Task.Delay(100000);

            tasks.Clear();


            foreach (var sid in sagaIds)
            {
                ChoirState instance = await GetSagaRetry(sid, TestTimeout, x => x.CurrentState == _machine.Harmony.Name);

                Assert.IsNotNull(instance);
                Assert.IsTrue(instance.CurrentState.Equals("Harmony"));
            }
        }