Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2f8ba83

Browse files
author
Atsushi Kanamori
committed
Fix LazyTests.Ctor_ExceptionRecovery test
On AoT, XUnit itself was invoking the Lazy object's Value property behind our backs. This particular test is sensitive to that. (The reason why this was happening on AoT and not CoreClr was because CoreClr finds Lazy's ToString() override and invokes that. We could make the same happen pm AoT with an rd.xml but it's simply more reliable not to give XUnit a chance to meddle in the first place.)
1 parent 6f07f7e commit 2f8ba83

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/System.Runtime/tests/System/LazyTests.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,35 @@ public InitiallyExceptionThrowingCtor()
150150
}
151151
}
152152

153-
public static IEnumerable<object[]> Ctor_ExceptionRecovery_MemberData()
153+
private static IEnumerable<Lazy<InitiallyExceptionThrowingCtor>> Ctor_ExceptionRecovery_MemberData()
154154
{
155-
yield return new object[] { new Lazy<InitiallyExceptionThrowingCtor>(), 5 };
156-
yield return new object[] { new Lazy<InitiallyExceptionThrowingCtor>(true), 5 };
157-
yield return new object[] { new Lazy<InitiallyExceptionThrowingCtor>(false), 5 };
158-
yield return new object[] { new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.ExecutionAndPublication), 5 };
159-
yield return new object[] { new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.None), 5 };
160-
yield return new object[] { new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.PublicationOnly), 5 };
155+
yield return new Lazy<InitiallyExceptionThrowingCtor>();
156+
yield return new Lazy<InitiallyExceptionThrowingCtor>(true);
157+
yield return new Lazy<InitiallyExceptionThrowingCtor>(false);
158+
yield return new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.ExecutionAndPublication);
159+
yield return new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.None);
160+
yield return new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.PublicationOnly);
161161
}
162162

163-
[Theory]
164-
[MemberData(nameof(Ctor_ExceptionRecovery_MemberData))]
165-
public static void Ctor_ExceptionRecovery(Lazy<InitiallyExceptionThrowingCtor> lazy, int expected)
163+
//
164+
// Do not use [Theory]. XUnit argument formatter can invoke the lazy.Value property underneath you and ruin the assumptions
165+
// made by the test.
166+
//
167+
[Fact]
168+
public static void Ctor_ExceptionRecovery()
166169
{
167-
InitiallyExceptionThrowingCtor.counter = 0;
168-
InitiallyExceptionThrowingCtor result = null;
169-
for (var i = 0; i < 10; ++i)
170+
foreach (Lazy<InitiallyExceptionThrowingCtor> lazy in Ctor_ExceptionRecovery_MemberData())
170171
{
171-
try { result = lazy.Value; } catch (Exception) { }
172+
InitiallyExceptionThrowingCtor.counter = 0;
173+
InitiallyExceptionThrowingCtor result = null;
174+
for (int i = 0; i < 10; ++i)
175+
{
176+
try
177+
{ result = lazy.Value; }
178+
catch (Exception) { }
179+
}
180+
Assert.Equal(5, result.Value);
172181
}
173-
Assert.Equal(result.Value, expected);
174182
}
175183

176184
private static void Value_ExceptionRecovery_IntImpl(Lazy<int> lazy, ref int counter, int expected)

0 commit comments

Comments
 (0)