Skip to content

Commit

Permalink
test: reset block demand if not changed
Browse files Browse the repository at this point in the history
[changelog skip]
  • Loading branch information
moreal committed Jan 25, 2021
1 parent 3650a39 commit 7b93570
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Libplanet.Tests/Menees.Analyzers.Settings.xml
Expand Up @@ -2,5 +2,5 @@
<Menees.Analyzers.Settings>
<MaxLineColumns>100</MaxLineColumns>
<MaxMethodLines>300</MaxMethodLines>
<MaxFileLines>2550</MaxFileLines>
<MaxFileLines>2600</MaxFileLines>
</Menees.Analyzers.Settings>
29 changes: 29 additions & 0 deletions Libplanet.Tests/Net/SwarmTest.cs
Expand Up @@ -2430,6 +2430,35 @@ public async Task BlockDemand()
}
}

[Fact]
public async Task ResetBlockDemandIfNotChanged()
{
var minerKey = new PrivateKey();
Swarm<DumbAction> sender = CreateSwarm();
Swarm<DumbAction> receiver = CreateSwarm();
for (var i = 0; i < 20; i++)
{
await sender.BlockChain.MineBlock(minerKey.ToAddress());
}

Block<DumbAction> lowerBlock = sender.BlockChain[19],
higherBlock = sender.BlockChain[20];

await StartAsync(sender);
await StartAsync(receiver);
await BootstrapAsync(sender, receiver.AsPeer);

sender.BroadcastBlock(lowerBlock);
await receiver.FillBlocksAsyncStarted.WaitAsync();
sender.BroadcastBlock(higherBlock);
await receiver.ProcessFillBlocksFinished.WaitAsync();

Assert.NotNull(receiver.BlockDemand);
Assert.Equal(
higherBlock.Hash,
new HashDigest<SHA256>(receiver.BlockDemand.Value.Header.Hash));
}

private async Task<Task> StartAsync<T>(
Swarm<T> swarm,
CancellationToken cancellationToken = default
Expand Down
4 changes: 4 additions & 0 deletions Libplanet/Net/Swarm.cs
Expand Up @@ -233,6 +233,8 @@ static Swarm()

internal AsyncAutoResetEvent FillBlocksAsyncFailed { get; } = new AsyncAutoResetEvent();

internal AsyncAutoResetEvent ProcessFillBlocksFinished { get; } = new AsyncAutoResetEvent();

internal SwarmOptions Options { get; }

/// <summary>
Expand Down Expand Up @@ -1765,6 +1767,8 @@ CancellationToken cancellationToken
_logger.Debug($"Reset {nameof(BlockDemand)}...");
BlockDemand = null;
}

ProcessFillBlocksFinished.Set();
}
}
}
Expand Down

0 comments on commit 7b93570

Please sign in to comment.