Remove delegate and use moq to replace implementation of SendAsyncCore.
MODIFY
WalletWasabi.Tests/UnitTests/Tor/Socks5/TorHttpPoolTests.cs
WalletWasabi.Tests/UnitTests/Tor/Socks5/TorHttpPoolTests.cs
@@ -40,7 +40,11 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Socks5
_ = mockTcpConnectionFactory.Setup(c => c.ConnectAsync(It.IsAny<Uri>(), defaultIdentity, It.IsAny<CancellationToken>())).ReturnsAsync(defaultConnection);
TorTcpConnectionFactory tcpConnectionFactory = mockTcpConnectionFactory.Object;
- using TorHttpPool pool = new(tcpConnectionFactory, (TorTcpConnection tcpConnection, HttpRequestMessage request, CancellationToken cancellationToken) =>
+
+ // Use implementation of TorHttpPool and only replace SendCoreAsync behavior.
+ Mock<TorHttpPool> mockTorHttpPool = new(MockBehavior.Loose, tcpConnectionFactory) { CallBase = true };
+ mockTorHttpPool.Setup(x => x.SendCoreAsync(It.IsAny<TorTcpConnection>(), It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
+ .Returns((TorTcpConnection tcpConnection, HttpRequestMessage request, CancellationToken cancellationToken) =>
{
HttpResponseMessage httpResponse = new(HttpStatusCode.OK);
@@ -64,6 +68,8 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Socks5
return Task.FromResult(httpResponse);
});
+ using TorHttpPool pool = mockTorHttpPool.Object;
+
using HttpRequestMessage request = new(HttpMethod.Get, "http://wasabi.backend");
using HttpResponseMessage aliceResponse = await pool.SendAsync(request, aliceIdentity);

MODIFY
WalletWasabi/AssemblyInfo.cs
WalletWasabi/AssemblyInfo.cs
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("WalletWasabi.Tests")]
+
+// For moq access to internals of this project.
+[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
\ No newline at end of file

MODIFY
WalletWasabi/Tor/Socks5/Pool/TorHttpPool.cs
WalletWasabi/Tor/Socks5/Pool/TorHttpPool.cs
@@ -30,21 +30,15 @@ namespace WalletWasabi.Tor.Socks5.Pool
/// <remarks>This parameter affects maximum parallelization for given URI host.</remarks>
public const int MaxConnectionsPerHost = 10;
- public delegate Task<HttpResponseMessage> SendCoreDelegateAsync(TorTcpConnection connection, HttpRequestMessage request, CancellationToken token = default);
-
- /// <seealso cref="SendCoreAsync(TorTcpConnection, HttpRequestMessage, CancellationToken)"/>
- private readonly SendCoreDelegateAsync _sendCoreDelegateAsync;
-
public TorHttpPool(EndPoint endpoint)
: this(new TorTcpConnectionFactory(endpoint))
{
}
/// <summary>Constructor that helps in tests.</summary>
- internal TorHttpPool(TorTcpConnectionFactory tcpConnectionFactory, SendCoreDelegateAsync? sendCoreDelegate = null)
+ internal TorHttpPool(TorTcpConnectionFactory tcpConnectionFactory)
{
TcpConnectionFactory = tcpConnectionFactory;
- _sendCoreDelegateAsync = sendCoreDelegate ?? SendCoreAsync;
}
private bool _disposedValue;
@@ -112,7 +106,7 @@ namespace WalletWasabi.Tor.Socks5.Pool
try
{
Logger.LogTrace($"['{connection}'][Attempt #{i}] About to send request.");
- HttpResponseMessage response = await _sendCoreDelegateAsync(connection, request, cancellationToken).ConfigureAwait(false);
+ HttpResponseMessage response = await SendCoreAsync(connection, request, cancellationToken).ConfigureAwait(false);
// Client works OK, no need to dispose.
connectionToDispose = null;
@@ -255,7 +249,7 @@ namespace WalletWasabi.Tor.Socks5.Pool
return connection;
}
- private async static Task<HttpResponseMessage> SendCoreAsync(TorTcpConnection connection, HttpRequestMessage request, CancellationToken token = default)
+ internal virtual async Task<HttpResponseMessage> SendCoreAsync(TorTcpConnection connection, HttpRequestMessage request, CancellationToken token = default)
{
// https://tools.ietf.org/html/rfc7230#section-2.6
// Intermediaries that process HTTP messages (i.e., all intermediaries

Remove delegate and use moq to replace implementation of SendAsyncCore.