Make tests async
MODIFY
MagicalCryptoWallet.Tests/ClientSideFilter/PersistenceTest.cs
MagicalCryptoWallet.Tests/ClientSideFilter/PersistenceTest.cs
@@ -11,14 +11,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class PersistenceTest
+ public class PersistenceTest : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public PersistenceTest(SharedFixture fixture)
+ public PersistenceTest(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
[Fact]

MODIFY
MagicalCryptoWallet.Tests/ClientSideFilter/StoreTest.cs
MagicalCryptoWallet.Tests/ClientSideFilter/StoreTest.cs
@@ -8,14 +8,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class StoreTest
+ public class StoreTest : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public StoreTest(SharedFixture fixture)
+ public StoreTest(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
[Fact]

MODIFY
MagicalCryptoWallet.Tests/CryptoTests.cs
MagicalCryptoWallet.Tests/CryptoTests.cs
@@ -8,8 +8,7 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class CryptoTests
+ public class CryptoTests : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }

MODIFY
MagicalCryptoWallet.Tests/ExternalApiTests.cs
MagicalCryptoWallet.Tests/ExternalApiTests.cs
@@ -14,8 +14,15 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- public class ExternalApiTests
+ public class ExternalApiTests : IClassFixture<SharedFixture>
{
+ public SharedFixture SharedFixture { get; }
+
+ public ExternalApiTests(SharedFixture sharedFixture)
+ {
+ SharedFixture = sharedFixture;
+ }
+
[Theory]
[InlineData("test")]
[InlineData("main")]

MODIFY
MagicalCryptoWallet.Tests/KeyManagementTests.cs
MagicalCryptoWallet.Tests/KeyManagementTests.cs
@@ -9,14 +9,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class KeyManagementTests
+ public class KeyManagementTests : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public KeyManagementTests(SharedFixture fixture)
+ public KeyManagementTests(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
[Fact]

MODIFY
MagicalCryptoWallet.Tests/P2pTests.cs
MagicalCryptoWallet.Tests/P2pTests.cs
@@ -19,14 +19,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class P2pTests
+ public class P2pTests : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public P2pTests(SharedFixture fixture)
+ public P2pTests(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
[Theory]

RENAME
MagicalCryptoWallet.Tests/SharedCollection.cs
MagicalCryptoWallet.Tests/RegTestCollection.cs
@@ -5,8 +5,8 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [CollectionDefinition("Shared collection")]
- public class SharedCollection : ICollectionFixture<SharedFixture>
+ [CollectionDefinition("RegTest collection")]
+ public class RegTestCollection : ICollectionFixture<RegTestFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the

MODIFY
MagicalCryptoWallet.Tests/RegTests.cs
MagicalCryptoWallet.Tests/RegTests.cs
@@ -26,14 +26,17 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class RegTests
+ [Collection("RegTest collection")]
+ public class RegTests: IClassFixture<SharedFixture>
{
- private SharedFixture Fixture { get; }
+ private SharedFixture SharedFixture { get; }
- public RegTests(SharedFixture fixture)
+ private RegTestFixture RegTestFixture { get; }
+
+ public RegTests(SharedFixture sharedFixture, RegTestFixture regTestFixture)
{
- Fixture = fixture;
+ SharedFixture = sharedFixture;
+ RegTestFixture = regTestFixture;
}
private async Task AssertFiltersInitializedAsync()
@@ -41,7 +44,7 @@ namespace MagicalCryptoWallet.Tests
var firstHash = await Global.RpcClient.GetBlockHashAsync(0);
while (true)
{
- using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var client = new TorHttpClient(new Uri(RegTestFixture.BackendEndPoint)))
using (var response = await client.SendAsync(HttpMethod.Get, "/api/v1/btc/Blockchain/filters/" + firstHash))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -64,7 +67,7 @@ namespace MagicalCryptoWallet.Tests
[Fact]
public async void GetExchangeRatesAsyncAsync()
{
- using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var client = new TorHttpClient(new Uri(RegTestFixture.BackendEndPoint)))
using (var response = await client.SendAsync(HttpMethod.Get, "/api/v1/btc/Blockchain/exchange-rates"))
{
Assert.True(response.IsSuccessStatusCode);
@@ -91,7 +94,7 @@ namespace MagicalCryptoWallet.Tests
var signedTx = await Global.RpcClient.SignRawTransactionAsync(tx);
var content = new StringContent($"'{signedTx.ToHex()}'", Encoding.UTF8, "application/json");
- using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var client = new TorHttpClient(new Uri(RegTestFixture.BackendEndPoint)))
using (var response = await client.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
{
@@ -108,7 +111,7 @@ namespace MagicalCryptoWallet.Tests
var utxo = utxos[0];
var tx = await Global.RpcClient.GetRawTransactionAsync(utxo.OutPoint.Hash);
var content = new StringContent($"'{tx.ToHex()}'", Encoding.UTF8, "application/json");
- using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var client = new TorHttpClient(new Uri(RegTestFixture.BackendEndPoint)))
using (var response = await client.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
{
Assert.True(response.IsSuccessStatusCode);
@@ -120,7 +123,7 @@ namespace MagicalCryptoWallet.Tests
public async void BroadcastInvalidTxAsync()
{
var content = new StringContent($"''", Encoding.UTF8, "application/json");
- using (var client = new TorHttpClient(new Uri(Fixture.BackendEndPoint)))
+ using (var client = new TorHttpClient(new Uri(RegTestFixture.BackendEndPoint)))
using (var response = await client.SendAsync(HttpMethod.Post, "/api/v1/btc/Blockchain/broadcast", content))
{
Assert.False(response.IsSuccessStatusCode);
@@ -137,7 +140,7 @@ namespace MagicalCryptoWallet.Tests
public async Task MempoolAsync()
{
var memPoolService = new MemPoolService();
- Node node = Fixture.BackendRegTestNode.CreateNodeClient();
+ Node node = RegTestFixture.BackendRegTestNode.CreateNodeClient();
node.Behaviors.Add(new MemPoolBehavior(memPoolService));
node.VersionHandshake();
@@ -185,7 +188,7 @@ namespace MagicalCryptoWallet.Tests
var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(FilterDownloaderTestAsync), $"Index{Global.RpcClient.Network}.dat");
- var downloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(Fixture.BackendEndPoint));
+ var downloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint));
try
{
downloader.Synchronize(requestInterval: TimeSpan.FromSeconds(1));
@@ -205,7 +208,7 @@ namespace MagicalCryptoWallet.Tests
}
// Test later synchronization.
- Fixture.BackendRegTestNode.Generate(10);
+ RegTestFixture.BackendRegTestNode.Generate(10);
times = 0;
while ((filterCount = downloader.GetFiltersIncluding(new Height(0)).Count()) < 112)
{
@@ -264,10 +267,10 @@ namespace MagicalCryptoWallet.Tests
_reorgTestAsync_ReorgCount = 0;
- var node = Fixture.BackendRegTestNode;
+ var node = RegTestFixture.BackendRegTestNode;
var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(ReorgTestAsync), $"Index{Global.RpcClient.Network}.dat");
- var downloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(Fixture.BackendEndPoint));
+ var downloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint));
try
{
downloader.Synchronize(requestInterval: TimeSpan.FromSeconds(3));
@@ -396,17 +399,17 @@ namespace MagicalCryptoWallet.Tests
RequiredServices = NodeServices.Network,
MinVersion = ProtocolVersion.WITNESS_VERSION
});
- nodes.ConnectedNodes.Add(Fixture.BackendRegTestNode.CreateNodeClient());
+ nodes.ConnectedNodes.Add(RegTestFixture.BackendRegTestNode.CreateNodeClient());
// 2. Create mempool service.
var memPoolService = new MemPoolService();
memPoolService.TransactionReceived += WalletTestsAsync_MemPoolService_TransactionReceived;
- Node node = Fixture.BackendRegTestNode.CreateNodeClient();
+ Node node = RegTestFixture.BackendRegTestNode.CreateNodeClient();
node.Behaviors.Add(new MemPoolBehavior(memPoolService));
// 3. Create index downloader service.
var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(WalletTestsAsync), $"Index{Global.RpcClient.Network}.dat");
- var indexDownloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(Fixture.BackendEndPoint));
+ var indexDownloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint));
// 4. Create key manager service.
var keyManager = KeyManager.CreateNew(out Mnemonic mnemonic, "password");
@@ -632,16 +635,16 @@ namespace MagicalCryptoWallet.Tests
RequiredServices = NodeServices.Network,
MinVersion = ProtocolVersion.WITNESS_VERSION
});
- nodes.ConnectedNodes.Add(Fixture.BackendRegTestNode.CreateNodeClient());
+ nodes.ConnectedNodes.Add(RegTestFixture.BackendRegTestNode.CreateNodeClient());
// 2. Create mempool service.
var memPoolService = new MemPoolService();
- Node node = Fixture.BackendRegTestNode.CreateNodeClient();
+ Node node = RegTestFixture.BackendRegTestNode.CreateNodeClient();
node.Behaviors.Add(new MemPoolBehavior(memPoolService));
// 3. Create index downloader service.
var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(SendingTestsAsync), $"Index{Global.RpcClient.Network}.dat");
- var indexDownloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(Fixture.BackendEndPoint));
+ var indexDownloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(RegTestFixture.BackendEndPoint));
// 4. Create key manager service.
var keyManager = KeyManager.CreateNew(out Mnemonic mnemonic, "password");

