Skip to content

Commit bd5cd48

Browse files
Fixed deadlock issues with TaskCompletionSource, Test passes locally now!
1 parent 00e5965 commit bd5cd48

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1919
<UnitTestPlatformVersion Condition="'$(UnitTestPlatformVersion)' == ''">$(VisualStudioVersion)</UnitTestPlatformVersion>
2020
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
21-
<LangVersion>8.0</LangVersion>
21+
<LangVersion>9.0</LangVersion>
2222
</PropertyGroup>
2323
<Target Name="Pack">
2424
</Target>

UnitTests/UnitTests.UWP/VisualUITestBase.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ public class VisualUITestBase
2424
/// </summary>
2525
/// <param name="content">Content to set in test app.</param>
2626
/// <returns>When UI is loaded.</returns>
27-
protected Task<bool> SetTestContentAsync(FrameworkElement content, TaskCreationOptions? options = null)
27+
protected Task SetTestContentAsync(FrameworkElement content)
2828
{
29-
var taskCompletionSource = options.HasValue ? new TaskCompletionSource<bool>(options.Value)
30-
: new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
31-
32-
App.DispatcherQueue.EnqueueAsync(() =>
29+
return App.DispatcherQueue.EnqueueAsync(() =>
3330
{
31+
var taskCompletionSource = new TaskCompletionSource<bool>();
32+
3433
async void Callback(object sender, RoutedEventArgs args)
3534
{
3635
content.Loaded -= Callback;
3736

3837
// Wait for first Render pass
39-
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }, TaskCreationOptions.RunContinuationsAsynchronously);
38+
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
4039

4140
taskCompletionSource.SetResult(true);
4241
}
@@ -53,35 +52,26 @@ async void Callback(object sender, RoutedEventArgs args)
5352
{
5453
taskCompletionSource.SetException(e);
5554
}
56-
});
5755

58-
return taskCompletionSource.Task;
56+
return taskCompletionSource.Task;
57+
});
5958
}
6059

6160
[TestCleanup]
6261
public async Task Cleanup()
6362
{
6463
var taskCompletionSource = new TaskCompletionSource<bool>();
6564

66-
// Need to await TaskCompletionSource before Task
67-
// See https://devblogs.microsoft.com/premier-developer/the-danger-of-taskcompletionsourcet-class/
68-
await taskCompletionSource.Task;
69-
7065
await App.DispatcherQueue.EnqueueAsync(() =>
7166
{
72-
void Callback(object sender, RoutedEventArgs args)
73-
{
74-
App.ContentRoot.Unloaded -= Callback;
75-
76-
taskCompletionSource.SetResult(true);
77-
}
78-
7967
// Going to wait for our original content to unload
80-
App.ContentRoot.Unloaded += Callback;
68+
App.ContentRoot.Unloaded += (_, _) => taskCompletionSource.SetResult(true);
8169

8270
// Trigger that now
8371
App.ContentRoot = null;
8472
});
73+
74+
await taskCompletionSource.Task;
8575
}
8676
}
8777
}

0 commit comments

Comments
 (0)