Skip to content

Commit

Permalink
Merge pull request #108 from longfin/bugfix/stabilize-tests
Browse files Browse the repository at this point in the history
Stabilize Swarm and SwarmTest
  • Loading branch information
longfin authored Mar 5, 2019
2 parents 93dd375 + 4ada47e commit 86f399a
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 116 deletions.
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ script:
elif [[ "$TRAVIS_TAG" != "" ]]; then
! grep -i "to be released" CHANGES.md
else
if [[ "$TRAVIS_PULL_REQUEST_SHA" = "" ]]; then
commit_range="$TRAVIS_COMMIT_RANGE"
else
commit_range="$TRAVIS_BRANCH...$TRAVIS_PULL_REQUEST_SHA"
fi
[[ "$(git diff --name-only "$commit_range" | grep CHANGES\.md)" != "" ]]
[[ "$(git diff --name-only "$TRAVIS_COMMIT_RANGE" | grep CHANGES\.md)" != "" ]]
fi
# Check coding styles
Expand Down
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Libplanet changelog
Version 0.1.1
-------------

To be released.
- Improved stability of `Swarm` and `SwarmTest`.


Version 0.1.0
Expand Down
33 changes: 27 additions & 6 deletions Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class SwarmTest : IDisposable
public SwarmTest(ITestOutputHelper output)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Debug()
.Enrich.WithThreadId()
.WriteTo.TestOutput(output, outputTemplate: "{Timestamp:HH:mm:ss}[@{Swarm_listenUrl}][{ThreadId}] - {Message}")
.WriteTo.TestOutput(output, outputTemplate: "{Timestamp:HH:mm:ss}[@{SwarmId}][{ThreadId}] - {Message}")
.CreateLogger()
.ForContext<SwarmTest>();

Expand Down Expand Up @@ -71,6 +71,11 @@ public void Dispose()
_fx1.Dispose();
_fx2.Dispose();
_fx3.Dispose();

foreach (Swarm s in _swarms)
{
s.Dispose();
}
}

[Fact]
Expand Down Expand Up @@ -468,6 +473,7 @@ public async Task CanBroadcastTx()

await EnsureExchange(swarmA, swarmB);
await EnsureExchange(swarmA, swarmC);
await EnsureExchange(swarmB, swarmC);

await swarmA.BroadcastTxsAsync(new[] { tx });

Expand Down Expand Up @@ -525,10 +531,12 @@ public async Task CanBroadcastBlock()

await EnsureExchange(swarmA, swarmB);
await EnsureExchange(swarmA, swarmC);
await EnsureExchange(swarmB, swarmC);

await swarmB.BroadcastBlocksAsync(new[] { chainB.Last() });

await Task.Delay(5000);
await swarmC.BlockReceived.WaitAsync();
await swarmA.BlockReceived.WaitAsync();

Assert.Equal(chainB.AsEnumerable(), chainC);

Expand All @@ -538,7 +546,8 @@ public async Task CanBroadcastBlock()

await swarmA.BroadcastBlocksAsync(new[] { chainA.Last() });

await Task.Delay(5000);
await swarmB.BlockReceived.WaitAsync();
await swarmC.BlockReceived.WaitAsync();

Assert.Equal(chainA.AsEnumerable(), chainB);
Assert.Equal(chainA.AsEnumerable(), chainC);
Expand Down Expand Up @@ -575,18 +584,30 @@ public void CanResolveUrl()
s.AsPeer.Urls);
}

[Fact]
public async Task CanStopGracefullyWhileStarting()
{
Swarm a = _swarms[0];
Swarm b = _swarms[1];

await StartAsync(b, _blockchains[1]);
await a.AddPeersAsync(new[] { b.AsPeer });

Task t = await StartAsync(a, _blockchains[0]);
await Task.WhenAll(a.StopAsync(), t);
}

private async Task<Task> StartAsync<T>(
Swarm swarm,
BlockChain<T> blockChain,
int millisecondsDistributeInterval = 1500,
CancellationToken cancellationToken = default
)
where T : IAction
{
Task task = Task.Run(
async () => await swarm.StartAsync(
blockChain,
millisecondsDistributeInterval,
200,
cancellationToken
)
);
Expand Down
Loading

0 comments on commit 86f399a

Please sign in to comment.