From 64d40ef0ea9f552d1fab026c8336342b013cbc81 Mon Sep 17 00:00:00 2001 From: "Marcelo C." Date: Wed, 12 Mar 2025 10:40:20 -0300 Subject: [PATCH] Fix to StopNow tests Since the core changes in the cancelation process where now cancellationTokens are observed rather than a thread.abort attempt, the expected exception is now OperationCanceledException when calling .Abort on the controller. Also eliminated the time based awaiting to abort in favor of monitoring the backup phase to ensure the .Abort happens after the controller has had time to initiate the backend and backend is at Backup_ProcessingFiles state. --- Duplicati/UnitTest/DisruptionTests.cs | 39 ++++++++++++++++++--------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Duplicati/UnitTest/DisruptionTests.cs b/Duplicati/UnitTest/DisruptionTests.cs index 6e27637bf..b3b49d605 100644 --- a/Duplicati/UnitTest/DisruptionTests.cs +++ b/Duplicati/UnitTest/DisruptionTests.cs @@ -708,23 +708,38 @@ namespace Duplicati.UnitTest this.ModifySourceFiles(); using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) { - // ReSharper disable once AccessToDisposedClosure - Task backupTask = Task.Run(() => c.Backup(new[] { this.DATAFOLDER })); - - // Block for a small amount of time to allow the ITaskControl to be associated - // with the Controller. Otherwise, the call to Stop will simply be a no-op. - Thread.Sleep(1000); - - c.Abort(); - try + var startedTcs = new TaskCompletionSource(); + c.OnOperationStarted += results => { - await backupTask.ConfigureAwait(false); + ((BackupResults)results).OperationProgressUpdater.PhaseChanged += (phase, previousPhase) => + { + if (phase == OperationPhase.Backup_ProcessingFiles) + startedTcs.SetResult(true); + }; + }; + + Task backupTask = Task.Run(() => c.Backup([DATAFOLDER])); + + Task completedTask = await Task.WhenAny(startedTcs.Task, Task.Delay(5000)); + if (completedTask == startedTcs.Task) + { + c.Abort(); + try + { + await backupTask.ConfigureAwait(false); + Assert.Fail("Expected OperationCanceledException but none was thrown."); + } + catch (OperationCanceledException) + { + // Success, exception thrown as expected + } } - catch (TaskCanceledException) + else { + Assert.Fail("Operation did not reach ready state within timeout."); } } - + // The next backup should proceed without issues. using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null)) {