Proper locking
MODIFY
WalletWasabi.Tests/UnitTests/AsyncMutexTests.cs
WalletWasabi.Tests/UnitTests/AsyncMutexTests.cs
@@ -48,9 +48,9 @@ namespace WalletWasabi.Tests.UnitTests
await Task.Delay(1);
}
- ManualResetEvent locked = new ManualResetEvent(false);
+ using (ManualResetEvent locked = new ManualResetEvent(false))
+ {
// Acquire the Mutex with a background thread.
-
var myTask = Task.Run(async () =>
{
using (await asyncMutex.LockAsync())
@@ -74,9 +74,9 @@ namespace WalletWasabi.Tests.UnitTests
};
Assert.True(myTask.IsCompletedSuccessfully);
-
var elapsed = timeOfAcquired - timeOfstart;
Assert.InRange(elapsed, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(4));
+ }
// Standard Mutex test.
int cnt = 0;
@@ -147,10 +147,11 @@ namespace WalletWasabi.Tests.UnitTests
// Different AsyncMutex object but same name.
AsyncMutex asyncMutex2 = new AsyncMutex(mutexName1);
-
+ using (ManualResetEvent locked = new ManualResetEvent(false))
+ {
locked.Reset();
// Acquire the first mutex with a background thread and hold it for a while.
- var myTask2 = Task.Run(async () =>
+ var myTask = Task.Run(async () =>
{
using (await asyncMutex.LockAsync())
{
@@ -162,20 +163,21 @@ namespace WalletWasabi.Tests.UnitTests
// Make sure the task started.
Assert.True(locked.WaitOne(TimeSpan.FromSeconds(5)));
- timeOfstart = DateTime.Now;
- timeOfAcquired = default;
+ var timeOfstart = DateTime.Now;
+ DateTime timeOfAcquired = default;
// Now try to acquire another AsyncMutex object but with the same name!
using (await asyncMutex2.LockAsync())
{
timeOfAcquired = DateTime.Now;
}
- await myTask2;
- Assert.True(myTask2.IsCompletedSuccessfully);
+ await myTask;
+ Assert.True(myTask.IsCompletedSuccessfully);
- elapsed = timeOfAcquired - timeOfstart;
+ var elapsed = timeOfAcquired - timeOfstart;
Assert.InRange(elapsed, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(4));
}
}
}
+}
;

Proper locking