Skip to content

Commit 8539c9e

Browse files
Merge pull request #1415 from SixLabors/af/fix-Decode_IsCancellable
Fix JpegDecoderTests.Decode_IsCancellable
2 parents 10a9853 + 5f10f9a commit 8539c9e

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,32 @@ public async Task Decode_IsCancellable(string fileName, int cancellationDelayMs)
141141
TestEnvironment.InputImagesDirectoryFullPath,
142142
fileName);
143143

144-
var cts = new CancellationTokenSource();
145-
if (cancellationDelayMs == 0)
146-
{
147-
cts.Cancel();
148-
}
149-
else
144+
const int NumberOfRuns = 5;
145+
146+
for (int i = 0; i < NumberOfRuns; i++)
150147
{
151-
cts.CancelAfter(cancellationDelayMs);
148+
var cts = new CancellationTokenSource();
149+
if (cancellationDelayMs == 0)
150+
{
151+
cts.Cancel();
152+
}
153+
else
154+
{
155+
cts.CancelAfter(cancellationDelayMs);
156+
}
157+
158+
try
159+
{
160+
using var image = await Image.LoadAsync(hugeFile, cts.Token);
161+
}
162+
catch (TaskCanceledException)
163+
{
164+
// Succesfully observed a cancellation
165+
return;
166+
}
152167
}
153168

154-
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync(hugeFile, cts.Token));
169+
throw new Exception($"No cancellation happened out of {NumberOfRuns} runs!");
155170
}
156171

157172
[Theory(Skip = "Identify is too fast, doesn't work reliably.")]

0 commit comments

Comments
 (0)