Skip to content

Commit a4e97ec

Browse files
committed
Add fix/workaround for race in MSBuild completion tests on CI
1 parent c3a7208 commit a4e97ec

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Editor.Tests/Extensions/CommandServiceExtensions.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,34 @@ public static void CheckAndExecute<T> (
6464
throw new InvalidOperationException ($"No handler available for `{typeof (T)}`");
6565
}
6666

67+
// There is a race here where a completion session may have triggered on the UI thread
68+
// but the task to compute the completion items is still running. This can cause the
69+
// completion to be dismissed before the items are computed.
70+
//
71+
// We mitigate this by checking if a session is open and attempting to wait for it.
6772
if (textView != null) {
6873
if (textView.Properties.TryGetProperty (typeof (IAsyncCompletionSession), out IAsyncCompletionSession session)) {
6974
if (EnableDebugTrace) {
7075
LogTrace ("Session open");
7176
RegisterTraceHandlers (session);
7277
}
73-
//ensure the computation is completed before we continue typing
78+
79+
// The first time we see the session, wait for a short time to allow it to initialize,
80+
// otherwise completion will dismiss via TryDismissSafelyAsync if the snapshot is updated
81+
// before the session is initialized.
82+
//
83+
// This wait is not necessary on my local machine, but it mitigates nondeterministic
84+
// failures on GitHub Actions CI.
85+
//
86+
// Note that polling IAsyncCompletionSessionOperations.IsStarted does not help.
87+
if (IsGithubActions && !session.Properties.TryGetProperty (HasWaitedForCompletionToInitializeKey, out bool hasWaited)) {
88+
session.Properties.AddProperty (HasWaitedForCompletionToInitializeKey, true);
89+
Thread.Sleep (500);
90+
}
91+
92+
// Block until the computation is updated before we run more actions. This makes the
93+
// test reliable on my local machine.
7494
session.GetComputedItems (CancellationToken.None);
75-
LogTrace ("Session open");
7695
}
7796
} else{
7897
LogTrace ("Session not open");
@@ -81,6 +100,10 @@ public static void CheckAndExecute<T> (
81100

82101
const string TraceID = "CommandServiceExtensions.Trace";
83102

103+
static readonly object HasWaitedForCompletionToInitializeKey = new();
104+
105+
static readonly bool IsGithubActions = Environment.GetEnvironmentVariable("GITHUB_ACTIONS") != null;
106+
84107
static void LogTrace(string message) => Console.WriteLine ($"{TraceID}: {message}");
85108

86109
static void RegisterTraceHandlers (IAsyncCompletionSession session)

0 commit comments

Comments
 (0)