Merge pull request #597 from rosenbjerg/improve-tests-usage-of-cancellation-token

Improve tests usage of cancellation token
This commit is contained in:
Malte Rosenbjerg 2025-10-27 19:47:19 +01:00 committed by GitHub
commit dbf672fd6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 14 deletions

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)

View file

@ -1050,7 +1050,7 @@ public class VideoTest
{ {
using var outputFile = new TemporaryFile("out.mp4"); using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource(); using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
var task = FFMpegArguments var task = FFMpegArguments
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args .FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
@ -1061,7 +1061,6 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token) .CancellableThrough(cts.Token)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false); .ProcessAsynchronously(false);
cts.CancelAfter(300); cts.CancelAfter(300);
@ -1077,7 +1076,7 @@ public class VideoTest
{ {
using var outputFile = new TemporaryFile("out.mp4"); using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource(); using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
var task = FFMpegArguments var task = FFMpegArguments
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args .FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
@ -1088,7 +1087,6 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264) .WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast)) .WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token) .CancellableThrough(cts.Token)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(); .ProcessAsynchronously();
cts.CancelAfter(300); cts.CancelAfter(300);
@ -1102,7 +1100,7 @@ public class VideoTest
{ {
using var outputFile = new TemporaryFile("out.mp4"); using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource(); using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
var task = FFMpegArguments var task = FFMpegArguments
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args .FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
@ -1126,7 +1124,7 @@ public class VideoTest
{ {
using var outputFile = new TemporaryFile("out.mp4"); using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource(); using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
cts.Cancel(); cts.Cancel();
var task = FFMpegArguments var task = FFMpegArguments
@ -1149,7 +1147,7 @@ public class VideoTest
{ {
using var outputFile = new TemporaryFile("out.mp4"); using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource(); using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
var task = FFMpegArguments var task = FFMpegArguments
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args .FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args