From ec7ba4a4fc52f2f0e0a7335f22c7990d287fa270 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Fri, 23 Aug 2019 12:34:55 +1200 Subject: [PATCH] fix:(Swarm): allow connections when running, fixes #46 --- src/Swarm.cs | 18 ++++++++ test/PeerManagerTest.cs | 12 +++-- test/SwarmTest.cs | 98 ++++++++++++++++++++++++++++++++--------- 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/src/Swarm.cs b/src/Swarm.cs index 76be904..994a052 100644 --- a/src/Swarm.cs +++ b/src/Swarm.cs @@ -476,6 +476,11 @@ public async Task StopAsync() /// public async Task 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. @@ -856,6 +861,19 @@ public Task StartListeningAsync(MultiAddress address) /// 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)) diff --git a/test/PeerManagerTest.cs b/test/PeerManagerTest.cs index c7c20d5..2082201 100644 --- a/test/PeerManagerTest.cs +++ b/test/PeerManagerTest.cs @@ -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 }; @@ -74,6 +74,7 @@ public async Task Backoff_Increases() try { + await swarm.StartAsync(); await manager.StartAsync(); try { @@ -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) @@ -92,6 +93,7 @@ public async Task Backoff_Increases() } finally { + await swarm.StopAsync(); await manager.StopAsync(); } } @@ -118,6 +120,7 @@ public async Task PermanentlyDead() try { + await swarm.StartAsync(); await manager.StartAsync(); try { @@ -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) @@ -136,6 +139,7 @@ public async Task PermanentlyDead() } finally { + await swarm.StopAsync(); await manager.StopAsync(); } } diff --git a/test/SwarmTest.cs b/test/SwarmTest.cs index 34c7524..c8c00e7 100644 --- a/test/SwarmTest.cs +++ b/test/SwarmTest.cs @@ -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(() => + swarm.StartAsync().Wait(); + try { - var _ = swarm.ConnectAsync(remoteAddress).Result; - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(remoteAddress).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } } [TestMethod] @@ -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(() => + swarm.StartAsync().Wait(); + try { - var _ = swarm.ConnectAsync(remoteAddress).Result; - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(remoteAddress).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } } [TestMethod] @@ -524,10 +540,18 @@ public void Connect_Failure_Event() { unreachable = e; }; - ExceptionAssert.Throws(() => + swarm.StartAsync().Wait(); + try { - var _ = swarm.ConnectAsync(remoteAddress).Result; - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(remoteAddress).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } Assert.IsNotNull(unreachable); Assert.AreEqual(remoteId, unreachable.Id.ToBase58()); } @@ -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(() => + swarm.StartAsync().Wait(); + try { - var _ = swarm.ConnectAsync(remoteAddress).Result; - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(remoteAddress).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } } [TestMethod] @@ -552,10 +584,18 @@ 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(() => + swarm.StartAsync().Wait(); + try { - var _ = swarm.ConnectAsync(remoteAddress, cs.Token).Result; - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(remoteAddress, cs.Token).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } } [TestMethod] @@ -563,20 +603,36 @@ public void Connecting_To_Blacklisted_Address() { var swarm = new Swarm { LocalPeer = self }; swarm.BlackList.Add(mars); - ExceptionAssert.Throws(() => + swarm.StartAsync().Wait(); + try { - swarm.ConnectAsync(mars).Wait(); - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(mars).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } } [TestMethod] public void Connecting_To_Self() { var swarm = new Swarm { LocalPeer = self }; - ExceptionAssert.Throws(() => + swarm.StartAsync().Wait(); + try { - swarm.ConnectAsync(earth).Wait(); - }); + ExceptionAssert.Throws(() => + { + var _ = swarm.ConnectAsync(earth).Result; + }); + } + finally + { + swarm.StopAsync().Wait(); + } } [TestMethod]