예제 #1
0
        public void EventsAreDeletedWhenUnitOfWorkIsCommitted()
        {
            var bus   = new MockServiceBus();
            var store = new MockEventStore();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard(), DateTimeNowTimeSource.Instance))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant(session);

                var aggregate1 = new Guid("92EC4FE2-26A8-4274-8674-DC5D95513C83");
                var aggregate2 = new Guid("F08200E4-8790-4ECC-9F06-A3D3BAC9E21C");

                session.Delete(aggregate1);
                session.Delete(aggregate2);

                session.SaveChanges();

                Assert.That(store.DeletedAggregates, Is.Empty);

                uow.Commit();

                Assert.That(store.DeletedAggregates, Is.EquivalentTo(new[] { aggregate1, aggregate2 }));
            }
        }
예제 #2
0
        public void EventsArePublishedOnSaveChangesAndThisInteractsWithUnitOfWorkParticipations()
        {
            var bus   = new MockServiceBus();
            var store = new MockEventStore();

            var users = 1.Through(9).Select(i => { var u = new User(); u.Register(i + "@test.com", "abcd", Guid.NewGuid()); u.ChangeEmail("new" + i + "@test.com"); return(u); }).ToList();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard(), DateTimeNowTimeSource.Instance))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant(session);

                users.Take(3).ForEach(u => session.Save(u));
                Assert.That(bus.Published.Count, Is.EqualTo(0));
                session.SaveChanges();
                Assert.That(bus.Published.Count, Is.EqualTo(6));

                users.Skip(3).Take(3).ForEach(u => session.Save(u));
                Assert.That(bus.Published.Count, Is.EqualTo(6));
                session.SaveChanges();
                Assert.That(bus.Published.Count, Is.EqualTo(12));

                users.Skip(6).Take(3).ForEach(u => session.Save(u));

                Assert.That(bus.Published.Count, Is.EqualTo(12));
                Assert.That(store.SavedEvents.Count, Is.EqualTo(0));
                uow.Commit();
                Assert.That(bus.Published.Count, Is.EqualTo(18));

                Assert.That(bus.Published.Select(e => e.EventId).Distinct().Count(), Is.EqualTo(18));
                Assert.That(bus.Published, Is.EquivalentTo(store.SavedEvents));
            }
        }
예제 #3
0
        public void Exception_for_some_items_still_writes_the_others()
        {
            // the writer will fail on the batch and on the next 3 requests
            var writer = CreateWriter(writer: MockEventStore.ThrowsOnWrite(new Exception(), new[] { 1, 2, 3, 4 }));

            var requests = Enumerable.Range(0, 100).Select(_ => CreatePersistenceRequest(1)).ToList();

            foreach (var r in requests)
            {
                writer.Tell(r);
            }

            var list = new List <object>();

            for (var i = 0; i < requests.Count; i++)
            {
                var request = requests[i];

                if (i <= 2)
                {
                    ExpectMsg <PersistenceFailure>(msg => msg.PersistenceID == request.PersistenceID);
                }
                else
                {
                    ExpectMsg <PersistenceSuccess>(msg => msg.PersistenceID == request.PersistenceID);
                }
            }
        }
예제 #4
0
        public void DuplicatedEventException_causes_duplicatedevent_message()
        {
            var writer  = CreateWriter(writer: MockEventStore.ThrowsOnWrite <DuplicatedEntryException>());
            var request = CreatePersistenceRequest();

            writer.Tell(request);

            ExpectMsg <DuplicatedEntry>(msg => msg.PersistenceID == request.PersistenceID, TimeSpan.FromMinutes(5));
        }
        public void UnexpectedStreamSequenceException_causes_unexpectedstreamsequence_message()
        {
            var writer = CreateWriter(writer: MockEventStore.ThrowsOnWrite <UnexpectedStreamSequenceException>());

            var request = CreatePersistenceRequest();

            writer.Tell(request);

            ExpectMsg <UnexpectedStreamSequence>(msg => msg.PersistenceID == request.PersistenceID);
        }
