Files
Chronos/tests/Titano.Server.Tests/TestServer.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

154 lines
7.0 KiB
C#

using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Protocol;
using Titano.Server.Configuration;
using Titano.Server.Hosting;
using Titano.Server.Storage;
using Titano.Testing;
namespace Titano.Server.Tests;
/// <summary>
/// In-process Kestrel on ephemeral ports with temporary archive/data folders and a SQLite database (BRIEF §20.1).
/// The archive module is replaced by <see cref="FakeArchiveIngest"/> and <see cref="FakeAssetLocator"/>.
/// </summary>
public sealed class TestServer : IAsyncDisposable
{
public const string AdminPassword = "first-admin-pass";
public const string Password = "a-good-password-1";
private readonly WebApplication _app;
private readonly List<HttpClient> _clients = [];
private string _adminPassword = AdminPassword;
private TestServer(WebApplication app, ServerOptions options, Uri http, Uri? https)
{
_app = app;
Options = options;
HttpBase = http;
HttpsBase = https;
Client = NewClient();
}
public ServerOptions Options { get; }
public Uri HttpBase { get; }
public Uri? HttpsBase { get; }
/// <summary>An anonymous client.</summary>
public HttpClient Client { get; }
public IServiceProvider Services => _app.Services;
public TitanoDatabase Database => Services.GetRequiredService<TitanoDatabase>();
public static async Task<TestServer> StartAsync(TestContext context, Action<ServerOptions>? configure = null, Action<IServiceCollection>? services = null)
{
var root = context.CreateTempDirectory("server");
var options = new ServerOptions
{
DataPath = Path.Combine(root, "data"),
ArchivePath = Path.Combine(root, "archive"),
HttpPort = 0,
HttpsPort = 0,
TlsMode = TlsMode.Auto,
Database = DatabaseKind.Sqlite,
Discovery = false,
Parallelism = 2,
AdminUser = "admin",
AdminPassword = AdminPassword,
// Tests sign in many times from one address; the rate-limit test sets the production value.
LoginAttemptsPerMinute = 1000,
// Info lines of the ~55 servers the suite starts would bury failures in the CI log.
LogLevel = Microsoft.Extensions.Logging.LogLevel.Warning,
};
Directory.CreateDirectory(options.ArchivePath);
configure?.Invoke(options);
var app = ServerHost.Build(options, [], s =>
{
s.AddSingleton<FakeArchiveIngest>();
s.TryAddSingleton<IArchiveIngest>(sp => sp.GetRequiredService<FakeArchiveIngest>());
s.AddSingleton<FakeAssetLocator>();
s.TryAddSingleton<IFrameAssetLocator>(sp => sp.GetRequiredService<FakeAssetLocator>());
services?.Invoke(s);
});
await app.StartAsync(context.Cancellation);
var addresses = app.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>()!.Addresses
.Select(a => new Uri(a.Replace("[::]", "127.0.0.1", StringComparison.Ordinal).Replace("0.0.0.0", "127.0.0.1", StringComparison.Ordinal)))
.ToList();
var http = addresses.First(a => a.Scheme == "http");
var https = addresses.FirstOrDefault(a => a.Scheme == "https");
return new TestServer(app, options, http, https);
}
/// <summary>A plain HTTP client (owned by the server, disposed with it).</summary>
public HttpClient NewClient(string? token = null)
{
var client = new HttpClient { BaseAddress = HttpBase, Timeout = TimeSpan.FromSeconds(60) };
if (token is not null) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
_clients.Add(client);
return client;
}
/// <summary>An HTTPS client with a cookie jar that trusts the self-signed certificate (admin panel sessions).</summary>
public HttpClient NewCookieClient(CookieContainer cookies)
{
var handler = new HttpClientHandler { CookieContainer = cookies, ServerCertificateCustomValidationCallback = (_, _, _, _) => true };
var client = new HttpClient(handler) { BaseAddress = HttpsBase, Timeout = TimeSpan.FromSeconds(60) };
_clients.Add(client);
return client;
}
public async Task<LoginResponse> LoginAsync(string username, string password, string clientName = "tests")
{
using var response = await Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(username, password, clientName), ProtocolJson.Default.LoginRequest);
if (!response.IsSuccessStatusCode) throw new InvalidOperationException($"Login of {username} failed: {(int)response.StatusCode} {await response.Content.ReadAsStringAsync()}");
return (await response.Content.ReadFromJsonAsync(ProtocolJson.Default.LoginResponse))!;
}
/// <summary>The bootstrap administrator, signed in; its first sign-in replaces the bootstrap password with <see cref="Password"/>.</summary>
public async Task<HttpClient> AdminAsync()
{
var login = await LoginAsync("admin", _adminPassword);
var client = NewClient(login.Token);
if (login.MustChangePassword)
{
using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(_adminPassword, Password),
ProtocolJson.Default.ChangePasswordRequest);
changed.EnsureSuccessStatusCode();
_adminPassword = Password;
}
return client;
}
/// <summary>Creates a user (through the admin API) who has set <see cref="Password"/>, and returns a client signed in as them.</summary>
public async Task<(HttpClient Client, UserDto User, string Token)> UserAsync(string username, string role, long quotaBytes = 0, bool renderNode = false)
{
var admin = await AdminAsync();
using var created = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest(username, null, role, quotaBytes, null, renderNode),
ProtocolJson.Default.CreateUserRequest);
created.EnsureSuccessStatusCode();
var temporary = (await created.Content.ReadFromJsonAsync(ProtocolJson.Default.TemporaryPasswordResponse))!;
var first = await LoginAsync(username, temporary.TemporaryPassword);
var client = NewClient(first.Token);
using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(temporary.TemporaryPassword, Password),
ProtocolJson.Default.ChangePasswordRequest);
changed.EnsureSuccessStatusCode();
return (client, temporary.User, first.Token!);
}
public async ValueTask DisposeAsync()
{
foreach (var client in _clients) client.Dispose();
await _app.StopAsync();
await _app.DisposeAsync();
}
}