public async void TestAsync()
        {
            var blocks = new Dictionary <uint256, Block>
            {
                [uint256.Zero] = Block.CreateBlock(Network.Main),
                [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]);
            };
            using var cache = new MemoryCache(new MemoryCacheOptions());
            var blockProvider = new SmartBlockProvider(source, cache);

            var b1 = blockProvider.GetBlockAsync(uint256.Zero, CancellationToken.None);
            var b2 = blockProvider.GetBlockAsync(uint256.One, CancellationToken.None);
            var b3 = blockProvider.GetBlockAsync(uint256.Zero, CancellationToken.None);

            await Task.WhenAll(b1, b2, b3);

            Assert.Equal(await b1, await b3);
            Assert.NotEqual(await b1, await b2);
        }
Exemplo n.º 2
0
        public async void TestAsync()
        {
            var blocks = new Dictionary <uint256, Block>
            {
                [uint256.Zero] = Block.CreateBlock(Network.Main),
                [uint256.One]  = Block.CreateBlock(Network.Main)
            };

            var blockProvider = new TestBlockProvider(blocks);

            using var cache = new MemoryCache(new MemoryCacheOptions());
            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);

            // 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]);

            Assert.NotEqual(blocks[0], result[1]);
        }