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

added AddHoconFile method #58

Merged
merged 1 commit into from
Jun 9, 2022
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
7 changes: 7 additions & 0 deletions src/Akka.Hosting.Tests/Akka.Hosting.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
<ItemGroup>
<ProjectReference Include="..\Akka.Hosting\Akka.Hosting.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="test.hocon" />
<Content Include="test.hocon">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
17 changes: 2 additions & 15 deletions src/Akka.Hosting.Tests/DISanityCheckSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
using Akka.DependencyInjection;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
using static Akka.Hosting.Tests.TestHelpers;

namespace Akka.Hosting.Tests;

public class DiSanityCheckSpecs
public class DiSanityCheckSpecs
{
public interface IMySingletonInterface{}

Expand All @@ -36,19 +36,6 @@ public SingletonActor(IMySingletonInterface singleton)
});
}
}

private static async Task<IHost> StartHost(Action<IServiceCollection> testSetup)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to a separate TestHelpers class so it can be re-used across multiple tests

{
var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<IMySingletonInterface, MySingletonImpl>();
testSetup(services);
}).Build();

await host.StartAsync();
return host;
}

/// <summary>
/// Sanity check: things registered as singletons prior to the creation of the <see cref="ActorSystem"/> should
Expand Down
28 changes: 28 additions & 0 deletions src/Akka.Hosting.Tests/HoconSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Akka.Actor;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using static Akka.Hosting.Tests.TestHelpers;

namespace Akka.Hosting.Tests;

public class HoconSpecs
{
[Fact]
public async Task Should_load_HOCON_from_file()
{
// arrange
using var host = await StartHost(collection => collection.AddAkka("Test", builder =>
{
builder.AddHoconFile("test.hocon");
}));

// act
var sys = host.Services.GetRequiredService<ActorSystem>();
var hocon = sys.Settings.Config;

// assert
hocon.HasPath("petabridge.cmd").Should().BeTrue();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a custom HOCON section in the test.hocon file that wouldn't normally appear in default HOCON - thus it demonstrates that the file is being correctly loaded into the ActorSystem as a result of the AddHoconFile method.

}
}
22 changes: 22 additions & 0 deletions src/Akka.Hosting.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Akka.Hosting.Tests;

public static class TestHelpers
{
public static async Task<IHost> StartHost(Action<IServiceCollection> testSetup)
{
var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<DiSanityCheckSpecs.IMySingletonInterface, DiSanityCheckSpecs.MySingletonImpl>();
testSetup(services);
}).Build();

await host.StartAsync();
return host;
}
}
9 changes: 9 additions & 0 deletions src/Akka.Hosting.Tests/test.hocon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# See petabridge.cmd configuration options here: https://cmd.petabridge.com/articles/install/host-configuration.html
petabridge.cmd{
# default IP address used to listen for incoming petabridge.cmd client connections
# should be a safe default as it listens on "all network interfaces".
host = "0.0.0.0"

# default port number used to listen for incoming petabridge.cmd client connections
port = 9110
}
16 changes: 16 additions & 0 deletions src/Akka.Hosting/AkkaHostingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using Akka.Actor;
using Akka.Actor.Setup;
using Akka.Configuration;
Expand Down Expand Up @@ -90,6 +91,21 @@ public static AkkaConfigurationBuilder AddHocon(this AkkaConfigurationBuilder bu
return builder.AddHoconConfiguration(hocon, addMode);
}

/// <summary>
/// Automatically loads the given HOCON file from <see cref="hoconFilePath"/>
/// and inserts it into the <see cref="ActorSystem"/>s' configuration.
/// </summary>
/// <param name="builder">The builder instance being configured.</param>
/// <param name="hoconFilePath">The path to the HOCON file. Can be relative or absolute.</param>
/// <param name="addMode">The <see cref="HoconAddMode"/> - defaults to appending this HOCON as a fallback.</param>
/// <returns>The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.</returns>
public static AkkaConfigurationBuilder AddHoconFile(this AkkaConfigurationBuilder builder, string hoconFilePath,
HoconAddMode addMode = HoconAddMode.Append)
{
var hoconText = ConfigurationFactory.ParseString(File.ReadAllText(hoconFilePath));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parses the file synchronously - this will throw an exception if the file is not found, which is to be expected.

return AddHocon(builder, hoconText, addMode);
}

/// <summary>
/// Configures the <see cref="ProviderSelection"/> for this <see cref="ActorSystem"/>. Can be used to
/// configure whether or not Akka, Akka.Remote, or Akka.Cluster starts.
Expand Down