Make mempool integration test a unit test.
MODIFY
WalletWasabi.Tests/IntegrationTests/RegTests.cs
WalletWasabi.Tests/IntegrationTests/RegTests.cs
@@ -229,53 +229,6 @@ namespace WalletWasabi.Tests.IntegrationTests
#region ServicesTests
- [Fact]
- public async Task MempoolAsync()
- {
- (string password, RPCClient rpc, Network network, Coordinator coordinator, ServiceConfiguration serviceConfiguration, BitcoinStore bitcoinStore, Backend.Global global) = await InitializeTestEnvironmentAsync(1);
-
- Node node = await RegTestFixture.BackendRegTestNode.CreateP2pNodeAsync();
- node.Behaviors.Add(bitcoinStore.CreateMempoolBehavior());
- node.VersionHandshake();
-
- try
- {
- bitcoinStore.MempoolService.TransactionReceived += MempoolAsync_MempoolService_TransactionReceived;
-
- // Using the interlocked, not because it makes sense in this context, but to
- // set an example that these values are often concurrency sensitive
- for (int i = 0; i < 10; i++)
- {
- var addr = await rpc.GetNewAddressAsync();
- var res = await rpc.SendToAddressAsync(addr, Money.Coins(0.01m));
- Assert.NotNull(res);
- }
-
- var times = 0;
- while (Interlocked.Read(ref _mempoolTransactionCount) < 10)
- {
- if (times > 100) // 10 seconds
- {
- throw new TimeoutException($"{nameof(MempoolService)} test timed out.");
- }
- await Task.Delay(100);
- times++;
- }
- }
- finally
- {
- bitcoinStore.MempoolService.TransactionReceived -= MempoolAsync_MempoolService_TransactionReceived;
- }
- }
-
- private long _mempoolTransactionCount = 0;
-
- private void MempoolAsync_MempoolService_TransactionReceived(object sender, SmartTransaction e)
- {
- Interlocked.Increment(ref _mempoolTransactionCount);
- Logger.LogDebug($"Mempool transaction received: {e.GetHash()}.");
- }
-
[Fact]
public async Task FilterDownloaderTestAsync()
{

ADD
WalletWasabi.Tests/UnitTests/BitcoinCore/P2pTests.cs
+using NBitcoin;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using WalletWasabi.Helpers;
+using WalletWasabi.Models;
+using WalletWasabi.Stores;
+using WalletWasabi.Tests.NodeBuilding;
+using WalletWasabi.Transactions;
+using Xunit;
+
+namespace WalletWasabi.Tests.UnitTests.BitcoinCore
+{
+ public class P2pTests
+ {
+ [Fact]
+ public async Task MempoolWorksAsync()
+ {
+ var coreNode = await CoreNode.CreateAsync();
+ using var node = await coreNode.CreateP2pNodeAsync();
+ try
+ {
+ var rpc = coreNode.RpcClient;
+ await rpc.GenerateAsync(101);
+
+ var network = rpc.Network;
+ var bitcoinStore = new BitcoinStore();
+ var dir = Path.Combine(Global.Instance.DataDir, EnvironmentHelpers.GetMethodName());
+ await bitcoinStore.InitializeAsync(dir, network);
+
+ node.Behaviors.Add(bitcoinStore.CreateMempoolBehavior());
+ node.VersionHandshake();
+
+ var addr = await rpc.GetNewAddressAsync();
+
+ for (int i = 0; i < 10; i++)
+ {
+ var valueChangedEventAwaiter = new EventAwaiter<SmartTransaction>(
+ h => bitcoinStore.MempoolService.TransactionReceived += h,
+ h => bitcoinStore.MempoolService.TransactionReceived -= h);
+
+ var txid = await rpc.SendToAddressAsync(addr, Money.Coins(1));
+ Assert.NotNull(txid);
+
+ using var cts = new CancellationTokenSource(1000);
+ var stx = await valueChangedEventAwaiter.Task.WithCancellation(cts.Token);
+
+ Assert.Equal(txid, stx.GetHash());
+ }
+ }
+ finally
+ {
+ node.Disconnect();
+ await coreNode.StopAsync();
+ }
+ }
+ }
+}

ADD
WalletWasabi/Helpers/EventAwaiter.cs
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace System
+{
+ /// <summary>
+ /// https://stackoverflow.com/a/42117130/2061103
+ /// </summary>
+ public class EventAwaiter<TEventArgs>
+ {
+ private TaskCompletionSource<TEventArgs> EventArrived { get; }
+
+ private readonly Action<EventHandler<TEventArgs>> Unsubscribe;
+
+ public EventAwaiter(Action<EventHandler<TEventArgs>> subscribe, Action<EventHandler<TEventArgs>> unsubscribe)
+ {
+ EventArrived = new TaskCompletionSource<TEventArgs>();
+ subscribe(Subscription);
+ Unsubscribe = unsubscribe;
+ }
+
+ public Task<TEventArgs> Task => EventArrived.Task;
+
+ private EventHandler<TEventArgs> Subscription => (s, e) =>
+ {
+ EventArrived.TrySetResult(e);
+ Unsubscribe(Subscription);
+ };
+ }
+}

Make mempool integration test a unit test.