Skip to content

Commit 5337af9

Browse files
committed
Fix caching of Lazy<T> in tests
1 parent c82e8b2 commit 5337af9

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/libraries/Common/tests/TestUtilities/System/IO/FileCleanupTestBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ protected virtual void Dispose(bool disposing)
8484
protected string GetRandomLinkName() => GetTestFileName() + ".link";
8585
protected string GetRandomDirName() => GetTestFileName() + "_dir";
8686

87-
protected string GetRandomFilePath() => Path.Combine(ActualTestDirectory.Value, GetRandomFileName());
88-
protected string GetRandomLinkPath() => Path.Combine(ActualTestDirectory.Value, GetRandomLinkName());
89-
protected string GetRandomDirPath() => Path.Combine(ActualTestDirectory.Value, GetRandomDirName());
87+
protected string GetRandomFilePath() => Path.Combine(_actualTestDirectory.Value, GetRandomFileName());
88+
protected string GetRandomLinkPath() => Path.Combine(_actualTestDirectory.Value, GetRandomLinkName());
89+
protected string GetRandomDirPath() => Path.Combine(_actualTestDirectory.Value, GetRandomDirName());
9090

91-
private Lazy<string> ActualTestDirectory => new Lazy<string>(() => GetTestDirectoryActualCasing());
91+
private readonly Lazy<string> _actualTestDirectory = new Lazy<string>(() => GetTestDirectoryActualCasing());
9292

9393
/// <summary>Gets a test file full path that is associated with the call site.</summary>
9494
/// <param name="index">An optional index value to use as a suffix on the file name. Typically a loop index.</param>

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static partial class PlatformDetection
2323
// do it in a way that failures don't cascade.
2424
//
2525

26-
private static Lazy<bool> s_IsInHelix => new Lazy<bool>(() => Environment.GetEnvironmentVariables().Keys.Cast<string>().Any(key => key.StartsWith("HELIX")));
26+
private static readonly Lazy<bool> s_IsInHelix = new Lazy<bool>(() => Environment.GetEnvironmentVariables().Keys.Cast<string>().Any(key => key.StartsWith("HELIX")));
2727
public static bool IsInHelix => s_IsInHelix.Value;
2828

2929
public static bool IsNetCore => Environment.Version.Major >= 5 || RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);
@@ -66,9 +66,9 @@ public static partial class PlatformDetection
6666
public static bool Is64BitProcess => IntPtr.Size == 8;
6767
public static bool IsNotWindows => !IsWindows;
6868

69-
private static Lazy<bool> s_isCheckedRuntime => new Lazy<bool>(() => AssemblyConfigurationEquals("Checked"));
70-
private static Lazy<bool> s_isReleaseRuntime => new Lazy<bool>(() => AssemblyConfigurationEquals("Release"));
71-
private static Lazy<bool> s_isDebugRuntime => new Lazy<bool>(() => AssemblyConfigurationEquals("Debug"));
69+
private static readonly Lazy<bool> s_isCheckedRuntime = new Lazy<bool>(() => AssemblyConfigurationEquals("Checked"));
70+
private static readonly Lazy<bool> s_isReleaseRuntime = new Lazy<bool>(() => AssemblyConfigurationEquals("Release"));
71+
private static readonly Lazy<bool> s_isDebugRuntime = new Lazy<bool>(() => AssemblyConfigurationEquals("Debug"));
7272

7373
public static bool IsCheckedRuntime => s_isCheckedRuntime.Value;
7474
public static bool IsReleaseRuntime => s_isReleaseRuntime.Value;
@@ -209,7 +209,7 @@ public static bool IsMetadataTokenSupported
209209
// Linux - OpenSsl supports alpn from openssl 1.0.2 and higher.
210210
// OSX - SecureTransport doesn't expose alpn APIs. TODO https://github.com/dotnet/runtime/issues/27727
211211
// Android - Platform supports alpn from API level 29 and higher
212-
private static Lazy<bool> s_supportsAlpn = new Lazy<bool>(GetAlpnSupport);
212+
private static readonly Lazy<bool> s_supportsAlpn = new Lazy<bool>(GetAlpnSupport);
213213
private static bool GetAlpnSupport()
214214
{
215215
if (IsWindows && !IsWindows7 && !IsNetFramework)
@@ -238,11 +238,11 @@ private static bool GetAlpnSupport()
238238
public static bool SupportsAlpn => s_supportsAlpn.Value;
239239
public static bool SupportsClientAlpn => SupportsAlpn || IsOSX || IsMacCatalyst || IsiOS || IstvOS;
240240

241-
private static Lazy<bool> s_supportsTls10 = new Lazy<bool>(GetTls10Support);
242-
private static Lazy<bool> s_supportsTls11 = new Lazy<bool>(GetTls11Support);
243-
private static Lazy<bool> s_supportsTls12 = new Lazy<bool>(GetTls12Support);
244-
private static Lazy<bool> s_supportsTls13 = new Lazy<bool>(GetTls13Support);
245-
private static Lazy<bool> s_sendsCAListByDefault = new Lazy<bool>(GetSendsCAListByDefault);
241+
private static readonly Lazy<bool> s_supportsTls10 = new Lazy<bool>(GetTls10Support);
242+
private static readonly Lazy<bool> s_supportsTls11 = new Lazy<bool>(GetTls11Support);
243+
private static readonly Lazy<bool> s_supportsTls12 = new Lazy<bool>(GetTls12Support);
244+
private static readonly Lazy<bool> s_supportsTls13 = new Lazy<bool>(GetTls13Support);
245+
private static readonly Lazy<bool> s_sendsCAListByDefault = new Lazy<bool>(GetSendsCAListByDefault);
246246

247247
public static bool SupportsTls10 => s_supportsTls10.Value;
248248
public static bool SupportsTls11 => s_supportsTls11.Value;
@@ -251,7 +251,7 @@ private static bool GetAlpnSupport()
251251
public static bool SendsCAListByDefault => s_sendsCAListByDefault.Value;
252252
public static bool SupportsSendingCustomCANamesInTls => UsesAppleCrypto || IsOpenSslSupported || (PlatformDetection.IsWindows8xOrLater && SendsCAListByDefault);
253253

254-
private static Lazy<bool> s_largeArrayIsNotSupported = new Lazy<bool>(IsLargeArrayNotSupported);
254+
private static readonly Lazy<bool> s_largeArrayIsNotSupported = new Lazy<bool>(IsLargeArrayNotSupported);
255255

256256
[MethodImpl(MethodImplOptions.NoOptimization)]
257257
private static bool IsLargeArrayNotSupported()

0 commit comments

Comments
 (0)