Skip to content

Commit db981d9

Browse files
authored
Merge pull request #2097 from ahoppen/request-handling-in-order
Fix a race condition leading to out-of-order notifications in tests
2 parents bfd4ef6 + a151ca4 commit db981d9

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Sources/SKTestSupport/TestSourceKitLSPClient.swift

+6-8
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ fileprivate struct NotificationTimeoutError: Error, CustomStringConvertible {
5252
/// We can't use an `AsyncStream` for this because an `AsyncStream` is cancelled if a task that calls
5353
/// `AsyncStream.Iterator.next` is cancelled and we want to be able to wait for new notifications even if waiting for a
5454
/// a previous notification timed out.
55-
actor PendingNotifications {
56-
private var values: [any NotificationType] = []
55+
final class PendingNotifications: Sendable {
56+
private let values = ThreadSafeBox<[any NotificationType]>(initialValue: [])
5757

58-
func add(_ value: any NotificationType) {
59-
values.insert(value, at: 0)
58+
nonisolated func add(_ value: any NotificationType) {
59+
values.value.insert(value, at: 0)
6060
}
6161

6262
func next(timeout: Duration, pollingInterval: Duration = .milliseconds(10)) async throws -> any NotificationType {
6363
for _ in 0..<Int(timeout.seconds / pollingInterval.seconds) {
64-
if let value = values.popLast() {
64+
if let value = values.value.popLast() {
6565
return value
6666
}
6767
try await Task.sleep(for: pollingInterval)
@@ -358,9 +358,7 @@ package final class TestSourceKitLSPClient: MessageHandler, Sendable {
358358

359359
/// - Important: Implementation detail of `TestSourceKitLSPServer`. Do not call from tests.
360360
package func handle(_ notification: some NotificationType) {
361-
Task {
362-
await notifications.add(notification)
363-
}
361+
notifications.add(notification)
364362
}
365363

366364
/// - Important: Implementation detail of `TestSourceKitLSPClient`. Do not call from tests.

0 commit comments

Comments
 (0)