Skip to content

Commit 068b6df

Browse files
committed
Fix non-deterministic test failure in BackgroundIndexingTests.testPrepareTarget
We could get `DocumentDiagnosticsRequest` triggered from file changes or old preparation operations during the initial build without actually having re-prepared the target after the file change. Make the test wait until it receives expected diagnostics after the file change.
1 parent 490871b commit 068b6df

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -554,27 +554,30 @@ final class BackgroundIndexingTests: XCTestCase {
554554
DidChangeWatchedFilesNotification(changes: [FileEvent(uri: try project.uri(for: "MyFile.swift"), type: .changed)])
555555
)
556556

557-
let diagnosticRefreshReceived = self.expectation(description: "Received diagnostic refresh request")
558-
project.testClient.handleNextRequest { (_: DiagnosticsRefreshRequest) in
559-
diagnosticRefreshReceived.fulfill()
560-
return VoidResponse()
561-
}
562-
563557
// Send a document request for `uri` to trigger re-preparation of its target. We don't actually care about the
564558
// response for this request. Instead, we wait until SourceKit-LSP sends us a `DiagnosticsRefreshRequest`,
565559
// indicating that the target of `uri` has been prepared.
566560
_ = try await project.testClient.send(
567561
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
568562
)
569-
try await fulfillmentOfOrThrow([diagnosticRefreshReceived])
570563

571-
let updatedDiagnostics = try await project.testClient.send(
572-
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
573-
)
574-
guard case .full(let updatedDiagnostics) = updatedDiagnostics else {
575-
XCTFail("Expected full diagnostics")
576-
return
564+
let receivedEmptyDiagnostics = self.expectation(description: "Received diagnostic refresh request")
565+
project.testClient.handleNextRequest { (_: DiagnosticsRefreshRequest) in
566+
Task {
567+
let updatedDiagnostics = try await project.testClient.send(
568+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
569+
)
570+
guard case .full(let updatedDiagnostics) = updatedDiagnostics else {
571+
XCTFail("Expected full diagnostics")
572+
return
573+
}
574+
if updatedDiagnostics.items.isEmpty {
575+
receivedEmptyDiagnostics.fulfill()
576+
}
577+
}
578+
return VoidResponse()
577579
}
578-
XCTAssertEqual(updatedDiagnostics.items, [])
580+
581+
try await fulfillmentOfOrThrow([receivedEmptyDiagnostics])
579582
}
580583
}

0 commit comments

Comments
 (0)