-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatic support for Marten's new async aggregation side effect model.
Closes GH-938. Also 3.0-beta-2
- Loading branch information
1 parent
5717f1d
commit d1c76ef
Showing
8 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...enTests/AsyncDaemonIntegration/end_to_end_publish_messages_through_marten_to_wolverine.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System.Diagnostics; | ||
using IntegrationTests; | ||
using JasperFx.Core; | ||
using Marten; | ||
using Marten.Events; | ||
using Marten.Events.Aggregation; | ||
using Marten.Events.Daemon.Resiliency; | ||
using Marten.Events.Projections; | ||
using Marten.Metadata; | ||
using Microsoft.Extensions.Hosting; | ||
using Npgsql; | ||
using Shouldly; | ||
using Weasel.Postgresql; | ||
using Wolverine; | ||
using Wolverine.Marten; | ||
using Wolverine.Tracking; | ||
using Xunit.Sdk; | ||
|
||
namespace MartenTests.AsyncDaemonIntegration; | ||
|
||
public class end_to_end_publish_messages_through_marten_to_wolverine | ||
{ | ||
[Fact] | ||
public async Task can_publish_messages_through_outbox() | ||
{ | ||
await dropSchema(); | ||
|
||
using var host = await Host.CreateDefaultBuilder() | ||
.UseWolverine(opts => | ||
{ | ||
opts.Services.AddMarten(m => | ||
{ | ||
m.Connection(Servers.PostgresConnectionString); | ||
m.DatabaseSchemaName = "wolverine_side_effects"; | ||
|
||
m.Projections.Add<Projection3>(ProjectionLifecycle.Async); | ||
}) | ||
.IntegrateWithWolverine() | ||
.AddAsyncDaemon(DaemonMode.Solo); | ||
|
||
opts.Policies.UseDurableLocalQueues(); | ||
}).StartAsync(); | ||
|
||
var streamId = Guid.NewGuid(); | ||
|
||
Func<IMessageContext, Task> publish = async _ => | ||
{ | ||
using var session = host.DocumentStore().LightweightSession(); | ||
session.Events.StartStream<SideEffects1>(streamId, new AEvent(), new AEvent(), new BEvent()); | ||
await session.SaveChangesAsync(); | ||
}; | ||
|
||
var tracked = await host | ||
.TrackActivity() | ||
.Timeout(30.Seconds()) | ||
.WaitForMessageToBeReceivedAt<GotB>(host) | ||
.ExecuteAndWaitAsync(publish); | ||
|
||
tracked.Executed.SingleMessage<GotB>() | ||
.StreamId.ShouldBe(streamId); | ||
} | ||
|
||
private static async Task dropSchema() | ||
{ | ||
using var conn = new NpgsqlConnection(Servers.PostgresConnectionString); | ||
await conn.OpenAsync(); | ||
await conn.DropSchemaAsync("wolverine_side_effects"); | ||
await conn.CloseAsync(); | ||
} | ||
} | ||
|
||
public class Projection3: SingleStreamProjection<SideEffects1> | ||
{ | ||
public void Apply(SideEffects1 aggregate, AEvent _) | ||
{ | ||
aggregate.A++; | ||
} | ||
|
||
public void Apply(SideEffects1 aggregate, BEvent _) | ||
{ | ||
|
||
} | ||
|
||
public override ValueTask RaiseSideEffects(IDocumentOperations operations, IEventSlice<SideEffects1> slice) | ||
{ | ||
if (slice.Aggregate != null && slice.Events().OfType<IEvent<BEvent>>().Any()) | ||
{ | ||
slice.PublishMessage(new GotB(slice.Aggregate.Id)); | ||
} | ||
|
||
return new ValueTask(); | ||
} | ||
} | ||
|
||
public record GotB(Guid StreamId); | ||
|
||
public static class GotBHandler | ||
{ | ||
public static void Handle(GotB message) => Debug.WriteLine("Got B for stream " + message.StreamId); | ||
} | ||
|
||
public class SideEffects1: IRevisioned | ||
{ | ||
public Guid Id { get; set; } | ||
public int A { get; set; } | ||
public int B { get; set; } | ||
public int C { get; set; } | ||
public int D { get; set; } | ||
public int Version { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Persistence/Wolverine.Marten/Publishing/MartenToWolverineMessageBatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Marten; | ||
using Marten.Events.Aggregation; | ||
using Marten.Internal.Sessions; | ||
using Marten.Services; | ||
using Wolverine.Runtime; | ||
|
||
namespace Wolverine.Marten.Publishing; | ||
|
||
internal class MartenToWolverineMessageBatch(MessageContext Context, DocumentSessionBase Session) : IMessageBatch | ||
{ | ||
public async ValueTask PublishAsync<T>(T message) | ||
{ | ||
await Context.PublishAsync(message); | ||
} | ||
|
||
public Task AfterCommitAsync(IDocumentSession session, IChangeSet commit, CancellationToken token) | ||
{ | ||
return Context.FlushOutgoingMessagesAsync(); | ||
} | ||
|
||
public Task BeforeCommitAsync(IDocumentSession session, IChangeSet commit, CancellationToken token) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Persistence/Wolverine.Marten/Publishing/MartenToWolverineOutbox.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Marten.Events.Aggregation; | ||
using Marten.Internal.Sessions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Wolverine.Runtime; | ||
|
||
namespace Wolverine.Marten.Publishing; | ||
|
||
internal class MartenToWolverineOutbox : IMessageOutbox | ||
{ | ||
private readonly Lazy<IWolverineRuntime> _runtime; | ||
|
||
public MartenToWolverineOutbox(IServiceProvider services) | ||
{ | ||
_runtime = new Lazy<IWolverineRuntime>(() => services.GetRequiredService<IWolverineRuntime>()); | ||
} | ||
|
||
public async ValueTask<IMessageBatch> CreateBatch(DocumentSessionBase session) | ||
{ | ||
var context = new MessageContext(_runtime.Value, session.TenantId); | ||
await context.EnlistInOutboxAsync(new MartenEnvelopeTransaction(session, context)); | ||
|
||
return new MartenToWolverineMessageBatch(context, session); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters