Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3597: InvalidCastException in FetchForWriting #3599

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,89 @@ await Should.ThrowAsync<StreamLockedException>(async () =>
}


[Fact]
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version_using_identity_map()
{
StoreOptions(
opts =>
{
opts.Events.UseIdentityMapForAggregates = true;
opts.Projections.Snapshot<SimpleAggregate>(SnapshotLifecycle.Inline);
});

var streamId = Guid.NewGuid();

theSession.Events.StartStream<SimpleAggregate>(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(),
new CEvent(), new CEvent());
await theSession.SaveChangesAsync();

var stream = await theSession.Events.FetchForWriting<SimpleAggregate>(streamId, 6);
stream.Aggregate.ShouldNotBeNull();
stream.CurrentVersion.ShouldBe(6);

stream.AppendOne(new EEvent());
await theSession.SaveChangesAsync();

var latest = await theSession.Events.FetchLatest<SimpleAggregate>(streamId);
latest.Version.ShouldBe(7);
}
[Fact]
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version_using_identity_map_immediate_sad_path()
{
StoreOptions(
opts =>
{
opts.Events.UseIdentityMapForAggregates = true;
opts.Projections.Snapshot<SimpleAggregate>(SnapshotLifecycle.Inline);
});


var streamId = Guid.NewGuid();

theSession.Events.StartStream<SimpleAggregate>(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(),
new CEvent(), new CEvent());
await theSession.SaveChangesAsync();

await Should.ThrowAsync<ConcurrencyException>(async () =>
{
var stream = await theSession.Events.FetchForWriting<SimpleAggregate>(streamId, 5);
});
}

[Fact]
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version_using_identity_map_sad_path_on_save_changes()
{
StoreOptions(
opts =>
{
opts.Events.UseIdentityMapForAggregates = true;
opts.Projections.Snapshot<SimpleAggregate>(SnapshotLifecycle.Inline);
});


var streamId = Guid.NewGuid();

theSession.Events.StartStream<SimpleAggregate>(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(),
new CEvent(), new CEvent());
await theSession.SaveChangesAsync();

// This should be fine
var stream = await theSession.Events.FetchForWriting<SimpleAggregate>(streamId, 6);
stream.AppendOne(new EEvent());

// Get in between and run other events in a different session
await using (var otherSession = theStore.LightweightSession())
{
otherSession.Events.Append(streamId, new EEvent());
await otherSession.SaveChangesAsync();
}

// The version is now off
await Should.ThrowAsync<ConcurrencyException>(async () =>
{
await theSession.SaveChangesAsync();
});
}

[Fact]
public async Task fetch_existing_stream_for_writing_Guid_identifier_with_expected_version()
Expand Down
2 changes: 1 addition & 1 deletion src/Marten/Events/Fetching/FetchInlinedPlan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public async Task<IEventStream<TDoc>> FetchForWriting(DocumentSessionBase sessio
IDocumentStorage<TDoc, TId> storage = null;
if (session.Options.Events.UseIdentityMapForAggregates)
{
storage = (IDocumentStorage<TDoc, TId>)session.Options.Providers.StorageFor<TDoc>();
storage = session.Options.ResolveCorrectedDocumentStorage<TDoc, TId>(DocumentTracking.IdentityOnly);
// Opt into the identity map mechanics for this aggregate type just in case
// you're using a lightweight session
session.UseIdentityMapFor<TDoc>();
Expand Down
Loading