Files
Chronos/tests/Titano.Server.Tests/Hosting/StartupTests.cs
T
Alby96andClaude Opus 5.5 43b6871cdc feat: jobs run one at a time, parallelism per photo and a priority queue
Owner review 1, phase 2 (docs/requests/2026-10-15-owner-review-1.md, R2).

Jobs now run one at a time on the desktop and on the server. Parallelism
(Settings > Rendering, TITANO_PARALLELISM) is how many photos the running
job processes at once: thumbnail and proxy passes draw that many photos
together and still write the pack and checkpoints in frame order, decode
tiers are probed that many at a time, the deep analysis measures that many
frames and a render decodes that many ahead. Wherever originals are
developed whole (TIFF, PNG, RAW without a usable preview), fewer run at
once within the memory budget, which the running job now has whole; the
server's frame window is sized to the host instead of 1 GiB.

A new job waits its turn and pausing the running job starts the next one.
An optional priority queue gives each job kind a level (renders, imports
and downloads high, verifies and proxies low by default): a queued job of
a higher level pauses the running one without asking, and that job goes
back to the queue with its checkpoint and resumes by itself afterwards.
Jobs of the same level, and a job's own follow-ups, never interrupt it.
The Jobs screen and the admin panel show the parallelism and the queue;
Settings > Rendering and the admin panel's Settings set them, at once.

TITANO_WORKERS and the Workers key of server.json still set the
parallelism (1 to 64, with a warning at start for the variable).
Titano.Tools gains i18n prune, which removed the renamed keys from the 36
machine-translated languages; the new strings are translated in all 38.

Found while testing: uploads and imports failed when a pass read the
sidecar while they wrote it (atomic writes now retry the rename while a
reader holds the file, and never for a read-only one), and taking a lease
just released while another process read it failed on Windows (the create
now waits for the reader). Five adversarial review rounds checked the
phase before this commit.

Also updates BRIEF (12.8, 14.6, 14.8, 14.9, 24), DECISIONS, CHANGELOG,
ARCHITECTURE, STATUS, the request file and the compose/Unraid templates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 14:56:24 +02:00

164 lines
8.0 KiB
C#

