Skip to content

chore: upgrade Proto code to Swift 6 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Coder Desktop/Coder Desktop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.coder.Coder-Desktop";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
};
name = Debug;
};
Expand Down Expand Up @@ -690,7 +690,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.coder.Coder-Desktop";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
};
name = Release;
};
Expand Down Expand Up @@ -835,7 +835,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.coder.Coder-Desktop.ProtoTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Coder Desktop.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Coder Desktop";
};
name = Debug;
Expand All @@ -853,7 +853,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.coder.Coder-Desktop.ProtoTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Coder Desktop.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/Coder Desktop";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "961678FB2CFF100D00B2B6DF"
BuildableName = "Coder Desktop.app"
BlueprintName = "Coder Desktop"
ReferencedContainer = "container:Coder Desktop.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
4 changes: 2 additions & 2 deletions Coder Desktop/Proto/Receiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ actor Receiver<RecvMsg: Message> {
dispatch.read(offset: 0, length: 4, queue: queue) { done, data, error in
guard error == 0 else {
let errStrPtr = strerror(error)
let errStr = String(validatingUTF8: errStrPtr!)!
let errStr = String(validatingCString: errStrPtr!)!
continuation.resume(throwing: ReceiveError.readError(errStr))
return
}
Expand All @@ -42,7 +42,7 @@ actor Receiver<RecvMsg: Message> {
dispatch.read(offset: 0, length: Int(length), queue: queue) { done, data, error in
guard error == 0 else {
let errStrPtr = strerror(error)
let errStr = String(validatingUTF8: errStrPtr!)!
let errStr = String(validatingCString: errStrPtr!)!
continuation.resume(throwing: ReceiveError.readError(errStr))
return
}
Expand Down
22 changes: 11 additions & 11 deletions Coder Desktop/Proto/Speaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ class Speaker<SendMsg: RPCMessage & Message, RecvMsg: RPCMessage & Message> {
/// Send a unary RPC message and handle the response
func unaryRPC(_ req: SendMsg) async throws -> RecvMsg {
return try await withCheckedThrowingContinuation { continuation in
Task {
let msgID = await self.secretary.record(continuation: continuation)
Task { [sender, secretary, logger] in
let msgID = await secretary.record(continuation: continuation)
var req = req
req.rpc = Vpn_RPC()
req.rpc.msgID = msgID
do {
self.logger.debug("sending RPC with msgID: \(msgID)")
try await self.sender.send(req)
logger.debug("sending RPC with msgID: \(msgID)")
try await sender.send(req)
} catch {
self.logger.warning("failed to send RPC with msgID: \(msgID): \(error)")
await self.secretary.erase(id: req.rpc.msgID)
logger.warning("failed to send RPC with msgID: \(msgID): \(error)")
await secretary.erase(id: req.rpc.msgID)
continuation.resume(throwing: error)
}
self.logger.debug("sent RPC with msgID: \(msgID)")
logger.debug("sent RPC with msgID: \(msgID)")
}
}
}
Expand All @@ -169,7 +169,7 @@ class Speaker<SendMsg: RPCMessage & Message, RecvMsg: RPCMessage & Message> {
}

/// A class that performs the initial VPN protocol handshake and version negotiation.
class Handshaker {
class Handshaker: @unchecked Sendable {
private let writeFD: FileHandle
private let dispatch: DispatchIO
private var theirData: Data = .init()
Expand Down Expand Up @@ -219,7 +219,7 @@ class Handshaker {
private func handleRead(_: Bool, _ data: DispatchData?, _ error: Int32) {
guard error == 0 else {
let errStrPtr = strerror(error)
let errStr = String(validatingUTF8: errStrPtr!)!
let errStr = String(validatingCString: errStrPtr!)!
continuation?.resume(throwing: HandshakeError.readError(errStr))
return
}
Expand Down Expand Up @@ -277,7 +277,7 @@ enum HandshakeError: Error {
case unsupportedVersion([ProtoVersion])
}

struct RPCRequest<SendMsg: RPCMessage & Message, RecvMsg: RPCMessage> {
struct RPCRequest<SendMsg: RPCMessage & Message, RecvMsg: RPCMessage & Sendable>: Sendable {
let msg: RecvMsg
private let sender: Sender<SendMsg>

Expand All @@ -302,7 +302,7 @@ enum RPCError: Error {
}

/// An actor to record outgoing RPCs and route their replies to the original sender
actor RPCSecretary<RecvMsg: RPCMessage> {
actor RPCSecretary<RecvMsg: RPCMessage & Sendable> {
private var continuations: [UInt64: CheckedContinuation<RecvMsg, Error>] = [:]
private var nextMsgID: UInt64 = 1

Expand Down
63 changes: 41 additions & 22 deletions Coder Desktop/ProtoTests/SpeakerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,45 @@ import Testing

/// A concrete, test class for the abstract Speaker, which overrides the handlers to send things to
/// continuations we set in the test.
class TestTunnel: Speaker<Vpn_TunnelMessage, Vpn_ManagerMessage> {
var msgHandler: CheckedContinuation<Vpn_ManagerMessage, Error>?
class TestTunnel: Speaker<Vpn_TunnelMessage, Vpn_ManagerMessage>, @unchecked Sendable {
private var msgHandler: CheckedContinuation<Vpn_ManagerMessage, Error>?
override func handleMessage(_ msg: Vpn_ManagerMessage) {
msgHandler?.resume(returning: msg)
}

var rpcHandler: CheckedContinuation<RPCRequest<Vpn_TunnelMessage, Vpn_ManagerMessage>, Error>?
/// Runs the given closure asynchronously and returns the next non-RPC message received.
func expectMessage(with closure:
@escaping @Sendable () async -> Void) async throws -> Vpn_ManagerMessage
{
return try await withCheckedThrowingContinuation { continuation in
msgHandler = continuation
Task {
await closure()
}
}
}

private var rpcHandler: CheckedContinuation<RPCRequest<Vpn_TunnelMessage, Vpn_ManagerMessage>, Error>?
override func handleRPC(_ req: RPCRequest<Vpn_TunnelMessage, Vpn_ManagerMessage>) {
rpcHandler?.resume(returning: req)
}

/// Runs the given closure asynchronously and return the next non-RPC message received
func expectRPC(with closure:
@escaping @Sendable () async -> Void) async throws ->
RPCRequest<Vpn_TunnelMessage, Vpn_ManagerMessage>
{
return try await withCheckedThrowingContinuation { continuation in
rpcHandler = continuation
Task {
await closure()
}
}
}
}

@Suite(.timeLimit(.minutes(1)))
struct SpeakerTests {
struct SpeakerTests: Sendable {
let pipeMT = Pipe()
let pipeTM = Pipe()
let uut: TestTunnel
Expand Down Expand Up @@ -56,14 +81,11 @@ struct SpeakerTests {
@Test func handleSingleMessage() async throws {
async let readDone: () = try uut.readLoop()

let got = try await withCheckedThrowingContinuation { continuation in
uut.msgHandler = continuation
Task {
var s = Vpn_ManagerMessage()
s.start = Vpn_StartRequest()
await #expect(throws: Never.self) {
try await sender.send(s)
}
let got = try await uut.expectMessage {
var s = Vpn_ManagerMessage()
s.start = Vpn_StartRequest()
await #expect(throws: Never.self) {
try await sender.send(s)
}
}
#expect(got.msg == .start(Vpn_StartRequest()))
Expand All @@ -74,16 +96,13 @@ struct SpeakerTests {
@Test func handleRPC() async throws {
async let readDone: () = try uut.readLoop()

let got = try await withCheckedThrowingContinuation { continuation in
uut.rpcHandler = continuation
Task {
var s = Vpn_ManagerMessage()
s.start = Vpn_StartRequest()
s.rpc = Vpn_RPC()
s.rpc.msgID = 33
await #expect(throws: Never.self) {
try await sender.send(s)
}
let got = try await uut.expectRPC {
var s = Vpn_ManagerMessage()
s.start = Vpn_StartRequest()
s.rpc = Vpn_RPC()
s.rpc.msgID = 33
await #expect(throws: Never.self) {
try await sender.send(s)
}
}
#expect(got.msg.msg == .start(Vpn_StartRequest()))
Expand Down