|
| 1 | +using System.Diagnostics; |
| 2 | +using IntegrationTests; |
| 3 | +using JasperFx.Core; |
| 4 | +using Marten; |
| 5 | +using Marten.Events; |
| 6 | +using Marten.Events.Aggregation; |
| 7 | +using Marten.Events.Daemon.Resiliency; |
| 8 | +using Marten.Events.Projections; |
| 9 | +using Marten.Metadata; |
| 10 | +using Microsoft.Extensions.Hosting; |
| 11 | +using Npgsql; |
| 12 | +using Shouldly; |
| 13 | +using Weasel.Postgresql; |
| 14 | +using Wolverine; |
| 15 | +using Wolverine.Marten; |
| 16 | +using Wolverine.Tracking; |
| 17 | +using Xunit.Sdk; |
| 18 | + |
| 19 | +namespace MartenTests.AsyncDaemonIntegration; |
| 20 | + |
| 21 | +public class end_to_end_publish_messages_through_marten_to_wolverine |
| 22 | +{ |
| 23 | + [Fact] |
| 24 | + public async Task can_publish_messages_through_outbox() |
| 25 | + { |
| 26 | + await dropSchema(); |
| 27 | + |
| 28 | + using var host = await Host.CreateDefaultBuilder() |
| 29 | + .UseWolverine(opts => |
| 30 | + { |
| 31 | + opts.Services.AddMarten(m => |
| 32 | + { |
| 33 | + m.Connection(Servers.PostgresConnectionString); |
| 34 | + m.DatabaseSchemaName = "wolverine_side_effects"; |
| 35 | + |
| 36 | + m.Projections.Add<Projection3>(ProjectionLifecycle.Async); |
| 37 | + }) |
| 38 | + .IntegrateWithWolverine() |
| 39 | + .AddAsyncDaemon(DaemonMode.Solo); |
| 40 | + |
| 41 | + opts.Policies.UseDurableLocalQueues(); |
| 42 | + }).StartAsync(); |
| 43 | + |
| 44 | + var streamId = Guid.NewGuid(); |
| 45 | + |
| 46 | + Func<IMessageContext, Task> publish = async _ => |
| 47 | + { |
| 48 | + using var session = host.DocumentStore().LightweightSession(); |
| 49 | + session.Events.StartStream<SideEffects1>(streamId, new AEvent(), new AEvent(), new BEvent()); |
| 50 | + await session.SaveChangesAsync(); |
| 51 | + }; |
| 52 | + |
| 53 | + var tracked = await host |
| 54 | + .TrackActivity() |
| 55 | + .Timeout(30.Seconds()) |
| 56 | + .WaitForMessageToBeReceivedAt<GotB>(host) |
| 57 | + .ExecuteAndWaitAsync(publish); |
| 58 | + |
| 59 | + tracked.Executed.SingleMessage<GotB>() |
| 60 | + .StreamId.ShouldBe(streamId); |
| 61 | + } |
| 62 | + |
| 63 | + private static async Task dropSchema() |
| 64 | + { |
| 65 | + using var conn = new NpgsqlConnection(Servers.PostgresConnectionString); |
| 66 | + await conn.OpenAsync(); |
| 67 | + await conn.DropSchemaAsync("wolverine_side_effects"); |
| 68 | + await conn.CloseAsync(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +public class Projection3: SingleStreamProjection<SideEffects1> |
| 73 | +{ |
| 74 | + public void Apply(SideEffects1 aggregate, AEvent _) |
| 75 | + { |
| 76 | + aggregate.A++; |
| 77 | + } |
| 78 | + |
| 79 | + public void Apply(SideEffects1 aggregate, BEvent _) |
| 80 | + { |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + public override ValueTask RaiseSideEffects(IDocumentOperations operations, IEventSlice<SideEffects1> slice) |
| 85 | + { |
| 86 | + if (slice.Aggregate != null && slice.Events().OfType<IEvent<BEvent>>().Any()) |
| 87 | + { |
| 88 | + slice.PublishMessage(new GotB(slice.Aggregate.Id)); |
| 89 | + } |
| 90 | + |
| 91 | + return new ValueTask(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +public record GotB(Guid StreamId); |
| 96 | + |
| 97 | +public static class GotBHandler |
| 98 | +{ |
| 99 | + public static void Handle(GotB message) => Debug.WriteLine("Got B for stream " + message.StreamId); |
| 100 | +} |
| 101 | + |
| 102 | +public class SideEffects1: IRevisioned |
| 103 | +{ |
| 104 | + public Guid Id { get; set; } |
| 105 | + public int A { get; set; } |
| 106 | + public int B { get; set; } |
| 107 | + public int C { get; set; } |
| 108 | + public int D { get; set; } |
| 109 | + public int Version { get; set; } |
| 110 | +} |
0 commit comments