From cbba63c3d8365f594ca3750a505c5f52416bb4db Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 10 Jan 2025 18:03:00 +0100 Subject: [PATCH] Make sure the Setup of the AppBuilder is only done once. --- .../AppBuilderHelper.cs | 40 +++++++++++++++++++ .../TheToWriteableBitmapMethod.cs | 5 +-- .../TheToWriteableBitmapWithDensityMethod.cs | 5 +-- 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 tests/Magick.NET.AvaloniaMediaImaging.Tests/AppBuilderHelper.cs diff --git a/tests/Magick.NET.AvaloniaMediaImaging.Tests/AppBuilderHelper.cs b/tests/Magick.NET.AvaloniaMediaImaging.Tests/AppBuilderHelper.cs new file mode 100644 index 0000000000..d42cecd5f2 --- /dev/null +++ b/tests/Magick.NET.AvaloniaMediaImaging.Tests/AppBuilderHelper.cs @@ -0,0 +1,40 @@ +// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. +// Licensed under the Apache License, Version 2.0. + +using System.Threading; +using Avalonia; + +namespace Magick.NET.AvaloniaMediaImaging.Tests +{ + internal static class AppBuilderHelper + { + private static readonly SemaphoreSlim _lock = new(1); + private static bool _setupDone; + + public static void Setup() + { + if (_setupDone) + return; + + _lock.Wait(); + + try + { + + if (_setupDone) + return; + + _setupDone = true; + + AppBuilder + .Configure() + .UsePlatformDetect() + .SetupWithoutStarting(); + } + finally + { + _lock.Release(); + } + } + } +} diff --git a/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapMethod.cs b/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapMethod.cs index 6d4a32766f..cd8120e71a 100644 --- a/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapMethod.cs +++ b/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapMethod.cs @@ -15,10 +15,7 @@ public class TheToWriteableBitmapMethod [Fact] public void ShouldReturnWriteableBitmap() { - AppBuilder - .Configure() - .UsePlatformDetect() - .SetupWithoutStarting(); + AppBuilderHelper.Setup(); using var input = new MagickImage(Files.MagickNETIconPNG); using var bitmap = input.ToWriteableBitmap(); diff --git a/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapWithDensityMethod.cs b/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapWithDensityMethod.cs index 099d1d99db..1477bbf7ae 100644 --- a/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapWithDensityMethod.cs +++ b/tests/Magick.NET.AvaloniaMediaImaging.Tests/IMagickImageExtentionsTests/TheToWriteableBitmapWithDensityMethod.cs @@ -15,10 +15,7 @@ public class TheToWriteableBitmapWithDensityMethod [Fact] public void ShouldReturnWriteableBitmap() { - AppBuilder - .Configure() - .UsePlatformDetect() - .SetupWithoutStarting(); + AppBuilderHelper.Setup(); using var input = new MagickImage(Files.MagickNETIconPNG); input.Density = new Density(150, 300);