Stabilize test
MODIFY
WalletWasabi.Tests/UnitTests/Clients/SingleInstanceCheckerTests.cs
WalletWasabi.Tests/UnitTests/Clients/SingleInstanceCheckerTests.cs
@@ -39,18 +39,18 @@ namespace WalletWasabi.Tests.UnitTests.Clients
await using (SingleInstanceChecker sic = new(mainNetPort))
{
await sic.EnsureSingleOrThrowAsync();
- await Assert.ThrowsAsync<InvalidOperationException>(async () => await sic.EnsureSingleOrThrowAsync());
+ await Assert.ThrowsAsync<OperationCanceledException>(async () => await sic.EnsureSingleOrThrowAsync());
await using SingleInstanceChecker sicMainNet2 = new(mainNetPort);
- await Assert.ThrowsAsync<InvalidOperationException>(async () => await sicMainNet2.EnsureSingleOrThrowAsync());
+ await Assert.ThrowsAsync<OperationCanceledException>(async () => await sicMainNet2.EnsureSingleOrThrowAsync());
await using SingleInstanceChecker sicTestNet = new(testNetPort);
await sicTestNet.EnsureSingleOrThrowAsync();
- await Assert.ThrowsAsync<InvalidOperationException>(async () => await sicTestNet.EnsureSingleOrThrowAsync());
+ await Assert.ThrowsAsync<OperationCanceledException>(async () => await sicTestNet.EnsureSingleOrThrowAsync());
await using SingleInstanceChecker sicRegTest = new(regTestPort);
await sicRegTest.EnsureSingleOrThrowAsync();
- await Assert.ThrowsAsync<InvalidOperationException>(async () => await sicRegTest.EnsureSingleOrThrowAsync());
+ await Assert.ThrowsAsync<OperationCanceledException>(async () => await sicRegTest.EnsureSingleOrThrowAsync());
}
}
@@ -80,13 +80,14 @@ namespace WalletWasabi.Tests.UnitTests.Clients
// Wait for the OtherInstanceStarted event to finish.
using CancellationTokenSource cts = new(TimeSpan.FromSeconds(5));
- while (!cts.IsCancellationRequested)
- {
+
while (Interlocked.Read(ref eventCalled) != 3)
{
cts.Token.ThrowIfCancellationRequested();
}
- }
+
+ // There should be the same number of events as the number of tries from the second instance.
+ Assert.Equal(3, Interlocked.Read(ref eventCalled));
}
finally
{

MODIFY
WalletWasabi/Services/SingleInstanceChecker.cs
WalletWasabi/Services/SingleInstanceChecker.cs
@@ -65,14 +65,22 @@ namespace WalletWasabi.Services
Logger.LogDebug("Detected another Wasabi instance.");
}
+ try
+ {
// Signal to the other instance, that there was an attempt to start the software.
using TcpClient client = new TcpClient();
await client.ConnectAsync(IPAddress.Loopback, Port).ConfigureAwait(false);
-
+ // I was able to signal to the other instance successfully so just continue.
+ }
+ catch (Exception)
+ {
// Do not log anything here as the first instance is writing the Log at this time.
throw new InvalidOperationException($"Wasabi is already running.");
}
+ throw new OperationCanceledException($"Wasabi is already running, signalled the first instance.");
+ }
+
private static int NetworkToPort(Network network)
{
if (network == Network.Main)

Stabilize test