diff --git a/Sources/Examples/Echo/Generated/echo.grpc.swift b/Sources/Examples/Echo/Generated/echo.grpc.swift index 17b6cde41..359557538 100644 --- a/Sources/Examples/Echo/Generated/echo.grpc.swift +++ b/Sources/Examples/Echo/Generated/echo.grpc.swift @@ -220,7 +220,10 @@ class Echo_EchoServiceTestStub: ServiceClientTestStubBase, Echo_EchoService { } @discardableResult func get(_ request: Echo_EchoRequest, metadata customMetadata: Metadata, completion: @escaping (Echo_EchoResponse?, CallResult) -> Void) throws -> Echo_EchoGetCall { - fatalError("not implemented") + let response = try self.get(request) + let callResult = CallResult(success: true, statusCode: .ok, statusMessage: "OK", resultData: nil, initialMetadata: nil, trailingMetadata: nil) + completion(response, callResult) + return Echo_EchoGetCallTestStub() } var expandRequests: [Echo_EchoRequest] = [] diff --git a/Sources/SwiftGRPC/Core/CallResult.swift b/Sources/SwiftGRPC/Core/CallResult.swift index 697b17e6d..fc3bc1ddf 100644 --- a/Sources/SwiftGRPC/Core/CallResult.swift +++ b/Sources/SwiftGRPC/Core/CallResult.swift @@ -70,8 +70,9 @@ public struct CallResult: CustomStringConvertible { trailingMetadata = op.receivedTrailingMetadata() } - fileprivate init(success: Bool, statusCode: StatusCode, statusMessage: String?, resultData: Data?, - initialMetadata: Metadata?, trailingMetadata: Metadata?) { + // This method is only public for use by test stubs. Please do not use for other purposes. + public init(success: Bool, statusCode: StatusCode, statusMessage: String?, resultData: Data?, + initialMetadata: Metadata?, trailingMetadata: Metadata?) { self.success = success self.statusCode = statusCode self.statusMessage = statusMessage diff --git a/Sources/protoc-gen-swiftgrpc/Generator-Client.swift b/Sources/protoc-gen-swiftgrpc/Generator-Client.swift index e06b14f38..0198b1d4c 100644 --- a/Sources/protoc-gen-swiftgrpc/Generator-Client.swift +++ b/Sources/protoc-gen-swiftgrpc/Generator-Client.swift @@ -377,7 +377,10 @@ extension Generator { println("@discardableResult") println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {") indent() - println("fatalError(\"not implemented\")") + println("let response = try self.\(methodFunctionName)(request)") + println("let callResult = CallResult(success: true, statusCode: .ok, statusMessage: \"OK\", resultData: nil, initialMetadata: nil, trailingMetadata: nil)") + println("completion(response, callResult)") + println("return \(callName)TestStub()") outdent() println("}") } diff --git a/Tests/SwiftGRPCTests/ClientTestExample.swift b/Tests/SwiftGRPCTests/ClientTestExample.swift index c4deeffea..b5c97e8c6 100644 --- a/Tests/SwiftGRPCTests/ClientTestExample.swift +++ b/Tests/SwiftGRPCTests/ClientTestExample.swift @@ -92,6 +92,27 @@ extension ClientTestExample { XCTAssertEqual([Echo_EchoRequest(text: "foo")], fakeService.getRequests) } + func testClientAsynchronous() throws { + let fakeService = Echo_EchoServiceTestStub() + fakeService.getResponses.append(Echo_EchoResponse(text: "bar")) + + let client = ClientUnderTest(service: fakeService) + + let completionHandlerExpectation = expectation(description: "request completion handler called") + + _ = try client.service.get(Echo_EchoRequest(text: "foo")) { response, _ in + XCTAssertEqual("bar", response?.text) + completionHandlerExpectation.fulfill() + } + + waitForExpectations(timeout: 1) + + // Ensure that all responses have been consumed. + XCTAssertEqual(0, fakeService.getResponses.count) + // Ensure that the expected requests have been sent. + XCTAssertEqual([Echo_EchoRequest(text: "foo")], fakeService.getRequests) + } + func testClientStreaming() { let inputStrings = ["foo", "bar", "baz"] let fakeService = Echo_EchoServiceTestStub()