test: Fix flushing in tests.
MODIFY
WalletWasabi.Tests/UnitTests/Tor/Control/PipeWriterExtensions.cs
WalletWasabi.Tests/UnitTests/Tor/Control/PipeWriterExtensions.cs
@@ -13,9 +13,12 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
return writer.WriteAsync(new ReadOnlyMemory<byte>(encoding.GetBytes(data)), cancellationToken);
}
- public static ValueTask<FlushResult> WriteAsciiAsync(this PipeWriter writer, string data, CancellationToken cancellationToken = default)
+ public static ValueTask WriteAsciiAndFlushAsync(this PipeWriter writer, string data, CancellationToken cancellationToken = default)
{
- return writer.WriteAsync(new ReadOnlyMemory<byte>(Encoding.ASCII.GetBytes(data)), cancellationToken);
+ _ = writer.WriteAsync(new ReadOnlyMemory<byte>(Encoding.ASCII.GetBytes(data)), cancellationToken);
+ _ = writer.FlushAsync(cancellationToken).ConfigureAwait(false);
+
+ return ValueTask.CompletedTask;
}
}
}

MODIFY
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlClientTests.cs
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlClientTests.cs
@@ -16,10 +16,10 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
[Fact]
public async Task ReceiveTorAsyncEventsUsingForeachAsync()
{
- using CancellationTokenSource timeoutCts = new(TimeSpan.FromMinutes(3));
+ using CancellationTokenSource timeoutCts = new(TimeSpan.FromMinutes(4));
// Test parameters.
- const int ExpectedEventsNo = 5;
+ const int ExpectedEventsNo = 3;
const string AsyncEventContent = "CIRC 1000 EXTENDED moria1,moria2";
Pipe toServer = new();
@@ -38,7 +38,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
for (int i = 0; i < ExpectedEventsNo; i++)
{
Logger.LogTrace($"Server: Send async Tor event (#{i}): '650 {AsyncEventContent}'.");
- await toClient.Writer.WriteAsciiAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
+ await toClient.Writer.WriteAsciiAndFlushAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
}
});
@@ -91,20 +91,20 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Task serverTask = Task.Run(async () =>
{
Logger.LogTrace($"Server: Send msg #1 (async) to client: '650 {AsyncEventContent}'.");
- await toClient.Writer.WriteAsciiAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
+ await toClient.Writer.WriteAsciiAndFlushAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
Logger.LogTrace($"Server: Send msg #2 (async) to client: '650 {AsyncEventContent}'.");
- await toClient.Writer.WriteAsciiAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
+ await toClient.Writer.WriteAsciiAndFlushAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
Logger.LogTrace("Server: Wait for TAKEOWNERSHIP command.");
string command = await toServer.Reader.ReadLineAsync(timeoutCts.Token).ConfigureAwait(false);
Assert.Equal("TAKEOWNERSHIP", command);
Logger.LogTrace("Server: Send msg #3 (sync) to client in response to TAKEOWNERSHIP command.");
- await toClient.Writer.WriteAsciiAsync($"250 OK\r\n", timeoutCts.Token).ConfigureAwait(false);
+ await toClient.Writer.WriteAsciiAndFlushAsync($"250 OK\r\n", timeoutCts.Token).ConfigureAwait(false);
Logger.LogTrace($"Server: Send msg #4 (async) to client: '650 {AsyncEventContent}'.");
- await toClient.Writer.WriteAsciiAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
+ await toClient.Writer.WriteAsciiAndFlushAsync($"650 {AsyncEventContent}\r\n", timeoutCts.Token).ConfigureAwait(false);
});
Logger.LogTrace("Client: Receive msg #1 (async).");
@@ -166,7 +166,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Assert.Equal("SETEVENTS CIRC", command);
Logger.LogTrace("Server: Reply with OK code.");
- await toClient.Writer.WriteAsciiAsync("250 OK\r\n", timeoutCts.Token);
+ await toClient.Writer.WriteAsciiAndFlushAsync("250 OK\r\n", timeoutCts.Token);
await task;
}
@@ -184,7 +184,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Assert.Equal("SETEVENTS CIRC,STATUS_CLIENT", command);
Logger.LogTrace("Server: Reply with OK code.");
- await toClient.Writer.WriteAsciiAsync("250 OK\r\n", timeoutCts.Token);
+ await toClient.Writer.WriteAsciiAndFlushAsync("250 OK\r\n", timeoutCts.Token);
await task;
}
@@ -202,7 +202,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Assert.Equal("SETEVENTS CIRC", command);
Logger.LogTrace("Server: Reply with OK code.");
- await toClient.Writer.WriteAsciiAsync("250 OK\r\n", timeoutCts.Token);
+ await toClient.Writer.WriteAsciiAndFlushAsync("250 OK\r\n", timeoutCts.Token);
await task;
}
@@ -219,7 +219,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Assert.Equal("SETEVENTS", command);
Logger.LogTrace("Server: Reply with OK code.");
- await toClient.Writer.WriteAsciiAsync("250 OK\r\n", timeoutCts.Token);
+ await toClient.Writer.WriteAsciiAndFlushAsync("250 OK\r\n", timeoutCts.Token);
await task;
}

