Ejemplo n.º 1
0
        public async Task Should_write_to_store_with_previous_position()
        {
            InvalidateMessage message = null;

            pubSub.Subscribe <InvalidateMessage>(m =>
            {
                message = m;
            });

            SetupEventStore(3);

            var actualObject = await sut.GetSingleAsync <MyStatefulObject>(key);

            Assert.Same(statefulObject, actualObject);

            await statefulObject.WriteEventsAsync(new MyEvent(), new MyEvent());

            await statefulObject.WriteEventsAsync(new MyEvent(), new MyEvent());

            A.CallTo(() => eventStore.AppendEventsAsync(A <Guid> .Ignored, key, 2, A <ICollection <EventData> > .That.Matches(x => x.Count == 2)))
            .MustHaveHappened();
            A.CallTo(() => eventStore.AppendEventsAsync(A <Guid> .Ignored, key, 4, A <ICollection <EventData> > .That.Matches(x => x.Count == 2)))
            .MustHaveHappened();

            Assert.NotNull(message);
            Assert.Equal(key, message.Key);
        }
Ejemplo n.º 2
0
        public async Task Should_append_events_and_publish()
        {
            var commitId = Guid.NewGuid();

            var event1 = new MyEvent();
            var event2 = new MyEvent();

            var eventData1 = new EventData();
            var eventData2 = new EventData();

            A.CallTo(() => formatter.ToEventData(A <Envelope <IEvent> > .That.Matches(e => e.Payload == event1), commitId, true))
            .Returns(eventData1);
            A.CallTo(() => formatter.ToEventData(A <Envelope <IEvent> > .That.Matches(e => e.Payload == event2), commitId, true))
            .Returns(eventData2);

            A.CallTo(() => eventStore.AppendEventsAsync(commitId, streamName, 123, A <ICollection <EventData> > .That.Matches(e => e.Count == 2)))
            .Returns(TaskHelper.Done);

            domainObject.AddEvent(event1);
            domainObject.AddEvent(event2);

            await sut.SaveAsync(domainObject, domainObject.GetUncomittedEvents(), commitId);

            A.CallTo(() => eventStore.AppendEventsAsync(commitId, streamName, 123, A <ICollection <EventData> > .That.Matches(e => e.Count == 2))).MustHaveHappened();
        }
Ejemplo n.º 3
0
        public async Task WriteEventsAsync(IEnumerable <Envelope <IEvent> > events)
        {
            Guard.NotNull(events, nameof(@events));

            var eventArray = events.ToArray();

            if (eventArray.Length > 0)
            {
                var expectedVersion = UseEventSourcing() ? version : EtagVersion.Any;

                var commitId = Guid.NewGuid();

                var eventStream = GetStreamName();
                var eventData   = GetEventData(eventArray, commitId);

                try
                {
                    await eventStore.AppendEventsAsync(commitId, GetStreamName(), expectedVersion, eventData);
                }
                catch (WrongEventVersionException ex)
                {
                    throw new DomainObjectVersionException(ownerKey.ToString(), typeof(TOwner), ex.CurrentVersion, ex.ExpectedVersion);
                }

                versionEvents += eventArray.Length;
            }

            UpdateVersion();
        }
Ejemplo n.º 4
0
        public long Run()
        {
            var streamName = Guid.NewGuid().ToString();

            for (var eventId = 0; eventId < NumEvents; eventId++)
            {
                var eventData = formatter.ToEventData(new Envelope <IEvent>(new MyEvent {
                    EventNumber = eventId + 1
                }), Guid.NewGuid());

                eventStore.AppendEventsAsync(Guid.NewGuid(), streamName, eventId - 1, new [] { eventData }).Wait();
            }

            eventConsumer.Wait();

            if (eventConsumer.EventNumbers.Count != NumEvents)
            {
                throw new InvalidOperationException($"{eventConsumer.EventNumbers.Count} Events have been handled");
            }

            for (var i = 0; i < eventConsumer.EventNumbers.Count; i++)
            {
                var value = eventConsumer.EventNumbers[i];

                if (value != i + 1)
                {
                    throw new InvalidOperationException($"Event[{i}] != value");
                }
            }

            return(NumEvents);
        }
