Skip to content

Commit

Permalink
fix:(Swarm): allow connections when running, fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 23, 2019
1 parent 70b652a commit ec7ba4a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 25 deletions.
18 changes: 18 additions & 0 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ public async Task StopAsync()
/// </remarks>
public async Task<PeerConnection> ConnectAsync(Peer peer, CancellationToken cancel = default(CancellationToken))
{
if (!IsRunning)
{
throw new Exception("The swarm is not running.");
}

peer = RegisterPeer(peer);

// If connected and still open, then use the existing connection.
Expand Down Expand Up @@ -856,6 +861,19 @@ public Task<MultiAddress> StartListeningAsync(MultiAddress address)
/// </remarks>
async void OnRemoteConnect(Stream stream, MultiAddress local, MultiAddress remote)
{
if (!IsRunning)
{
try
{
stream.Dispose();
}
catch (Exception)
{
// eat it.
}
return;
}

// If the remote is already trying to establish a connection, then we
// can just refuse this one.
if (!pendingRemoteConnections.TryAdd(remote, null))
Expand Down
12 changes: 8 additions & 4 deletions test/PeerManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public async Task Backoff_Increases()
{
var peer = new Peer
{
Id = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb",
Id = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTxx",
Addresses = new MultiAddress[]
{
"/ip4/127.0.0.1/tcp/4040/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb"
"/ip4/127.0.0.1/tcp/4040/ipfs/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTxx"
}
};
var swarm = new Swarm { LocalPeer = self };
Expand All @@ -74,6 +74,7 @@ public async Task Backoff_Increases()

try
{
await swarm.StartAsync();
await manager.StartAsync();
try
{
Expand All @@ -82,7 +83,7 @@ public async Task Backoff_Increases()
catch { }
Assert.AreEqual(1, manager.DeadPeers.Count);

var end = DateTime.Now + TimeSpan.FromSeconds(2);
var end = DateTime.Now + TimeSpan.FromSeconds(4);
while (DateTime.Now <= end)
{
if (manager.DeadPeers[peer].Backoff > manager.InitialBackoff)
Expand All @@ -92,6 +93,7 @@ public async Task Backoff_Increases()
}
finally
{
await swarm.StopAsync();
await manager.StopAsync();
}
}
Expand All @@ -118,6 +120,7 @@ public async Task PermanentlyDead()

try
{
await swarm.StartAsync();
await manager.StartAsync();
try
{
Expand All @@ -126,7 +129,7 @@ public async Task PermanentlyDead()
catch { }
Assert.AreEqual(1, manager.DeadPeers.Count);

var end = DateTime.Now + TimeSpan.FromSeconds(2);
var end = DateTime.Now + TimeSpan.FromSeconds(6);
while (DateTime.Now <= end)
{
if (manager.DeadPeers[peer].NextAttempt == DateTime.MaxValue)
Expand All @@ -136,6 +139,7 @@ public async Task PermanentlyDead()
}
finally
{
await swarm.StopAsync();
await manager.StopAsync();
}
}
Expand Down
98 changes: 77 additions & 21 deletions test/SwarmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,18 @@ public void Connect_No_Transport()
var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb";
MultiAddress remoteAddress = $"/ip4/127.0.0.1/ipfs/{remoteId}";
var swarm = new Swarm { LocalPeer = self };
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
}

[TestMethod]
Expand All @@ -507,10 +515,18 @@ public void Connect_Refused()
var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb";
MultiAddress remoteAddress = $"/ip4/127.0.0.1/tcp/4040/ipfs/{remoteId}";
var swarm = new Swarm { LocalPeer = self };
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
}

[TestMethod]
Expand All @@ -524,10 +540,18 @@ public void Connect_Failure_Event()
{
unreachable = e;
};
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
Assert.IsNotNull(unreachable);
Assert.AreEqual(remoteId, unreachable.Id.ToBase58());
}
Expand All @@ -538,10 +562,18 @@ public void Connect_Not_Peer()
var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb";
MultiAddress remoteAddress = $"/dns/npmjs.com/tcp/80/ipfs/{remoteId}";
var swarm = new Swarm { LocalPeer = self };
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(remoteAddress).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
}

[TestMethod]
Expand All @@ -552,31 +584,55 @@ public void Connect_Cancelled()
var remoteId = "QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb";
MultiAddress remoteAddress = $"/ip4/127.0.0.1/tcp/4002/ipfs/{remoteId}";
var swarm = new Swarm { LocalPeer = self };
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
var _ = swarm.ConnectAsync(remoteAddress, cs.Token).Result;
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(remoteAddress, cs.Token).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
}

[TestMethod]
public void Connecting_To_Blacklisted_Address()
{
var swarm = new Swarm { LocalPeer = self };
swarm.BlackList.Add(mars);
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
swarm.ConnectAsync(mars).Wait();
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(mars).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
}

[TestMethod]
public void Connecting_To_Self()
{
var swarm = new Swarm { LocalPeer = self };
ExceptionAssert.Throws<Exception>(() =>
swarm.StartAsync().Wait();
try
{
swarm.ConnectAsync(earth).Wait();
});
ExceptionAssert.Throws<Exception>(() =>
{
var _ = swarm.ConnectAsync(earth).Result;
});
}
finally
{
swarm.StopAsync().Wait();
}
}

[TestMethod]
Expand Down

0 comments on commit ec7ba4a

Please sign in to comment.