예제 #6
0
        protected IActorRef CreateWriter(IEventStoreWriter writer = null, ISerializer serializer = null, IActorRef dispatcher = null)
        {
            writer     = writer ?? MockEventStore.SuccessfulWriter();
            serializer = serializer ?? new MockSerializer();
            dispatcher = dispatcher ?? CreateTestProbe();

            var props = BufferedEventWriter.CreateProps(writer, serializer, dispatcher, new GlobalOptions());

            return(Sys.ActorOf(props));
        }
        public void UnexpectedExceptions_during_write_causes_reply_with_persistencefailure(Type exceptionType)
        {
            var exception = Activator.CreateInstance(exceptionType) as Exception;

            var writer = CreateWriter(writer: MockEventStore.ThrowsOnWrite(exception));

            var request = CreatePersistenceRequest();

            writer.Tell(request);

            ExpectMsg <PersistenceFailure>(msg => msg.PersistenceID == request.PersistenceID && msg.Exception == exception);
        }
        public void Writer_does_not_publish_to_event_stream()
        {
            var dispatcher = CreateTestProbe();
            var writer     = CreateWriter(writer: MockEventStore.SuccessfulWriter(), dispatcher: dispatcher);
            var request    = CreatePersistenceRequest();

            var probe = CreateTestProbe();

            Sys.EventStream.Subscribe(probe, typeof(IPersistedEvent));

            writer.Tell(request);

            probe.ExpectNoMsg(TimeSpan.FromMilliseconds(500));
        }
        public void Receives_ReadAborted_on_store_exception()
        {
            var ex      = new Exception();
            var store   = MockEventStore.ThrowsOnReadStreams(ex);
            var factory = Substitute.For <IPersistedEventFactory>();

            var props = Props.Create <ReadIndexedProjectionStreamWorker>(store, factory);
            var actor = Sys.ActorOf(props);

            var req = new ReadIndexedProjectionStreamRequest("a", 1, EventCount.Unlimited);

            actor.Tell(req);
            ExpectMsg <Aborted>(m => m.RequestID == req.RequestID && m.Exception == ex);
        }
        public void WhenAFakeUserIsCreated_TheVersionIsOne()
        {
            // Arrange
            IEventStore       eventStore       = new MockEventStore(new List <IAggregateEvent>());
            IEventBus         eventBus         = new MockEventBus();
            IDateTimeProvider dateTimeProvider = new DateTimeProvider();
            Guid   userGuid = Guid.NewGuid();
            string username = "******";

            // Act
            FakeUser fakeUser = FakeUser.CreateNew(userGuid, eventStore, eventBus, dateTimeProvider, dateTimeProvider.CurrentDateTime, username);

            // Assert
            Assert.AreEqual(1, fakeUser.Version);
        }
예제 #11
0
        public void Writer_tells_persistedevents_to_dispatcher_in_order()
        {
            var dispatcher = CreateTestProbe();
            var writer     = CreateWriter(writer: MockEventStore.SuccessfulWriter(), dispatcher: dispatcher);

            var request = new PersistenceRequest(new[] {
                new UnpersistedEvent("a", new SampleEvent3()),
                new UnpersistedEvent("a", new SampleEvent1()),
                new UnpersistedEvent("a", new SampleEvent2())
            });

            writer.Tell(request);

            dispatcher.ExpectMsg <IPersistedEvent <SampleEvent3> >();
            dispatcher.ExpectMsg <IPersistedEvent <SampleEvent1> >();
            dispatcher.ExpectMsg <IPersistedEvent <SampleEvent2> >();
            dispatcher.ExpectNoMsg(50);
        }
예제 #12
0
        public void EventsAreDeletedWhenNotAUnitOfWorkParticipant()
        {
            var bus   = new MockServiceBus();
            var store = new MockEventStore();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard(), DateTimeNowTimeSource.Instance))
            {
                var aggregate1 = new Guid("92EC4FE2-26A8-4274-8674-DC5D95513C83");
                var aggregate2 = new Guid("F08200E4-8790-4ECC-9F06-A3D3BAC9E21C");

                session.Delete(aggregate1);
                session.Delete(aggregate2);

                session.SaveChanges();
                session.SaveChanges();  // Verify that SaveChanges() does not delete the events twice.

                Assert.That(store.DeletedAggregates, Is.EquivalentTo(new[] { aggregate1, aggregate2 }));
            }
        }
        public void WhenAFakeUserIsCreated_TheEventIsPublishedToTheEventBus()
        {
            // Arrange
            IEventStore       eventStore       = new MockEventStore(new List <IAggregateEvent>());
            MockEventBus      eventBus         = new MockEventBus();
            IDateTimeProvider dateTimeProvider = new DateTimeProvider();
            Guid   userGuid = Guid.NewGuid();
            string username = "******";

            // Act
            FakeUser fakeUser = FakeUser.CreateNew(userGuid, eventStore, eventBus, dateTimeProvider, dateTimeProvider.CurrentDateTime, username);

            // Assert
            Assert.IsTrue(eventBus.PublishedEvents.Count() >= 1);

            IEvent @event = eventBus.PublishedEvents.Dequeue();

            Assert.IsInstanceOfType(@event, typeof(FakeUserCreated));
        }
        public void Run()
        {
            Event[] updateEvents = null;
            var     es           = new MockEventStore();
            var     sut          = new CommandPipeline(es, Load, Process_with_notifications, Update);

            var cmd = new MyCommand {
                Parameter = "123"
            };
            var result = sut.Handle(cmd);

            Assert.IsType <Success>(result.Msg);
            Assert.Equal(2, es.Events.Count);
            Assert.Equal("54321", (es.Events[0] as MyEvent).Reversed);
            Assert.Equal(5, (es.Events[1] as YourEvent).Count);
            Assert.Equal(2, updateEvents.Length);
            Assert.IsType <MyNotification>(result.Notifications[0]);


            (CommandStatus, Event[], string, Notification[]) Process_with_notifications(IMessage msg, IMessageContextModel ctx, string version)
        public void EventsAreDeletedWhenUnitOfWorkIsCommitted()
        {
            var bus = new MockServiceBus();
            var store = new MockEventStore();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard()))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant(session);

                var aggregate1 = new Guid("92EC4FE2-26A8-4274-8674-DC5D95513C83");
                var aggregate2 = new Guid("F08200E4-8790-4ECC-9F06-A3D3BAC9E21C");

                session.Delete(aggregate1);
                session.Delete(aggregate2);

                session.SaveChanges();

                Assert.That(store.DeletedAggregates, Is.Empty);

                uow.Commit();

                Assert.That(store.DeletedAggregates, Is.EquivalentTo(new[] { aggregate1, aggregate2 }));
            }
        }
        public void EventsAreDeletedWhenNotAUnitOfWorkParticipant()
        {
            var bus = new MockServiceBus();
            var store = new MockEventStore();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard()))
            {
                var aggregate1 = new Guid("92EC4FE2-26A8-4274-8674-DC5D95513C83");
                var aggregate2 = new Guid("F08200E4-8790-4ECC-9F06-A3D3BAC9E21C");

                session.Delete(aggregate1);
                session.Delete(aggregate2);

                session.SaveChanges();
                session.SaveChanges();  // Verify that SaveChanges() does not delete the events twice.

                Assert.That(store.DeletedAggregates, Is.EquivalentTo(new[] { aggregate1, aggregate2 }));
            }
        }
        public void EventsArePublishedOnSaveChangesAndThisInteractsWithUnitOfWorkParticipations()
        {
            var bus = new MockServiceBus();
            var store = new MockEventStore();

            var users = 1.Through(9).Select(i => { var u = new User(); u.Register(i + "@test.com", "abcd", Guid.NewGuid()); u.ChangeEmail("new" + i + "@test.com"); return u; }).ToList();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard()))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant(session);

                users.Take(3).ForEach(u => session.Save(u));
                Assert.That(bus.Published.Count, Is.EqualTo(0));
                session.SaveChanges();
                Assert.That(bus.Published.Count, Is.EqualTo(6));

                users.Skip(3).Take(3).ForEach(u => session.Save(u));
                Assert.That(bus.Published.Count, Is.EqualTo(6));
                session.SaveChanges();
                Assert.That(bus.Published.Count, Is.EqualTo(12));

                users.Skip(6).Take(3).ForEach(u => session.Save(u));

                Assert.That(bus.Published.Count, Is.EqualTo(12));
                Assert.That(store.SavedEvents.Count, Is.EqualTo(0));
                uow.Commit();
                Assert.That(bus.Published.Count, Is.EqualTo(18));

                Assert.That(bus.Published.Select(e => e.EventId).Distinct().Count(), Is.EqualTo(18));
                Assert.That(bus.Published, Is.EquivalentTo(store.SavedEvents));
            }
        }