MODIFY
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlFactoryTests.cs
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlFactoryTests.cs
@@ -50,7 +50,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Logger.LogTrace("Server: Respond to client's AUTHCHALLENGE request.");
string challengeResponse = $"250 AUTHCHALLENGE SERVERHASH={serverHash} SERVERNONCE={serverNonce}\r\n";
- await toClient.Writer.WriteAsciiAsync(challengeResponse, timeoutCts.Token);
+ await toClient.Writer.WriteAsciiAndFlushAsync(challengeResponse, timeoutCts.Token);
Logger.LogTrace("Server: Read 'AUTHENTICATE' command from the client.");
string authCommand = await toServer.Reader.ReadLineAsync(timeoutCts.Token);
@@ -59,7 +59,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
Assert.Equal("AUTHENTICATE 6013EA09D4E36B6CF01C18A707D350C1B5AFF8C1A21527266B9FC40C89BDCB4A", authCommand);
Logger.LogTrace("Server: Respond to the client's AUTHENTICATION request.");
- await toClient.Writer.WriteAsciiAsync("250 OK\r\n", timeoutCts.Token);
+ await toClient.Writer.WriteAsciiAndFlushAsync("250 OK\r\n", timeoutCts.Token);
Logger.LogTrace("Client: Verify the authentication task finishes correctly.");
TorControlClient authenticatedClient = await authenticationTask;

MODIFY
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlReplyReaderTests.cs
WalletWasabi.Tests/UnitTests/Tor/Control/TorControlReplyReaderTests.cs
@@ -69,8 +69,7 @@ namespace WalletWasabi.Tests.UnitTests.Tor.Control
using CancellationTokenSource timeoutCts = new(TimeSpan.FromMinutes(1));
Pipe pipe = new();
- await pipe.Writer.WriteAsciiAsync(data, timeoutCts.Token);
- await pipe.Writer.FlushAsync(timeoutCts.Token);
+ await pipe.Writer.WriteAsciiAndFlushAsync(data, timeoutCts.Token);
await pipe.Writer.CompleteAsync();
return await TorControlReplyReader.ReadReplyAsync(pipe.Reader, timeoutCts.Token);
}

MODIFY
WalletWasabi/Tor/Control/TorControlClient.cs
WalletWasabi/Tor/Control/TorControlClient.cs
@@ -204,7 +204,7 @@ namespace WalletWasabi.Tor.Control
Logger.LogTrace($"ReadEventsAsync: subscribers: {newList.Count}.");
- await foreach (TorControlReply item in channel.Reader.ReadAllAsync(cancellationToken))
+ await foreach (TorControlReply item in channel.Reader.ReadAllAsync(cancellationToken).ConfigureAwait(false))
{
yield return item;
}

MODIFY
WalletWasabi/Tor/Control/Utils/AsyncEventParser.cs
WalletWasabi/Tor/Control/Utils/AsyncEventParser.cs
@@ -6,6 +6,9 @@ using WalletWasabi.Tor.Control.Messages.Events.StatusEvents;
namespace WalletWasabi.Tor.Control.Utils
{
+ /// <summary>
+ /// Parses an incoming Tor control event based on its name.
+ /// </summary>
public static class AsyncEventParser
{
/// <exception cref="TorControlReplyParseException"/>

test: Fix flushing in tests.