Make tests exact
MODIFY
WalletWasabi.Tests/UnitTests/WabiSabi/Backend/UtxoPrisonWardenTests.cs
WalletWasabi.Tests/UnitTests/WabiSabi/Backend/UtxoPrisonWardenTests.cs
@@ -32,7 +32,7 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
await IoHelpers.TryDeleteDirectoryAsync(workDir);
// Create prison.
- CoordinatorParameters coordinatorParameters = new(workDir) { UtxoWardenPeriod = TimeSpan.FromMilliseconds(1) };
+ CoordinatorParameters coordinatorParameters = new(workDir);
using var w = new Warden(coordinatorParameters.UtxoWardenPeriod, coordinatorParameters.PrisonFilePath, coordinatorParameters.RuntimeCoordinatorConfig);
await w.StartAsync(CancellationToken.None);
var i1 = new Inmate(BitcoinFactory.CreateOutPoint(), Punishment.Noted, DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.UtcNow.ToUnixTimeSeconds()), 1);
@@ -41,11 +41,11 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
w.Prison.Punish(i2);
// Wait until serializes.
- await Task.Delay(50);
+ await w.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(3));
await w.StopAsync(CancellationToken.None);
// See if prev UTXOs are loaded.
- CoordinatorParameters coordinatorParameters2 = new(workDir) { UtxoWardenPeriod = TimeSpan.FromMilliseconds(1) };
+ CoordinatorParameters coordinatorParameters2 = new(workDir);
using var w2 = new Warden(coordinatorParameters2.UtxoWardenPeriod, coordinatorParameters2.PrisonFilePath, coordinatorParameters2.RuntimeCoordinatorConfig);
await w2.StartAsync(CancellationToken.None);
@@ -69,7 +69,7 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
await IoHelpers.TryDeleteDirectoryAsync(workDir);
// Create prison.
- CoordinatorParameters coordinatorParameters = new(workDir) { UtxoWardenPeriod = TimeSpan.FromMilliseconds(1) };
+ CoordinatorParameters coordinatorParameters = new(workDir);
using var w = new Warden(coordinatorParameters.UtxoWardenPeriod, coordinatorParameters.PrisonFilePath, coordinatorParameters.RuntimeCoordinatorConfig);
await w.StartAsync(CancellationToken.None);
var i1 = new Inmate(BitcoinFactory.CreateOutPoint(), Punishment.Noted, DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.UtcNow.ToUnixTimeSeconds()), 1);
@@ -78,11 +78,11 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
w.Prison.Punish(i2);
// Wait until serializes.
- await Task.Delay(50);
+ await w.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(3));
// Make sure it does not serialize again as there was no change.
File.Delete(w.PrisonFilePath);
- await Task.Delay(50);
+ await w.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(3));
Assert.False(File.Exists(w.PrisonFilePath));
await w.StopAsync(CancellationToken.None);
}
@@ -94,7 +94,7 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
await IoHelpers.TryDeleteDirectoryAsync(workDir);
// Create prison.
- CoordinatorParameters coordinatorParameters = new(workDir) { UtxoWardenPeriod = TimeSpan.FromMilliseconds(1) };
+ CoordinatorParameters coordinatorParameters = new(workDir);
coordinatorParameters.RuntimeCoordinatorConfig.ReleaseUtxoFromPrisonAfter = TimeSpan.FromMilliseconds(1);
using var w = new Warden(coordinatorParameters.UtxoWardenPeriod, coordinatorParameters.PrisonFilePath, coordinatorParameters.RuntimeCoordinatorConfig);
@@ -107,7 +107,7 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
Assert.NotEmpty(p.GetInmates());
// Wait until releases from prison.
- await Task.Delay(50);
+ await w.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(3));
Assert.Empty(p.GetInmates());
await w.StopAsync(CancellationToken.None);
}

MODIFY
WalletWasabi.Tests/UnitTests/Wabisabi/Backend/ConfigTests.cs
WalletWasabi.Tests/UnitTests/Wabisabi/Backend/ConfigTests.cs
@@ -96,10 +96,7 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
var workDir = Common.GetWorkDir();
await IoHelpers.TryDeleteDirectoryAsync(workDir);
- var configChangeMonitoringPeriod = TimeSpan.FromMilliseconds(10);
- var configChangeAwaitDuration = TimeSpan.FromMilliseconds(200);
-
- CoordinatorParameters coordinatorParameters = new(workDir) { ConfigChangeMonitoringPeriod = configChangeMonitoringPeriod };
+ CoordinatorParameters coordinatorParameters = new(workDir);
using WabiSabiCoordinator coordinator = new(coordinatorParameters);
await coordinator.StartAsync(CancellationToken.None);
@@ -110,9 +107,8 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
configChanger.ConfirmationTarget = newTarget;
Assert.NotEqual(newTarget, coordinator.Config.ConfirmationTarget);
configChanger.ToFile();
-
- // Leave time for the config change to be picked up.
- await Task.Delay(configChangeAwaitDuration);
+ var configWatcher = coordinator.ConfigWatcher;
+ await configWatcher.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(3));
Assert.Equal(newTarget, coordinator.Config.ConfirmationTarget);
// Do it one more time.
@@ -120,7 +116,7 @@ namespace WalletWasabi.Tests.UnitTests.WabiSabi.Backend
configChanger.ConfirmationTarget = newTarget;
Assert.NotEqual(newTarget, coordinator.Config.ConfirmationTarget);
configChanger.ToFile();
- await Task.Delay(configChangeAwaitDuration);
+ await configWatcher.TriggerAndWaitRoundAsync(TimeSpan.FromSeconds(3));
Assert.Equal(newTarget, coordinator.Config.ConfirmationTarget);
await coordinator.StopAsync(CancellationToken.None);

MODIFY
WalletWasabi/Bases/PeriodicRunner.cs
WalletWasabi/Bases/PeriodicRunner.cs
@@ -20,6 +20,11 @@ namespace WalletWasabi.Bases
ExceptionTracker = new LastExceptionTracker();
}
+ /// <summary>
+ /// Action successfully executed. Returns how long it took.
+ /// </summary>
+ public event EventHandler<TimeSpan>? Tick;
+
public TimeSpan Period { get; }
private LastExceptionTracker ExceptionTracker { get; }
@@ -39,6 +44,18 @@ namespace WalletWasabi.Bases
_tcs?.TrySetResult(true);
}
+ /// <summary>
+ /// Triggers and waits for the action to execute.
+ /// </summary>
+ public async Task TriggerAndWaitRoundAsync(TimeSpan timeout)
+ {
+ EventAwaiter<TimeSpan> eventAwaiter = new(
+ h => Tick += h,
+ h => Tick -= h);
+ TriggerRound();
+ await eventAwaiter.WaitAsync(timeout).ConfigureAwait(false);
+ }
+
/// <summary>
/// Abstract method that is called every <see cref="Period"/> or sooner when <see cref="TriggerRound"/> is called.
/// </summary>
@@ -55,7 +72,9 @@ namespace WalletWasabi.Bases
try
{
// Do user action.
+ var before = DateTimeOffset.UtcNow;
await ActionAsync(stoppingToken).ConfigureAwait(false);
+ Tick?.Invoke(this, DateTimeOffset.UtcNow - before);
ExceptionInfo? info = ExceptionTracker.LastException;

Make tests exact