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 codegen ahead of time with sticky handlers. Closes GH-1163 #1180

Merged
merged 2 commits into from
Dec 18, 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
24 changes: 24 additions & 0 deletions src/Testing/CoreTests/Acceptance/sticky_message_handlers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections;
using JasperFx.CodeGeneration;
using JasperFx.CodeGeneration.Model;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Wolverine.ComplianceTests;
Expand Down Expand Up @@ -35,6 +37,28 @@ public async Task send_message_is_handled_by_both_handlers_independently_by_attr
records.ShouldContain(new StickyMessageResponse("blue", stickyMessage, new Uri("local://blue")));
}

//[Fact]
public void generate_code_with_sticky_handlers()
{
/*
* Steps -->
* HandlerChain needs to have some kind of suffix added to the generated handler type to mark by the endpoint. Use the full handler type name maybe
* HandlerChain if sticky needs to disambiguate the file name too
* HandlerGraph implementation of the getting files needs to change
*
*/


var collections = Host.Services.GetServices<ICodeFileCollection>().ToArray();

var builder = new DynamicCodeBuilder(Host.Services, collections)
{
ServiceVariableSource = Host.Services.GetService<IServiceVariableSource>()
};

builder.GenerateAllCode();
}

public class FakeChainPolicy : IChainPolicy
{
public List<HandlerChain> Chains = new();
Expand Down
9 changes: 8 additions & 1 deletion src/Testing/CoreTests/IntegrationContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using JasperFx.Core.Reflection;
using CoreTests.Acceptance;
using CoreTests.Bugs;
using JasperFx.Core.Reflection;
using Lamar.Microsoft.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Oakton.Resources;
using Wolverine.ComplianceTests;
using Wolverine.ComplianceTests.Compliance;
Expand All @@ -25,6 +29,9 @@ public DefaultApp()

opts.IncludeType<MessageConsumer>();
opts.IncludeType<InvokedMessageHandler>();

opts.Services.AddSingleton(Substitute.For<IIdentityService>());
opts.Services.AddSingleton(Substitute.For<ITrackedTaskRepository>());
})
.UseResourceSetupOnStartup(StartupAction.ResetState).Start();
}
Expand Down
20 changes: 14 additions & 6 deletions src/Wolverine/Runtime/Handlers/HandlerChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ private HandlerChain(MethodCall call, HandlerGraph parent) : this(call.Method.Me
Handlers.Add(call);
}

internal HandlerChain(MethodCall call, HandlerGraph parent, Endpoint[] endpoints) : this(call, parent)
{
foreach (var endpoint in endpoints)
{
RegisterEndpoint(endpoint);
}

TypeName = call.HandlerType.ToSuffixedTypeName(HandlerSuffix).Replace("[]", "Array");

Description = $"Message Handler for {MessageType.FullNameInCode()} using {call}";
}

public HandlerChain(WolverineOptions options, IGrouping<Type, HandlerCall> grouping, HandlerGraph parent) : this(grouping.Key, parent)
{
Handlers.AddRange(grouping);
Expand Down Expand Up @@ -111,11 +123,7 @@ private void tryAssignStickyEndpoints(HandlerCall handlerCall, WolverineOptions
stub.Subscriptions.Add(Subscription.ForType(MessageType));
}

var chain = new HandlerChain(handlerCall, options.HandlerGraph);
foreach (var endpoint in endpoints)
{
chain.RegisterEndpoint(endpoint);
}
var chain = new HandlerChain(handlerCall, options.HandlerGraph, endpoints);

Handlers.Remove(handlerCall);

Expand Down Expand Up @@ -199,7 +207,7 @@ public LogLevel ExecutionLogLevel
/// <summary>
/// Wolverine's string identification for this message type
/// </summary>
public string TypeName { get; }
public string TypeName { get; private set; }

internal MessageHandler? Handler { get; private set; }

Expand Down
21 changes: 20 additions & 1 deletion src/Wolverine/Runtime/Handlers/HandlerGraph.GeneratesCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ public partial class HandlerGraph

IReadOnlyList<ICodeFile> ICodeFileCollection.BuildFiles()
{
return Chains.ToList();

return explodeAllFiles().ToList();
}

private IEnumerable<ICodeFile> explodeAllFiles()
{
foreach (var chain in Chains)
{
if (chain.Handlers.Any())
{
yield return chain;
}
else
{
foreach (var handlerChain in chain.ByEndpoint)
{
yield return handlerChain;
}
}
}
}
}
Loading