Skip to content

Commit

Permalink
Fixed the ordering of message flushing and marten transactions to acc…
Browse files Browse the repository at this point in the history
…omodate After() methods. Closes GH-1083
  • Loading branch information
jeremydmiller committed Oct 16, 2024
1 parent 2e02283 commit aadff61
Show file tree
Hide file tree
Showing 106 changed files with 142 additions and 4,788 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Alba;
using Shouldly;
using Wolverine.Tracking;
using WolverineWebApi;

namespace Wolverine.Http.Tests;

public class publishing_messages_from_middleware : IntegrationContext
{
public publishing_messages_from_middleware(AppFixture fixture) : base(fixture)
{
}

[Fact]
public async Task receive_messages_from_before_and_after_middleware()
{
Func<IMessageContext, Task> execute = async c =>
{
await Host.Scenario(x =>
{
x.Post.Url("/middleware-messages/leia");
});
};

var tracked = await Host
.TrackActivity()
.WaitForMessageToBeReceivedAt<AfterMessage1>(Host)
.ExecuteAndWaitAsync(execute);

tracked.Received.SingleMessage<BeforeMessage1>()
.Name.ShouldBe("leia");

tracked.Received.SingleMessage<AfterMessage1>()
.Name.ShouldBe("leia");
}
}
19 changes: 10 additions & 9 deletions src/Http/Wolverine.Http/HttpChain.Codegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Wolverine.Http.CodeGen;
using Wolverine.Http.Resources;
using Wolverine.Logging;
using Wolverine.Persistence;
using Wolverine.Runtime;
using Wolverine.Runtime.Handlers;

Expand Down Expand Up @@ -145,19 +146,19 @@ internal IEnumerable<Frame> DetermineFrames(GenerationRules rules)
.Select(x => x.ReturnAction(this)).SelectMany(x => x.Frames()).ToArray();
foreach (var frame in actionsOnOtherReturnValues) yield return frame;

foreach (var frame in Postprocessors) yield return frame;

if (!Postprocessors.OfType<MethodCall>().Any(x =>
x.HandlerType == typeof(MessageContext) &&
x.Method.Name == nameof(MessageContext.EnqueueCascadingAsync)))
if (Postprocessors.Any(x => x.MaySendMessages()))
{
if (actionsOnOtherReturnValues.OfType<CaptureCascadingMessages>().Any() && !Postprocessors.OfType<MethodCall>().Any(x => x.Method.Name == nameof(MessageContext.FlushOutgoingMessagesAsync)))
var flush = Postprocessors.OfType<FlushOutgoingMessages>().FirstOrDefault();
if (flush != null)
{
var flush = MethodCall.For<MessageContext>(x => x.FlushOutgoingMessagesAsync());
flush.CommentText = "Making sure there is at least one call to flush outgoing, cascading messages";
yield return flush;
Postprocessors.Remove(flush);
}

flush ??= new FlushOutgoingMessages();
Postprocessors.Add(flush);
}

foreach (var frame in Postprocessors) yield return frame;
}

private string determineFileName()
Expand Down
36 changes: 36 additions & 0 deletions src/Http/WolverineWebApi/CascadingEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Diagnostics;
using Marten;
using Wolverine;
using Wolverine.Attributes;
using Wolverine.Http;

namespace WolverineWebApi;

public static class CascadingEndpoint
{
public static OutgoingMessages Before(string name)
{
return [new BeforeMessage1(name)];
}

[Transactional]
[WolverinePost("/middleware-messages/{name}")]
public static string Post(string name, IDocumentSession session)
{
return "Hey";
}

public static async Task After(IMessageBus bus, string name)
{
await bus.PublishAsync(new AfterMessage1(name));
}
}

public record BeforeMessage1(string Name);
public record AfterMessage1(string Name);

public static class MiddlewareMessageHandler
{
public static void Handle(BeforeMessage1 message) => Debug.WriteLine("Got " + message);
public static void Handle(AfterMessage1 message) => Debug.WriteLine("Got " + message);
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit aadff61

Please sign in to comment.