From 8e49e500eaed6064a95a3be7e75c2a98a727cfdd Mon Sep 17 00:00:00 2001 From: Antonin Lenfant-Kodia Date: Sun, 17 May 2026 23:26:17 +0200 Subject: [PATCH] fix(ci): Resolve race conditions in tests and fix unused variable warning - Refactored Torrents.cs to use local DbSettings instead of global Settings.Get - Removed unused exception variable in Torrents.cs - Disabled xUnit parallelization globally to prevent test race conditions caused by shared global state - Enforced sequential test execution in CI workflow --- .github/workflows/dotnet-test.yml | 2 +- .../GlobalTestConfig.cs | 3 +++ .../Services/TorrentsTest.cs | 20 +++++++++---------- server/RdtClient.Service/Services/Torrents.cs | 12 +++++------ server/RdtClient.Web.Test/GlobalTestConfig.cs | 3 +++ 5 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 server/RdtClient.Service.Test/GlobalTestConfig.cs create mode 100644 server/RdtClient.Web.Test/GlobalTestConfig.cs diff --git a/.github/workflows/dotnet-test.yml b/.github/workflows/dotnet-test.yml index 7345ced..bc852d1 100644 --- a/.github/workflows/dotnet-test.yml +++ b/.github/workflows/dotnet-test.yml @@ -21,4 +21,4 @@ jobs: - name: Build run: dotnet build --no-restore server - name: Test - run: dotnet test --no-build --verbosity normal server + run: dotnet test --no-build --verbosity normal -maxcpucount:1 server diff --git a/server/RdtClient.Service.Test/GlobalTestConfig.cs b/server/RdtClient.Service.Test/GlobalTestConfig.cs new file mode 100644 index 0000000..fa16e4c --- /dev/null +++ b/server/RdtClient.Service.Test/GlobalTestConfig.cs @@ -0,0 +1,3 @@ +using Xunit; + +[assembly: CollectionBehavior(DisableParallelization = true)] diff --git a/server/RdtClient.Service.Test/Services/TorrentsTest.cs b/server/RdtClient.Service.Test/Services/TorrentsTest.cs index eb4435d..ad7103e 100644 --- a/server/RdtClient.Service.Test/Services/TorrentsTest.cs +++ b/server/RdtClient.Service.Test/Services/TorrentsTest.cs @@ -96,7 +96,7 @@ public class TorrentsTest if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - Settings.Get.DownloadClient.DownloadPath = settings.DownloadClient.DownloadPath = @"C:\Downloads"; + settings.DownloadClient.DownloadPath = @"C:\Downloads"; } var downloadPath = Path.Combine(settings.DownloadClient.DownloadPath, category); @@ -165,9 +165,9 @@ public class TorrentsTest mocks.TorrentDataMock.Setup(t => t.GetById(torrent.TorrentId)).Returns(Task.FromResult(torrent)); mocks.DownloadsMock.Setup(d => d.GetForTorrent(torrent.TorrentId)).ReturnsAsync(downloads); - var downloadPath = $"{settings.DownloadClient.DownloadPath}/{torrent.Category}"; - var torrentPath = $"{downloadPath}/{torrent.RdName}"; - var filePath = $"{torrentPath}/{downloads[0].FileName}"; + var downloadPath = Path.Combine(settings.DownloadClient.DownloadPath, torrent.Category ?? ""); + var torrentPath = Path.Combine(downloadPath, torrent.RdName ?? ""); + var filePath = Path.Combine(torrentPath, downloads[0].FileName ?? ""); var fileSystemMock = new MockFileSystem(new Dictionary { @@ -213,9 +213,9 @@ public class TorrentsTest mocks.TorrentDataMock.Setup(t => t.GetById(torrent.TorrentId)).Returns(Task.FromResult(torrent)); mocks.DownloadsMock.Setup(d => d.GetForTorrent(torrent.TorrentId)).ReturnsAsync(downloads); - var downloadPath = $"{settings.DownloadClient.DownloadPath}/{torrent.Category}"; - var torrentPath = $"{downloadPath}/{torrent.RdName}"; - var filePath = $"{torrentPath}/{downloads[0].FileName}"; + var downloadPath = Path.Combine(settings.DownloadClient.DownloadPath, torrent.Category ?? ""); + var torrentPath = Path.Combine(downloadPath, torrent.RdName ?? ""); + var filePath = Path.Combine(torrentPath, downloads[0].FileName ?? ""); var fileSystemMock = new MockFileSystem(new Dictionary { @@ -280,9 +280,9 @@ public class TorrentsTest mocks.TorrentDataMock.Setup(t => t.GetById(torrent.TorrentId)).Returns(Task.FromResult(torrent)); mocks.DownloadsMock.Setup(d => d.GetForTorrent(torrent.TorrentId)).ReturnsAsync(downloads); - var downloadPath = $"{settings.DownloadClient.DownloadPath}/{torrent.Category}"; - var torrentPath = $"{downloadPath}/{torrent.RdName}"; - var filePath = $"{torrentPath}/{downloads[0].FileName}"; + var downloadPath = Path.Combine(settings.DownloadClient.DownloadPath, torrent.Category ?? ""); + var torrentPath = Path.Combine(downloadPath, torrent.RdName ?? ""); + var filePath = Path.Combine(torrentPath, downloads[0].FileName ?? ""); var fileSystemMock = new MockFileSystem(new Dictionary { diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index b0eed08..bdd34b8 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -569,7 +569,7 @@ public class Torrents( if (deleteLocalFiles && !String.IsNullOrWhiteSpace(torrent.RdName)) { - var downloadPath = DownloadPath(torrent); + var downloadPath = DownloadPath(torrent, Settings.Get); downloadPath = Path.Combine(downloadPath, torrent.RdName); Log($"Deleting local files in {downloadPath}", torrent); @@ -828,7 +828,7 @@ public class Torrents( await Task.Delay(100); } - var downloadPath = DownloadPath(download.Torrent!); + var downloadPath = DownloadPath(download.Torrent!, Settings.Get); var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download); @@ -929,9 +929,9 @@ public class Torrents( return torrent; } - private static String DownloadPath(Torrent torrent) + private static String DownloadPath(Torrent torrent, DbSettings settings) { - var settingDownloadPath = Settings.Get.DownloadClient.DownloadPath; + var settingDownloadPath = settings.DownloadClient.DownloadPath; if (!String.IsNullOrWhiteSpace(torrent.Category)) { @@ -988,7 +988,7 @@ public class Torrents( Log($"Parsing external program {fileName} with arguments {arguments}", torrent); - var downloadPath = DownloadPath(torrent); + var downloadPath = DownloadPath(torrent, settings); var torrentPath = Path.Combine(downloadPath, torrent.RdName ?? "Unknown"); var filePath = torrentPath; @@ -1083,7 +1083,7 @@ public class Torrents( await torrentData.UpdateRdData(torrent); } } - catch (Exception ex) + catch (Exception) { // ignored } diff --git a/server/RdtClient.Web.Test/GlobalTestConfig.cs b/server/RdtClient.Web.Test/GlobalTestConfig.cs new file mode 100644 index 0000000..fa16e4c --- /dev/null +++ b/server/RdtClient.Web.Test/GlobalTestConfig.cs @@ -0,0 +1,3 @@ +using Xunit; + +[assembly: CollectionBehavior(DisableParallelization = true)]