Skip to content
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
18 changes: 8 additions & 10 deletions Core/tests/Managed/Stellar.Core.Tests/EngineInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using Stellar.Core.Global;
using Stellar.Core.Native;
using Stellar.Core.Quantization;
using Stellar.Kernel;
using Stellar.Kernel.Configuration;
using Stellar.Kernel.EntryPoint;
using Stellar.Kernel.Lifecycle;
using Stellar.Kernel.Services;
using Stellar.Kernel.Threading;
Expand All @@ -18,7 +16,7 @@ namespace Stellar.Core.Tests;
/// </summary>
public class EngineInstanceTests
{
[Fact]
[NativeFact]
public void TwoEngines_HaveTheirOwnServicesAndRegistries()
{
var editor = new FakeEngine("Editor");
Expand Down Expand Up @@ -57,7 +55,7 @@ public void Outside_AnyEngine_TheDefaultIsUsed()
}
}

[Fact]
[NativeFact]
public async Task TheScope_FollowsTheWork_IntoAwaitAndOtherThreads()
{
var engine = new FakeEngine("Flowing");
Expand All @@ -67,7 +65,7 @@ public async Task TheScope_FollowsTheWork_IntoAwaitAndOtherThreads()
await Task.Yield();
CoreEngine.Current.Instance.Should().BeSameAs(engine);

var seen = await Task.Run(() => CoreEngine.Current.Instance);
IEngineInstance? seen = await Task.Run(() => CoreEngine.Current.Instance);
seen.Should().BeSameAs(engine);

IEngineInstance? onThread = null;
Expand All @@ -80,7 +78,7 @@ public async Task TheScope_FollowsTheWork_IntoAwaitAndOtherThreads()
CoreEngine.Release(engine);
}

[Fact]
[NativeFact]
public void AQuant_LeavesTheRegistryOfTheEngineItWasMadeIn()
{
var first = new FakeEngine("First");
Expand All @@ -106,11 +104,11 @@ public void AQuant_LeavesTheRegistryOfTheEngineItWasMadeIn()
CoreEngine.Release(second);
}

[Fact]
[NativeFact]
public void Releasing_AnEngine_DisposesItsNativeRegistries()
{
var engine = new FakeEngine("Short");
var native = CoreEngine.For(engine).Native;
CoreContext native = CoreEngine.For(engine).Native;

CoreEngine.Release(engine).Should().BeTrue();

Expand All @@ -119,7 +117,7 @@ public void Releasing_AnEngine_DisposesItsNativeRegistries()
CoreEngine.Release(engine).Should().BeFalse();
}

[Fact]
[NativeFact]
public void TheCoreModule_RunsForTheEngineOfItsContext()
{
var engine = new FakeEngine("Hosted");
Expand Down Expand Up @@ -155,6 +153,6 @@ private sealed class FakeEngine(string name)

public IMainThreadDispatcher Dispatcher => throw new NotSupportedException();

public IServiceCatalog? Services => CoreEngine.For(this).Services;
public IServiceCatalog Services => CoreEngine.For(this).Services;
}
}
73 changes: 49 additions & 24 deletions Core/tests/Managed/Stellar.Core.Tests/FailureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ private sealed class RecordingLogger : ILogger

public Kernel.Data.Registry.IRegistrations Registrations => throw new NotSupportedException();

public TMeta? FindMeta<TMeta>() where TMeta : class, IMetaQuant => null;
public TMeta? FindMeta<TMeta>()
where TMeta : class, IMetaQuant
{
return null;
}

public void Log(LogLevel level, string message) => Lines.Add((level, message));
public void Log(LogLevel level, string message)
{
Lines.Add((level, message));
}

public void LogWithoutFormat(string message) => Lines.Add((LogLevel.Info, message));
public void LogWithoutFormat(string message)
{
Lines.Add((LogLevel.Info, message));
}

