using System.Diagnostics;
using Titano.App.DesignTime;
using Titano.App.Infrastructure;
using Titano.App.Tests.Uploader;
using Titano.Testing;
namespace Titano.App.Tests.Server;
public sealed class SyncHostTests
{
/// 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;
}
///
/// DESK-1: a signed-in user whose archive is not polled (no archive, or offline at start) exits. Nothing before the
/// sync agent's stop leaves the UI thread, and the agent stops asynchronously: its continuation must not need the UI
/// thread that App.OnExit blocks, or the exit waits 10 s and never closes the engine and the index.
///
[Test]
[WindowsOnly]
public void ExitWithASignedInSessionAndNoPollingDoesNotWaitForTheUiThread()
{
var environment = AppEnvironment.ForCapture();
try
{
SingleThreadedContext.Run(async () =>
{
var services = new AppServices(environment, new AppSettings { Language = "en" }, () => { }, new NoDialogs());
var sample = SampleData.Configure(services);
// An index without the archive poller (OpenAsync never ran) and a saved session, as for a server-only user.
await sample.OpenEmptyAsync();
await sample.FillAsync();
services.Settings.RenderNodeOptIn = false;
await services.Server.RestoreAsync();
Assert.True(services.Server.IsConnected, "session restored");
var until = DateTime.UtcNow + TimeSpan.FromSeconds(10);
while (services.Sync.Agent is null)
{
Assert.True(DateTime.UtcNow < until, "sync agent not started");
await Task.Delay(20);
}
// App.OnExit blocks the UI thread until the services stop.
var clock = Stopwatch.StartNew();
Assert.True(services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)), "services stopped");
Assert.Less(clock.Elapsed.TotalSeconds, 5, "the exit waited for the UI thread");
Assert.Null(services.Sync.Agent);
});
}
finally
{
environment.DeleteCaptureData();
}
}
}