Skip to content

Provide the codegen with an option to generate test clients #870

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 4 commits into from
Jul 3, 2020
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: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ ECHO_PROTO=Sources/Examples/Echo/Model/echo.proto
ECHO_PB=$(ECHO_PROTO:.proto=.pb.swift)
ECHO_GRPC=$(ECHO_PROTO:.proto=.grpc.swift)

# For Echo we'll generate the test client as well.
${ECHO_GRPC}: ${ECHO_PROTO} ${PROTOC_GEN_GRPC_SWIFT}
protoc $< \
--proto_path=$(dir $<) \
--plugin=${PROTOC_GEN_GRPC_SWIFT} \
--grpc-swift_opt=Visibility=Public,TestClient=true \
--grpc-swift_out=$(dir $<)

# Generates protobufs and gRPC client and server for the Echo example
.PHONY:
generate-echo: ${ECHO_PB} ${ECHO_GRPC}
Expand Down
192 changes: 142 additions & 50 deletions Sources/Examples/Echo/Model/echo.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,65 +31,27 @@ import SwiftProtobuf
public protocol Echo_EchoClientProtocol: GRPCClient {
func get(
_ request: Echo_EchoRequest,
callOptions: CallOptions
callOptions: CallOptions?
) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse>

func expand(
_ request: Echo_EchoRequest,
callOptions: CallOptions,
callOptions: CallOptions?,
handler: @escaping (Echo_EchoResponse) -> Void
) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse>

func collect(
callOptions: CallOptions
callOptions: CallOptions?
) -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse>

func update(
callOptions: CallOptions,
callOptions: CallOptions?,
handler: @escaping (Echo_EchoResponse) -> Void
) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse>

}

extension Echo_EchoClientProtocol {
public func get(
_ request: Echo_EchoRequest
) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.get(request, callOptions: self.defaultCallOptions)
}

public func expand(
_ request: Echo_EchoRequest,
handler: @escaping (Echo_EchoResponse) -> Void
) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.expand(request, callOptions: self.defaultCallOptions, handler: handler)
}

public func collect() -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.collect(callOptions: self.defaultCallOptions)
}

public func update(
handler: @escaping (Echo_EchoResponse) -> Void
) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.update(callOptions: self.defaultCallOptions, handler: handler)
}

}

