Rund 2.600 Kommentarblöcke in 310 C#-Dateien übersetzt, Begründungen vollständig erhalten: blockweise gleiche Anzahl wie vorher, kein Block unter 60 % seiner Länge, der Code außerhalb der Kommentare per Tokenizer byte-identisch geprüft. Zitate deutscher Oberflächentexte und Testdaten bleiben deutsch. WebUI: Kommentare in JS/CSS/HTML übersetzt, JSDoc-@param an die in 4b umbenannten Parameter angeglichen. Dabei behoben: - 4b hatte "tag" (HTML-Tag) als deutsches "Tag" gelesen und zu "dayValue" gemacht (makeElement, svg, Tagesübersicht) - zurück auf tag/day. - Aufgaben "InProgress" bekamen die Plakettenart "laufend", für die es nie eine CSS-Klasse gab - jetzt "success" wie laufende Instanzen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
257 lines
8.5 KiB
C#
257 lines
8.5 KiB
C#
using ClawdDotNet.Core.Instances;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
|
|
namespace ClawdDotNet.Manager.Tests;
|
|
|
|
/// <summary>
|
|
/// The registry of all instances of a machine (#528, stage 3).
|
|
///
|
|
/// <para>Two promises stand above everything here, and both follow from E-M6: <b>at startup
|
|
/// things are taken over, not started</b>, and <b>on exit nothing is ended.</b>
|
|
/// If the manager fails, the instances keep running — it must not be the single point
|
|
/// this whole project is built against.</para>
|
|
/// </summary>
|
|
public sealed class InstanceRegistryTests : IAsyncDisposable
|
|
{
|
|
private readonly string _root = Path.Combine(
|
|
Path.GetTempPath(), "clawd-tests", "reg-" + Guid.NewGuid().ToString("N")[..8]);
|
|
|
|
private readonly FakeLauncher _launcher = new();
|
|
private readonly FakeChannel _kanal = new();
|
|
private readonly List<SupervisionEvent> _ereignisse = [];
|
|
|
|
private readonly InstanceDirectoryManager _verzeichnisse;
|
|
|
|
private static readonly SupervisionOptions Options = new(
|
|
ProbeInterval: TimeSpan.FromMilliseconds(50),
|
|
StartTimeout: TimeSpan.FromMilliseconds(600),
|
|
SilentChecksBeforeAlarm: 3,
|
|
StopGrace: TimeSpan.FromMilliseconds(300),
|
|
SilentKillGrace: TimeSpan.FromMilliseconds(300),
|
|
MaxDrainWait: TimeSpan.FromSeconds(2));
|
|
|
|
public InstanceRegistryTests()
|
|
{
|
|
_verzeichnisse = new InstanceDirectoryManager(Path.Combine(_root, "Instances"));
|
|
// The socket only answers if a process is really running - exactly as in
|
|
// operation (see FakeChannel.Launcher).
|
|
_kanal.Launcher = _launcher;
|
|
}
|
|
|
|
private InstanceRegistry Register()
|
|
{
|
|
var register = new InstanceRegistry(
|
|
_verzeichnisse, _launcher, _kanal, NullLoggerFactory.Instance, Options);
|
|
|
|
register.Observed += _ereignisse.Add;
|
|
return register;
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await Task.CompletedTask;
|
|
try { Directory.Delete(_root, recursive: true); }
|
|
catch { /* A leftover test folder does not turn a run red. */ }
|
|
}
|
|
|
|
// ─── What is there ───
|
|
|
|
[Fact]
|
|
public async Task Ein_leeres_Instanzenverzeichnis_ergibt_ein_leeres_Register()
|
|
{
|
|
await using var register = Register();
|
|
await register.RefreshAsync();
|
|
|
|
register.Instances.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Angelegte_Instanzen_stehen_im_Register()
|
|
{
|
|
_verzeichnisse.CreateInstance("Alpha");
|
|
_verzeichnisse.CreateInstance("Beta");
|
|
|
|
await using var register = Register();
|
|
await register.RefreshAsync();
|
|
|
|
register.Instances.Select(i => i.Name).ShouldBe(["Alpha", "Beta"]);
|
|
register.Instances.ShouldAllBe(i => i.Condition == InstanceCondition.Stopped);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A manager that starts everything it finds when coming up would, after a
|
|
/// restart of the machine, also start what someone stopped on purpose.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Beim_Nachsehen_wird_nichts_von_selbst_gestartet()
|
|
{
|
|
_verzeichnisse.CreateInstance("Alpha");
|
|
|
|
await using var register = Register();
|
|
await register.RefreshAsync();
|
|
|
|
_launcher.Starts.ShouldBe(0);
|
|
_ereignisse.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Eine_weggeraeumte_Instanz_faellt_aus_dem_Register()
|
|
{
|
|
var pfad = _verzeichnisse.CreateInstance("Vergaenglich");
|
|
|
|
await using var register = Register();
|
|
await register.RefreshAsync();
|
|
register.Instances.Count.ShouldBe(1);
|
|
|
|
Directory.Delete(pfad, recursive: true);
|
|
await register.RefreshAsync();
|
|
|
|
register.Instances.ShouldBeEmpty();
|
|
}
|
|
|
|
// ─── Create, start, stop ───
|
|
|
|
[Fact]
|
|
public async Task Eine_neu_angelegte_Instanz_ist_sofort_im_Register_und_laeuft_nicht()
|
|
{
|
|
await using var register = Register();
|
|
|
|
var pfad = register.Create("Frisch");
|
|
|
|
Directory.Exists(pfad).ShouldBeTrue();
|
|
File.Exists(Path.Combine(pfad, "InstanceSettings.json")).ShouldBeTrue();
|
|
|
|
var instanz = register.Instances.ShouldHaveSingleItem();
|
|
instanz.Condition.ShouldBe(InstanceCondition.Unknown);
|
|
_launcher.Starts.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Starten_und_Anhalten_gehen_ueber_das_Register()
|
|
{
|
|
_kanal.Answer = new InstanceIdentity("i-1", "Alpha", "0.1.2");
|
|
|
|
await using var register = Register();
|
|
var pfad = register.Create("Alpha");
|
|
|
|
(await register.StartAsync(pfad)).ShouldBeTrue();
|
|
register.Instances.Single().Condition.ShouldBe(InstanceCondition.Running);
|
|
|
|
// The instance ends upon the request.
|
|
var prozess = _launcher.Last!;
|
|
_kanal.OnStopRequest = _ => { _kanal.Answer = null; prozess.Exit(0); };
|
|
|
|
(await register.StopAsync(pfad)).ShouldBeTrue();
|
|
register.Instances.Single().Condition.ShouldBe(InstanceCondition.Stopped);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Ein_unbekanntes_Verzeichnis_wird_nicht_stillschweigend_angelegt()
|
|
{
|
|
await using var register = Register();
|
|
|
|
await Should.ThrowAsync<KeyNotFoundException>(
|
|
() => register.StartAsync(Path.Combine(_root, "gibtsnicht")));
|
|
}
|
|
|
|
// ─── E-M6 ───
|
|
|
|
[Fact]
|
|
public async Task Laufende_Instanzen_werden_beim_Nachsehen_uebernommen()
|
|
{
|
|
string pfad;
|
|
|
|
// The first manager creates two and starts only one.
|
|
await using (var erster = Register())
|
|
{
|
|
pfad = erster.Create("Alpha");
|
|
erster.Create("Beta");
|
|
|
|
// Only Alpha answers. Information that names the same instance for every
|
|
// directory would turn green here and in operation mix up two
|
|
// instances.
|
|
_kanal.AnswerFor[Path.GetFullPath(pfad)] =
|
|
new InstanceIdentity("i-1", "Alpha", "0.1.2");
|
|
|
|
(await erster.StartAsync(pfad)).ShouldBeTrue();
|
|
}
|
|
|
|
_ereignisse.Clear();
|
|
|
|
// Second manager, the same machine.
|
|
await using var zweiter = Register();
|
|
await zweiter.RefreshAsync();
|
|
|
|
var alpha = zweiter.Instances.Single(i => i.Name == "Alpha");
|
|
alpha.Condition.ShouldBe(InstanceCondition.Running);
|
|
alpha.ProcessId.ShouldBe(_launcher.Last!.Id);
|
|
|
|
// Beta does not answer and stays off - it is not pulled up along with it.
|
|
zweiter.Instances.Single(i => i.Name == "Beta").Condition
|
|
.ShouldBe(InstanceCondition.Stopped);
|
|
|
|
// Taking over is not starting.
|
|
_launcher.Starts.ShouldBe(1);
|
|
_ereignisse.ShouldBeEmpty();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whoever closes the manager wants to close the manager — not its instances.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Das_Beenden_des_Managers_beendet_keine_Instanz()
|
|
{
|
|
_kanal.Answer = new InstanceIdentity("i-1", "Alpha", "0.1.2");
|
|
|
|
var register = Register();
|
|
var pfad = register.Create("Alpha");
|
|
await register.StartAsync(pfad);
|
|
|
|
await register.DisposeAsync();
|
|
|
|
_kanal.StopRequests.ShouldBe(0);
|
|
_launcher.Last!.WasKilled.ShouldBeFalse();
|
|
_launcher.Last.HasExited.ShouldBeFalse();
|
|
|
|
// And the state file stays behind - it is the hint for the next one.
|
|
InstanceStateStore.Read(pfad).ShouldNotBeNull();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The one exception: a license revocation is not an outage, but a verdict.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Bei_einem_Grund_der_alle_betrifft_werden_alle_angehalten()
|
|
{
|
|
_kanal.Answer = new InstanceIdentity("i-1", "Alpha", "0.1.2");
|
|
|
|
await using var register = Register();
|
|
var a = register.Create("Alpha");
|
|
await register.StartAsync(a);
|
|
|
|
var prozess = _launcher.Last!;
|
|
_kanal.OnStopRequest = _ => { _kanal.Answer = null; prozess.Exit(0); };
|
|
|
|
await register.StopAllAsync("Lizenz widerrufen");
|
|
|
|
register.Instances.Single().Condition.ShouldBe(InstanceCondition.Stopped);
|
|
_kanal.StopRequests.ShouldBe(1);
|
|
}
|
|
|
|
// ─── Occurrences ───
|
|
|
|
[Fact]
|
|
public async Task Vorkommnisse_einzelner_Instanzen_kommen_beim_Register_an()
|
|
{
|
|
_kanal.Answer = new InstanceIdentity("i-1", "Alpha", "0.1.2");
|
|
|
|
await using var register = Register();
|
|
var pfad = register.Create("Alpha");
|
|
await register.StartAsync(pfad);
|
|
|
|
_ereignisse.Select(e => e.Kind).ShouldBe([InstanceEventKind.Started]);
|
|
_ereignisse[0].InstanceDirectory.ShouldBe(Path.GetFullPath(pfad));
|
|
}
|
|
}
|