public void ScopeCompleteAndSerializable() { Reinitialize(); const int loop = 10; using (var scope = new TransactionScope( TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { Parallel.For(0, loop, i => { Console.WriteLine("Creating stream {0} on thread {1}", i, Thread.CurrentThread.ManagedThreadId); var eventStore = new OptimisticEventStore(Persistence, null); string streamId = i.ToString(CultureInfo.InvariantCulture); using (var stream = eventStore.OpenStream(streamId)) { stream.Add(new EventMessage { Body = "body1" }); stream.Add(new EventMessage { Body = "body2" }); stream.CommitChanges(Guid.NewGuid()); } }); scope.Complete(); } ICommit[] commits = Persistence.GetFrom(0).ToArray(); commits.Length.Should().Be(loop); }
protected override void Because() { Parallel.For(0, Loop, i => { var eventStore = new OptimisticEventStore(Persistence, null); using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable })) { int j; for (j = 0; j < StreamsPerTransaction; j++) { using (var stream = eventStore.OpenStream(i.ToString() + "-" + j.ToString())) { for (int k = 0; k < 10; k++) { stream.Add(new EventMessage { Body = "body" + k }); } stream.CommitChanges(Guid.NewGuid()); } } scope.Complete(); } }); _commits = Persistence.GetFrom().ToArray(); }
protected override void InitializeEnvironment() { var factory = new AbsoluteOrderingSqlPersistenceFactory("SqlCeEventStore", new BinarySerializer(), false); var streamPersister = factory.Build(); streamPersister.Initialize(); var store = new OptimisticEventStore(streamPersister, new NullDispatcher()); var snapshotStore = new JoesSnapshotStoreAdapter(streamPersister); NcqrsEnvironment.SetDefault <ISnapshotStore>(snapshotStore); var uowFactory = new JoesUnitOfWorkFactory(store); NcqrsEnvironment.SetDefault <IUnitOfWorkFactory>(uowFactory); }
protected override void Because() { _thrown = Catch.Exception(() => { // multiple connections on a single thread var eventStore = new OptimisticEventStore(Persistence, null); // Single transaction scope using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = _transationIsolationLevel } #if NET451 || NETSTANDARD2_0 , TransactionScopeAsyncFlowOption.Enabled #endif )) { for (int i = 0; i < Loop; i++) { int j; for (j = 0; j < StreamsPerTransaction; j++) { var streamId = i.ToString() + "-" + j.ToString(); using (var stream = eventStore.OpenStream(streamId)) { for (int k = 0; k < 10; k++) { stream.Add(new EventMessage { Body = "body" + k }); } Debug.WriteLine("Committing Stream: " + streamId); stream.CommitChanges(Guid.NewGuid()); } } } ; Debug.WriteLine("Completing transaction"); if (_completeTransaction) { scope.Complete(); } } }); }
protected override void Because() { _moreThanPageSize = ConfiguredPageSizeForTesting + 1; var eventStore = new OptimisticEventStore(Persistence, null); // TODO: Not sure how to set the actual pagesize to the const defined above for (int i = 0; i < _moreThanPageSize; i++) { using (IEventStream stream = eventStore.OpenStream(Guid.NewGuid())) { stream.Add(new EventMessage { Body = i }); stream.CommitChanges(Guid.NewGuid()); } } ICommit[] commits = Persistence.GetFrom(DateTime.MinValue).ToArray(); _commits = Persistence.GetFrom().ToArray(); }
protected override void Because() { _thrown = Catch.Exception(() => { // multiple parallel connections (OpenStream is called inside the parallel for) var eventStore = new OptimisticEventStore(Persistence, null); // Single transaction scope using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = _transationIsolationLevel } , TransactionScopeAsyncFlowOption.Enabled )) { var res = Parallel.For(0, Loop, i => { int j; for (j = 0; j < StreamsPerTransaction; j++) { var streamId = i.ToString() + "-" + j.ToString(); using (var stream = eventStore.OpenStream(streamId)) { for (int k = 0; k < 10; k++) { stream.Add(new EventMessage { Body = "body" + k }); } Debug.WriteLine("Committing Stream: " + streamId); stream.CommitChanges(Guid.NewGuid()); } } }); Debug.WriteLine("Completing transaction"); if (_completeTransaction) { scope.Complete(); } } }); }
protected override void Because() { _thrown = Catch.Exception(() => Parallel.For(0, Loop, i => { // multiple parallel connections (open stream is called inside the for loop) var eventStore = new OptimisticEventStore(Persistence, null); // multiple transaction scopes: 1 for each connection using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = _transationIsolationLevel } #if NET451 || NETSTANDARD2_0 , TransactionScopeAsyncFlowOption.Enabled #endif )) { int j; for (j = 0; j < StreamsPerTransaction; j++) { using (var stream = eventStore.OpenStream(i.ToString() + "-" + j.ToString())) { for (int k = 0; k < 10; k++) { stream.Add(new EventMessage { Body = "body" + k }); } stream.CommitChanges(Guid.NewGuid()); } } if (_completeTransaction) { scope.Complete(); } } }) ); }
public void Aggregates_should_be_persisted_in_one_transaction() { var factory = new AbsoluteOrderingSqlPersistenceFactory("SqlJoesEventStore", new BinarySerializer(), true); var streamPersister = factory.Build(); streamPersister.Initialize(); var store = new OptimisticEventStore(streamPersister, new NullDispatcher()); var uowFactory = new JoesUnitOfWorkFactory(store); NcqrsEnvironment.SetDefault <IUnitOfWorkFactory>(uowFactory); var note1Id = Guid.NewGuid(); var note2Id = Guid.NewGuid(); //Create using (var uow = uowFactory.CreateUnitOfWork(Guid.NewGuid())) { var note1 = new Note(note1Id, "Text 1"); var note2 = new Note(note2Id, "Text 2"); uow.Accept(); } try { using (var tx = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted })) { using (var uow = uowFactory.CreateUnitOfWork(Guid.NewGuid())) { var note1 = (Note)uow.GetById(typeof(Note), note1Id, null); note1.ChangeText("Text 1 Modified"); var note2 = (Note)uow.GetById(typeof(Note), note2Id, null); note2.ChangeText("Text 2 Modified"); var t = new Thread(() => { using (var nestedUow = uowFactory.CreateUnitOfWork(Guid.NewGuid())) { note2 = (Note)nestedUow.GetById(typeof(Note), note2Id, null); note2.ChangeText("Text 2 Modified from mested UoW"); nestedUow.Accept(); } }); t.Start(); t.Join(); uow.Accept(); //Throws } tx.Complete(); } } catch (Exception) { //Swallow } //Nothing should be modified using (var uow = uowFactory.CreateUnitOfWork(Guid.NewGuid())) { var note1 = (Note)uow.GetById(typeof(Note), note1Id, null); note1.Text.Should().Be("Text 1"); var note2 = (Note)uow.GetById(typeof(Note), note2Id, null); note2.Text.Should().Be("Text 2 Modified from mested UoW"); } }
protected override async Task Because() { _moreThanPageSize = ConfiguredPageSizeForTesting + 1; var eventStore = new OptimisticEventStore(Persistence, null, SystemTimeProvider); // TODO: Not sure how to set the actual pagesize to the const defined above for (int i = 0; i < _moreThanPageSize; i++) { using (IEventStream stream = await eventStore.OpenStream(Guid.NewGuid())) { stream.Add(new EventMessage { Body = i }); await stream.CommitChanges(Guid.NewGuid()); } } ICommit[] commits = await (Persistence.GetFrom(DateTime.MinValue)).ToArray(); _commits = await (Persistence.GetFrom()).ToArray(); }