using Chronos.App.Server; using Chronos.Core.Model; using Chronos.Testing; namespace Chronos.App.Tests.Server; /// /// The local detection that runs by itself (owner review 6, #73; design ยง3.4): once after the index opened, after a job that /// changes photos ended, one at a time, and the requests that arrive meanwhile coalesce into one more. /// public sealed class CopyWatchTests { private static readonly TimeSpan Deadline = TimeSpan.FromSeconds(30); private static readonly CopyWatchOptions Quick = new() { StartDelay = TimeSpan.FromMilliseconds(20), AfterJobDelay = TimeSpan.FromMilliseconds(50) }; private static Job Ended(JobKind kind, JobState state = JobState.Done) => new() { Id = Guid.CreateVersion7(), Kind = kind, State = state, Created = DateTimeOffset.UtcNow, }; private static async Task WaitAsync(Func condition, string what) { var until = DateTime.UtcNow + Deadline; while (!condition()) { if (DateTime.UtcNow > until) Assert.Fail($"timed out waiting for {what}"); await Task.Delay(10); } } [Test] public async Task ItLooksOnceAfterStartAndAfterAJobThatChangesPhotos() { var runs = 0; await using var watch = new CopyWatch(_ => { Interlocked.Increment(ref runs); return Task.CompletedTask; }, Quick); watch.Start(null); await WaitAsync(() => watch.Runs == 1, "the first detection"); watch.OnJobChanged(Ended(JobKind.Render)); watch.OnJobChanged(Ended(JobKind.Scan, JobState.Running)); await Task.Delay(200); Assert.Equal(1, watch.Runs, "a render, or a scan still running, asks for none"); watch.OnJobChanged(Ended(JobKind.Scan)); watch.OnJobChanged(Ended(JobKind.Import, JobState.Failed)); watch.OnJobChanged(Ended(JobKind.MergeSequences, JobState.Cancelled)); await WaitAsync(() => watch.Runs == 2, "the detection after the jobs"); await Task.Delay(200); Assert.Equal(2, Volatile.Read(ref runs), "jobs that end close together ask for one detection"); } [Test] public async Task RequestsWhileOneRunsGiveExactlyOneMore() { var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var running = 0; var overlapped = false; await using var watch = new CopyWatch(async _ => { if (Interlocked.Increment(ref running) > 1) overlapped = true; await release.Task; Interlocked.Decrement(ref running); }, Quick with { StartDelay = TimeSpan.Zero, AfterJobDelay = TimeSpan.Zero }); watch.Start(null); await WaitAsync(() => Volatile.Read(ref running) == 1, "the first detection to start"); foreach (var kind in CopyWatch.Triggers) watch.OnJobChanged(Ended(kind)); await Task.Delay(100); release.TrySetResult(); await WaitAsync(() => watch.Runs == 2, "the one more detection"); await Task.Delay(200); Assert.Equal(2, watch.Runs); Assert.False(overlapped, "never two at once"); } [Test] public async Task AFailedDetectionDoesNotStopTheWatch() { var calls = 0; await using var watch = new CopyWatch(_ => Interlocked.Increment(ref calls) == 1 ? throw new InvalidOperationException("index busy") : Task.CompletedTask, Quick); watch.Start(null); await WaitAsync(() => watch.Runs == 1, "the failed detection"); watch.OnJobChanged(Ended(JobKind.Download)); await WaitAsync(() => watch.Runs == 2, "the next detection"); Assert.Equal(2, Volatile.Read(ref calls)); } }