using System.Net;
using System.Text.RegularExpressions;
using Titano.Data;
using Titano.Server.Configuration;
using Titano.Server.Hosting;
using Titano.Testing;
namespace Titano.Server.Tests.Hosting;
[Category("server")]
public sealed class StartupTests(TestContext context)
{
[Test]
public void TheServerRefusesToStartWithoutTheArchiveModule()
{
var root = context.CreateTempDirectory("bare");
var options = new ServerOptions
{
DataPath = Path.Combine(root, "data"),
ArchivePath = Path.Combine(root, "archive"),
HttpPort = 0,
HttpsPort = 0,
Database = DatabaseKind.Sqlite,
Discovery = false,
};
var error = Assert.Throws<InvalidOperationException>(() => ServerHost.Build(options, []));
Assert.Contains("IArchiveIngest", error.Message);
Assert.Contains("IFrameAssetLocator", error.Message);
}
[Test]
public void TitanoDbAcceptsAConnectionString()
{
var root = context.CreateTempDirectory("env");
var options = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "Host=db.lan;Port=6543;Database=archive;Username=titano;Password=secret",
});
Assert.Equal(DatabaseKind.Postgres, options.Database);
var database = options.ToDatabaseOptions();
Assert.Equal(Dialect.Postgres, database.Dialect);
Assert.Equal(6543, Titano.Data.Postgres.PostgresSettings.Parse(database.ConnectionString).Port);
}
[Test]
public void PartsBuildAQuotedPostgresConnectionString()
{
var options = new ServerOptions
{
Database = DatabaseKind.Postgres, // the Windows default is SQLite
DbHost = "postgres",
DbName = "titano",
DbUser = "titano",
DbPassword = "it's;secret",
DbSslMode = "disable",
};
var settings = Titano.Data.Postgres.PostgresSettings.Parse(options.ToDatabaseOptions().ConnectionString);
Assert.Equal(("postgres", "titano", "it's;secret"), (settings.Host, settings.Database, settings.Password));
Assert.Equal(Titano.Data.Postgres.SslMode.Disable, settings.SslMode);
}
[Test]
public void EnvironmentVariablesLockTheirSettings()
{
var root = context.CreateTempDirectory("locked");
var options = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "postgres",
["TITANO_HTTP_PORT"] = "9090",
["TITANO_DISCOVERY"] = "false",
});
Assert.SequenceEqual(["Database", "HttpPort", "Discovery"], options.Locked);
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "many" }));
}
[Test]
public void TheFormerWorkersSettingStillSetsTheParallelism()
{
// Owner review 1: "workers" (jobs at once) became the parallelism (photos the running job processes at once).
var root = context.CreateTempDirectory("workers");
File.WriteAllText(Path.Combine(root, "server.json"), """{ "Workers": 6 }""");
Assert.Equal(6, ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).Parallelism);
var former = ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "5" });
Assert.Equal((5, true), (former.Parallelism, former.FormerWorkersVariable));
Assert.SequenceEqual(["Database", "Parallelism"], former.Locked);
var both = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "postgres",
["TITANO_WORKERS"] = "5",
["TITANO_PARALLELISM"] = "7",
["TITANO_PRIORITY_QUEUE"] = "true",
});
Assert.Equal((7, true), (both.Parallelism, both.PriorityQueue));
Assert.SequenceEqual(["Database", "Parallelism", "PriorityQueue"], both.Locked);
Assert.False(ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PARALLELISM"] = "2" })
.FormerWorkersVariable);
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PRIORITY_QUEUE"] = "often" }));
// A value the admin panel would refuse is brought within its range, so the panel can still save the other settings.
Assert.Equal(ServerOptions.MaxParallelism,
ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PARALLELISM"] = "96" }).Parallelism);
Assert.Equal(1, ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "0" }).Parallelism);
Assert.InRange(new ServerOptions().Parallelism, 1, ServerOptions.MaxParallelism);
}
[Test]
public void TrustedProxiesAreAddressesOrNetworks()
{
var root = context.CreateTempDirectory("proxies");
// Loopback only: machines of the home network reach the HTTP port directly from private addresses.
var defaults = ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).TrustedProxyNetworks();
foreach (var address in new[] { "127.0.0.1", "127.0.0.53", "::1" })
Assert.True(defaults.Any(n => n.Contains(IPAddress.Parse(address))), address);
foreach (var address in new[] { "10.20.30.40", "172.18.0.3", "192.168.1.2", "fd00::7", "203.0.113.5" })
Assert.False(defaults.Any(n => n.Contains(IPAddress.Parse(address))), address);
var options = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "postgres",
["TITANO_TRUSTED_PROXIES"] = "10.1.2.3, 2001:db8::/32;172.20.0.5/16",
});
Assert.Contains("TrustedProxies", options.Locked);
Assert.SequenceEqual(["10.1.2.3/32", "2001:db8::/32", "172.20.0.0/16"], options.TrustedProxyNetworks().Select(n => n.ToString()));
foreach (var invalid in new[] { "proxy.lan", "10.0.0.0/33", "fe80::1%2" })
{
var error = Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "postgres",
["TITANO_TRUSTED_PROXIES"] = "127.0.0.1," + invalid,
}));
Assert.Contains(invalid, error.Message);
}
}
[Test]
public void PublicPortsComeFromTheEnvironment()
{
var root = context.CreateTempDirectory("public");
var options = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "postgres",
["TITANO_PUBLIC_HTTP_PORT"] = "18080",
["TITANO_PUBLIC_HTTPS_PORT"] = "18443",
});
Assert.Equal((18080, 18443), (options.PublicHttpPort, options.PublicHttpsPort));
Assert.Null(ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).PublicHttpsPort);
foreach (var invalid in new[] { "0", "70000", "https" })
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PUBLIC_HTTPS_PORT"] = invalid }));
}
[Test]
public void HelpListsEveryVariable()
{
foreach (var variable in ServerOptions.EnvironmentVariables.Concat(["PUID", "PGID", "TZ"]))
Assert.True(Regex.IsMatch(ServerCommandLine.Usage, $@"\b{variable}\b"), variable);
}
}