Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/System.Runtime/tests/System/LazyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Tests;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Tests
Expand Down Expand Up @@ -463,27 +464,35 @@ public static void EnsureInitialized_ComplexRefTypes()
object aLock = null;
Assert.NotNull(LazyInitializer.EnsureInitialized(ref a, ref aInit, ref aLock));
Assert.NotNull(a);
Assert.True(aInit);
Assert.NotNull(aLock);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub I have added asserts for true / not null in earlier test cases as well. Please let me know if this is ok...


// Activator.CreateInstance (already initialized).
HasDefaultCtor b = hdcTemplate;
bool bInit = true;
object bLock = null;
Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized(ref b, ref bInit, ref bLock));
Assert.Equal(hdcTemplate, b);
Assert.True(bInit);
Assert.Null(bLock);

// Func based initialization (uninitialized).
string c = null;
bool cInit = false;
object cLock = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref c, ref cInit, ref cLock, () => strTemplate));
Assert.Equal(strTemplate, c);
Assert.True(cInit);
Assert.NotNull(cLock);

// Func based initialization (already initialized).
string d = strTemplate;
bool dInit = true;
object dLock = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref d, ref dInit, ref dLock, () => strTemplate + "bar"));
Assert.Equal(strTemplate, d);
Assert.True(dInit);
Assert.Null(dLock);

// Func based initialization (nulls *ARE* permitted).
string e = null;
Expand All @@ -494,7 +503,28 @@ public static void EnsureInitialized_ComplexRefTypes()
Assert.Null(LazyInitializer.EnsureInitialized(ref e, ref einit, ref elock, () => { initCount++; return null; }));
Assert.Null(e);
Assert.Equal(1, initCount);
Assert.True(einit);
Assert.NotNull(elock);
Assert.Null(LazyInitializer.EnsureInitialized(ref e, ref einit, ref elock, () => { initCount++; return null; }));

// Func based initialization without tracking boolean (uninitialized).
string f = null;
object fLock = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref f, ref fLock, () => strTemplate));
Assert.Equal(strTemplate, f);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also Assert.NotNull(fLock)

Assert.NotNull(fLock);

// Func based initialization without tracking boolean (already initialized).
string g = strTemplate;
object gLock = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref g, ref gLock, () => strTemplate + "bar"));
Assert.Equal(strTemplate, g);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should Assert.Null(gLock)

Assert.Null(gLock);

// Func based initialization without tracking boolean (nulls not permitted).
string h = null;
object hLock = null;
Assert.Throws<InvalidOperationException>(() => LazyInitializer.EnsureInitialized(ref h, ref hLock, () => null));
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions src/System.Threading/ref/System.Threading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public static partial class LazyInitializer
public static T EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock) { throw null; }
public static T EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock, System.Func<T> valueFactory) { throw null; }
public static T EnsureInitialized<T>(ref T target, System.Func<T> valueFactory) where T : class { throw null; }
public static T EnsureInitialized<T>(ref T target, ref object syncLock, System.Func<T> valueFactory) where T : class { throw null; }
}
public partial struct LockCookie
{
Expand Down
3 changes: 2 additions & 1 deletion src/System.Threading/src/ApiCompatBaseline.uapaot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ MembersMustExist : Member 'System.Threading.SynchronizationContext.Wait(System.I
MembersMustExist : Member 'System.Threading.SynchronizationContext.WaitHelper(System.IntPtr[], System.Boolean, System.Int32)' does not exist in the implementation but it does exist in the contract.
CannotRemoveBaseTypeOrInterface : Type 'System.Threading.SynchronizationLockException' does not inherit from base type 'System.SystemException' in the implementation but it does in the contract.
CannotRemoveBaseTypeOrInterface : Type 'System.Threading.WaitHandleCannotBeOpenedException' does not inherit from base type 'System.ApplicationException' in the implementation but it does in the contract.
Total Issues: 26
MembersMustExist : Member 'System.Threading.LazyInitializer.EnsureInitialized<T>(T, System.Object, System.Func<T>)' does not exist in the implementation but it does exist in the contract.
Total Issues: 27