This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Fix #16274 - LazyInitializer.EnsureInitialized overload for reference types. #16507
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
53cb5a8
LazyInitializer.EnsureInitialized overload for reference types that does
WinCPP b63255c
Merge remote-tracking branch 'refs/remotes/dotnet/master' into issue-…
WinCPP 93de8cb
LazyInitializer.EnsureInitialized overload for reference types that does
WinCPP 0ca3ee2
Merge branch 'master' into issue-3862
WinCPP 2daa31e
Update ApiCompatBaseline.uapaot.txt
WinCPP 393548f
Update ApiCompatBaseline.uapaot.txt
WinCPP 36a8ceb
Update ApiCompatBaseline.uapaot.txt
WinCPP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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); | ||
|
|
||
| // 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; | ||
|
|
@@ -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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...