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

Fixed the ordering of message flushing and marten transactions to acc… #1085

Merged
merged 1 commit into from
Oct 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

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
Loading