| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * Copyright 2018, 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 Dispatch
- import Foundation
- @testable import SwiftGRPC
- import XCTest
- // Sample test suite to demonstrate how one would test client code that
- // uses an object that implements the `...Service` protocol.
- // These tests don't really test the logic of the SwiftGRPC library, but are meant
- // as an example of how one would go about testing their own client/server code that
- // relies on SwiftGRPC.
- fileprivate class ClientUnderTest {
- let service: Echo_EchoService
-
- init(service: Echo_EchoService) {
- self.service = service
- }
-
- func getWord(_ input: String) throws -> String {
- return try service.get(Echo_EchoRequest(text: input)).text
- }
-
- func collectWords(_ input: [String]) throws -> String {
- let call = try service.collect(completion: nil)
- for text in input {
- try call.send(Echo_EchoRequest(text: text), completion: { _ in })
- }
- call.waitForSendOperationsToFinish()
- return try call.closeAndReceive().text
- }
-
- func expandWords(_ input: String) throws -> [String] {
- let call = try service.expand(Echo_EchoRequest(text: input), completion: nil)
- var results: [String] = []
- while let response = try call.receive() {
- results.append(response.text)
- }
- return results
- }
-
- func updateWords(_ input: [String]) throws -> [String] {
- let call = try service.update(completion: nil)
- for text in input {
- try call.send(Echo_EchoRequest(text: text), completion: { _ in })
- }
- call.waitForSendOperationsToFinish()
-
- var results: [String] = []
- while let response = try call.receive() {
- results.append(response.text)
- }
- return results
- }
- }
- class ClientTestExample: XCTestCase {
- static var allTests: [(String, (ClientTestExample) -> () throws -> Void)] {
- return [
- ("testUnary", testUnary),
- ("testClientStreaming", testClientStreaming),
- ("testServerStreaming", testServerStreaming),
- ("testBidirectionalStreaming", testBidirectionalStreaming)
- ]
- }
- }
- extension ClientTestExample {
- func testUnary() {
- let fakeService = Echo_EchoServiceTestStub()
- fakeService.getResponses.append(Echo_EchoResponse(text: "bar"))
-
- let client = ClientUnderTest(service: fakeService)
- XCTAssertEqual("bar", try client.getWord("foo"))
-
- // 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()
- let fakeCall = Echo_EchoCollectCallTestStub()
- fakeCall.output = Echo_EchoResponse(text: "response")
- fakeService.collectCalls.append(fakeCall)
-
- let client = ClientUnderTest(service: fakeService)
- XCTAssertEqual("response", try client.collectWords(inputStrings))
-
- // Ensure that the expected requests have been sent.
- XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
- }
-
- func testServerStreaming() {
- let outputStrings = ["foo", "bar", "baz"]
- let fakeService = Echo_EchoServiceTestStub()
- let fakeCall = Echo_EchoExpandCallTestStub()
- fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
- fakeService.expandCalls.append(fakeCall)
-
- let client = ClientUnderTest(service: fakeService)
- XCTAssertEqual(outputStrings, try client.expandWords("inputWord"))
-
- // Ensure that all responses have been consumed.
- XCTAssertEqual(0, fakeCall.outputs.count)
- // Ensure that the expected requests have been sent.
- XCTAssertEqual([Echo_EchoRequest(text: "inputWord")], fakeService.expandRequests)
- }
-
- func testBidirectionalStreaming() {
- let inputStrings = ["foo", "bar", "baz"]
- let outputStrings = ["foo2", "bar2", "baz2"]
- let fakeService = Echo_EchoServiceTestStub()
- let fakeCall = Echo_EchoUpdateCallTestStub()
- fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
- fakeService.updateCalls.append(fakeCall)
-
- let client = ClientUnderTest(service: fakeService)
- XCTAssertEqual(outputStrings, try client.updateWords(inputStrings))
-
- // Ensure that all responses have been consumed.
- XCTAssertEqual(0, fakeCall.outputs.count)
- // Ensure that the expected requests have been sent.
- XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
- }
- }
|