Skip to content

Commit 8861f3a

Browse files
authored
Merge pull request #1721 from ahoppen/improve-bsp-crash-recovery-test
Improve `BuildServerBuildSystemTests.testCrashRecovery`
2 parents d842579 + f09f7e8 commit 8861f3a

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

Sources/SKTestSupport/RepeatUntilExpectedResult.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ import XCTest
2020
///
2121
/// This is useful to test some request that requires global state to be updated but will eventually converge on the
2222
/// correct result.
23-
///
24-
/// If `bodyHasOneSecondDelay` is true, it is assume that the body already has a one-second delay between iterations.
2523
package func repeatUntilExpectedResult(
26-
_ body: () async throws -> Bool,
27-
bodyHasOneSecondDelay: Bool = false,
2824
timeout: TimeInterval = defaultTimeout,
25+
_ body: () async throws -> Bool,
2926
file: StaticString = #filePath,
3027
line: UInt = #line
3128
) async throws {
3229
for _ in 0..<Int(timeout) {
3330
if try await body() {
3431
return
3532
}
36-
if !bodyHasOneSecondDelay {
37-
try await Task.sleep(for: .seconds(1))
38-
}
33+
try await Task.sleep(for: .seconds(1))
3934
}
4035
XCTFail("Failed to get expected result", file: file, line: line)
4136
}

Tests/BuildSystemIntegrationTests/BuildServerBuildSystemTests.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,11 @@ final class BuildServerBuildSystemTests: XCTestCase {
247247
#error("DEBUG NOT SET")
248248
#endif
249249
""",
250+
"should_crash": "dummy file to indicate that BSP server should crash",
250251
],
251252
buildServer: """
252253
import threading
254+
import os
253255
254256
class BuildServer(AbstractBuildServer):
255257
def workspace_build_targets(self, request: Dict[str, object]) -> Dict[str, object]:
@@ -279,23 +281,25 @@ final class BuildServerBuildSystemTests: XCTestCase {
279281
}
280282
281283
def textdocument_sourcekitoptions(self, request: Dict[str, object]) -> Dict[str, object]:
282-
# Crash when getting build settings for Crash.swift
283-
assert "Crash.swift" not in request["textDocument"]["uri"]
284+
if os.path.exists("$TEST_DIR/should_crash"):
285+
assert False
284286
return {
285287
"compilerArguments": ["$TEST_DIR/Test.swift", "-DDEBUG", $SDK_ARGS]
286288
}
287289
"""
288290
)
289291

290-
// Crash the build server
291-
let (crashUri, _) = try project.openDocument("Crash.swift")
292-
_ = try await project.testClient.send(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(crashUri)))
293-
project.testClient.send(DidCloseTextDocumentNotification(textDocument: TextDocumentIdentifier(crashUri)))
294-
295292
// Check that we still get results for Test.swift (after relaunching the BSP server)
296293
let (uri, _) = try project.openDocument("Test.swift")
297294

298-
try await repeatUntilExpectedResult {
295+
// While the BSP server is crashing, we shouldn't get any build settings and thus get empty diagnostics.
296+
let diagnosticsBeforeCrash = try await project.testClient.send(
297+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
298+
)
299+
XCTAssertEqual(diagnosticsBeforeCrash.fullReport?.items, [])
300+
try FileManager.default.removeItem(at: project.scratchDirectory.appendingPathComponent("should_crash"))
301+
302+
try await repeatUntilExpectedResult(timeout: 20) {
299303
let diagnostics = try await project.testClient.send(
300304
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
301305
)

0 commit comments

Comments
 (0)