|
| 1 | +// -------------------------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="SingleConcurrentActionRunnerTestFixture.cs" company="Starion Group S.A."> |
| 3 | +// Copyright (c) 2015-2025 Starion Group S.A. |
| 4 | +// |
| 5 | +// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary |
| 6 | +// |
| 7 | +// This file is part of CDP4-COMET-IME Community Edition. |
| 8 | +// The CDP4-COMET-IME Community Edition is the Starion Concurrent Design Desktop Application and Excel Integration |
| 9 | +// compliant with ECSS-E-TM-10-25 Annex A and Annex C. |
| 10 | +// |
| 11 | +// The CDP4-COMET-IME Community Edition is free software; you can redistribute it and/or |
| 12 | +// modify it under the terms of the GNU Affero General Public |
| 13 | +// License as published by the Free Software Foundation; either |
| 14 | +// version 3 of the License, or any later version. |
| 15 | +// |
| 16 | +// The CDP4-COMET-IME Community Edition is distributed in the hope that it will be useful, |
| 17 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +// GNU Affero General Public License for more details. |
| 20 | +// |
| 21 | +// You should have received a copy of the GNU Affero General Public License |
| 22 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +// </copyright> |
| 24 | +// -------------------------------------------------------------------------------------------------------------------- |
| 25 | + |
| 26 | +namespace CDP4Composition.Tests.Utilities |
| 27 | +{ |
| 28 | + using System; |
| 29 | + using System.Diagnostics; |
| 30 | + using System.Threading; |
| 31 | + using System.Threading.Tasks; |
| 32 | + |
| 33 | + using CDP4Composition.Utilities; |
| 34 | + |
| 35 | + using NUnit.Framework; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Suite of tests for the <see cref="SingleConcurrentActionRunner"/> class |
| 39 | + /// </summary> |
| 40 | + [TestFixture] |
| 41 | + public class SingleConcurrentActionRunnerTestFixture |
| 42 | + { |
| 43 | + /// <summary> |
| 44 | + /// Polls a condition until it becomes true or the timeout elapses. |
| 45 | + /// </summary> |
| 46 | + private static async Task WaitUntilAsync(Func<bool> condition, int timeoutMilliseconds = 5000) |
| 47 | + { |
| 48 | + var stopwatch = Stopwatch.StartNew(); |
| 49 | + |
| 50 | + while (!condition() && stopwatch.ElapsedMilliseconds < timeoutMilliseconds) |
| 51 | + { |
| 52 | + await Task.Delay(20); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public async Task Verify_that_DelayRunTaskWithInnerCancellationToken_executes_the_func_after_the_delay() |
| 58 | + { |
| 59 | + var runner = new SingleConcurrentActionRunner(); |
| 60 | + var executed = false; |
| 61 | + |
| 62 | + runner.DelayRunTaskWithInnerCancellationToken( |
| 63 | + _ => |
| 64 | + { |
| 65 | + executed = true; |
| 66 | + return Task.CompletedTask; |
| 67 | + }, |
| 68 | + 50); |
| 69 | + |
| 70 | + Assert.That(executed, Is.False, "the func should not be invoked synchronously"); |
| 71 | + |
| 72 | + await WaitUntilAsync(() => executed); |
| 73 | + |
| 74 | + Assert.That(executed, Is.True, "the func should be invoked after the delay"); |
| 75 | + } |
| 76 | + |
| 77 | + [Test] |
| 78 | + public async Task Verify_that_DelayRunTaskWithInnerCancellationToken_passes_a_non_cancelled_token_to_the_func() |
| 79 | + { |
| 80 | + var runner = new SingleConcurrentActionRunner(); |
| 81 | + var wasCancellationRequested = true; |
| 82 | + var executed = false; |
| 83 | + |
| 84 | + runner.DelayRunTaskWithInnerCancellationToken( |
| 85 | + token => |
| 86 | + { |
| 87 | + wasCancellationRequested = token.IsCancellationRequested; |
| 88 | + executed = true; |
| 89 | + return Task.CompletedTask; |
| 90 | + }, |
| 91 | + 50); |
| 92 | + |
| 93 | + await WaitUntilAsync(() => executed); |
| 94 | + |
| 95 | + Assert.That(executed, Is.True); |
| 96 | + Assert.That(wasCancellationRequested, Is.False); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public async Task Verify_that_a_subsequent_call_cancels_the_previously_scheduled_func() |
| 101 | + { |
| 102 | + var runner = new SingleConcurrentActionRunner(); |
| 103 | + var firstExecuted = false; |
| 104 | + var secondExecuted = false; |
| 105 | + |
| 106 | + runner.DelayRunTaskWithInnerCancellationToken( |
| 107 | + _ => |
| 108 | + { |
| 109 | + firstExecuted = true; |
| 110 | + return Task.CompletedTask; |
| 111 | + }, |
| 112 | + 500); |
| 113 | + |
| 114 | + // reschedule before the first delay elapses, which should cancel the first task |
| 115 | + runner.DelayRunTaskWithInnerCancellationToken( |
| 116 | + _ => |
| 117 | + { |
| 118 | + secondExecuted = true; |
| 119 | + return Task.CompletedTask; |
| 120 | + }, |
| 121 | + 50); |
| 122 | + |
| 123 | + await WaitUntilAsync(() => secondExecuted); |
| 124 | + |
| 125 | + // give the first (cancelled) delay enough time to have fired if it was not cancelled |
| 126 | + await Task.Delay(700); |
| 127 | + |
| 128 | + Assert.That(secondExecuted, Is.True, "the most recently scheduled func should be invoked"); |
| 129 | + Assert.That(firstExecuted, Is.False, "the previously scheduled func should have been cancelled"); |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public async Task Verify_that_CancelCurrentTask_prevents_the_func_from_being_executed() |
| 134 | + { |
| 135 | + var runner = new SingleConcurrentActionRunner(); |
| 136 | + var executed = false; |
| 137 | + |
| 138 | + runner.DelayRunTaskWithInnerCancellationToken( |
| 139 | + _ => |
| 140 | + { |
| 141 | + executed = true; |
| 142 | + return Task.CompletedTask; |
| 143 | + }, |
| 144 | + 300); |
| 145 | + |
| 146 | + runner.CancelCurrentTask(); |
| 147 | + |
| 148 | + await Task.Delay(500); |
| 149 | + |
| 150 | + Assert.That(executed, Is.False, "a cancelled task should not invoke the func"); |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void Verify_that_CancelCurrentTask_does_not_throw_when_no_task_is_scheduled() |
| 155 | + { |
| 156 | + var runner = new SingleConcurrentActionRunner(); |
| 157 | + |
| 158 | + Assert.DoesNotThrow(() => runner.CancelCurrentTask()); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments