public async Task Commit_ShouldCommitAndDisposeAllPendingTransactions()
        {
            var wrappedTransactionMocks = new List <Mock <IWrappedTransaction> >();

            var createTransactionMock = new Mock <Func <IEventStoreConnection, string, long, Task <IWrappedTransaction> > >();

            createTransactionMock
            .Setup(x => x(_eventStoreConnectionMock.Object, "some_id", 0))
            .ReturnsAsync(() =>
            {
                var wrappedTransactionMock = new Mock <IWrappedTransaction>();
                wrappedTransactionMocks.Add(wrappedTransactionMock);
                return(wrappedTransactionMock.Object);
            });

            var transaction = new EventStoreTransaction <string, object>(
                connection: _eventStoreConnectionMock.Object,
                jsonSerializerSettings: _jsonSerializerSettings,
                createTransaction: createTransactionMock.Object);

            await transaction.StoreEvents("some_id", 1, new object[] { new EventA(), new EventB() });

            await transaction.StoreEvents("some_id", 1, new object[] { new EventA(), new EventB() });

            await transaction.StoreEvents("some_id", 1, new object[] { new EventA(), new EventB() });

            wrappedTransactionMocks.Should().HaveCount(3);
            wrappedTransactionMocks[0].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[1].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[2].Verify(x => x.Rollback(), Times.Never);

            await transaction.Commit();

            wrappedTransactionMocks[0].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.CommitAsync(), Times.Once);

            wrappedTransactionMocks[0].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[1].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[2].Verify(x => x.Rollback(), Times.Never);

            wrappedTransactionMocks[0].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.Dispose(), Times.Once);

            // Dispose shouldn't have any side effect
            transaction.Dispose();

            wrappedTransactionMocks[0].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.CommitAsync(), Times.Once);

            wrappedTransactionMocks[0].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[1].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[2].Verify(x => x.Rollback(), Times.Never);

            wrappedTransactionMocks[0].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.Dispose(), Times.Once);
        }
Esempio n. 2
0
 private void ValidateAllEvent(IEnumerator <long> idEnumerator, IEnumerator <IDomainEvent <int> > evtEnumerator)
 {
     idEnumerator.MoveNext();
     evtEnumerator.MoveNext();
     try
     {
         do
         {
             var evt = evtEnumerator.Current;
             _tran.ValidateEvent(idEnumerator.Current, evt);
         } while (idEnumerator.MoveNext() && evtEnumerator.MoveNext());
         _tran.Commit();
     }
     catch
     {
         _tran.Rollback();
     }
 }
Esempio n. 3
0
        void SaveEventsInBatch(string stream_name, int expected_version,
                               IEnumerable <Event> events, IEventSerializer serializer)
        {
            EventStoreTransaction trasaction = connection_
                                               .StartTransaction(stream_name, expected_version);

            EventData[] serialized_events = Serialize(events, serializer).ToArray();

            int position = 0;

            while (position < serialized_events.Length)
            {
                var page_events = serialized_events
                                  .Skip(position)
                                  .Take(kWritePageSize);
                trasaction.Write(page_events);
                position += kWritePageSize;
            }
            trasaction.Commit();
        }
Esempio n. 4
0
 public WriteResult Commit()
 {
     return(_transaction.Commit());
 }
Esempio n. 5
0
 public void Commit()
 {
     _transaction.Commit();
 }