Skip to content

Commit

Permalink
Add support for file-containing-symbol reflection request. (#1675)
Browse files Browse the repository at this point in the history
Motivation:

The file-containing-symbol request is part of the Reflection Service and enables users to find the proto file
containing a symbol they specify and its transitive dependencies.

Modifications:

Added a dictionary of the fully qualified names of symbols and their corresponding file names, in the ReflectionServiceData struct.
Added the function that creates the server response, after getting the file name corresponding to the symbol name and
getting its tranaitive dependencies. Also, split the tests into Integration and Unit tests, and added tests for the new request.

Result:

Users of the Reflection Service will be able to get from the server the proto file that contains the symbols they are specifying in the request and its transitive dependencies.
  • Loading branch information
stefanadranca authored Oct 17, 2023
1 parent a313fcf commit 4df985f
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 192 deletions.
74 changes: 74 additions & 0 deletions Sources/GRPCReflectionService/Server/ReflectionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ internal struct ReflectionServiceData: Sendable {

internal var fileDescriptorDataByFilename: [String: FileDescriptorProtoData]
internal var serviceNames: [String]
internal var fileNameBySymbol: [String: String]

internal init(fileDescriptors: [Google_Protobuf_FileDescriptorProto]) throws {
self.serviceNames = []
self.fileDescriptorDataByFilename = [:]
self.fileNameBySymbol = [:]

for fileDescriptorProto in fileDescriptors {
let serializedFileDescriptorProto: Data
do {
Expand All @@ -67,6 +70,19 @@ internal struct ReflectionServiceData: Sendable {
)
self.fileDescriptorDataByFilename[fileDescriptorProto.name] = protoData
self.serviceNames.append(contentsOf: fileDescriptorProto.service.map { $0.name })
for qualifiedSybolName in fileDescriptorProto.qualifiedSymbolNames {
let oldValue = self.fileNameBySymbol.updateValue(
fileDescriptorProto.name,
forKey: qualifiedSybolName
)
if let oldValue = oldValue {
throw GRPCStatus(
code: .alreadyExists,
message:
"The \(qualifiedSybolName) symbol from \(fileDescriptorProto.name) already exists in \(oldValue)."
)
}
}
}
}

Expand Down Expand Up @@ -99,6 +115,10 @@ internal struct ReflectionServiceData: Sendable {
}
return serializedFileDescriptorProtos
}

internal func nameOfFileContainingSymbol(named symbolName: String) -> String? {
return self.fileNameBySymbol[symbolName]
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
Expand Down Expand Up @@ -139,6 +159,19 @@ internal final class ReflectionServiceProvider: Reflection_ServerReflectionAsync
)
}

internal func findFileBySymbol(
_ symbolName: String,
request: Reflection_ServerReflectionRequest
) throws -> Reflection_ServerReflectionResponse {
guard let fileName = self.protoRegistry.nameOfFileContainingSymbol(named: symbolName) else {
throw GRPCStatus(
code: .notFound,
message: "The provided symbol could not be found."
)
}
return try self.findFileByFileName(fileName, request: request)
}

internal func serverReflectionInfo(
requestStream: GRPCAsyncRequestStream<Reflection_ServerReflectionRequest>,
responseStream: GRPCAsyncResponseStreamWriter<Reflection_ServerReflectionResponse>,
Expand All @@ -157,6 +190,13 @@ internal final class ReflectionServiceProvider: Reflection_ServerReflectionAsync
let response = try self.getServicesNames(request: request)
try await responseStream.send(response)

case let .fileContainingSymbol(symbolName):
let response = try self.findFileBySymbol(
symbolName,
request: request
)
try await responseStream.send(response)

default:
throw GRPCStatus(code: .unimplemented)
}
Expand Down Expand Up @@ -187,3 +227,37 @@ extension Reflection_ServerReflectionResponse {
}
}
}