Ejemplo n.º 5
0
        public async Task Should_write_to_store_with_previous_position()
        {
            SetupEventStore(3);

            var actualObject = await sut.GetSingleAsync <MyStatefulObject>(key);

            Assert.Same(statefulObject, actualObject);

            await statefulObject.WriteEventsAsync(new MyEvent(), new MyEvent());

            await statefulObject.WriteEventsAsync(new MyEvent(), new MyEvent());

            A.CallTo(() => eventStore.AppendEventsAsync(A <Guid> .Ignored, key, 2, A <ICollection <EventData> > .That.Matches(x => x.Count == 2)))
            .MustHaveHappened();
            A.CallTo(() => eventStore.AppendEventsAsync(A <Guid> .Ignored, key, 4, A <ICollection <EventData> > .That.Matches(x => x.Count == 2)))
            .MustHaveHappened();
        }
Ejemplo n.º 6
0
        public long Run()
        {
            var streamName = Guid.NewGuid().ToString();

            for (var eventId = 0; eventId < NumEvents; eventId++)
            {
                var eventData = formatter.ToEventData(new Envelope <IEvent>(new MyEvent {
                    EventNumber = eventId + 1
                }), Guid.NewGuid());

                eventStore.AppendEventsAsync(Guid.NewGuid(), streamName, eventId - 1, new [] { eventData }).Wait();
            }

            eventConsumer.WaitAndVerify();

            return(NumEvents);
        }
Ejemplo n.º 7
0
        public long Run()
        {
            const long numCommits = 200;
            const long numStreams = 100;

            Parallel.For(0, numStreams, streamId =>
            {
                var streamName = streamId.ToString();

                for (var commitId = 0; commitId < numCommits; commitId++)
                {
                    eventStore.AppendEventsAsync(Guid.NewGuid(), streamName, new[] { Helper.CreateEventData() }).Wait();
                }
            });

            return(numCommits * numStreams);
        }
Ejemplo n.º 8
0
        public long Run()
        {
            const long numCommits = 100;
            const long numStreams = 20;

            for (var streamId = 0; streamId < numStreams; streamId++)
            {
                var eventOffset = -1;
                var streamName  = streamId.ToString();

                for (var commitId = 0; commitId < numCommits; commitId++)
                {
                    eventStore.AppendEventsAsync(Guid.NewGuid(), streamName, eventOffset, new[] { Helper.CreateEventData() }).Wait();
                    eventOffset++;
                }
            }

            return(numCommits * numStreams);
        }
        public async Task SaveAsync(IAggregate domainObject, ICollection <Envelope <IEvent> > events, Guid commitId)
        {
            Guard.NotNull(domainObject, nameof(domainObject));

            var streamName = nameResolver.GetStreamName(domainObject.GetType(), domainObject.Id);

            var versionCurrent  = domainObject.Version;
            var versionExpected = versionCurrent - events.Count;

            var eventsToSave = events.Select(x => formatter.ToEventData(x, commitId)).ToList();

            try
            {
                await eventStore.AppendEventsAsync(commitId, streamName, versionExpected, eventsToSave);
            }
            catch (WrongEventVersionException)
            {
                throw new DomainObjectVersionException(domainObject.Id.ToString(), domainObject.GetType(), versionCurrent, versionExpected);
            }
        }
        public long Run()
        {
            Parallel.For(0, NumStreams, streamId =>
            {
                var eventOffset = -1;
                var streamName  = streamId.ToString();

                for (var commitId = 0; commitId < NumCommits; commitId++)
                {
                    var eventData = formatter.ToEventData(new Envelope <IEvent>(new MyEvent()), Guid.NewGuid());

                    eventStore.AppendEventsAsync(Guid.NewGuid(), streamName, eventOffset - 1, new[] { eventData }).Wait();
                    eventOffset++;
                }
            });

            eventConsumer.WaitAndVerify();

            return(NumStreams * NumCommits);
        }
Ejemplo n.º 11
0
 public static void Warmup(this IEventStore eventStore)
 {
     eventStore.AppendEventsAsync(Guid.NewGuid(), "my-stream", new List <EventData> {
         CreateEventData()
     }).Wait();
 }