public void Start()
{
Expand All @@ -47,21 +57,28 @@ private sealed class RecordingControl : IEngineControl

public Failure? Stopped { get; private set; }

public void RequestStop(StopReason reason, Failure? failure = null) => Stopped = failure;
public void RequestStop(StopReason reason, Failure? failure = null)
{
Stopped = failure;
}
}

private sealed class Recorder : IFailureHandler
{
public List<Failure> Heard { get; } = [];

public void Handle(Failure failure) => Heard.Add(failure);
public void Handle(Failure failure)
{
Heard.Add(failure);
}
}

[Theory]
[InlineData(FailureLevel.NonCritical, FailureReaction.None)]
[InlineData(FailureLevel.Warning, FailureReaction.Log)]
[InlineData(FailureLevel.Error, FailureReaction.Log | FailureReaction.StopExecution)]
[InlineData(FailureLevel.CriticalError, FailureReaction.Log | FailureReaction.StopExecution | FailureReaction.StopEngine)]
[InlineData(FailureLevel.CriticalError,
FailureReaction.Log | FailureReaction.StopExecution | FailureReaction.StopEngine)]
public void Levels_HaveTheirReactionsByDefault(FailureLevel level, FailureReaction reaction)
{
new FailureSystem().ReactionTo(new Failure(FailureKind.Engine, level, "x")).Should().Be(reaction);
Expand Down Expand Up @@ -130,8 +147,8 @@ public void AFailureThatLetsTheWorkGoOn_GivesTheFallback()
{
using var catcher = new Catcher(FailureKind.FileSystem, FailureLevel.Warning);

var save = catcher.Try<string>(() => throw new IOException("no save"), fallback: "empty");
var next = catcher.Run(() => { });
string save = catcher.Try<string>(() => throw new IOException("no save"), "empty");
bool next = catcher.Run(() => { });

save.Should().Be("empty");
next.Should().BeTrue();
Expand All @@ -142,9 +159,9 @@ public void AFailureThatLetsTheWorkGoOn_GivesTheFallback()
[Fact]
public void AFailureThatStopsTheWork_IsThrownOn_AndTheWholeTryStops()
{
var reached = false;
bool reached = false;

var block = () =>
Action block = () =>
{
using var catcher = new Catcher(FailureKind.FileSystem);
catcher.Run(() => throw new IOException("disk gone"));
Expand All @@ -158,8 +175,8 @@ public void AFailureThatStopsTheWork_IsThrownOn_AndTheWholeTryStops()
[Fact]
public void CatchBlocks_HandleTheirExceptions_AndAreNotReported()
{
var firstRun = false;
using var catcher = new Catcher(FailureKind.FileSystem)
bool firstRun = false;
using Catcher catcher = new Catcher(FailureKind.FileSystem)
.Catch<FileNotFoundException>(_ => firstRun = true)
.Catch<IOException>(FailureLevel.Warning);

Expand All @@ -175,9 +192,9 @@ public void ErrorsOfTheEngine_KeepTheirFailure()
{
using var catcher = new Catcher(FailureKind.Application, FailureLevel.Warning);

var block = () => catcher.Run(() => throw new DomainNotFoundException("assets"));
Func<bool> block = () => catcher.Run(() => throw new DomainNotFoundException("assets"));

var thrown = block.Should().Throw<DomainNotFoundException>().Which;
DomainNotFoundException thrown = block.Should().Throw<DomainNotFoundException>().Which;
thrown.Failure.Kind.Should().Be(FailureKind.FileSystem);
catcher.Failures.Should().ContainSingle().Which.Should().BeSameAs(thrown.Failure);
}
Expand All @@ -192,7 +209,7 @@ public void AFailureCaughtAgain_KeepsItsFirstReaction()
using var outer = new Catcher(system, FailureKind.Engine, FailureLevel.Warning);

// A stop goes up through every catcher, as if none were there, until a plain try; it is reported once
var block = () => outer.Run(() =>
Func<bool> block = () => outer.Run(() =>
{
using var inner = new Catcher(system, FailureKind.Engine);
inner.Run(() => throw new InvalidOperationException("deep"));
Expand All @@ -209,7 +226,7 @@ public void ACatcherThatKeepsAFew_KeepsTheLastOnes_AndCountsThemAll()
using var catcher = new Catcher(FailureKind.Engine, FailureLevel.Warning) { Keeps = 2 };
using var forgetful = new Catcher(FailureKind.Engine, FailureLevel.Warning) { Keeps = 0 };

for (var i = 0; i < 5; i++)
for (int i = 0; i < 5; i++)
{
catcher.Report($"failure {i}");
forgetful.Report($"failure {i}");
Expand All @@ -219,7 +236,7 @@ public void ACatcherThatKeepsAFew_KeepsTheLastOnes_AndCountsThemAll()
catcher.Failures.Select(failure => failure.Message).Should().Equal("failure 3", "failure 4");
forgetful.HasFailed.Should().BeTrue();
forgetful.Failures.Should().BeEmpty();
var negative = () => new Catcher { Keeps = -1 };
Func<Catcher> negative = () => new Catcher { Keeps = -1 };
negative.Should().Throw<ArgumentOutOfRangeException>();
}

Expand All @@ -228,7 +245,8 @@ public void Guard_NeverThrows_AndGivesTheFailure()
{
using var catcher = new Catcher(FailureKind.Engine, FailureLevel.CriticalError);

var failure = catcher.Guard(() => throw new InvalidOperationException("boom"), "the frame", phase: FramePhase.Update);
Failure? failure = catcher.Guard(() => throw new InvalidOperationException("boom"), "the frame",
phase: FramePhase.Update);

failure.Should().NotBeNull();
failure!.Message.Should().Be("the frame: boom");
Expand Down Expand Up @@ -257,7 +275,8 @@ public void Cancellation_IsNotAFailure()
{
using var catcher = new Catcher();

catcher.Invoking(c => c.Run(() => throw new OperationCanceledException())).Should().Throw<OperationCanceledException>();
catcher.Invoking(c => c.Run(() => throw new OperationCanceledException())).Should()
.Throw<OperationCanceledException>();
catcher.HasFailed.Should().BeFalse();
}

Expand All @@ -269,10 +288,11 @@ public void ThrowIfFailed_ThrowsTheWorst()
catcher.Report(new InvalidOperationException("worse"), level: FailureLevel.NonCritical);
catcher.Report("the worst", FailureLevel.Warning);

catcher.Invoking(c => c.ThrowIfFailed()).Should().Throw<FailureException>().WithMessage("a placeholder was used");
catcher.Invoking(c => c.ThrowIfFailed()).Should().Throw<FailureException>()
.WithMessage("a placeholder was used");
}

[Fact]
[NativeFact]
public void InAnEngine_TheCatcherReportsToItsSystem()
{
var engine = new ScopeEngine("Failing");
Expand All @@ -283,7 +303,9 @@ public void InAnEngine_TheCatcherReportsToItsSystem()

using (EngineScope.Enter(engine))
using (var catcher = new Catcher(FailureKind.Engine, FailureLevel.Warning))
{
catcher.Run(() => throw new InvalidOperationException("in the engine"));
}

recorder.Count.Should().Be(1);
CoreEngine.Release(engine);
Expand All @@ -293,7 +315,10 @@ private sealed class FailureSystemRecorder : IFailureHandler
{
public int Count { get; private set; }

public void Handle(Failure failure) => Count++;
public void Handle(Failure failure)
{
Count++;
}
}
}

Expand All @@ -310,12 +335,12 @@ public void EntryPoint_IsQuantAndDisposable()
[Fact]
public void Constructor_WithNullConfiguration_Throws()
{
var action = () => new EntryPoint(null!);
Func<EntryPoint> action = () => new EntryPoint(null!);

action.Should().Throw<ArgumentNullException>();
}

[Fact]
[NativeFact]
public void Run_PublishesTheFailureSystem_AndDisposeWithdrawsIt()
{
var engine = new ScopeEngine("Entry");
Expand Down
Loading
Loading