Use using for CancellationTokenSource

This commit is contained in:
Malte Rosenbjerg 2025-10-27 19:39:13 +01:00
parent 919c6ef526
commit 3c8d2c23c1

View file

@ -1,4 +1,5 @@
using FFMpegCore.Exceptions; using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using FFMpegCore.Test.Resources; using FFMpegCore.Test.Resources;
namespace FFMpegCore.Test; namespace FFMpegCore.Test;
@ -292,7 +293,7 @@ public class FFProbeTests
public async Task Parallel_FFProbe_Cancellation_Should_Throw_Only_OperationCanceledException() public async Task Parallel_FFProbe_Cancellation_Should_Throw_Only_OperationCanceledException()
{ {
// Warm up FFMpegCore environment // Warm up FFMpegCore environment
Helpers.FFProbeHelper.VerifyFFProbeExists(GlobalFFOptions.Current); FFProbeHelper.VerifyFFProbeExists(GlobalFFOptions.Current);
var mp4 = TestResources.Mp4Video; var mp4 = TestResources.Mp4Video;
if (!File.Exists(mp4)) if (!File.Exists(mp4))
@ -301,28 +302,26 @@ public class FFProbeTests
return; return;
} }
var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken); using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
var token = cts.Token;
using var semaphore = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount); using var semaphore = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount);
var tasks = Enumerable.Range(0, 50).Select(x => Task.Run(async () => var tasks = Enumerable.Range(0, 50).Select(x => Task.Run(async () =>
{ {
await semaphore.WaitAsync(token); await semaphore.WaitAsync(cts.Token);
try try
{ {
var analysis = await FFProbe.AnalyseAsync(mp4, cancellationToken: token); var analysis = await FFProbe.AnalyseAsync(mp4, cancellationToken: cts.Token);
return analysis; return analysis;
} }
finally finally
{ {
semaphore.Release(); semaphore.Release();
} }
}, token)).ToList(); }, cts.Token)).ToList();
// Wait for 2 tasks to finish, then cancel all // Wait for 2 tasks to finish, then cancel all
await Task.WhenAny(tasks); await Task.WhenAny(tasks);
await Task.WhenAny(tasks); await Task.WhenAny(tasks);
await cts.CancelAsync(); await cts.CancelAsync();
cts.Dispose();
var exceptions = new List<Exception>(); var exceptions = new List<Exception>();
foreach (var task in tasks) foreach (var task in tasks)