Пример #1
0
        public async Task Should_have_the_state_machine()
        {
            Guid correlationId = Guid.NewGuid();

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

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

            Assert.IsTrue(sagaId.HasValue);

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

            sagaId = await _repository.Value.ShouldContainSaga(x => x.CorrelationId == correlationId &&
                                                               x.CurrentState == _machine.Dead.Name, TestTimeout);

            Assert.IsTrue(sagaId.HasValue);

            ShoppingChore instance = await GetSaga(correlationId);

            Assert.IsTrue(instance.Screwed);
        }
Пример #2
0
        public async Task Should_have_the_state_machine()
        {
            Guid correlationId = Guid.NewGuid();

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

            var saga = await GetSagaRetry(correlationId, TestTimeout);

            Assert.IsNotNull(saga);

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

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

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

            ShoppingChore instance = await GetSaga(correlationId);

            Assert.IsTrue(instance.Screwed);
        }
Пример #3
0
        async Task RaiseEvent <T>(Guid id, Event <T> @event, T data)
        {
            using (ISession session = _sessionFactory.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    var instance = session.Get <ShoppingChore>(id, LockMode.Upgrade);
                    if (instance == null)
                    {
                        instance = new ShoppingChore(id);
                    }

                    await _machine.RaiseEvent(instance, @event, data);

                    session.SaveOrUpdate(instance);

                    transaction.Commit();
                }
        }
Пример #4
0
        async Task <ShoppingChore> GetNoSagaRetry(Guid id, TimeSpan timeout)
        {
            DateTime      giveUpAt = DateTime.Now + timeout;
            ShoppingChore saga     = null;

            while (DateTime.Now < giveUpAt)
            {
                try
                {
                    var document = await _documentClient.ReadDocumentAsync(UriFactory.CreateDocumentUri(_databaseName, _collectionName, id.ToString()));

                    saga = JsonConvert.DeserializeObject <ShoppingChore>(document.Resource.ToString());

                    await Task.Delay(10).ConfigureAwait(false);
                }
                catch (DocumentClientException e) when(e.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    saga = null;
                    break;
                }
            }

            return(saga);
        }