-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add several tests for application startup
- Loading branch information
1 parent
80de32c
commit 9cd5eb3
Showing
5 changed files
with
109 additions
and
10 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/Anexia.E5E.Tests/Extensions/HostApplicationBuilderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |