Skip to content

Commit

Permalink
Fix Swarm() hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
longfin committed Apr 2, 2019
1 parent 452b28c commit 1029349
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ To be released.
already has and deal with only longer (higher) blocks.
- Fixed a bug that occured when `Swarm` was handling multiple responses at the
same time.
- Fixed a bug that hangs when calling the `Swarm` constructor in certain
runtimes. (e.g. Unity3d)
- Removed `AddressTransactionSet` which handles handle `Address` to
`IEnumerable<TxId>` indices, and the following methods in `IStore`:
- `IStore.IterateAddresses()`
Expand Down
16 changes: 15 additions & 1 deletion Libplanet.Tests/Net/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,26 @@ await Task.WhenAll(
}

[Fact]
public void CanDenyNullParams()
public void ThrowArgumentExceptionInConstructor()
{
Assert.Throws<ArgumentNullException>(() =>
{
new Swarm(null);
});

// Swarm needs host or iceServers.
Assert.Throws<ArgumentException>(() =>
{
new Swarm(new PrivateKey());
});

// Swarm needs host or iceServers.
Assert.Throws<ArgumentException>(() =>
{
new Swarm(
new PrivateKey(),
iceServers: new IceServer[] { });
});
}

[Fact]
Expand Down
18 changes: 10 additions & 8 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public partial class Swarm : ICollection<Peer>, IDisposable
private readonly AsyncLock _receiveMutex;
private readonly AsyncLock _blockSyncMutex;
private readonly string _host;
private readonly TurnClient _turnClient;
private readonly IList<IceServer> _iceServers;

private readonly ILogger _logger;

private TaskCompletionSource<object> _runningEvent;
private int? _listenPort;
private TurnClient _turnClient;

private NetMQQueue<Message> _replyQueue;

Expand Down Expand Up @@ -115,15 +116,11 @@ public Swarm(
EndPoint = new DnsEndPoint(_host, listenPort.Value);
}

if (iceServers != null)
{
_turnClient = IceServer.CreateTurnClient(iceServers).Result;
}

if (_host == null && _turnClient == null)
_iceServers = iceServers?.ToList();
if (_host == null && (_iceServers == null || !_iceServers.Any()))
{
throw new ArgumentException(
$"Swarm needs {nameof(host)} or {iceServers}.");
$"Swarm needs {nameof(host)} or {nameof(iceServers)}.");
}

string loggerId = _privateKey.PublicKey.ToAddress().ToHex();
Expand Down Expand Up @@ -399,6 +396,11 @@ public async Task StartAsync<T>(
throw new SwarmException("Swarm is already running.");
}

if (_iceServers != null)
{
_turnClient = await IceServer.CreateTurnClient(_iceServers);
}

if (_listenPort == null)
{
_listenPort = _router.BindRandomPort("tcp://*");
Expand Down

0 comments on commit 1029349

Please sign in to comment.