public EventConsumerGrainTests() { state.Position = initialPosition; consumerName = eventConsumer.GetType().Name; A.CallTo(() => eventStore.CreateSubscription(A <IEventSubscriber> .Ignored, A <string> .Ignored, A <string> .Ignored)) .Returns(eventSubscription); A.CallTo(() => eventConsumer.Name). Returns(consumerName); A.CallTo(() => stateHolder.State) .ReturnsLazily(() => state); A.CallToSet(() => stateHolder.State) .Invokes(new Action <EventConsumerState>(s => state = s)); A.CallTo(() => formatter.Parse(eventData, true)).Returns(envelope); sut = new MyEventConsumerGrain(formatter, eventStore, log); sutSubscriber = sut; sut.ActivateAsync(stateHolder).Wait(); }
public async Task Should_apply_domain_objects_to_event() { var eventData1 = new EventData(); var eventData2 = new EventData(); var event1 = new MyEvent(); var event2 = new MyEvent(); var events = new List <StoredEvent> { new StoredEvent("0", 0, eventData1), new StoredEvent("1", 1, eventData2) }; A.CallTo(() => eventStore.GetEventsAsync(streamName)) .Returns(events); A.CallTo(() => formatter.Parse(eventData1, true)) .Returns(new Envelope <IEvent>(event1)); A.CallTo(() => formatter.Parse(eventData2, true)) .Returns(new Envelope <IEvent>(event2)); await sut.LoadAsync(domainObject); Assert.Equal(domainObject.AppliedEvents, new[] { event1, event2 }); }
public async Task <TDomainObject> GetByIdAsync <TDomainObject>(Guid id, long?expectedVersion = null) where TDomainObject : class, IAggregate { var streamName = nameResolver.GetStreamName(typeof(TDomainObject), id); var events = await eventStore.GetEventsAsync(streamName).ToList(); if (events.Count == 0) { throw new DomainObjectNotFoundException(id.ToString(), typeof(TDomainObject)); } var domainObject = (TDomainObject)factory.CreateNew(typeof(TDomainObject), id); foreach (var storedEvent in events) { var envelope = formatter.Parse(storedEvent.Data); domainObject.ApplyEvent(envelope); } if (expectedVersion != null && domainObject.Version != expectedVersion.Value) { throw new DomainObjectVersionException(id.ToString(), typeof(TDomainObject), domainObject.Version, expectedVersion.Value); } return(domainObject); }
private Envelope <IEvent> HandleAsync(StoredEvent input) { var eventNumber = input.EventNumber; if (eventNumber <= lastReceivedEventNumber || !isRunning) { return(null); } try { var result = formatter.Parse(input.Data); result.SetEventNumber(input.EventNumber); result.SetEventStreamNumber(input.EventStreamNumber); return(result); } catch (Exception ex) { OnError?.Invoke(ex); log.LogFatal(ex, w => w .WriteProperty("action", "ParseEvent") .WriteProperty("state", "Failed") .WriteProperty("eventId", input.Data.EventId.ToString()) .WriteProperty("eventNumber", input.EventNumber)); return(null); } }
private Envelope <IEvent> ParseEvent(StoredEvent message) { var @event = formatter.Parse(message.Data); @event.SetEventPosition(message.EventPosition); @event.SetEventStreamNumber(message.EventStreamNumber); return(@event); }
private Envelope <IEvent> ParseOrNull(StoredEvent storedEvent) { try { return(formatter.Parse(storedEvent.Data)); } catch (TypeNameNotFoundException) { return(null); } }
public EventConsumerActorTests() { consumerInfo.Position = Guid.NewGuid().ToString(); consumerName = eventConsumer.GetType().Name; A.CallTo(() => eventStore.CreateSubscription(A <IEventSubscriber> .Ignored, A <string> .Ignored, A <string> .Ignored)).Returns(eventSubscription); A.CallTo(() => eventConsumer.Name).Returns(consumerName); A.CallTo(() => eventConsumerInfoRepository.FindAsync(consumerName)).Returns(consumerInfo); A.CallTo(() => formatter.Parse(eventData, true)).Returns(envelope); sut = new EventConsumerActor(formatter, eventStore, eventConsumerInfoRepository, log) { ReconnectWaitMs = 0 }; sutActor = sut; sutSubscriber = sut; }
private Envelope <IEvent> ParseKnownEvent(StoredEvent message) { try { var @event = formatter.Parse(message.Data); @event.SetEventPosition(message.EventPosition); @event.SetEventStreamNumber(message.EventStreamNumber); return(@event); } catch (TypeNameNotFoundException) { log.LogDebug(w => w.WriteProperty("oldEventFound", message.Data.Type)); return(null); } }
public async Task Should_throw_exception_when_content_is_from_another_event() { var eventData1 = new EventData(); var event1 = new ContentCreated { Data = new NamedContentData(), AppId = new NamedId <Guid>(Guid.NewGuid(), "my-app") }; var events = new List <StoredEvent> { new StoredEvent("0", 0, eventData1) }; A.CallTo(() => eventStore.GetEventsAsync(streamName)) .Returns(events); A.CallTo(() => formatter.Parse(eventData1, true)) .Returns(new Envelope <IEvent>(event1)); await Assert.ThrowsAsync <DomainObjectNotFoundException>(() => sut.LoadAsync(appId, id, 0)); }