-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide the internal API more, and move the tests
- Loading branch information
Showing
4 changed files
with
124 additions
and
103 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/Microsoft.VisualStudio.Threading/JoinableTaskInternals.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.Threading; | ||
|
||
using System.ComponentModel; | ||
|
||
#pragma warning disable RS0016 // Add public types and members to the declared API | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
/// <summary> | ||
/// A helper class for integration with Visual Studio. | ||
/// APIs in this file are intended for Microsoft internal use only | ||
/// and are subject to change without notice. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class JoinableTaskInternals | ||
{ | ||
public static bool IsMainThreadBlockedByAnyJoinableTask(JoinableTaskContext? joinableTaskContext) | ||
{ | ||
return joinableTaskContext?.IsMainThreadBlockedByAnyJoinableTask == true; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
95 changes: 95 additions & 0 deletions
95
test/Microsoft.VisualStudio.Threading.Tests/JoinableTaskInternalsTests.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.Threading; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
public class JoinableTaskInternalsTests : JoinableTaskTestBase | ||
{ | ||
public JoinableTaskInternalsTests(ITestOutputHelper logger) | ||
: base(logger) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void IsMainThreadBlockedByAnyJoinableTask_True() | ||
{ | ||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
AsyncManualResetEvent mainThreadBlockerEvent = new AsyncManualResetEvent(false); | ||
AsyncManualResetEvent backgroundThreadMonitorEvent = new AsyncManualResetEvent(false); | ||
|
||
// Start task to monitor IsMainThreadBlockedByAnyJoinableTask | ||
Task monitorTask = Task.Run(async () => | ||
{ | ||
await mainThreadBlockerEvent.WaitAsync(this.TimeoutToken); | ||
|
||
while (!JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)) | ||
{ | ||
// Give the main thread time to enter a blocking state, if the test hasn't already timed out. | ||
await Task.Delay(50, this.TimeoutToken); | ||
} | ||
|
||
backgroundThreadMonitorEvent.Set(); | ||
}); | ||
|
||
JoinableTask? joinable = this.asyncPump.RunAsync(async delegate | ||
{ | ||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
await this.asyncPump.SwitchToMainThreadAsync(this.TimeoutToken); | ||
|
||
this.asyncPump.Run(async () => | ||
{ | ||
await TaskScheduler.Default.SwitchTo(alwaysYield: true); | ||
mainThreadBlockerEvent.Set(); | ||
await backgroundThreadMonitorEvent.WaitAsync(this.TimeoutToken); | ||
}); | ||
}); | ||
|
||
joinable.Join(); | ||
monitorTask.WaitWithoutInlining(throwOriginalException: true); | ||
|
||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
} | ||
|
||
[Fact] | ||
public void IsMainThreadBlockedByAnyJoinableTask_False() | ||
{ | ||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
ManualResetEventSlim backgroundThreadBlockerEvent = new(); | ||
|
||
JoinableTask? joinable = this.asyncPump.RunAsync(async delegate | ||
{ | ||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
await TaskScheduler.Default.SwitchTo(alwaysYield: true); | ||
|
||
this.asyncPump.Run(async () => | ||
{ | ||
backgroundThreadBlockerEvent.Set(); | ||
|
||
// Set a delay sufficient for the other thread to have noticed if IsMainThreadBlockedByAnyJoinableTask is true | ||
// while we're suspended. | ||
await Task.Delay(AsyncDelay); | ||
}); | ||
}); | ||
|
||
backgroundThreadBlockerEvent.Wait(UnexpectedTimeout); | ||
|
||
do | ||
{ | ||
// Give the background thread time to enter a blocking state, if the test hasn't already timed out. | ||
this.TimeoutToken.ThrowIfCancellationRequested(); | ||
|
||
// IsMainThreadBlockedByAnyJoinableTask should be false when a background thread is blocked. | ||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
Thread.Sleep(10); | ||
} | ||
while (!joinable.IsCompleted); | ||
|
||
joinable.Join(); | ||
|
||
Assert.False(JoinableTaskInternals.IsMainThreadBlockedByAnyJoinableTask(this.context)); | ||
} | ||
} |