Skip to content

Commit

Permalink
simpler async trampoline function, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Dec 4, 2022
1 parent 25fb169 commit e88ae94
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
37 changes: 9 additions & 28 deletions Sources/LanguageServerProtocol/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ extension Server {
case .failure(let error):
completionHandler(error)
case .success:
completionHandler(nil)
completionHandler(nil)
}
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func sendRequestWithErrorOnlyResult(_ request: ClientRequest) async throws {
let _: UnusedResult = try await sendRequest(request)
}
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
Expand Down Expand Up @@ -94,15 +99,7 @@ public extension Server {

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func initialized(params: InitializedParams) async throws {
try await withCheckedThrowingContinuation { (continutation: CheckedContinuation<Void, Error>) in
self.initialized(params: params) { error in
if let error = error {
continutation.resume(throwing: error)
} else {
continutation.resume()
}
}
}
try await sendNotification(.initialized(params))
}

func shutdown(block: @escaping (ServerError?) -> Void) {
Expand All @@ -111,15 +108,7 @@ public extension Server {

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func shutdown() async throws {
try await withCheckedThrowingContinuation { (continutation: CheckedContinuation<Void, Error>) in
self.shutdown { error in
if let error = error {
continutation.resume(throwing: error)
} else {
continutation.resume()
}
}
}
try await sendRequestWithErrorOnlyResult(.shutdown)
}

func exit(block: @escaping (ServerError?) -> Void) {
Expand All @@ -128,15 +117,7 @@ public extension Server {

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func exit() async throws {
try await withCheckedThrowingContinuation { (continutation: CheckedContinuation<Void, Error>) in
self.exit { error in
if let error = error {
continutation.resume(throwing: error)
} else {
continutation.resume()
}
}
}
try await sendNotification(.exit)
}

func cancelRequest(params: CancelParams, block: @escaping (ServerError?) -> Void) {
Expand Down
26 changes: 26 additions & 0 deletions Tests/LanguageServerProtocolTests/ServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,30 @@ final class ServerTests: XCTestCase {
}
}
}

func testAsyncSendWithNoResponse() async throws {
let server = MockServer()

let response: String? = nil

server.responseData = try JSONEncoder().encode(response)

try await server.shutdown()

// check to make sure this throws correctly
server.responseData = nil

do {
try await server.shutdown()

XCTFail()
} catch {
switch error {
case ServerError.missingExpectedResult:
break
default:
throw error
}
}
}
}

0 comments on commit e88ae94

Please sign in to comment.