From 6dbc5ec70a396b0d8dc20d74077706112d27bd88 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 18 Jul 2022 02:32:47 +1000 Subject: [PATCH] Fix param caching test --- .../ImageProviders/FileProvider.cs | 5 ++ .../Tests/TestImageProviderTests.cs | 65 ++++++++++++++----- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index 5bf28b3c8b..1fc0100a69 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -82,6 +82,11 @@ namespace SixLabors.ImageSharp.Tests { foreach (PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { + if (p.PropertyType == typeof(DecoderOptions)) + { + continue; + } + string key = $"{type.FullName}.{p.Name}"; data[key] = p.GetValue(options); } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index fed4c75871..d75ac21dca 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -109,19 +109,31 @@ namespace SixLabors.ImageSharp.Tests TestDecoderWithParameters.DoTestThreadSafe( () => { - string testName = nameof(this + const string testName = nameof(this .GetImage_WithCustomParametricDecoder_ShouldNotUtilizeCache_WhenParametersAreNotEqual); - var decoder1 = new TestDecoderWithParameters { Param1 = "Lol", Param2 = 42 }; + TestDecoderWithParameters decoder1 = new(); + TestDecoderWithParametersOptions options1 = new() + { + Param1 = "Lol", + Param2 = 42 + }; + decoder1.InitCaller(testName); - var decoder2 = new TestDecoderWithParameters { Param1 = "LoL", Param2 = 42 }; + TestDecoderWithParameters decoder2 = new(); + TestDecoderWithParametersOptions options2 = new() + { + Param1 = "LoL", + Param2 = 42 + }; + decoder2.InitCaller(testName); - provider.GetImage(decoder1); + provider.GetImage(decoder1, options1); Assert.Equal(1, TestDecoderWithParameters.GetInvocationCount(testName)); - provider.GetImage(decoder2); + provider.GetImage(decoder2, options2); Assert.Equal(2, TestDecoderWithParameters.GetInvocationCount(testName)); }); } @@ -143,19 +155,31 @@ namespace SixLabors.ImageSharp.Tests TestDecoderWithParameters.DoTestThreadSafe( () => { - string testName = nameof(this + const string testName = nameof(this .GetImage_WithCustomParametricDecoder_ShouldUtilizeCache_WhenParametersAreEqual); - var decoder1 = new TestDecoderWithParameters { Param1 = "Lol", Param2 = 666 }; + TestDecoderWithParameters decoder1 = new(); + TestDecoderWithParametersOptions options1 = new() + { + Param1 = "Lol", + Param2 = 666 + }; + decoder1.InitCaller(testName); - var decoder2 = new TestDecoderWithParameters { Param1 = "Lol", Param2 = 666 }; + TestDecoderWithParameters decoder2 = new(); + TestDecoderWithParametersOptions options2 = new() + { + Param1 = "Lol", + Param2 = 666 + }; + decoder2.InitCaller(testName); - provider.GetImage(decoder1); + provider.GetImage(decoder1, options1); Assert.Equal(1, TestDecoderWithParameters.GetInvocationCount(testName)); - provider.GetImage(decoder2); + provider.GetImage(decoder2, options2); Assert.Equal(1, TestDecoderWithParameters.GetInvocationCount(testName)); }); } @@ -365,7 +389,7 @@ namespace SixLabors.ImageSharp.Tests } } - private class TestDecoderWithParameters : ImageDecoder + private class TestDecoderWithParameters : ImageDecoder { private static readonly ConcurrentDictionary InvocationCounts = new(); @@ -373,10 +397,6 @@ namespace SixLabors.ImageSharp.Tests private string callerName; - public string Param1 { get; set; } - - public int Param2 { get; set; } - public static void DoTestThreadSafe(Action action) { lock (Monitor) @@ -385,16 +405,16 @@ namespace SixLabors.ImageSharp.Tests } } - public override IImageInfo IdentifySpecialized(TestDecoderOptions options, Stream stream, CancellationToken cancellationToken) + public override IImageInfo IdentifySpecialized(TestDecoderWithParametersOptions options, Stream stream, CancellationToken cancellationToken) => this.DecodeSpecialized(options, stream, cancellationToken); - public override Image DecodeSpecialized(TestDecoderOptions options, Stream stream, CancellationToken cancellationToken) + public override Image DecodeSpecialized(TestDecoderWithParametersOptions options, Stream stream, CancellationToken cancellationToken) { InvocationCounts[this.callerName]++; return new Image(42, 42); } - public override Image DecodeSpecialized(TestDecoderOptions options, Stream stream, CancellationToken cancellationToken) + public override Image DecodeSpecialized(TestDecoderWithParametersOptions options, Stream stream, CancellationToken cancellationToken) => this.DecodeSpecialized(options, stream, cancellationToken); internal static int GetInvocationCount(string callerName) => InvocationCounts[callerName]; @@ -410,5 +430,14 @@ namespace SixLabors.ImageSharp.Tests { public DecoderOptions GeneralOptions { get; set; } = new(); } + + private class TestDecoderWithParametersOptions : ISpecializedDecoderOptions + { + public string Param1 { get; set; } + + public int Param2 { get; set; } + + public DecoderOptions GeneralOptions { get; set; } = new(); + } } }