Improve SmartBlockProviderTests.
MODIFY
WalletWasabi.Tests/UnitTests/SmartBlockProviderTests.cs
WalletWasabi.Tests/UnitTests/SmartBlockProviderTests.cs
@@ -4,7 +4,6 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using NBitcoin;
-using WalletWasabi.Legal;
using WalletWasabi.Wallets;
using Xunit;
@@ -12,6 +11,9 @@ namespace WalletWasabi.Tests.UnitTests
{
public class SmartBlockProviderTests
{
+ /// <summary>
+ /// Tests <see cref="SmartBlockProvider.GetBlockAsync(uint256, CancellationToken)"/> behavior.
+ /// </summary>
[Fact]
public async void TestAsync()
{
@@ -21,31 +23,39 @@ namespace WalletWasabi.Tests.UnitTests
[uint256.One] = Block.CreateBlock(Network.Main)
};
- var source = new MockProvider();
- source.OnGetBlockAsync = async (hash, cancel) =>
- {
- await Task.Delay(TimeSpan.FromSeconds(0.5));
- return blocks[hash];
- };
+ var blockProvider = new TestBlockProvider(blocks);
using var cache = new MemoryCache(new MemoryCacheOptions());
- var blockProvider = new SmartBlockProvider(source, cache);
+ var smartBlockProvider = new SmartBlockProvider(blockProvider, cache);
+
+ Task<Block> b0 = smartBlockProvider.GetBlockAsync(uint256.Zero, CancellationToken.None);
+ Task<Block> b1 = smartBlockProvider.GetBlockAsync(uint256.One, CancellationToken.None);
+ Task<Block> b2 = smartBlockProvider.GetBlockAsync(uint256.Zero, CancellationToken.None);
+
+ // Wait for all blocks to be fetched.
+ Block[] result = await Task.WhenAll(b0, b1, b2);
- var b1 = blockProvider.GetBlockAsync(uint256.Zero, CancellationToken.None);
- var b2 = blockProvider.GetBlockAsync(uint256.One, CancellationToken.None);
- var b3 = blockProvider.GetBlockAsync(uint256.Zero, CancellationToken.None);
+ // We assert here that SmartBlockProvider used internal BlockProvider to get blocks
+ // and that those blocks correspond to expected blocks.
+ Assert.Equal(blocks[0], result[0]);
+ Assert.Equal(blocks[0], result[2]);
- await Task.WhenAll(b1, b2, b3);
- Assert.Equal(await b1, await b3);
- Assert.NotEqual(await b1, await b2);
+ Assert.NotEqual(blocks[0], result[1]);
}
- private class MockProvider : IBlockProvider
+ private class TestBlockProvider : IBlockProvider
{
- public Func<uint256, CancellationToken, Task<Block>> OnGetBlockAsync { get; set; }
+ private Dictionary<uint256, Block> Blocks { get; }
- public Task<Block> GetBlockAsync(uint256 hash, CancellationToken cancel)
+ public TestBlockProvider(Dictionary<uint256, Block> blocks)
{
- return OnGetBlockAsync(hash, cancel);
+ Blocks = blocks;
+ }
+
+ public async Task<Block> GetBlockAsync(uint256 hash, CancellationToken cancel)
+ {
+ // This does simulate work necessary to actually fetch the block.
+ await Task.Delay(TimeSpan.FromSeconds(0.5));
+ return Blocks[hash];
}
}
}

MODIFY
WalletWasabi/Wallets/SmartBlockProvider.cs
WalletWasabi/Wallets/SmartBlockProvider.cs
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace WalletWasabi.Wallets
{
/// <summary>
- /// SmartP2pBlockProvider is a block provider that can provide
+ /// <see cref="SmartBlockProvider"/> is a block provider that can provide
/// blocks from multiple requesters.
/// </summary>
public class SmartBlockProvider : IBlockProvider
@@ -21,7 +21,7 @@ namespace WalletWasabi.Wallets
private IBlockProvider InnerBlockProvider { get; }
- public IMemoryCache Cache { get; }
+ private IMemoryCache Cache { get; }
public async Task<Block> GetBlockAsync(uint256 blockHash, CancellationToken cancel)
{

Improve SmartBlockProviderTests.