Skip to content

Commit

Permalink
Implement async client test stub methods (#378)
Browse files Browse the repository at this point in the history
* Added implementation in the generator of asynchronous calls for the ServiceClientTestStubs

* Added test with asynchronous call

* Fixed indentation

* Simplified test testClientAsynchronous

* Changed format in the expectation so it's compatible with linux
Removed getWordasynchronous private method

* Added explanation why the method is public

* Added white space between parameters

* Rename channel to fakeChannel

* Fixed CallResult init method

* Added space before initialMetadata

* Replaced without closure

* Add extra space

* Added fake channel to the generator

* Fix whitespace and use the appropriate unary call test stub instead of a real call object.
  • Loading branch information
cepages authored and MrMage committed Mar 21, 2019
1 parent 4df501a commit 630dfa5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Sources/Examples/Echo/Generated/echo.grpc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftGRPC/Core/CallResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Sources/protoc-gen-swiftgrpc/Generator-Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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("}")
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftGRPCTests/ClientTestExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 630dfa5

Please sign in to comment.