From eec8d8200ff2f3892b269a8e2dff90830bd3bc44 Mon Sep 17 00:00:00 2001 From: Chanhyuck Ko Date: Mon, 23 Dec 2024 14:38:07 +0900 Subject: [PATCH] clean up exceptions --- .../Protocols/KademliaProtocol.cs | 36 ++++++++++--------- ...outException.cs => PingFailedException.cs} | 16 +++------ .../Protocols/TestTransport.cs | 2 +- .../SwarmTest.AppProtocolVersion.cs | 33 +++++++++-------- 4 files changed, 45 insertions(+), 42 deletions(-) rename src/Libplanet.Net/Protocols/{PingTimeoutException.cs => PingFailedException.cs} (67%) diff --git a/src/Libplanet.Net/Protocols/KademliaProtocol.cs b/src/Libplanet.Net/Protocols/KademliaProtocol.cs index 2a0e9f035ac..37b117fdb6d 100644 --- a/src/Libplanet.Net/Protocols/KademliaProtocol.cs +++ b/src/Libplanet.Net/Protocols/KademliaProtocol.cs @@ -95,7 +95,7 @@ await PingAsync(peer, dialTimeout, cancellationToken) dialTimeout, cancellationToken)); } - catch (PingTimeoutException) + catch (PingFailedException) { _logger.Warning("A timeout exception occurred connecting to seed peer"); RemovePeer(peer); @@ -140,19 +140,25 @@ public async Task AddPeersAsync( var tasks = new List(); foreach (BoundPeer peer in peers) { - tasks.Add(PingAsync( - peer, - timeout: timeout, - cancellationToken: cancellationToken)); + tasks.Add( + PingAsync( + peer, + timeout: timeout, + cancellationToken: cancellationToken)); } _logger.Verbose("Trying to ping {PeerCount} peers", tasks.Count); await Task.WhenAll(tasks).ConfigureAwait(false); _logger.Verbose("Update complete"); } - catch (PingTimeoutException e) + catch (PingFailedException pfe) { - _logger.Debug(e, "Ping timed out"); + if (pfe.InnerException is { } e) + { + throw e; + } + + throw; } catch (TaskCanceledException e) { @@ -284,7 +290,7 @@ public async Task CheckReplacementCacheAsync(CancellationToken cancellationToken await PingAsync(replacement, _requestTimeout, cancellationToken) .ConfigureAwait(false); } - catch (PingTimeoutException) + catch (PingFailedException) { _logger.Verbose( "Removed stale peer {Peer} from replacement cache", @@ -327,7 +333,7 @@ await PingAsync(replacement, _requestTimeout, cancellationToken) await PingAsync(boundPeer, _requestTimeout, cancellationToken) .ConfigureAwait(false); } - catch (PingTimeoutException) + catch (PingFailedException) { var msg = "{BoundPeer}, a target peer, is in the routing table does not respond"; @@ -394,7 +400,7 @@ await PingAsync(found, _requestTimeout, cancellationToken) throw new TaskCanceledException( $"Task is cancelled during {nameof(FindSpecificPeerAsync)}()"); } - catch (PingTimeoutException) + catch (PingFailedException) { // Ignore peer not responding } @@ -439,11 +445,9 @@ internal async Task PingAsync( AddPeer(peer); } - catch (CommunicationFailException) + catch (Exception e) { - throw new PingTimeoutException( - $"Failed to send Ping to {peer}.", - peer); + throw new PingFailedException(peer, e); } } @@ -497,7 +501,7 @@ private async Task ValidateAsync( await PingAsync(peer, timeout, cancellationToken).ConfigureAwait(false); _table.Check(peer, check, DateTimeOffset.UtcNow); } - catch (PingTimeoutException) + catch (PingFailedException) { _logger.Verbose("Removing invalid peer {Peer}...", peer); RemovePeer(peer); @@ -711,7 +715,7 @@ private async Task ProcessFoundAsync( AggregateException aggregateException = aggregateTask.Exception!; foreach (Exception e in aggregateException.InnerExceptions) { - if (e is PingTimeoutException pte) + if (e is PingFailedException pte) { peers.Remove(pte.Target); } diff --git a/src/Libplanet.Net/Protocols/PingTimeoutException.cs b/src/Libplanet.Net/Protocols/PingFailedException.cs similarity index 67% rename from src/Libplanet.Net/Protocols/PingTimeoutException.cs rename to src/Libplanet.Net/Protocols/PingFailedException.cs index ec40b608c44..576d366de2f 100644 --- a/src/Libplanet.Net/Protocols/PingTimeoutException.cs +++ b/src/Libplanet.Net/Protocols/PingFailedException.cs @@ -4,27 +4,21 @@ namespace Libplanet.Net.Protocols { [Serializable] - public class PingTimeoutException : TimeoutException + public class PingFailedException : Exception { - public PingTimeoutException(BoundPeer target) - : base() + public PingFailedException(BoundPeer target, Exception innerException) + : base($"Failed to send ping to target peer {target}", innerException) { Target = target; } - public PingTimeoutException(string message, BoundPeer target) - : base(message) - { - Target = target; - } - - public PingTimeoutException(string message, BoundPeer target, Exception innerException) + public PingFailedException(string message, BoundPeer target, Exception innerException) : base(message, innerException) { Target = target; } - protected PingTimeoutException(SerializationInfo info, StreamingContext context) + protected PingFailedException(SerializationInfo info, StreamingContext context) : base(info, context) { Target = info.GetValue(nameof(Target), typeof(BoundPeer)) is BoundPeer target diff --git a/test/Libplanet.Net.Tests/Protocols/TestTransport.cs b/test/Libplanet.Net.Tests/Protocols/TestTransport.cs index 730e94d32fb..33a82ab379d 100644 --- a/test/Libplanet.Net.Tests/Protocols/TestTransport.cs +++ b/test/Libplanet.Net.Tests/Protocols/TestTransport.cs @@ -255,7 +255,7 @@ async Task DoAddPeersAsync() "Different version encountered during {MethodName}()", nameof(AddPeersAsync)); } - catch (PingTimeoutException) + catch (PingFailedException) { var msg = $"Timeout occurred during {nameof(AddPeersAsync)}() after {timeout}"; diff --git a/test/Libplanet.Net.Tests/SwarmTest.AppProtocolVersion.cs b/test/Libplanet.Net.Tests/SwarmTest.AppProtocolVersion.cs index 8e39c2f777b..b179bd7ebb0 100644 --- a/test/Libplanet.Net.Tests/SwarmTest.AppProtocolVersion.cs +++ b/test/Libplanet.Net.Tests/SwarmTest.AppProtocolVersion.cs @@ -31,13 +31,12 @@ public async Task DetectAppProtocolVersion() await StartAsync(c); await StartAsync(d); - var peers = new[] { c.AsPeer, d.AsPeer }; - - foreach (var peer in peers) - { - await a.AddPeersAsync(new[] { peer }, null); - await b.AddPeersAsync(new[] { peer }, null); - } + await a.AddPeersAsync(new[] { c.AsPeer }, null); + await Assert.ThrowsAsync( + () => a.AddPeersAsync(new[] { d.AsPeer }, null)); + await Assert.ThrowsAsync( + () => b.AddPeersAsync(new[] { c.AsPeer }, null)); + await b.AddPeersAsync(new[] { d.AsPeer }, null); Assert.Equal(new[] { c.AsPeer }, a.Peers.ToArray()); Assert.Equal(new[] { d.AsPeer }, b.Peers.ToArray()); @@ -163,14 +162,20 @@ AppProtocolVersion localVersion await StartAsync(f); await a.AddPeersAsync(new[] { c.AsPeer }, TimeSpan.FromSeconds(1)); - await a.AddPeersAsync(new[] { d.AsPeer }, TimeSpan.FromSeconds(1)); - await a.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1)); - await a.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1)); - - await b.AddPeersAsync(new[] { c.AsPeer }, TimeSpan.FromSeconds(1)); + await Assert.ThrowsAsync( + () => a.AddPeersAsync(new[] { d.AsPeer }, TimeSpan.FromSeconds(1))); + await Assert.ThrowsAsync( + () => a.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1))); + await Assert.ThrowsAsync( + () => a.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1))); + + await Assert.ThrowsAsync( + () => b.AddPeersAsync(new[] { c.AsPeer }, TimeSpan.FromSeconds(1))); await b.AddPeersAsync(new[] { d.AsPeer }, TimeSpan.FromSeconds(1)); - await b.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1)); - await b.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1)); + await Assert.ThrowsAsync( + () => b.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1))); + await Assert.ThrowsAsync( + () => b.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1))); Assert.Equal(new[] { c.AsPeer }, a.Peers.ToArray()); Assert.Equal(new[] { d.AsPeer }, b.Peers.ToArray());