Skip to content

Commit

Permalink
feat(AutoDialer): do use peer with a pending connection
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed May 7, 2019
1 parent b1f1c36 commit 5dd2341
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/AutoDialer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void OnPeerDisconnected(object sender, Peer disconnectedPeer)
var peer = swarm.KnownPeers
.Where(p => p.ConnectedAddress == null)
.Where(p => p != disconnectedPeer)
.Where(p => !swarm.HasPendingConnection(p))
.FirstOrDefault();
if (peer != null)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ public Peer RegisterPeer(Peer peer)
return p;
}

/// <summary>
/// Determines if a connection is being made to the peer.
/// </summary>
/// <param name="peer">
/// A <see cref="Peer"/>.
/// </param>
/// <returns>
/// <b>true</b> is the <paramref name="peer"/> has a pending connection.
/// </returns>
public bool HasPendingConnection(Peer peer)
{
return pendingConnections.TryGetValue(peer, out Task<PeerConnection> _);
}

/// <summary>
/// The addresses that cannot be used.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,33 @@ public async Task Connect_CancelsOnStop()

}

[TestMethod]
public async Task Connect_IsPending()
{
var swarm = new Swarm { LocalPeer = self };
var venus = new Peer
{
Id = "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
Addresses = new MultiAddress[]
{
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
}
};

await swarm.StartAsync();
try
{
Assert.IsFalse(swarm.HasPendingConnection(venus));

var a = swarm.ConnectAsync(venus);
Assert.IsTrue(swarm.HasPendingConnection(venus));
}
finally
{
await swarm.StopAsync();
}
}

[TestMethod]
public async Task Connect_WithSomeUnreachableAddresses()
{
Expand Down

0 comments on commit 5dd2341

Please sign in to comment.