Skip to content

Commit

Permalink
Added Wolverine service-bus sample
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Aug 18, 2024
1 parent 14b581f commit bb5a926
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cleipnir.Flows.sln
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cleipnir.Flows.MassTransit.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cleipnir.Flows.MassTransit.Tests", "ServiceBuses\MassTransit\Cleipnir.Flows.MassTransit.Tests\Cleipnir.Flows.MassTransit.Tests.csproj", "{55263D9D-8024-49A5-9F22-336B3F5901AF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Wolverine", "Wolverine", "{5E80C9B0-3EBD-4D8A-BBED-3D1CCB1A6756}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cleipnir.Flows.Wolverine.Console", "ServiceBuses\Wolverine\Cleipnir.Flows.Wolverine.Console\Cleipnir.Flows.Wolverine.Console.csproj", "{D627AC0C-A012-4EB5-A1F6-79A217636D11}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cleipnir.Flows.Wolverine.Tests", "ServiceBuses\Wolverine\Cleipnir.Flows.Wolverine.Tests\Cleipnir.Flows.Wolverine.Tests.csproj", "{F71833DB-A6DB-4EE5-911A-FF1B8EDABEBC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -162,6 +168,14 @@ Global
{55263D9D-8024-49A5-9F22-336B3F5901AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55263D9D-8024-49A5-9F22-336B3F5901AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55263D9D-8024-49A5-9F22-336B3F5901AF}.Release|Any CPU.Build.0 = Release|Any CPU
{D627AC0C-A012-4EB5-A1F6-79A217636D11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D627AC0C-A012-4EB5-A1F6-79A217636D11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D627AC0C-A012-4EB5-A1F6-79A217636D11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D627AC0C-A012-4EB5-A1F6-79A217636D11}.Release|Any CPU.Build.0 = Release|Any CPU
{F71833DB-A6DB-4EE5-911A-FF1B8EDABEBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F71833DB-A6DB-4EE5-911A-FF1B8EDABEBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F71833DB-A6DB-4EE5-911A-FF1B8EDABEBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F71833DB-A6DB-4EE5-911A-FF1B8EDABEBC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BC470FDE-839E-4B2C-8C56-8B20DEEF2CCD} = {EAD350F1-8EF2-48AC-98EE-A5E739F15787}
Expand All @@ -187,5 +201,8 @@ Global
{339E2C3B-13B4-4A10-B162-511E3F0C1B52} = {43DF25B8-E678-48DC-8F8D-142F76F22300}
{45A3D239-1736-443B-9B0E-0FE0D830B63B} = {43DF25B8-E678-48DC-8F8D-142F76F22300}
{55263D9D-8024-49A5-9F22-336B3F5901AF} = {43DF25B8-E678-48DC-8F8D-142F76F22300}
{5E80C9B0-3EBD-4D8A-BBED-3D1CCB1A6756} = {0872461B-3FC1-4659-AE2D-80109CDA3F5E}
{D627AC0C-A012-4EB5-A1F6-79A217636D11} = {5E80C9B0-3EBD-4D8A-BBED-3D1CCB1A6756}
{F71833DB-A6DB-4EE5-911A-FF1B8EDABEBC} = {5E80C9B0-3EBD-4D8A-BBED-3D1CCB1A6756}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="WolverineFx" Version="2.17.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Cleipnir.Flows\Cleipnir.Flows.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Cleipnir.Flows.MassTransit.Console;

public record MyMessage(string Value);
49 changes: 49 additions & 0 deletions ServiceBuses/Wolverine/Cleipnir.Flows.Wolverine.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Cleipnir.Flows.AspNet;
using Cleipnir.Flows.MassTransit.Console;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Wolverine;

namespace Cleipnir.Flows.Wolverine.Console;

internal static class Program
{
public static async Task Main(string[] args)
{
var host = await CreateHostBuilder([]).StartAsync();
var bus = host.Services.GetRequiredService<IMessageBus>();
var store = host.Services.GetRequiredService<IFunctionStore>();

var testSize = 1_000;
for (var i = 0; i < testSize; i++)
await bus.PublishAsync(new MyMessage(i.ToString()));

while (true)
{
var succeeded = await store.GetSucceededFunctions(
nameof(SimpleFlow),
DateTime.UtcNow.Ticks + 1_000_000
).SelectAsync(f => f.Count);
if (succeeded == testSize)
break;

await Task.Delay(250);
}

System.Console.WriteLine("All completed");
}

private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddFlows(c => c
.UseInMemoryStore()
.RegisterFlow<SimpleFlow, SimpleFlows>()
//.RegisterFlowsAutomatically()
);
})
.UseWolverine();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Cleipnir.Flows.MassTransit.Console;
using Cleipnir.ResilientFunctions.Reactive.Extensions;

namespace Cleipnir.Flows.Wolverine.Console;

public class SimpleFlow : Flow
{
public override async Task Run()
{
var msg = await Messages.FirstOfType<MyMessage>();
System.Console.WriteLine($"SimpleFlow({msg}) executed");
}
}

public class SimpleFlows : Flows<SimpleFlow>
{
public SimpleFlows(FlowsContainer flowsContainer)
: base("SimpleFlow", flowsContainer, options: null) { }
}

public class SimpleFlowsHandler(SimpleFlows flows)
{
public Task Handle(MyMessage myMessage)
=> flows.SendMessage(myMessage.Value, myMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="WolverineFx" Version="2.17.0" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Cleipnir.Flows\Cleipnir.Flows.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Cleipnir.Flows.AspNet;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Reactive.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Shouldly;
using Wolverine;

namespace Cleipnir.Flows.Wolverine.Tests;

[TestClass]
public class IntegrationTests
{
public record MyMessage(string Value);

public class TestFlow : Flow
{
public static volatile MyMessage? ReceivedMyMessage;

public override async Task Run()
{
ReceivedMyMessage = await Messages.FirstOfType<MyMessage>();
}
}

public class TestFlows : Flows<TestFlow>
{
public TestFlows(FlowsContainer flowsContainer) : base(flowName: "RebusTestFlow", flowsContainer) { }
}

private class TestHostedService : IHostedService
{
public static IServiceProvider? ServiceProvider { get; set; }

public TestHostedService(IServiceProvider serviceProvider) => ServiceProvider = serviceProvider;

public async Task StartAsync(CancellationToken cancellationToken)
{
await ServiceProvider!.GetRequiredService<IMessageBus>().SendAsync(new MyMessage("SomeMessage"));
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

[TestMethod]
public async Task SunshineScenario()
{
var host = await Host
.CreateDefaultBuilder([])
.UseWolverine()
.ConfigureServices((_, services) =>
{
services.AddHostedService<TestHostedService>();

services.AddFlows(c => c
.UseInMemoryStore()
.RegisterFlow<TestFlow, TestFlows>()
);
})
.StartAsync();

await BusyWait.Until(() => TestFlow.ReceivedMyMessage is not null, maxWait: TimeSpan.FromSeconds(5));
TestFlow.ReceivedMyMessage!.Value.ShouldBe("SomeMessage");

host.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Cleipnir.Flows.Wolverine.Tests;

public class TestFlowHandler(IntegrationTests.TestFlows flows)
{
public Task Handle(IntegrationTests.MyMessage message) => flows.SendMessage(message.Value, message);
}

0 comments on commit bb5a926

Please sign in to comment.