extension Google_Protobuf_FileDescriptorProto {
var qualifiedServiceAndMethodNames: [String] {
var names: [String] = []

for service in self.service {
names.append(self.package + "." + service.name)
names.append(
contentsOf: service.method
.map { self.package + "." + service.name + "." + $0.name }
)
}
return names
}

var qualifiedMessageTypes: [String] {
return self.messageType.map {
self.package + "." + $0.name
}
}

var qualifiedEnumTypes: [String] {
return self.enumType.map {
self.package + "." + $0.name
}
}

var qualifiedSymbolNames: [String] {
var names = self.qualifiedServiceAndMethodNames
names.append(contentsOf: self.qualifiedMessageTypes)
names.append(contentsOf: self.qualifiedEnumTypes)
return names
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 2023, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import GRPC
import GRPCReflectionService
import NIOPosix
import SwiftProtobuf
import XCTest

@testable import GRPCReflectionService

final class ReflectionServiceIntegrationTests: GRPCTestCase {
private var server: Server?
private var channel: GRPCChannel?
private let protos: [Google_Protobuf_FileDescriptorProto] = makeProtosWithDependencies()
private let independentProto: Google_Protobuf_FileDescriptorProto = generateFileDescriptorProto(
fileName: "independentBar",
suffix: 5
)

private func setUpServerAndChannel() throws {
let reflectionServiceProvider = try ReflectionService(
fileDescriptors: self.protos + [self.independentProto]
)

let server = try Server.insecure(group: MultiThreadedEventLoopGroup.singleton)
.withServiceProviders([reflectionServiceProvider])
.withLogger(self.serverLogger)
.bind(host: "127.0.0.1", port: 0)
.wait()
self.server = server

let channel = try GRPCChannelPool.with(
target: .hostAndPort("127.0.0.1", server.channel.localAddress!.port!),
transportSecurity: .plaintext,
eventLoopGroup: MultiThreadedEventLoopGroup.singleton
) {
$0.backgroundActivityLogger = self.clientLogger
}

self.channel = channel
}

override func tearDown() {
if let channel = self.channel {
XCTAssertNoThrow(try channel.close().wait())
}
if let server = self.server {
XCTAssertNoThrow(try server.close().wait())
}

super.tearDown()
}

func testFileByFileName() async throws {
try self.setUpServerAndChannel()
let client = Reflection_ServerReflectionAsyncClient(channel: self.channel!)
let serviceReflectionInfo = client.makeServerReflectionInfoCall()
try await serviceReflectionInfo.requestStream.send(
.with {
$0.host = "127.0.0.1"
$0.fileByFilename = "bar1.proto"
}
)
serviceReflectionInfo.requestStream.finish()

var iterator = serviceReflectionInfo.responseStream.makeAsyncIterator()
guard let message = try await iterator.next() else {
return XCTFail("Could not get a response message.")
}

let receivedFileDescriptorProto =
try Google_Protobuf_FileDescriptorProto(
serializedData: (message.fileDescriptorResponse
.fileDescriptorProto[0])
)

XCTAssertEqual(receivedFileDescriptorProto.name, "bar1.proto")
XCTAssertEqual(receivedFileDescriptorProto.service.count, 1)

guard let service = receivedFileDescriptorProto.service.first else {
return XCTFail("The received file descriptor proto doesn't have any services.")
}
guard let method = service.method.first else {
return XCTFail("The service of the received file descriptor proto doesn't have any methods.")
}
XCTAssertEqual(method.name, "testMethod1")
XCTAssertEqual(message.fileDescriptorResponse.fileDescriptorProto.count, 4)
}

func testListServices() async throws {
try self.setUpServerAndChannel()
let client = Reflection_ServerReflectionAsyncClient(channel: self.channel!)
let serviceReflectionInfo = client.makeServerReflectionInfoCall()

try await serviceReflectionInfo.requestStream.send(
.with {
$0.host = "127.0.0.1"
$0.listServices = "services"
}
)

serviceReflectionInfo.requestStream.finish()
var iterator = serviceReflectionInfo.responseStream.makeAsyncIterator()
guard let message = try await iterator.next() else {
return XCTFail("Could not get a response message.")
}

let receivedServices = message.listServicesResponse.service.map { $0.name }.sorted()
let servicesNames = (self.protos + [self.independentProto]).serviceNames.sorted()

XCTAssertEqual(receivedServices, servicesNames)
}

func testFileBySymbol() async throws {
try self.setUpServerAndChannel()
let client = Reflection_ServerReflectionAsyncClient(channel: self.channel!)
let serviceReflectionInfo = client.makeServerReflectionInfoCall()

try await serviceReflectionInfo.requestStream.send(
.with {
$0.host = "127.0.0.1"
$0.fileContainingSymbol = "packagebar1.enumType1"
}
)

serviceReflectionInfo.requestStream.finish()
var iterator = serviceReflectionInfo.responseStream.makeAsyncIterator()
guard let message = try await iterator.next() else {
return XCTFail("Could not get a response message.")
}
let receivedData: [Google_Protobuf_FileDescriptorProto]
do {
receivedData = try message.fileDescriptorResponse.fileDescriptorProto.map {
try Google_Protobuf_FileDescriptorProto(serializedData: $0)
}
} catch {
return XCTFail("Could not serialize data received as a message.")
}

let fileToFind = self.protos[0]
let dependentProtos = self.protos[1...]
for fileDescriptorProto in receivedData {
if fileDescriptorProto == fileToFind {
XCTAssert(
fileDescriptorProto.enumType.names.contains("enumType1"),
"""
The response doesn't contain the serialized file descriptor proto \
containing the \"packagebar1.enumType1\" symbol.
"""
)
} else {
XCTAssert(
dependentProtos.contains(fileDescriptorProto),
"""
The \(fileDescriptorProto.name) is not a dependency of the \
proto file containing the \"packagebar1.enumType1\" symbol.
"""
)
}
}
}
}
Loading

0 comments on commit 4df985f

Please sign in to comment.