Skip to content

Commit 9d0a853

Browse files
authored
[RuntimeAsync] make a test compatible with runtime async. (#120701)
* make a test compatible with runtime async. * use UnsafeAccessor
1 parent 13566e1 commit 9d0a853

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/System.Runtime.CompilerServices/AsyncTaskMethodBuilderTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,22 @@ public static void DroppedIncompleteStateMachine_RaisesIncompleteAsyncMethodEven
651651
}).Dispose();
652652
}
653653

654+
// NOTE: This depends on private implementation details generally only used by the debugger.
655+
// If those ever change, this test will need to be updated as well.
656+
[UnsafeAccessor(UnsafeAccessorKind.StaticField, Name = "s_asyncDebuggingEnabled")]
657+
private extern static ref bool AsyncDebuggingEnabled(Task t);
658+
659+
// NOTE: This depends on private implementation details generally only used by the debugger.
660+
// If those ever change, this test will need to be updated as well.
661+
[UnsafeAccessor(UnsafeAccessorKind.StaticField, Name = "s_currentActiveTasks")]
662+
private extern static ref Dictionary<int, Task>? CurrentActiveTasks(Task t);
663+
654664
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
655665
public void AsyncTaskMethodBuilder_Completed_RemovedFromTracking()
656666
{
657667
RemoteExecutor.Invoke(() =>
658668
{
659-
// NOTE: This depends on private implementation details generally only used by the debugger.
660-
// If those ever change, this test will need to be updated as well.
661-
typeof(Task).GetField("s_asyncDebuggingEnabled", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, true);
669+
AsyncDebuggingEnabled(null) = true;
662670

663671
for (int i = 0; i < 1000; i++)
664672
{
@@ -670,8 +678,10 @@ public void AsyncTaskMethodBuilder_Completed_RemovedFromTracking()
670678
t.Wait();
671679
}
672680

673-
int activeCount = ((dynamic)typeof(Task).GetField("s_currentActiveTasks", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null)).Count;
681+
Dictionary<int, Task>? activeTasks = CurrentActiveTasks(null);
682+
int activeCount = activeTasks?.Count ?? 0;
674683
Assert.InRange(activeCount, 0, 10); // some other tasks may be created by the runtime, so this is just using a reasonably small upper bound
684+
675685
}).Dispose();
676686
}
677687

0 commit comments

Comments
 (0)