Add SubscriberCount and improve test to make sure `ReadEventsAsync` works as expected.
MODIFY
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlClientTests.cs
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlClientTests.cs
@@ -4,6 +4,7 @@ using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using WalletWasabi.Logging;
+using WalletWasabi.Tests.Helpers;
using WalletWasabi.Tor.Control;
using WalletWasabi.Tor.Control.Messages;
using Xunit;
@@ -16,6 +17,9 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
[Fact]
public async Task ReceiveTorAsyncEventsUsingForeachAsync()
{
+ Common.GetWorkDir();
+ Logger.SetMinimumLevel(LogLevel.Trace);
+
using CancellationTokenSource timeoutCts = new(TimeSpan.FromSeconds(120));
// Test parameters.
@@ -57,9 +61,15 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
if (counter == ExpectedEventsNo)
{
+ Assert.Equal(1, client.SubscriberCount);
break;
}
}
+
+ // Verifies that "break" in "await foreach" actually removes the internal Channel<T> (subscription).
+ Assert.Equal(0, client.SubscriberCount);
+
+ Logger.LogTrace("Client: Done.");
}
/// <summary>Verifies a correct result of a mix of sync and async messages from Tor.</summary>

MODIFY
WalletWasabi/Tor/Control/TorControlClient.cs
WalletWasabi/Tor/Control/TorControlClient.cs
@@ -67,6 +67,19 @@ namespace WalletWasabi.Tor.Control
/// <remarks>Tor control protocol does not provide a foolproof way to recognize that a response belongs to a request.</remarks>
private AsyncLock MessageLock { get; }
+ /// <summary>Number of subscribers that currently listen to Tor's async events using <see cref="ReadEventsAsync"/>.</summary>
+ /// <remarks>Mainly for tests.</remarks>
+ internal int SubscriberCount
+ {
+ get
+ {
+ lock (AsyncChannelsLock)
+ {
+ return AsyncChannels.Count;
+ }
+ }
+ }
+
/// <summary>
/// Gets protocol info (for version 1).
/// </summary>

Add SubscriberCount and improve test to make sure `ReadEventsAsync` works as expected.