protected async Task Given_No_Existing_Stream_When_The_Snapshot_Is_Added_Then_An_Error_Is_Raised(IEventStoreProvider eventStoreProvider) { // Arrange string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString(); string snapshotStreamId = string.Format(CultureInfo.InvariantCulture, "{0}-snapshot", eventStreamId); Guid snapshotId = Guid.NewGuid(); const int streamRevision = 3; // Act EventStoreSnapshot eventStoreSnapshotToBeWritten = new EventStoreSnapshot(snapshotId, streamRevision, new object()); // Assert await AssertExtensions.ThrowsAsync <EventStreamNotFoundException>( async() => await eventStoreProvider.AddSnapshotAsync(eventStreamId, snapshotStreamId, eventStoreSnapshotToBeWritten).ConfigureAwait(false)).ConfigureAwait(false); }
public void Given_A_Valid_Snapshot_When_The_Snapshot_Is_Loaded_By_The_Aggregate_Then_The_Aggregate_State_Is_As_Expected() { // Arrange TestAggregate testAggregate = new TestAggregate(Guid.NewGuid().ToEventStreamIdFormattedString()); const string value = "the value"; const int streamRevision = 4; TestAggregateSnapshot testAggregateSnapshot = new TestAggregateSnapshot(new TestMessageA { Stuff = value }); EventStoreSnapshot eventStoreSnapshot = new EventStoreSnapshot(Guid.NewGuid(), streamRevision, testAggregateSnapshot); // Act testAggregate.Load(eventStoreSnapshot); // Assert Assert.Equal(streamRevision, testAggregate.EventStreamRevision); Assert.Equal(0, testAggregate.UncommittedEvents.Count); Assert.Equal(value, testAggregate.LastTestMessageA.Stuff); }
protected async Task Given_A_Snapshot_Of_Different_Type_Exists_When_Checking_A_Snapshot_Then_False_Is_Returned(IEventStoreProvider eventStoreProvider) { // Arrange string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString(); string snapshotStreamId = string.Format(CultureInfo.InvariantCulture, "{0}-snapshot", eventStreamId); Guid snapshotId = Guid.NewGuid(); const int streamRevision = 3; // Act eventStoreProvider.AppendEvents(eventStreamId, new object(), new object(), new object()); await eventStoreProvider.CommitEventsAsync(eventStreamId, ExpectedStreamRevision.New).ConfigureAwait(false); EventStoreSnapshot eventStoreSnapshotToBeWritten = new EventStoreSnapshot(snapshotId, streamRevision, new object()); await eventStoreProvider.AddSnapshotAsync(eventStreamId, snapshotStreamId, eventStoreSnapshotToBeWritten).ConfigureAwait(false); bool snapshotExists = await eventStoreProvider.CheckSnapshotExistsAsync <TestAggregateSnapshot>(snapshotStreamId).ConfigureAwait(false); // Assert Assert.False(snapshotExists); }
public override async Task <bool> CheckSnapshotExistsAsync <T>(string snapshotStreamId) { lock (SnapshotStreamsSync) { bool streamExists = SnapshotStreams.ContainsKey(snapshotStreamId); if (!streamExists) { return(false); } IList <EventStoreSnapshot> snapshots = SnapshotStreams[snapshotStreamId]; if (snapshots.Count < 1) { return(false); } // We are checking not just whether a snapshot // exists but also if it is of the expected type. EventStoreSnapshot eventStoreSnapshot = snapshots.Last(); return(eventStoreSnapshot.Body is T); } }
protected async Task Given_A_Valid_Snapshot_When_The_Snapshot_Is_Added_Then_The_Snapshot_Can_Be_Read_And_The_Retrieved_Snapshot_Has_The_Correct_Metadata(IEventStoreProvider eventStoreProvider) { // Arrange string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString(); string snapshotStreamId = string.Format(CultureInfo.InvariantCulture, "{0}-snapshot", eventStreamId); Guid snapshotId = Guid.NewGuid(); const int streamRevision = 3; // Act eventStoreProvider.AppendEvents(eventStreamId, new object(), new object(), new object()); await eventStoreProvider.CommitEventsAsync(eventStreamId, ExpectedStreamRevision.New).ConfigureAwait(false); EventStoreSnapshot eventStoreSnapshotToBeWritten = new EventStoreSnapshot(snapshotId, streamRevision, new object()); await eventStoreProvider.AddSnapshotAsync(eventStreamId, snapshotStreamId, eventStoreSnapshotToBeWritten).ConfigureAwait(false); EventStoreSnapshot eventStoreSnapshotThatWasRead = await eventStoreProvider.ReadSnapshotAsync(snapshotStreamId).ConfigureAwait(false); // Assert Assert.NotNull(eventStoreSnapshotThatWasRead); Assert.Equal(snapshotId, eventStoreSnapshotThatWasRead.SnapshotId); Assert.Equal(streamRevision, eventStoreSnapshotThatWasRead.StreamRevision); Assert.Equal(3, eventStoreSnapshotThatWasRead.Headers.Count); }
public override async Task AddSnapshotAsync(string eventStreamId, string snapshotStreamId, EventStoreSnapshot eventStoreSnapshot) { throw new System.NotImplementedException(); }
public override async Task AddSnapshotAsync(string eventStreamId, string snapshotStreamId, EventStoreSnapshot eventStoreSnapshot) { if (eventStoreSnapshot == null) { throw new ArgumentNullException(nameof(eventStoreSnapshot)); } lock (SnapshotStreamsSync) { lock (EventStreamsSync) { bool eventStreamExists = this.StreamExistsAsync(eventStreamId).Result; if (!eventStreamExists) { throw new EventStreamNotFoundException($"No Event Stream was found for ID '{eventStreamId}'."); } Dictionary <string, object> metadata = EventStoreMetadata.CreateDefaultForSnapshot(eventStoreSnapshot.Body, eventStoreSnapshot.SnapshotId, eventStreamId, eventStoreSnapshot.StreamRevision); foreach (KeyValuePair <string, object> keyValuePair in metadata) { if (!string.Equals(keyValuePair.Key, EventStoreMessageHeaderKey.EventId, StringComparison.Ordinal) && !string.Equals(keyValuePair.Key, EventStoreMetadataKey.StreamRevision, StringComparison.Ordinal)) { eventStoreSnapshot.Headers.Add(keyValuePair.Key, keyValuePair.Value); } } IList <EventStoreSnapshot> stream = GetOrCreateSnapshotStream(snapshotStreamId); stream.Add(eventStoreSnapshot); } } }