Fix cancellation issue.
MODIFY
WalletWasabi.Tests/UnitTests/BitcoinCore/P2pBasedTests.cs
WalletWasabi.Tests/UnitTests/BitcoinCore/P2pBasedTests.cs
@@ -36,18 +36,27 @@ namespace WalletWasabi.Tests.UnitTests.BitcoinCore
var addr = new Key().PubKey.GetSegwitAddress(network);
- for (int i = 0; i < 10; i++)
- {
- var eventAwaiter = new EventAwaiter<SmartTransaction>(
+ var txNum = 10;
+ var eventAwaiter = new EventsAwaiter<SmartTransaction>(
h => bitcoinStore.MempoolService.TransactionReceived += h,
- h => bitcoinStore.MempoolService.TransactionReceived -= h);
+ h => bitcoinStore.MempoolService.TransactionReceived -= h,
+ txNum);
- var txid = await rpc.SendToAddressAsync(addr, Money.Coins(1));
- Assert.NotNull(txid);
+ var txTasks = new List<Task<uint256>>();
+ var batch = rpc.PrepareBatch();
+ for (int i = 0; i < txNum; i++)
+ {
+ txTasks.Add(batch.SendToAddressAsync(addr, Money.Coins(1)));
+ }
+ var batchTask = batch.SendBatchAsync();
- var stx = await eventAwaiter.Task.WithAwaitCancellationAsync(1000);
+ var stxs = await Task.WhenAll(eventAwaiter.Tasks).WithAwaitCancellationAsync(TimeSpan.FromSeconds(7));
- Assert.Equal(txid, stx.GetHash());
+ await batchTask;
+ var hashes = await Task.WhenAll(txTasks);
+ foreach (var stx in stxs)
+ {
+ Assert.Contains(stx.GetHash(), hashes);
}
}
finally

MODIFY
WalletWasabi/Extensions/TaskExtensions.cs
WalletWasabi/Extensions/TaskExtensions.cs
@@ -68,15 +68,15 @@ namespace System.Threading.Tasks
}
public static async Task<T> WithAwaitCancellationAsync<T>(this Task<T> me, int millisecondsDelay)
- => await me.WithAwaitCancellationAsync(CancellationToken.None, millisecondsDelay).ConfigureAwait(false);
+ => await me.WithAwaitCancellationAsync(ThreadingHelpers.Cancelled, millisecondsDelay).ConfigureAwait(false);
public static async Task<T> WithAwaitCancellationAsync<T>(this Task<T> me, TimeSpan timeout)
- => await me.WithAwaitCancellationAsync(CancellationToken.None, (int)timeout.TotalMilliseconds).ConfigureAwait(false);
+ => await me.WithAwaitCancellationAsync(ThreadingHelpers.Cancelled, (int)timeout.TotalMilliseconds).ConfigureAwait(false);
public static async Task WithAwaitCancellationAsync(this Task me, int millisecondsDelay)
- => await me.WithAwaitCancellationAsync(CancellationToken.None, millisecondsDelay).ConfigureAwait(false);
+ => await me.WithAwaitCancellationAsync(ThreadingHelpers.Cancelled, millisecondsDelay).ConfigureAwait(false);
public static async Task WithAwaitCancellationAsync(this Task me, TimeSpan timeout)
- => await me.WithAwaitCancellationAsync(CancellationToken.None, (int)timeout.TotalMilliseconds).ConfigureAwait(false);
+ => await me.WithAwaitCancellationAsync(ThreadingHelpers.Cancelled, (int)timeout.TotalMilliseconds).ConfigureAwait(false);
}
}

ADD
WalletWasabi/Helpers/ThreadingHelpers.cs
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace System.Threading
+{
+ public static class ThreadingHelpers
+ {
+ public static CancellationToken Cancelled
+ {
+ get
+ {
+ if (!CancelledBacking.HasValue)
+ {
+ using var cts = new CancellationTokenSource();
+ cts.Cancel();
+ CancelledBacking = cts.Token;
+ }
+ return CancelledBacking.Value;
+ }
+ }
+
+ private static CancellationToken? CancelledBacking { get; set; } = null;
+ }
+}

Fix cancellation issue.