Skip to content

Commit 7806cfe

Browse files
committed
Fix a nondeterministic test failure of testDontStackTargetPreparationForEditorFunctionality
We fulfilled the `allDocumentsOpened` expectation directly after dispatching the notification to open LibD.swift. This left a very short window in which LibB’s preparation could finish and LibC’s expectation could start before the open notification for LibD.swift was handled, causing an unexpected preparation of LibC. This fixes the issue first reported as rdar://129698768 and for which I added logging in #1478.
1 parent 3092e0c commit 7806cfe

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,16 @@ final class BackgroundIndexingTests: XCTestCase {
693693
DidChangeWatchedFilesNotification(changes: [FileEvent(uri: try project.uri(for: "LibA.swift"), type: .changed)])
694694
)
695695

696-
// Quickly flip through all files
696+
// Quickly flip through all files. The way the test is designed to work is as follows:
697+
// - LibB.swift gets opened and prepared. Preparation is simulated to take a long time until both LibC.swift and
698+
// LibD.swift have been opened.
699+
// - LibC.swift gets opened. This queues preparation of LibC but doesn't cancel preparation of LibB because we
700+
// don't cancel in-progress preparation tasks to guarantee forward progress (see comment at the end of
701+
// `SemanticIndexManager.prepare`).
702+
// - Now LibD.swift gets opened. This cancels preparation of LibC which actually cancels LibC's preparation for
703+
// real because LibC's preparation hasn't started yet (it's only queued).
704+
// Thus, the only targets that are being prepared are LibB and LibD, which is checked by the
705+
// `ExpectedIndexTaskTracker`.
697706
_ = try project.openDocument("LibB.swift")
698707
try libBStartedPreparation.waitOrThrow()
699708

@@ -705,6 +714,9 @@ final class BackgroundIndexingTests: XCTestCase {
705714
_ = try await project.testClient.send(BarrierRequest())
706715
_ = try project.openDocument("LibD.swift")
707716

717+
// Send a barrier request to ensure we have finished opening LibD before allowing the preparation of LibB to finish.
718+
_ = try await project.testClient.send(BarrierRequest())
719+
708720
allDocumentsOpened.signal()
709721
try libDPreparedForEditing.waitOrThrow()
710722
}

0 commit comments

Comments
 (0)