Skip to content

Commit

Permalink
Reuse fixture blocks for PreloadAsyncCancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Aug 30, 2019
1 parent 0286a47 commit 470bfed
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class SwarmTest : IDisposable
private const int Timeout = 60 * 1000;
private const int DisposeTimeout = 5 * 1000;

private static BlockChain<DumbAction> _fixtureBlocksForPreloadAsyncCancellationTest;

private readonly ITestOutputHelper _output;
private readonly StoreFixture _fx1;
private readonly StoreFixture _fx2;
Expand Down Expand Up @@ -1235,29 +1237,15 @@ public async Task PreloadAsyncCancellation(int cancelAfter)
{
Swarm<DumbAction> minerSwarm = _swarms[0];
Swarm<DumbAction> receiverSwarm = _swarms[1];
_output.WriteLine("Miner: {0}", minerSwarm.Address);
_output.WriteLine("Receiver: {0}", receiverSwarm.Address);
Log.Logger.Information("Miner: {0}", minerSwarm.Address);
Log.Logger.Information("Receiver: {0}", receiverSwarm.Address);

BlockChain<DumbAction> minerChain = _blockchains[0];
BlockChain<DumbAction> receiverChain = _blockchains[1];
Guid receiverChainId = _blockchains[1].Id;

var signer = new PrivateKey();
Address address = signer.PublicKey.ToAddress();
_output.WriteLine("Fixture blocks:");
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 5; j++)
{
minerChain.MakeTransaction(
signer,
new[] { new DumbAction(address, $"Item{i}.{j}") }
);
}

Block<DumbAction> block = minerChain.MineBlock(minerSwarm.Address);
_output.WriteLine(" #{0,2} {1}", block.Index, block.Hash);
}
(Address address, IEnumerable<Block<DumbAction>> blocks) =
MakeFixtureBlocksForPreloadAsyncCancellationTest();

Assert.NotNull(minerChain.Tip);
minerChain.FindNextHashesChunkSize = 2;
Expand All @@ -1274,11 +1262,11 @@ await receiverSwarm.PreloadAsync(
cancellationToken: cts.Token
);
canceled = false;
_output.WriteLine($"{nameof(receiverSwarm.PreloadAsync)}() normally finished.");
Log.Logger.Debug($"{nameof(receiverSwarm.PreloadAsync)}() normally finished.");
}
catch (OperationCanceledException)
{
_output.WriteLine($"{nameof(receiverSwarm.PreloadAsync)}() aborted.");
Log.Logger.Debug($"{nameof(receiverSwarm.PreloadAsync)}() aborted.");
}

cts.Dispose();
Expand Down Expand Up @@ -1307,6 +1295,42 @@ await receiverSwarm.PreloadAsync(
}
}

private static (Address, IEnumerable<Block<DumbAction>>)
MakeFixtureBlocksForPreloadAsyncCancellationTest()
{
BlockChain<DumbAction> chain = _fixtureBlocksForPreloadAsyncCancellationTest;

if (chain is null)
{
var policy = new BlockPolicy<DumbAction>(new MinerReward(1));
using (var storeFx = new LiteDBStoreFixture())
{
chain = new BlockChain<DumbAction>(policy, storeFx.Store);
Address miner = new PrivateKey().PublicKey.ToAddress();
var signer = new PrivateKey();
Address address = signer.PublicKey.ToAddress();
Log.Logger.Information("Fixture blocks:");
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 5; j++)
{
chain.MakeTransaction(
signer,
new[] { new DumbAction(address, $"Item{i}.{j}") }
);
}

Block<DumbAction> block = chain.MineBlock(miner);
Log.Logger.Information(" #{0,2} {1}", block.Index, block.Hash);
}

_fixtureBlocksForPreloadAsyncCancellationTest = chain;
}
}

return (chain.Tip.Transactions.First().Actions.First().TargetAddress, chain);
}

private async Task<Task> StartAsync(
Swarm<DumbAction> swarm,
CancellationToken cancellationToken = default
Expand Down

0 comments on commit 470bfed

Please sign in to comment.