Skip to content
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

Adopt ClientContext changes #56

Merged
merged 10 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let products: [Product] = [
let dependencies: [Package.Dependency] = [
.package(
url: "https://github.com/grpc/grpc-swift.git",
exact: "2.0.0-beta.2"
branch: "main"
),
.package(
url: "https://github.com/apple/swift-nio.git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ package final class GRPCChannel: ClientTransport {
package func withStream<T: Sendable>(
descriptor: MethodDescriptor,
options: CallOptions,
_ closure: (
_ stream: RPCStream<ClientTransport.Inbound, ClientTransport.Outbound>,
_ context: ClientContext
) async throws -> T
_ closure: (_ stream: RPCStream<Inbound, Outbound>, _ context: ClientContext) async throws -> T
) async throws -> T {
// Merge options from the call with those from the service config.
let methodConfig = self.config(forMethod: descriptor)
Expand Down
45 changes: 41 additions & 4 deletions Tests/GRPCNIOTransportHTTP2Tests/ControlService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ struct ControlService: RegistrableRPCService {
serializer: JSONSerializer<String>()
) { request, context in
return StreamingServerResponse { response in
let info = try await self.peerInfo(context: context)
try await response.write(info)
let responseString = """
\(self.serverRemotePeerInfo(context: context))\n
\(self.serverLocalPeerInfo(context: context))\n
\(self.clientRemotePeerInfo(request: request))\n
\(self.clientLocalPeerInfo(request: request))
"""

try await response.write(responseString)

return [:]
}
}
Expand Down Expand Up @@ -101,8 +108,22 @@ extension ControlService {
}
}

private func peerInfo(context: ServerContext) async throws -> String {
return context.peer
private func serverRemotePeerInfo(context: ServerContext) -> String {
"Server's remote peer: \(context.peer)"
}

private func serverLocalPeerInfo(context: ServerContext) -> String {
"Server's local peer: <not yet implemented>"
}

private func clientRemotePeerInfo<T>(request: StreamingServerRequest<T>) -> String {
let remotePeer = request.metadata[stringValues: "remotePeer"].first(where: { _ in true })!
return "Client's remote peer: \(remotePeer)"
}

private func clientLocalPeerInfo<T>(request: StreamingServerRequest<T>) -> String {
let localPeer = request.metadata[stringValues: "localPeer"].first(where: { _ in true })!
return "Client's local peer: \(localPeer)"
}

private func handle(
Expand Down Expand Up @@ -264,3 +285,19 @@ private struct UnsafeTransfer<Wrapped> {
}

extension UnsafeTransfer: @unchecked Sendable {}

struct PeerInfoClientInterceptor: ClientInterceptor {
func intercept<Input, Output>(
request: StreamingClientRequest<Input>,
context: ClientContext,
next: (
StreamingClientRequest<Input>,
ClientContext
) async throws -> StreamingClientResponse<Output>
) async throws -> StreamingClientResponse<Output> where Input: Sendable, Output: Sendable {
var request = request
request.metadata.addString(context.localPeer, forKey: "localPeer")
request.metadata.addString(context.remotePeer, forKey: "remotePeer")
return try await next(request, context)
}
}
91 changes: 85 additions & 6 deletions Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ final class HTTP2TransportTests: XCTestCase {
#endif
}

return GRPCClient(transport: transport)
return GRPCClient(transport: transport, interceptors: [PeerInfoClientInterceptor()])
}

func testUnaryOK() async throws {
Expand Down Expand Up @@ -1632,8 +1632,34 @@ final class HTTP2TransportTests: XCTestCase {
serverAddress: .ipv4(host: "127.0.0.1", port: 0)
) { control, _, _ in
let peerInfo = try await control.peerInfo()
let matches = peerInfo.matches(of: /ipv4:127.0.0.1:\d+/)
XCTAssertNotNil(matches)
let addresses = peerInfo.split(separator: "\n")

guard addresses.count == 4 else {
XCTFail("Unexpected response: should have received 4 different addresses, got \(addresses)")
return
}

let serverRemotePeer = addresses[0]
let serverRemotePeerMatches = serverRemotePeer.wholeMatch(
of: /Server's remote peer: ipv4:127.0.0.1:(\d+)/
)
let clientPort = try XCTUnwrap(serverRemotePeerMatches).1

// TODO: Uncomment when server local peer info is implemented

// let serverLocalPeer = addresses[1]
// let serverLocalPeerMatches = serverLocalPeer.wholeMatch(of: /Server's local peer: <not yet implemented>/)
// let serverPort = XCTUnwrap(serverLocalPeerMatches).1

// let clientRemotePeer = addresses[2]
// let clientRemotePeerMatches = clientRemotePeer.wholeMatch(of: /Client's remote peer: ipv4:127.0.0.1:(\d+)/)
// XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)

let clientLocalPeer = addresses[3]
let clientLocalPeerMatches = clientLocalPeer.wholeMatch(
of: /Client's local peer: ipv4:127.0.0.1:(\d+)/
)
XCTAssertEqual(try XCTUnwrap(clientLocalPeerMatches).1, clientPort)
}
}

Expand All @@ -1642,8 +1668,34 @@ final class HTTP2TransportTests: XCTestCase {
serverAddress: .ipv6(host: "::1", port: 0)
) { control, _, _ in
let peerInfo = try await control.peerInfo()
let matches = peerInfo.matches(of: /ipv6:[::1]:\d+/)
XCTAssertNotNil(matches)
let addresses = peerInfo.split(separator: "\n")

guard addresses.count == 4 else {
XCTFail("Unexpected response: should have received 4 different addresses, got \(addresses)")
return
}

let serverRemotePeer = addresses[0]
let serverRemotePeerMatches = serverRemotePeer.wholeMatch(
of: /Server's remote peer: ipv6:[::1]:(\d+)/
)
let clientPort = try XCTUnwrap(serverRemotePeerMatches).1

// TODO: Uncomment when server local peer info is implemented

// let serverLocalPeer = addresses[1]
// let serverLocalPeerMatches = serverLocalPeer.wholeMatch(of: /Server's local peer: <not yet implemented>/)
// let serverPort = XCTUnwrap(serverLocalPeerMatches).1

// let clientRemotePeer = addresses[2]
// let clientRemotePeerMatches = clientRemotePeer.wholeMatch(of: /Client's remote peer: ipv6:[::1]:(\d+)/)
// XCTAssertEqual(try XCTUnwrap(clientRemotePeerMatches).1, serverPort)

let clientLocalPeer = addresses[3]
let clientLocalPeerMatches = clientLocalPeer.wholeMatch(
of: /Client's local peer: ipv6:[::1]:(\d+)/
)
XCTAssertEqual(try XCTUnwrap(clientLocalPeerMatches).1, clientPort)
}
}

Expand All @@ -1653,7 +1705,34 @@ final class HTTP2TransportTests: XCTestCase {
serverAddress: .unixDomainSocket(path: path)
) { control, _, _ in
let peerInfo = try await control.peerInfo()
XCTAssertEqual(peerInfo, "unix:peer-info-uds")
let addresses = peerInfo.split(separator: "\n")

guard addresses.count == 4 else {
XCTFail("Unexpected response: should have received 4 different addresses, got \(addresses)")
return
}

let serverRemotePeer = addresses[0]
let serverRemotePeerMatches = serverRemotePeer.wholeMatch(
of: /Server's remote peer: unix:peer-info-uds/
)
XCTAssertNotNil(serverRemotePeerMatches)

let serverLocalPeer = addresses[1]
let serverLocalPeerMatches = serverLocalPeer.wholeMatch(
of: /Server's local peer: <not yet implemented>/
)
XCTAssertNotNil(serverLocalPeerMatches)

let clientRemotePeer = addresses[2]
let clientRemotePeerMatches = clientRemotePeer.wholeMatch(
of: /Client's remote peer: unix:peer-info-uds/
)
XCTAssertNotNil(clientRemotePeerMatches)

let clientLocalPeer = addresses[3]
let clientLocalPeerMatches = clientLocalPeer.wholeMatch(of: /Client's local peer: unix:/)
XCTAssertNotNil(clientLocalPeerMatches)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dev/license-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ check_copyright_headers() {

actual_sha=$(head -n "$((drop_first + expected_lines))" "$filename" \
| tail -n "$expected_lines" \
| sed -e 's/201[56789]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' \
| sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' \
| shasum \
| awk '{print $1}')

Expand Down
Loading