using System.IO; using Titano.App.Infrastructure; using Titano.App.Tests.Uploader; using Titano.Core.Model; using Titano.Data; using Titano.Data.Archiving; using Titano.Testing; namespace Titano.App.Tests.Infrastructure; public sealed class AppServicesTests { /// Dialogs that answer like a user who cancels everything. private sealed class NoDialogs : IAppDialogs { public string? PickFolder(string title, string? initialFolder = null) => null; public string? PickJsonFile(string title) => null; public bool Confirm(ConfirmRequest request) => false; public bool SignIn(string server, string? username, Func> signIn) => false; } [Test] [WindowsOnly] public void ExitStopsPollingAndTheEngineWhileTheUiThreadWaits() { var environment = AppEnvironment.ForCapture(); try { SingleThreadedContext.Run(async () => { var root = Path.Combine(environment.DataDirectory, "archive"); await using (var setup = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(environment.DataDirectory, "setup.db")))) await new ArchiveService(setup).CreateAsync(root, "Exit"); var services = new AppServices(environment, new AppSettings { Language = "en", LastArchivePath = root }, () => { }, new NoDialogs()); await services.OpenAsync(environment.DatabasePath); // The archive was reopened and its first rescan queued: polling now waits for the next one. var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); while ((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items.Count == 0) { Assert.True(DateTime.UtcNow < until, "no rescan queued"); await Task.Delay(50); } // App.OnExit blocks the UI thread until the services stop: nothing they await may continue on it. Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped"); }); } finally { environment.DeleteCaptureData(); } } }