Skip to content

Commit

Permalink
Add several tests for application startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nachtjasmin committed Oct 25, 2023
1 parent 149545a commit 49d334e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.IO;
using System.Text.Json;

using Anexia.E5E.Exceptions;
using Anexia.E5E.Extensions;
using Anexia.E5E.Runtime;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using Xunit;

namespace Anexia.E5E.Tests.Extensions;

public class HostApplicationBuilderExtensionsTests
{
[Fact]
public void EmptyListOfArgumentsThrowsException()
{
Assert.Throws<E5EMissingArgumentsException>(() =>
Host.CreateDefaultBuilder().ConfigureE5E(Array.Empty<string>()).Build());
}

[Fact]
public void IncorrectListOfArgumentsThrowsException()
{
Assert.Throws<E5EMissingArgumentsException>(() =>
Host.CreateDefaultBuilder().ConfigureE5E(new[] { "foo", "bar" }).Build());
}

[Fact]
public void MetadataIsReturned()
{
using var _ = Console.Out;
using var sw = new StringWriter();
Console.SetOut(sw);

var expected = JsonSerializer.Serialize(new E5ERuntimeMetadata());
Host.CreateDefaultBuilder().ConfigureE5E(new[] { "metadata" }).Build().RunE5E();

Assert.Equal(expected, sw.ToString());
}

[Fact]
public void ShouldReadEscapedNullBytes()
{
var host = Host.CreateDefaultBuilder().ConfigureE5E(new[] { "entrypoint", "\\0", "1", "\\0" }).Build();
var got = host.Services.GetRequiredService<E5ERuntimeOptions>();
var expected = new E5ERuntimeOptions("entrypoint", "\0", "\0", true, false);

Assert.Equal(expected, got);
}

[Fact]
public void StopsInvocationWithMissingEntrypoint()
{
var host = Host.CreateDefaultBuilder().ConfigureE5E(new[] { "entrypoint", "\\0", "1", "\\0" }).Build();
Assert.Throws<E5EMissingEntrypointException>(() => host.Run());
}
}
28 changes: 28 additions & 0 deletions src/Anexia.E5E/Extensions/GenericHostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json;

using Anexia.E5E.Runtime;
using Anexia.E5E.Serialization;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Anexia.E5E.Extensions;

public static class GenericHostExtensions
{
public static void RunE5E(this IHost host) => host.RunE5EAsync().GetAwaiter().GetResult();

public static Task RunE5EAsync(this IHost host, CancellationToken cancellationToken = default)
{
var runtime = host.Services.GetRequiredService<E5ERuntimeOptions>();
if (!runtime.WriteMetadataOnStartup)
return host.RunAsync(cancellationToken);

var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var metadata = JsonSerializer.Serialize(new E5ERuntimeMetadata(), E5EJsonSerializerOptions.Default);
Console.Out.Write(metadata);
lifetime.StopApplication();

return Task.CompletedTask;
}
}
23 changes: 15 additions & 8 deletions src/Anexia.E5E/Extensions/HostApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

using Anexia.E5E.Abstractions;
using Anexia.E5E.Exceptions;
using Anexia.E5E.Functions;
using Anexia.E5E.Hosting;
using Anexia.E5E.Runtime;
using Anexia.E5E.Serialization;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Anexia.E5E.Extensions;

Expand All @@ -23,17 +27,19 @@ public static IHostBuilder ConfigureE5E(this IHostBuilder hb, string[] args)
throw new E5EMissingArgumentsException(
$"There were no arguments given to the {nameof(ConfigureE5E)} method.");

if (args[0] == "metadata")
hb.ConfigureServices((_, services) =>
{
var metadata = JsonSerializer.Serialize(new E5ERuntimeMetadata());
Console.Out.WriteLine(metadata);
Environment.Exit(0);
return hb;
}
services.TryAddEntrypointServiceResolver();
services.TryAddSingleton<IConsoleAbstraction, ConsoleAbstraction>();
});

if (args[0] == "metadata")
return ConfigureE5E(hb, E5ERuntimeOptions.WriteMetadata);

if (args.Length != 4)
throw new E5EMissingArgumentsException($"Expected exactly four arguments given, got {args.Length}");


var entrypoint = args[0];
var stdoutTerminationSequence = args[1].Replace("\\0", "\0");
var keepAlive = args[2] == "1";
Expand All @@ -47,8 +53,9 @@ public static IHostBuilder ConfigureE5E(this IHostBuilder hb, E5ERuntimeOptions
{
hb.ConfigureServices((_, services) =>
{
services.AddSingleton<IConsoleAbstraction, ConsoleAbstraction>();
services.AddSingleton(options);
services.TryAddEntrypointServiceResolver();
services.TryAddSingleton<IConsoleAbstraction, ConsoleAbstraction>();
services.TryAddSingleton(options);
services.AddHostedService<E5ECommunicationService>();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static IServiceCollection AddE5EFunction(this IServiceCollection services
return services;
}

private static void TryAddEntrypointServiceResolver(this IServiceCollection services)
internal static void TryAddEntrypointServiceResolver(this IServiceCollection services)
{
services.TryAddScoped<E5EFunctionResolver>(svc => () =>
{
Expand Down
5 changes: 4 additions & 1 deletion src/Anexia.E5E/Runtime/E5ERuntimeOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Anexia.E5E.Runtime;

public record E5ERuntimeOptions(string Entrypoint, string StdoutTerminationSequence,
string DaemonExecutionTerminationSequence, bool KeepAlive);
string DaemonExecutionTerminationSequence, bool KeepAlive, bool WriteMetadataOnStartup = false)
{
internal static readonly E5ERuntimeOptions WriteMetadata = new("", "", "", false, true);
}

0 comments on commit 49d334e

Please sign in to comment.