mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-05-23 11:16:59 -04:00
Optimize export locking in tests
This commit is contained in:
parent
1fc27fd5c4
commit
2c67e502e4
1 changed files with 5 additions and 5 deletions
|
@ -20,7 +20,7 @@ namespace DiscordChatExporter.Cli.Tests.Infra;
|
||||||
|
|
||||||
public static class ExportWrapper
|
public static class ExportWrapper
|
||||||
{
|
{
|
||||||
private static readonly ConcurrentDictionary<string, SemaphoreSlim> Locks = new();
|
private static readonly ConcurrentDictionary<string, SemaphoreSlim> Locks = new(StringComparer.Ordinal);
|
||||||
|
|
||||||
private static readonly string DirPath = Path.Combine(
|
private static readonly string DirPath = Path.Combine(
|
||||||
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Directory.GetCurrentDirectory(),
|
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? Directory.GetCurrentDirectory(),
|
||||||
|
@ -42,15 +42,15 @@ public static class ExportWrapper
|
||||||
|
|
||||||
private static async ValueTask<string> ExportAsync(Snowflake channelId, ExportFormat format)
|
private static async ValueTask<string> ExportAsync(Snowflake channelId, ExportFormat format)
|
||||||
{
|
{
|
||||||
|
var fileName = channelId.ToString() + '.' + format.GetFileExtension();
|
||||||
|
var filePath = Path.Combine(DirPath, fileName);
|
||||||
|
|
||||||
// Lock separately for each channel and format
|
// 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();
|
await channelLock.WaitAsync();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var fileName = channelId.ToString() + '.' + format.GetFileExtension();
|
|
||||||
var filePath = Path.Combine(DirPath, fileName);
|
|
||||||
|
|
||||||
// Perform export only if it hasn't been done before
|
// Perform export only if it hasn't been done before
|
||||||
if (!File.Exists(filePath))
|
if (!File.Exists(filePath))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue