From 2c67e502e4c4317cf1fa0dc586af011bb68ef764 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Mon, 13 Feb 2023 16:42:03 +0200 Subject: [PATCH] Optimize export locking in tests --- DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs b/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs index af7a8ed8..d4776c4c 100644 --- a/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs +++ b/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs @@ -20,7 +20,7 @@ namespace DiscordChatExporter.Cli.Tests.Infra; public static class ExportWrapper { - private static readonly ConcurrentDictionary Locks = new(); + private static readonly ConcurrentDictionary Locks = new(StringComparer.Ordinal); private static readonly string DirPath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Directory.GetCurrentDirectory(), @@ -42,15 +42,15 @@ public static class ExportWrapper private static async ValueTask ExportAsync(Snowflake channelId, ExportFormat format) { + var fileName = channelId.ToString() + '.' + format.GetFileExtension(); + var filePath = Path.Combine(DirPath, fileName); + // Lock separately for each channel and format - var channelLock = Locks.GetOrAdd($"{channelId}_{format}", _ => new SemaphoreSlim(1, 1)); + var channelLock = Locks.GetOrAdd(filePath, _ => new SemaphoreSlim(1, 1)); await channelLock.WaitAsync(); try { - var fileName = channelId.ToString() + '.' + format.GetFileExtension(); - var filePath = Path.Combine(DirPath, fileName); - // Perform export only if it hasn't been done before if (!File.Exists(filePath)) {