From 579814f9496cd460ea78cb926b9e5659cf91c7de Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Wed, 20 Jul 2022 23:22:58 -0700 Subject: [PATCH] Fix race condition with multithreaded tests. --- .../CompletionFacts.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/CompletionFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/CompletionFacts.cs index be2f09f55d..6d4f33c7ea 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/CompletionFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/CompletionFacts.cs @@ -2301,7 +2301,12 @@ private OmniSharpTestHost GetImportCompletionHost() { var testHost = CreateOmniSharpHost(configurationData: new[] { new KeyValuePair("RoslynExtensionsOptions:EnableImportCompletion", "true") }); testHost.AddFilesToWorkspace(); - _completionFixture.ImportCompletionTestHost = testHost; + var result = Interlocked.CompareExchange(ref _completionFixture.ImportCompletionTestHost, testHost, null); + if (result != null) + { + testHost.Dispose(); + testHost = result; + } return testHost; } } @@ -2319,7 +2324,12 @@ private OmniSharpTestHost GetAsyncCompletionAndImportCompletionHost() new KeyValuePair("RoslynExtensionsOptions:EnableAsyncCompletion", "true"), }); testHost.AddFilesToWorkspace(); - _completionFixture.ImportAndAsyncCompletionTestHost = testHost; + var result = Interlocked.CompareExchange(ref _completionFixture.ImportCompletionTestHost, testHost, null); + if (result != null) + { + testHost.Dispose(); + testHost = result; + } return testHost; } } @@ -2352,8 +2362,8 @@ internal static class CompletionResponseExtensions #nullable enable public sealed class CompletionFixture : IDisposable { - public OmniSharpTestHost? ImportCompletionTestHost { get; set; } - public OmniSharpTestHost? ImportAndAsyncCompletionTestHost { get; set; } + public OmniSharpTestHost? ImportCompletionTestHost; + public OmniSharpTestHost? ImportAndAsyncCompletionTestHost; public void Dispose() {