Skip to content

Commit

Permalink
feat(Swarm): raise PeerNotReachable when connect fails
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 10, 2019
1 parent 46588bc commit 285739c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public class Swarm : IService, IPolicy<MultiAddress>
/// </remarks>
public event EventHandler<Peer> PeerRemoved;

/// <summary>
/// Raised when a peer cannot be connected to.
/// </summary>
public event EventHandler<Peer> PeerNotReachable;

/// <summary>
/// The local peer.
/// </summary>
Expand Down Expand Up @@ -497,6 +502,11 @@ public async Task StopAsync()
.ConfigureAwait(false);
}
}
catch (Exception)
{
PeerNotReachable?.Invoke(this, peer);
throw;
}
finally
{
pendingConnections.TryRemove(peer, out AsyncLazy<PeerConnection> _);
Expand Down
19 changes: 19 additions & 0 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,25 @@ public void Connect_Refused()
});
}

[TestMethod]
public void Connect_Failure_Event()
{
var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb";
MultiAddress remoteAddress = $"/ip4/127.0.0.1/tcp/4040/ipfs/{remoteId}";
var swarm = new Swarm { LocalPeer = self };
Peer unreachable = null;
swarm.PeerNotReachable += (s, e) =>
{
unreachable = e;
};
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
Assert.IsNotNull(unreachable);
Assert.AreEqual(remoteId, unreachable.Id.ToBase58());
}

[TestMethod]
public void Connect_Not_Peer()
{
Expand Down

0 comments on commit 285739c

Please sign in to comment.