From 5c79bb89545c5f0a52397fe320ac0e8144b4c8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Sat, 9 May 2026 11:52:03 +0800 Subject: [PATCH] =?UTF-8?q?ResilientUtils=20=E4=BB=A3=E7=A0=81=E5=A4=8D?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Utils/PathUtils.cs | 1 + Shared/Utils/ResilientUtils.cs | 26 +++++++++----------------- Tests/Utils/PathUtilsTest.cs | 8 ++++---- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index 521c044..c47adfd 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -25,6 +25,7 @@ public static class PathUtils { /// public static string ToShortPath(string fullName, bool keepFileName = false) { if (string.IsNullOrEmpty(fullName)) return fullName; + if (!fullName.Contains(":")) return fullName; fullName = PathUtils.Normalize(fullName, false); if (fullName.Length <= 200) return fullName; diff --git a/Shared/Utils/ResilientUtils.cs b/Shared/Utils/ResilientUtils.cs index 6cc97e1..a7e35d9 100644 --- a/Shared/Utils/ResilientUtils.cs +++ b/Shared/Utils/ResilientUtils.cs @@ -9,20 +9,8 @@ public static class ResilientUtils { /// /// 在抛出特定异常时,延迟并自动重试。 /// - public static void RetryOn(Action action, int maxAttempts = 2, int delayMs = 200, [CallerMemberName] string caller = "") where TException : Exception { - int attempt = 0; - while (true) { - try { - action(); - return; // 成功则退出 - } catch (TException ex) { - attempt++; - if (attempt >= maxAttempts) throw; // 超过最大尝试次数 - Logger.Warn(ex, $"{caller} 第 {attempt} 次尝试失败,将在 {delayMs}ms 后重试"); - Thread.Sleep(delayMs); - } - } - } + public static void RetryOn(Action action, int maxAttempts = 2, int delayMs = 200, [CallerMemberName] string caller = "") where TException : Exception + => RetryOn(() => { action(); return null; }, maxAttempts, delayMs, caller); /// /// 在抛出异常时,延迟并自动重试。 @@ -39,9 +27,13 @@ public static class ResilientUtils { return func(); // 成功则退出 } catch (TException ex) { attempt++; - if (attempt >= maxAttempts) throw; // 超过最大尝试次数 - Logger.Warn(ex, $"{caller} 第 {attempt} 次尝试失败,将在 {delayMs}ms 后重试"); - Thread.Sleep(delayMs); + if (attempt < maxAttempts) { + Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,将在 {delayMs}ms 后重试"); + Thread.Sleep(delayMs); + } else { // 超过最大尝试次数 + Logger.Warn(ex, $"第 {attempt} 次 {caller} 尝试失败,已到达最大尝试次数"); + throw; + } } } } diff --git a/Tests/Utils/PathUtilsTest.cs b/Tests/Utils/PathUtilsTest.cs index f798100..543b370 100644 --- a/Tests/Utils/PathUtilsTest.cs +++ b/Tests/Utils/PathUtilsTest.cs @@ -6,9 +6,9 @@ public class PathUtilsTests { [Test] [Arguments(@"\\?\C:\foo\bar.txt", @"\\?\C:\foo")] [Arguments(@"C:\foo\bar.txt", @"C:\foo")] - [Arguments(@"\\?\C:\foo\bar", @"\\?\C:\foo")] + [Arguments(@"\\?\C:\foo\Bar", @"\\?\C:\foo")] [Arguments(@"C:\foo\bar", @"C:\foo")] - [Arguments(@"C:\foo\bar\", @"C:\foo")] + [Arguments(@"C:\Foo\bar\", @"C:\Foo")] [Arguments(@"\\?\C:\foo\", @"\\?\C:")] [Arguments(@"C:\foo", @"C:")] [Arguments(@"C:\foo\", @"C:")] @@ -31,7 +31,7 @@ public class PathUtilsTests { [Arguments("https://foo.bar/file/long/pack.zip?arg=1", "pack.zip")] [Arguments("https://foo.bar/file/", "file")] [Arguments("https://foo.bar/file/?arg=1", "file")] - [Arguments("https://foo.bar/file?arg=1", "file")] + [Arguments("https://foo.bar/File?arg=1", "File")] public async Task 路径处理_GetLastPart(string input, string expected) => await Assert.That(PathUtils.GetLastPart(input)).IsEqualTo(expected); @@ -44,7 +44,7 @@ public class PathUtilsTests { [Arguments(@"\\?\C:\foo\", "foo")] [Arguments(@"C:\foo", "foo")] [Arguments(@"C:\foo\", "foo")] - [Arguments("create.jar.disabled", "create.jar")] + [Arguments("cREate.jar.disabled", "cREate.jar")] [Arguments("create.jar", "create")] [Arguments("create", "create")] [Arguments("https://foo.bar/file/page.xaml.vb?arg=1", "page.xaml")]