MODIFY
MagicalCryptoWallet.Tests/RpcMethodTests.cs
MagicalCryptoWallet.Tests/RpcMethodTests.cs
@@ -6,14 +6,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class RpcMethodTests
+ public class RpcMethodTests : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public RpcMethodTests(SharedFixture fixture)
+ public RpcMethodTests(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
#region RpcMethodTests

MODIFY
MagicalCryptoWallet.Tests/SharedFixture.cs
MagicalCryptoWallet.Tests/SharedFixture.cs
@@ -27,14 +27,6 @@ namespace MagicalCryptoWallet.Tests
}
}
- public string BackendEndPoint { get; internal set; }
-
- public IWebHost BackendHost { get; internal set; }
-
- public NodeBuilder BackendNodeBuilder { get; internal set; }
-
- public CoreNode BackendRegTestNode { get; internal set; }
-
public SharedFixture()
{
// Initialize tests...
@@ -42,38 +34,11 @@ namespace MagicalCryptoWallet.Tests
Logger.SetFilePath(Path.Combine(DataDir, "Logs.txt"));
Logger.SetMinimumLevel(LogLevel.Info);
Logger.SetModes(LogMode.Debug, LogMode.Console, LogMode.File);
-
- BackendNodeBuilder = NodeBuilder.CreateAsync().GetAwaiter().GetResult();
- BackendNodeBuilder.CreateNodeAsync().GetAwaiter().GetResult();
- BackendNodeBuilder.StartAllAsync().GetAwaiter().GetResult();
- BackendRegTestNode = BackendNodeBuilder.Nodes[0];
-
- var rpc = BackendRegTestNode.CreateRpcClient();
-
- var authString = rpc.Authentication.Split(':');
- Global.InitializeAsync(rpc.Network, authString[0], authString[1], rpc).GetAwaiter().GetResult();
-
- BackendEndPoint = $"http://localhost:{new Random().Next(37130, 38000)}/";
- BackendHost = WebHost.CreateDefaultBuilder()
- .UseStartup<Startup>()
- .UseUrls(BackendEndPoint)
- .Build();
-
- var hostInitializationTask = BackendHost.RunAsync();
- Logger.LogInfo<SharedFixture>($"Started Backend webhost: {BackendEndPoint}");
-
- var delayTask = Task.Delay(3000);
- Task.WaitAny(delayTask, hostInitializationTask); // Wait for server to initialize (Without this OSX CI will fail)
}
public void Dispose()
{
// Cleanup tests...
-
- BackendHost?.StopAsync();
- BackendHost?.Dispose();
- BackendRegTestNode?.Kill(cleanFolder: true);
- BackendNodeBuilder?.Dispose();
}
}
}

MODIFY
MagicalCryptoWallet.Tests/SmartModelTests.cs
MagicalCryptoWallet.Tests/SmartModelTests.cs
@@ -10,14 +10,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
- [Collection("Shared collection")]
- public class SmartModelTests
+ public class SmartModelTests : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public SmartModelTests(SharedFixture fixture)
+ public SmartModelTests(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
[Fact]

MODIFY
MagicalCryptoWallet.Tests/TorTests.cs
MagicalCryptoWallet.Tests/TorTests.cs
@@ -11,14 +11,13 @@ using Xunit;
namespace MagicalCryptoWallet.Tests
{
// Tor must be running
- [Collection("Shared collection")]
- public class TorTests
+ public class TorTests : IClassFixture<SharedFixture>
{
private SharedFixture SharedFixture { get; }
- public TorTests(SharedFixture fixture)
+ public TorTests(SharedFixture sharedFixture)
{
- SharedFixture = fixture;
+ SharedFixture = sharedFixture;
}
[Fact]

Make tests async