Browse Source

Implement async client test stub methods (#378)

* 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.
Carlos Pages 6 years ago
parent
commit
630dfa5e37

+ 4 - 1
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] = []

+ 3 - 2
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

+ 4 - 1
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("}")
         }

+ 21 - 0
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()