Skip to content

Commit d477da0

Browse files
bradwilsonandyleejordan
authored andcommitted
Fix deadlocks in tests when there's no sync context
Patch provided in xunit/xunit#2912
1 parent fb020be commit d477da0

File tree

5 files changed

+28
-52
lines changed

5 files changed

+28
-52
lines changed

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal class TestReadLine : IReadLine
3333
}
3434

3535
[Trait("Category", "DebugService")]
36-
public class DebugServiceTests : IDisposable
36+
public class DebugServiceTests : IAsyncLifetime
3737
{
3838
private readonly PsesInternalHost psesHost;
3939
private readonly BreakpointService breakpointService;
@@ -76,14 +76,13 @@ public DebugServiceTests()
7676
variableScriptFile = GetDebugScript("VariableTest.ps1");
7777
}
7878

79-
public void Dispose()
79+
public Task InitializeAsync() => Task.CompletedTask;
80+
81+
public async Task DisposeAsync()
8082
{
8183
debugService.Abort();
8284
debuggerStoppedQueue.Dispose();
83-
#pragma warning disable VSTHRD002
84-
psesHost.StopAsync().Wait();
85-
#pragma warning restore VSTHRD002
86-
GC.SuppressFinalize(this);
85+
await Task.Run(psesHost.StopAsync);
8786
}
8887

8988
/// <summary>

test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs

+6-14
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,25 @@
2020
namespace PowerShellEditorServices.Test.Extensions
2121
{
2222
[Trait("Category", "Extensions")]
23-
public class ExtensionCommandTests : IDisposable
23+
public class ExtensionCommandTests : IAsyncLifetime
2424
{
25-
private readonly PsesInternalHost psesHost;
25+
private PsesInternalHost psesHost;
2626

27-
private readonly ExtensionCommandService extensionCommandService;
27+
private ExtensionCommandService extensionCommandService;
2828

29-
public ExtensionCommandTests()
29+
public async Task InitializeAsync()
3030
{
3131
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
3232
ExtensionService extensionService = new(
3333
languageServer: null,
3434
serviceProvider: null,
3535
editorOperations: null,
3636
executionService: psesHost);
37-
#pragma warning disable VSTHRD002
38-
extensionService.InitializeAsync().Wait();
39-
#pragma warning restore VSTHRD002
37+
await extensionService.InitializeAsync();
4038
extensionCommandService = new(extensionService);
4139
}
4240

43-
public void Dispose()
44-
{
45-
#pragma warning disable VSTHRD002
46-
psesHost.StopAsync().Wait();
47-
#pragma warning restore VSTHRD002
48-
GC.SuppressFinalize(this);
49-
}
41+
public async Task DisposeAsync() => await psesHost.StopAsync();
5042

5143
[Fact]
5244
public async Task CanRegisterAndInvokeCommandWithCmdletName()

test/PowerShellEditorServices.Test/Language/CompletionHandlerTests.cs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Threading;
@@ -21,7 +20,7 @@
2120
namespace PowerShellEditorServices.Test.Language
2221
{
2322
[Trait("Category", "Completions")]
24-
public class CompletionHandlerTests : IDisposable
23+
public class CompletionHandlerTests : IAsyncLifetime
2524
{
2625
private readonly PsesInternalHost psesHost;
2726
private readonly WorkspaceService workspace;
@@ -34,13 +33,9 @@ public CompletionHandlerTests()
3433
completionHandler = new PsesCompletionHandler(NullLoggerFactory.Instance, psesHost, psesHost, workspace);
3534
}
3635

37-
public void Dispose()
38-
{
39-
#pragma warning disable VSTHRD002
40-
psesHost.StopAsync().Wait();
41-
#pragma warning restore VSTHRD002
42-
GC.SuppressFinalize(this);
43-
}
36+
public Task InitializeAsync() => Task.CompletedTask;
37+
38+
public async Task DisposeAsync() => await Task.Run(psesHost.StopAsync);
4439

4540
private ScriptFile GetScriptFile(ScriptRegion scriptRegion) => workspace.GetFile(TestUtilities.GetSharedPath(scriptRegion.File));
4641

test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Management.Automation;
@@ -30,7 +29,7 @@
3029
namespace PowerShellEditorServices.Test.Language
3130
{
3231
[Trait("Category", "Symbols")]
33-
public class SymbolsServiceTests : IDisposable
32+
public class SymbolsServiceTests : IAsyncLifetime
3433
{
3534
private readonly PsesInternalHost psesHost;
3635
private readonly WorkspaceService workspace;
@@ -53,14 +52,13 @@ public SymbolsServiceTests()
5352
new ConfigurationService());
5453
}
5554

56-
public void Dispose()
55+
public Task InitializeAsync() => Task.CompletedTask;
56+
57+
public async Task DisposeAsync()
5758
{
58-
#pragma warning disable VSTHRD002
59-
psesHost.StopAsync().GetAwaiter().GetResult();
60-
#pragma warning restore VSTHRD002
59+
psesHost.StopAsync();
6160
CommandHelpers.s_cmdletToAliasCache.Clear();
6261
CommandHelpers.s_aliasToCmdletCache.Clear();
63-
GC.SuppressFinalize(this);
6462
}
6563

6664
private static void AssertIsRegion(

test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs

+8-16
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@ namespace PowerShellEditorServices.Test.Session
2020
using System.Management.Automation.Runspaces;
2121

2222
[Trait("Category", "PsesInternalHost")]
23-
public class PsesInternalHostTests : IDisposable
23+
public class PsesInternalHostTests : IAsyncLifetime
2424
{
2525
private readonly PsesInternalHost psesHost;
2626

2727
public PsesInternalHostTests() => psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
2828

29-
public void Dispose()
30-
{
31-
#pragma warning disable VSTHRD002
32-
psesHost.StopAsync().Wait();
33-
#pragma warning restore VSTHRD002
34-
GC.SuppressFinalize(this);
35-
}
29+
public Task InitializeAsync() => Task.CompletedTask;
30+
31+
public async Task DisposeAsync() => await psesHost.StopAsync();
3632

3733
[Fact]
3834
public async Task CanExecutePSCommand()
@@ -238,19 +234,15 @@ public async Task CanHandleBadInitialWorkingDirectory(string path)
238234
}
239235

240236
[Trait("Category", "PsesInternalHost")]
241-
public class PsesInternalHostWithProfileTests : IDisposable
237+
public class PsesInternalHostWithProfileTests : IAsyncLifetime
242238
{
243239
private readonly PsesInternalHost psesHost;
244240

245241
public PsesInternalHostWithProfileTests() => psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance, loadProfiles: true);
246242

247-
public void Dispose()
248-
{
249-
#pragma warning disable VSTHRD002
250-
psesHost.StopAsync().Wait();
251-
#pragma warning restore VSTHRD002
252-
GC.SuppressFinalize(this);
253-
}
243+
public Task InitializeAsync() => Task.CompletedTask;
244+
245+
public async Task DisposeAsync() => await psesHost.StopAsync();
254246

255247
[Fact]
256248
public async Task CanResolveAndLoadProfilesForHostId()

0 commit comments

Comments
 (0)