public final class Echo_EchoClient: Echo_EchoClientProtocol {
public let channel: GRPCChannel
public var defaultCallOptions: CallOptions

/// Creates a client for the echo.Echo service.
///
/// - Parameters:
/// - channel: `GRPCChannel` to the service host.
/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
public init(channel: GRPCChannel, defaultCallOptions: CallOptions = CallOptions()) {
self.channel = channel
self.defaultCallOptions = defaultCallOptions
}

/// Immediately returns an echo of a request.
///
Expand All @@ -99,12 +61,12 @@ public final class Echo_EchoClient: Echo_EchoClientProtocol {
/// - Returns: A `UnaryCall` with futures for the metadata, status and response.
public func get(
_ request: Echo_EchoRequest,
callOptions: CallOptions
callOptions: CallOptions? = nil
) -> UnaryCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.makeUnaryCall(
path: "/echo.Echo/Get",
request: request,
callOptions: callOptions
callOptions: callOptions ?? self.defaultCallOptions
)
}

Expand All @@ -117,13 +79,13 @@ public final class Echo_EchoClient: Echo_EchoClientProtocol {
/// - Returns: A `ServerStreamingCall` with futures for the metadata and status.
public func expand(
_ request: Echo_EchoRequest,
callOptions: CallOptions,
callOptions: CallOptions? = nil,
handler: @escaping (Echo_EchoResponse) -> Void
) -> ServerStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.makeServerStreamingCall(
path: "/echo.Echo/Expand",
request: request,
callOptions: callOptions,
callOptions: callOptions ?? self.defaultCallOptions,
handler: handler
)
}
Expand All @@ -137,11 +99,11 @@ public final class Echo_EchoClient: Echo_EchoClientProtocol {
/// - callOptions: Call options.
/// - Returns: A `ClientStreamingCall` with futures for the metadata, status and response.
public func collect(
callOptions: CallOptions
callOptions: CallOptions? = nil
) -> ClientStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.makeClientStreamingCall(
path: "/echo.Echo/Collect",
callOptions: callOptions
callOptions: callOptions ?? self.defaultCallOptions
)
}

Expand All @@ -155,17 +117,147 @@ public final class Echo_EchoClient: Echo_EchoClientProtocol {
/// - handler: A closure called when each response is received from the server.
/// - Returns: A `ClientStreamingCall` with futures for the metadata and status.
public func update(
callOptions: CallOptions,
callOptions: CallOptions? = nil,
handler: @escaping (Echo_EchoResponse) -> Void
) -> BidirectionalStreamingCall<Echo_EchoRequest, Echo_EchoResponse> {
return self.makeBidirectionalStreamingCall(
path: "/echo.Echo/Update",
callOptions: callOptions,
callOptions: callOptions ?? self.defaultCallOptions,
handler: handler
)
}
}

public final class Echo_EchoClient: Echo_EchoClientProtocol {
public let channel: GRPCChannel
public var defaultCallOptions: CallOptions

/// Creates a client for the echo.Echo service.
///
/// - Parameters:
/// - channel: `GRPCChannel` to the service host.
/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them.
public init(channel: GRPCChannel, defaultCallOptions: CallOptions = CallOptions()) {
self.channel = channel
self.defaultCallOptions = defaultCallOptions
}
}

public final class Echo_EchoTestClient: Echo_EchoClientProtocol {
private let fakeChannel: FakeChannel
public var defaultCallOptions: CallOptions

public var channel: GRPCChannel {
return self.fakeChannel
}

public init(
fakeChannel: FakeChannel = FakeChannel(),
defaultCallOptions callOptions: CallOptions = CallOptions()
) {
self.fakeChannel = fakeChannel
self.defaultCallOptions = callOptions
}

/// Make a unary response for the Get RPC. This must be called
/// before calling 'get'. See also 'FakeUnaryResponse'.
///
/// - Parameter requestHandler: a handler for request parts sent by the RPC.
public func makeGetResponseStream(
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Get", requestHandler: requestHandler)
}

public func enqueueGetResponse(
_ response: Echo_EchoResponse,
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) {
let stream = self.makeGetResponseStream(requestHandler)
// This is the only operation on the stream; try! is fine.
try! stream.sendMessage(response)
}

/// Returns true if there are response streams enqueued for 'Get'
public var hasGetResponsesRemaining: Bool {
return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Get")
}

/// Make a streaming response for the Expand RPC. This must be called
/// before calling 'expand'. See also 'FakeStreamingResponse'.
///
/// - Parameter requestHandler: a handler for request parts sent by the RPC.
public func makeExpandResponseStream(
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Expand", requestHandler: requestHandler)
}

public func enqueueExpandResponses(
_ responses: [Echo_EchoResponse],
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) {
let stream = self.makeExpandResponseStream(requestHandler)
// These are the only operation on the stream; try! is fine.
responses.forEach { try! stream.sendMessage($0) }
try! stream.sendEnd()
}

/// Returns true if there are response streams enqueued for 'Expand'
public var hasExpandResponsesRemaining: Bool {
return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Expand")
}

/// Make a unary response for the Collect RPC. This must be called
/// before calling 'collect'. See also 'FakeUnaryResponse'.
///
/// - Parameter requestHandler: a handler for request parts sent by the RPC.
public func makeCollectResponseStream(
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) -> FakeUnaryResponse<Echo_EchoRequest, Echo_EchoResponse> {
return self.fakeChannel.makeFakeUnaryResponse(path: "/echo.Echo/Collect", requestHandler: requestHandler)
}

public func enqueueCollectResponse(
_ response: Echo_EchoResponse,
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) {
let stream = self.makeCollectResponseStream(requestHandler)
// This is the only operation on the stream; try! is fine.
try! stream.sendMessage(response)
}

/// Returns true if there are response streams enqueued for 'Collect'
public var hasCollectResponsesRemaining: Bool {
return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Collect")
}

/// Make a streaming response for the Update RPC. This must be called
/// before calling 'update'. See also 'FakeStreamingResponse'.
///
/// - Parameter requestHandler: a handler for request parts sent by the RPC.
public func makeUpdateResponseStream(
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) -> FakeStreamingResponse<Echo_EchoRequest, Echo_EchoResponse> {
return self.fakeChannel.makeFakeStreamingResponse(path: "/echo.Echo/Update", requestHandler: requestHandler)
}

public func enqueueUpdateResponses(
_ responses: [Echo_EchoResponse],
_ requestHandler: @escaping (FakeRequestPart<Echo_EchoRequest>) -> () = { _ in }
) {
let stream = self.makeUpdateResponseStream(requestHandler)
// These are the only operation on the stream; try! is fine.
responses.forEach { try! stream.sendMessage($0) }
try! stream.sendEnd()
}

/// Returns true if there are response streams enqueued for 'Update'
public var hasUpdateResponsesRemaining: Bool {
return self.fakeChannel.hasFakeResponseEnqueued(forPath: "/echo.Echo/Update")
}
}

/// To build a server, implement a class that conforms to this protocol.
public protocol Echo_EchoProvider: CallHandlerProvider {
/// Immediately returns an echo of a request.
Expand Down
36 changes: 15 additions & 21 deletions Sources/Examples/HelloWorld/Model/helloworld.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ import SwiftProtobuf
public protocol Helloworld_GreeterClientProtocol: GRPCClient {
func sayHello(
_ request: Helloworld_HelloRequest,
callOptions: CallOptions
callOptions: CallOptions?
) -> UnaryCall<Helloworld_HelloRequest, Helloworld_HelloReply>

}

extension Helloworld_GreeterClientProtocol {

/// Sends a greeting.
///
/// - Parameters:
/// - request: Request to send to SayHello.
/// - callOptions: Call options.
/// - Returns: A `UnaryCall` with futures for the metadata, status and response.
public func sayHello(
_ request: Helloworld_HelloRequest
_ request: Helloworld_HelloRequest,
callOptions: CallOptions? = nil
) -> UnaryCall<Helloworld_HelloRequest, Helloworld_HelloReply> {
return self.sayHello(request, callOptions: self.defaultCallOptions)
return self.makeUnaryCall(
path: "/helloworld.Greeter/SayHello",
request: request,
callOptions: callOptions ?? self.defaultCallOptions
)
}

}

public final class Helloworld_GreeterClient: Helloworld_GreeterClientProtocol {
Expand All @@ -58,23 +69,6 @@ public final class Helloworld_GreeterClient: Helloworld_GreeterClientProtocol {
self.channel = channel
self.defaultCallOptions = defaultCallOptions
}

/// Sends a greeting.
///
/// - Parameters:
/// - request: Request to send to SayHello.
/// - callOptions: Call options.
/// - Returns: A `UnaryCall` with futures for the metadata, status and response.
public func sayHello(
_ request: Helloworld_HelloRequest,
callOptions: CallOptions
) -> UnaryCall<Helloworld_HelloRequest, Helloworld_HelloReply> {
return self.makeUnaryCall(
path: "/helloworld.Greeter/SayHello",
request: request,
callOptions: callOptions
)
}
}

/// To build a server, implement a class that conforms to this protocol.
Expand Down
Loading